Skip to content
Browse files

total refactor!

  • Loading branch information...
1 parent b5bc004 commit 204cd72fa857f0c8bc841b52c452cbe26cb1b549 @mojombo committed Jan 16, 2008
Showing with 238 additions and 135 deletions.
  1. +40 −0 bin/glowstick
  2. +20 −0 extra.rb
  3. +10 −134 lib/glowstick.rb
  4. +35 −0 lib/glowstick/box.rb
  5. +94 −0 lib/glowstick/glowstick.rb
  6. +39 −1 lib/glowstick/graph.rb
View
40 bin/glowstick 100644 → 100755
@@ -0,0 +1,40 @@
+#!/usr/bin/env ruby
+
+$:.unshift(File.join(File.dirname(__FILE__), *%w[.. lib]))
+
+require 'glowstick'
+
+class SystemProcess
+ def initialize(pid)
+ @pid = pid.to_i
+ end
+
+ # Memory usage in kilobytes (resident set size)
+ def memory
+ ps_int('rss')
+ end
+
+ private
+
+ def ps_int(keyword)
+ `ps -o #{keyword}= -p #{@pid}`.to_i
+ end
+end
+
+class Memo
+ def initialize(pid)
+ @process = SystemProcess.new(pid)
+ @graph = Glowstick::Graph.new
+ end
+
+ def idle
+ # sleep 0.1
+ mem = @process.memory
+ @graph.add(mem)
+ true
+ end
+end
+
+memo = Memo.new(4871)
+
+Glowstick.new(memo).start
View
20 extra.rb
@@ -0,0 +1,20 @@
+ def print(text, size, x, y)
+ view_size = (@window.r - @window.l).to_f
+ window_size = @w.to_f
+ xscale = 8 / (window_size / view_size) * (size / 100.0)
+
+ GL.Translate(@window.l, @window.b + 100, 0)
+ GL.Scale(xscale, 2, 1)
+ text.each_byte { |x| glutStrokeCharacter(GLUT_STROKE_ROMAN, x) }
+ end
+
+ def read
+ @data = File.read(ARGV[0]).split("\n").map { |x| x.to_i }
+ puts @data.size
+
+ # temp = []
+ # @data.each_with_index do |x, i|
+ # temp << x if i % 15 == 0
+ # end
+ # @data = temp
+ end
View
144 lib/glowstick.rb
@@ -1,142 +1,18 @@
+# gems
require 'rubygems'
require 'opengl'
-# require 'mathn'
+
+# core
require 'enumerator'
+
include Gl, Glu, Glut
-class Box
- attr_accessor :l, :r, :b, :t
-
- def initialize(l, r, b, t)
- @l = l
- @r = r
- @b = b
- @t = t
- end
-
- def inset_percent(p)
- @l -= (r - l) * (p / 100.0)
- @r += (r - l) * (p / 100.0)
- @b -= (t - b) * (p / 100.0)
- @t += (t - b) * (p / 100.0)
- end
-end
+# internal
+require 'glowstick/glowstick'
+require 'glowstick/box'
+require 'glowstick/graph'
-module Glowstick
+
+class Glowstick
VERSION = '1.0.0'
-
- def self.start
- @window = Box.new(0, 500, 0, 20000)
-
- @w = 800
- @h = 400
-
- read
- calculate_domain_and_range
- main
- end
-
- def self.init
- glClearColor(0.0, 0.0, 0.0, 0.0)
-
- glEnable(GL_LINE_SMOOTH)
- glEnable(GL_BLEND)
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
- glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE)
-
- glShadeModel(GL_FLAT)
- end
-
- def self.read
- @data = File.read(ARGV[0]).split("\n").map { |x| x.to_i }
- puts @data.size
-
- temp = []
- @data.each_with_index do |x, i|
- temp << x if i % 15 == 0
- end
- @data = temp
- end
-
- def self.calculate_domain_and_range
- a = @data.sort
-
- @window.b = a.first
- @window.t = a.last
- @window.l = 0
- @window.r = a.size
-
- @window.inset_percent(10)
- end
-
- def self.plot
- i = 0
-
- @data.each_cons(2) do |a|
- line(i, a[0], i + 1, a[1])
- i += 1
- end
- end
-
- def self.display
- glClear(GL_COLOR_BUFFER_BIT)
-
- plot
-
- print("Hello Stroke", 2, 0, 10)
-
- glutSwapBuffers()
- end
-
- def self.print(text, size, x, y)
- view_size = (@window.r - @window.l).to_f
- window_size = @w.to_f
- xscale = 8 / (window_size / view_size) * (size / 100.0)
-
- GL.Translate(@window.l, @window.b + 100, 0)
- GL.Scale(xscale, 2, 1)
- text.each_byte { |x| glutStrokeCharacter(GLUT_STROKE_ROMAN, x) }
- end
-
- def self.reshape(w = @w, h = @h)
- @w = w
- @h = h
-
- glViewport(0, 0, w, h)
- glMatrixMode(GL_PROJECTION)
- glLoadIdentity()
- GLU.Ortho2D(@window.l, @window.r, @window.b, @window.t)
- end
-
- def self.keyboard(key, x, y)
- case (key)
- when ?l
- nil
- when ?r
- glutPostRedisplay()
- end
- end
-
- def self.main
- glutInit
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA)
- glutInitWindowSize(@w, @h)
- glutInitWindowPosition(100, 100)
- glutCreateWindow("Glowstick")
- init()
- glutDisplayFunc(self.method(:display).to_proc)
- glutReshapeFunc(self.method(:reshape).to_proc)
- glutKeyboardFunc(self.method(:keyboard).to_proc)
- glutMainLoop()
- end
-
- def self.line(x1, y1, x2, y2)
- glBegin(GL_LINES)
- glVertex((x1),(y1))
- glVertex((x2),(y2))
- glEnd()
- end
end
-
-ARGV[0] = "/Users/tom/memlog2.log"
-Glowstick.start
View
35 lib/glowstick/box.rb
@@ -0,0 +1,35 @@
+class Glowstick
+
+ class Box
+ attr_accessor :l, :r, :b, :t
+
+ def initialize(l, r, b, t)
+ @l = l
+ @r = r
+ @b = b
+ @t = t
+ end
+
+ def inset_percent(p)
+ @l -= (r - l) * (p / 100.0)
+ @r += (r - l) * (p / 100.0)
+ @b -= (t - b) * (p / 100.0)
+ @t += (t - b) * (p / 100.0)
+ self
+ end
+
+ # Calculate the smallest Box that can contain all of the given boxes
+ # +args+ is the list or Array of Boxes
+ #
+ # Returns Glowstick::Box
+ def self.container(*args)
+ boxes = Array(args).flatten
+ l = boxes.map { |box| box.l }.min
+ r = boxes.map { |box| box.r }.max
+ b = boxes.map { |box| box.b }.min
+ t = boxes.map { |box| box.t }.max
+ Box.new(l, r, b, t)
+ end
+ end # Box
+
+end # Glowstick
View
94 lib/glowstick/glowstick.rb
@@ -0,0 +1,94 @@
+class Glowstick
+
+ ###########################################################################
+ # Class
+
+ class << self
+ attr_accessor :graphs
+ end
+
+ self.graphs = []
+
+ def self.register(graph)
+ self.graphs << graph
+ end
+
+ ###########################################################################
+ # Instance
+
+ def initialize(handler)
+ @window = Box.new(0, 1, 0, 1)
+ @handler = handler
+ @w = 800
+ @h = 400
+ end
+
+ def display
+ # clear the buffer
+ glClear(GL_COLOR_BUFFER_BIT)
+
+ # # calculate and apply the current view size
+ boxes = Glowstick.graphs.map { |g| g.bounding_box }
+ if boxes.empty?
+ @window = Box.new(0, 1, 0, 1)
+ else
+ @window = Box.container(boxes).inset_percent(10)
+ end
+
+ reshape
+
+ # draw each graph
+ Glowstick.graphs.each do |graph|
+ graph.draw
+ end
+
+ # swap buffer
+ glutSwapBuffers()
+ end
+
+ def reshape(w = @w, h = @h)
+ @w = w
+ @h = h
+
+ glViewport(0, 0, w, h)
+ glMatrixMode(GL_PROJECTION)
+ glLoadIdentity()
+ GLU.Ortho2D(@window.l, @window.r, @window.b, @window.t)
+ end
+
+ def keyboard(key, x, y)
+ case (key)
+ when ?r
+ glutPostRedisplay()
+ end
+ end
+
+ def idle
+ should_draw = @handler.idle
+ if should_draw
+ glutPostRedisplay
+ end
+ end
+
+ def start
+ glutInit
+ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA)
+ glutInitWindowSize(@w, @h)
+ glutInitWindowPosition(100, 100)
+ glutCreateWindow("Glowstick")
+
+ glClearColor(0.0, 0.0, 0.0, 0.0)
+ glEnable(GL_LINE_SMOOTH)
+ glEnable(GL_BLEND)
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
+ glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE)
+ glShadeModel(GL_FLAT)
+
+ glutDisplayFunc(self.method(:display).to_proc)
+ glutReshapeFunc(self.method(:reshape).to_proc)
+ glutKeyboardFunc(self.method(:keyboard).to_proc)
+ glutIdleFunc(self.method(:idle).to_proc)
+
+ glutMainLoop()
+ end
+end
View
40 lib/glowstick/graph.rb
@@ -1,7 +1,45 @@
-module Glowstick
+class Glowstick
class Graph
+ attr_accessor :data
+ def initialize
+ @data = []
+ @box = Box.new(0, 1, 0, 1)
+ Glowstick.register(self)
+ end
+
+ def add(val)
+ @data << val
+ end
+
+ def bounding_box
+ @box.b = @data.min || 0
+ @box.t = @data.max || 1
+ @box.l = 0
+ @box.r = @data.size
+
+ @box.t = @box.b + 1 if @box.b == @box.t
+ @box.r = 1 if @box.r == 0
+
+ @box
+ end
+
+ def draw
+ i = 0
+
+ @data.each_cons(2) do |a|
+ line(i, a[0], i + 1, a[1])
+ i += 1
+ end
+ end
+
+ def line(x1, y1, x2, y2)
+ glBegin(GL_LINES)
+ glVertex((x1),(y1))
+ glVertex((x2),(y2))
+ glEnd()
+ end
end # Graph
end # Glowstick

0 comments on commit 204cd72

Please sign in to comment.
Something went wrong with that request. Please try again.