Skip to content
Browse files

switch to heap_dump lib, begin on readme

  • Loading branch information...
1 parent f269187 commit 933e0ac3bc54667ddea1cee9486249f0886e6272 mrb committed Jun 11, 2011
View
1 .gitignore
@@ -3,3 +3,4 @@
db/*.sqlite3
log/*.log
tmp/**/*
+*.rbc
View
4 .rvmrc
@@ -0,0 +1,4 @@
+rvm rbx-2.0.0pre
+rvm_project_rvmrc=0
+rvm_project_rvmrc_default=1
+
View
76 Gemfile.lock
@@ -0,0 +1,76 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ abstract (1.0.0)
+ actionmailer (3.0.1)
+ actionpack (= 3.0.1)
+ mail (~> 2.2.5)
+ actionpack (3.0.1)
+ activemodel (= 3.0.1)
+ activesupport (= 3.0.1)
+ builder (~> 2.1.2)
+ erubis (~> 2.6.6)
+ i18n (~> 0.4.1)
+ rack (~> 1.2.1)
+ rack-mount (~> 0.6.12)
+ rack-test (~> 0.5.4)
+ tzinfo (~> 0.3.23)
+ activemodel (3.0.1)
+ activesupport (= 3.0.1)
+ builder (~> 2.1.2)
+ i18n (~> 0.4.1)
+ activerecord (3.0.1)
+ activemodel (= 3.0.1)
+ activesupport (= 3.0.1)
+ arel (~> 1.0.0)
+ tzinfo (~> 0.3.23)
+ activeresource (3.0.1)
+ activemodel (= 3.0.1)
+ activesupport (= 3.0.1)
+ activesupport (3.0.1)
+ arel (1.0.1)
+ activesupport (~> 3.0.0)
+ builder (2.1.2)
+ erubis (2.6.6)
+ abstract (>= 1.0.0)
+ i18n (0.4.2)
+ mail (2.2.19)
+ activesupport (>= 2.3.6)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.16)
+ polyglot (0.3.1)
+ rack (1.2.3)
+ rack-mount (0.6.14)
+ rack (>= 1.0.0)
+ rack-test (0.5.7)
+ rack (>= 1.0)
+ rails (3.0.1)
+ actionmailer (= 3.0.1)
+ actionpack (= 3.0.1)
+ activerecord (= 3.0.1)
+ activeresource (= 3.0.1)
+ activesupport (= 3.0.1)
+ bundler (~> 1.0.0)
+ railties (= 3.0.1)
+ railties (3.0.1)
+ actionpack (= 3.0.1)
+ activesupport (= 3.0.1)
+ rake (>= 0.8.4)
+ thor (~> 0.14.0)
+ rake (0.9.2)
+ sqlite3 (1.3.3)
+ sqlite3-ruby (1.3.3)
+ sqlite3 (>= 1.3.3)
+ thor (0.14.6)
+ treetop (1.4.9)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.27)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ rails (= 3.0.1)
+ sqlite3-ruby
View
281 README
@@ -1,281 +0,0 @@
-== Welcome to Rails
-
-Rails is a web-application framework that includes everything needed to create
-database-backed web applications according to the Model-View-Control pattern.
-
-This pattern splits the view (also called the presentation) into "dumb"
-templates that are primarily responsible for inserting pre-built data in between
-HTML tags. The model contains the "smart" domain objects (such as Account,
-Product, Person, Post) that holds all the business logic and knows how to
-persist themselves to a database. The controller handles the incoming requests
-(such as Save New Account, Update Product, Show Post) by manipulating the model
-and directing data to the view.
-
-In Rails, the model is handled by what's called an object-relational mapping
-layer entitled Active Record. This layer allows you to present the data from
-database rows as objects and embellish these data objects with business logic
-methods. You can read more about Active Record in
-link:files/vendor/rails/activerecord/README.html.
-
-The controller and view are handled by the Action Pack, which handles both
-layers by its two parts: Action View and Action Controller. These two layers
-are bundled in a single package due to their heavy interdependence. This is
-unlike the relationship between the Active Record and Action Pack that is much
-more separate. Each of these packages can be used independently outside of
-Rails. You can read more about Action Pack in
-link:files/vendor/rails/actionpack/README.html.
-
-
-== Getting Started
-
-1. At the command prompt, create a new Rails application:
- <tt>rails myapp</tt> (where <tt>myapp</tt> is the application name)
-
-2. Change directory to <tt>myapp</tt> and start the web server:
- <tt>cd myapp; rails server</tt> (run with --help for options)
-
-3. Go to http://localhost:3000/ and you'll see:
- "Welcome aboard: You're riding the Rails!"
-
-4. Follow the guidelines to start developing your application. You can find
-the following resources handy:
-
-* The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
-* Ruby on Rails Tutorial Book: http://www.railstutorial.org/
-
-
-== Web Servers
-
-By default, Rails will try to use Mongrel if it's installed when started with
-<tt>rails server</tt>, otherwise Rails will use WEBrick, the web server that
-ships with Ruby.
-
-Mongrel is a Ruby-based web server with a C component (which requires
-compilation) that is suitable for development. If you have Ruby Gems installed,
-getting up and running with mongrel is as easy as:
- <tt>sudo gem install mongrel</tt>.
-
-You can find more info at: http://mongrel.rubyforge.org
-
-You can alternatively run Rails applications with other Ruby web servers, e.g.,
-{Thin}[http://code.macournoyer.com/thin/], {Ebb}[http://ebb.rubyforge.org/], and
-Apache with {mod_rails}[http://www.modrails.com/]. However, <tt>rails server</tt>
-doesn't search for or start them.
-
-For production use, often a web/proxy server, e.g., {Apache}[http://apache.org],
-{Nginx}[http://nginx.net/], {LiteSpeed}[http://litespeedtech.com/],
-{Lighttpd}[http://www.lighttpd.net/], or {IIS}[http://www.iis.net/], is deployed
-as the front end server with the chosen Ruby web server running in the back end
-and receiving the proxied requests via one of several protocols (HTTP, CGI, FCGI).
-
-
-== Debugging Rails
-
-Sometimes your application goes wrong. Fortunately there are a lot of tools that
-will help you debug it and get it back on the rails.
-
-First area to check is the application log files. Have "tail -f" commands
-running on the server.log and development.log. Rails will automatically display
-debugging and runtime information to these files. Debugging info will also be
-shown in the browser on requests from 127.0.0.1.
-
-You can also log your own messages directly into the log file from your code
-using the Ruby logger class from inside your controllers. Example:
-
- class WeblogController < ActionController::Base
- def destroy
- @weblog = Weblog.find(params[:id])
- @weblog.destroy
- logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
- end
- end
-
-The result will be a message in your log file along the lines of:
-
- Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
-
-More information on how to use the logger is at http://www.ruby-doc.org/core/
-
-Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
-several books available online as well:
-
-* Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
-* Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
-
-These two books will bring you up to speed on the Ruby language and also on
-programming in general.
-
-
-== Debugger
-
-Debugger support is available through the debugger command when you start your
-Mongrel or WEBrick server with --debugger. This means that you can break out of
-execution at any point in the code, investigate and change the model, and then,
-resume execution! You need to install ruby-debug to run the server in debugging
-mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
-
- class WeblogController < ActionController::Base
- def index
- @posts = Post.find(:all)
- debugger
- end
- end
-
-So the controller will accept the action, run the first line, then present you
-with a IRB prompt in the server window. Here you can do things like:
-
- >> @posts.inspect
- => "[#<Post:0x14a6be8
- @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
- #<Post:0x14a6620
- @attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
- >> @posts.first.title = "hello from a debugger"
- => "hello from a debugger"
-
-...and even better, you can examine how your runtime objects actually work:
-
- >> f = @posts.first
- => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
- >> f.
- Display all 152 possibilities? (y or n)
-
-Finally, when you're ready to resume execution, you can enter "cont".
-
-
-== Console
-
-The console is a Ruby shell, which allows you to interact with your
-application's domain model. Here you'll have all parts of the application
-configured, just like it is when the application is running. You can inspect
-domain models, change values, and save to the database. Starting the script
-without arguments will launch it in the development environment.
-
-To start the console, run <tt>rails console</tt> from the application
-directory.
-
-Options:
-
-* Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
- made to the database.
-* Passing an environment name as an argument will load the corresponding
- environment. Example: <tt>rails console production</tt>.
-
-To reload your controllers and models after launching the console run
-<tt>reload!</tt>
-
-More information about irb can be found at:
-link:http://www.rubycentral.com/pickaxe/irb.html
-
-
-== dbconsole
-
-You can go to the command line of your database directly through <tt>rails
-dbconsole</tt>. You would be connected to the database with the credentials
-defined in database.yml. Starting the script without arguments will connect you
-to the development database. Passing an argument will connect you to a different
-database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
-PostgreSQL and SQLite 3.
-
-== Description of Contents
-
-The default directory structure of a generated Ruby on Rails application:
-
- |-- app
- | |-- controllers
- | |-- helpers
- | |-- models
- | `-- views
- | `-- layouts
- |-- config
- | |-- environments
- | |-- initializers
- | `-- locales
- |-- db
- |-- doc
- |-- lib
- | `-- tasks
- |-- log
- |-- public
- | |-- images
- | |-- javascripts
- | `-- stylesheets
- |-- script
- | `-- performance
- |-- test
- | |-- fixtures
- | |-- functional
- | |-- integration
- | |-- performance
- | `-- unit
- |-- tmp
- | |-- cache
- | |-- pids
- | |-- sessions
- | `-- sockets
- `-- vendor
- `-- plugins
-
-app
- Holds all the code that's specific to this particular application.
-
-app/controllers
- Holds controllers that should be named like weblogs_controller.rb for
- automated URL mapping. All controllers should descend from
- ApplicationController which itself descends from ActionController::Base.
-
-app/models
- Holds models that should be named like post.rb. Models descend from
- ActiveRecord::Base by default.
-
-app/views
- Holds the template files for the view that should be named like
- weblogs/index.html.erb for the WeblogsController#index action. All views use
- eRuby syntax by default.
-
-app/views/layouts
- Holds the template files for layouts to be used with views. This models the
- common header/footer method of wrapping views. In your views, define a layout
- using the <tt>layout :default</tt> and create a file named default.html.erb.
- Inside default.html.erb, call <% yield %> to render the view using this
- layout.
-
-app/helpers
- Holds view helpers that should be named like weblogs_helper.rb. These are
- generated for you automatically when using generators for controllers.
- Helpers can be used to wrap functionality for your views into methods.
-
-config
- Configuration files for the Rails environment, the routing map, the database,
- and other dependencies.
-
-db
- Contains the database schema in schema.rb. db/migrate contains all the
- sequence of Migrations for your schema.
-
-doc
- This directory is where your application documentation will be stored when
- generated using <tt>rake doc:app</tt>
-
-lib
- Application specific libraries. Basically, any kind of custom code that
- doesn't belong under controllers, models, or helpers. This directory is in
- the load path.
-
-public
- The directory available for the web server. Contains subdirectories for
- images, stylesheets, and javascripts. Also contains the dispatchers and the
- default HTML files. This should be set as the DOCUMENT_ROOT of your web
- server.
-
-script
- Helper scripts for automation and generation.
-
-test
- Unit and functional tests along with fixtures. When using the rails generate
- command, template test files will be generated for you and placed in this
- directory.
-
-vendor
- External libraries that the application depends on. Also includes the plugins
- subdirectory. If the app has frozen rails, those gems also go here, under
- vendor/rails/. This directory is in the load path.
View
46 README.markdown
@@ -0,0 +1,46 @@
+# Gauge, A live status viewer for Rubinius
+
+Gauge is a non-database backed Rails 3.0 application that reads a heap dump from the Rubinius VM and then displays the heap dump in a manner which allows you to interact with the heap snapshot.
+
+## Creating a Heap Dump
+
+Following the instructions from the "Memory Analysis" section of the Rubinius documentation (http://rubini.us/doc/en/tools/memory-analysis/), prepare a dump file. Here is a brief overview of the process. From the docs:
+
+Rubinius provides access to the VM via an agent interface. The agent opens a network socket and responds to commands issued by the console program. The agent must be started with the program.
+
+```bash
+rbx -Xagent.start <script name>
+```
+
+Connect to the agent using the rbx console. This program opens an interactive session with the agent running inside the VM. Commands are issued to the agent. In this case we are saving a heap dump for offline analysis.
+
+```bash
+$ rbx console
+VM: rbx -Xagent.start leak.rb tcp://127.0.0.1:5549 1024 100000000
+Connecting to VM on port 60544
+Connected to localhost:60544, host type: x86_64-apple-darwin10.5.0
+console> set system.memory.dump heap.dump
+console> exit
+```
+
+The command is set system.memory.dump <filename>. The heap dump file is written to the current working directory for the program running the agent.
+
+## Configuring Gauge
+
+Set the ENV['DUMP'] environment variable in config/environment.rb, pointing to a heap dump file you want to analyze.
+
+Run the Gauge app with:
+
+```bash
+rails server
+```
+
+and you will be greeted with a menu. Enjoy!
+
+## Details
+
+Most recently tested with:
+
+```bash
+rubinius 2.0.0dev (1.8.7 96146df9 yyyy-mm-dd JI) [x86_64-apple-darwin10.7.3]
+```
View
4 app/controllers/heap_viewer_controller.rb
@@ -1,10 +1,6 @@
require 'read_dump'
class HeapViewerController < ApplicationController
- def index
- redirect_to :action => :show
- end
-
def show
@session = HeapDumpSession.session
@histogram = @session.histogram
View
2 app/helpers/heap_viewer_helper.rb
@@ -20,7 +20,7 @@ def show_ref(ref)
case ref
when Rubinius::HeapDump::Reference
show_object sess.decoder.deref(ref)
- when Rubinius::HeapDump::Symbol
+ when Rubinius::HeapDump::XSymbol
"#<Symbol #{ref.data.inspect}>"
when Array
ref.map { |x| x.inspect }.join(", ")
View
3 config/environment.rb
@@ -1,5 +1,8 @@
# Load the rails application
require File.expand_path('../application', __FILE__)
+# Set ENV['DUMP'] as such:
+# ENV['DUMP'] = File.expand_path("heap.dump")
+
# Initialize the rails application
Gauge::Application.initialize!
View
58 config/routes.rb
@@ -9,61 +9,5 @@
get "/heap_viewer" => "heap_viewer#show"
-
- # The priority is based upon order of creation:
- # first created -> highest priority.
-
- # Sample of regular route:
- # match 'products/:id' => 'catalog#view'
- # Keep in mind you can assign values other than :controller and :action
-
- # Sample of named route:
- # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
- # This route can be invoked with purchase_url(:id => product.id)
-
- # Sample resource route (maps HTTP verbs to controller actions automatically):
- # resources :products
-
- # Sample resource route with options:
- # resources :products do
- # member do
- # get :short
- # post :toggle
- # end
- #
- # collection do
- # get :sold
- # end
- # end
-
- # Sample resource route with sub-resources:
- # resources :products do
- # resources :comments, :sales
- # resource :seller
- # end
-
- # Sample resource route with more complex sub-resources
- # resources :products do
- # resources :comments
- # resources :sales do
- # get :recent, :on => :collection
- # end
- # end
-
- # Sample resource route within a namespace:
- # namespace :admin do
- # # Directs /admin/products/* to Admin::ProductsController
- # # (app/controllers/admin/products_controller.rb)
- # resources :products
- # end
-
- # You can have the root of your site routed with "root"
- # just remember to delete public/index.html.
- # root :to => "welcome#index"
-
- # See how all your routes lay out with "rake routes"
-
- # This is a legacy wild controller route that's not recommended for RESTful applications.
- # Note: This route will make all actions in every controller accessible via GET requests.
- # match ':controller(/:action(/:id(.:format)))'
+ root :to => "heap_viewer#show"
end
View
197 lib/heap_dump/decoder.rb
@@ -0,0 +1,197 @@
+require 'heap_dump/dumped_object'
+require 'heap_dump/objects'
+
+module Rubinius
+ module HeapDump
+
+ Reference = Struct.new(:id)
+ XSymbol = Struct.new(:id, :data)
+
+ class XSymbol
+ def ==(other)
+ case other
+ when String
+ data == other
+ else
+ super
+ end
+ end
+
+ def <=>(other)
+ data <=> other.data
+ end
+
+ def to_s
+ data
+ end
+ end
+
+ class Decoder
+
+ Tuple = Struct.new(:objects)
+ Bytes = Struct.new(:data)
+
+ def initialize
+ @symbols = []
+ @objects = []
+ @layouts = []
+ end
+
+ attr_reader :symbols, :objects, :layouts
+
+ def deref(id)
+ if id.kind_of? Reference
+ @objects[id.id]
+ else
+ @objects[id]
+ end
+ end
+
+ def int
+ @f.read(4).unpack("N").first
+ end
+
+ def ints(n)
+ @f.read(4*n).unpack("N#{n}")
+ end
+
+ def short
+ @f.read(2).unpack("n").first
+ end
+
+ def char
+ @f.read(1)[0]
+ end
+
+ def decode_reference
+ subcmd = @f.read(1)[0]
+ case subcmd
+ when ?r
+ obj = Reference.new(int)
+ when ?x
+ ref = int
+ obj = @symbols[ref]
+ when ?s
+ id, sz = ints(2)
+ obj = XSymbol.new(id, @f.read(sz))
+ @symbols[id] = obj
+ when ?f
+ obj = int
+ when ?t
+ obj = []
+ sz = int
+ sz.times do
+ obj << decode_reference
+ end
+
+ obj = Tuple.new(obj)
+ when ?b
+ obj = Bytes.new(@f.read(int))
+ when ?i
+ obj = case char
+ when 0
+ false
+ when 1
+ true
+ when 2
+ nil
+ end
+ when ?c
+ sz = int
+ obj = @f.read(sz)
+ else
+ raise "invalid sub code - #{subcmd}, #{subcmd.chr}"
+ end
+
+ return obj
+ end
+
+ def decode(file)
+ File.open(file) do |f|
+ magic = f.read(12)
+ if magic != "RBXHEAPDUMP\0"
+ raise "Invalid file format"
+ end
+
+ @f = f
+
+ version = int
+
+ unless version == 1
+ raise "Invalid version - #{version}"
+ end
+
+ while true
+ str = @f.read(1)
+ break unless str
+ cmd = str[0]
+
+ case cmd
+ when ?s
+ id, sz = ints(2)
+ str = @f.read(sz)
+ @symbols[id] = str
+ when ?o
+ id, bytes, layout, klass = ints(4)
+ ivar_ref = decode_reference
+ syms = short # , syms = @f.read(18).unpack("NNNNn")
+ ivars = []
+
+ syms.times do
+ ivars << decode_reference
+ end
+
+ if @objects[id]
+ raise "redefined object #{id}"
+ end
+
+ @objects[id] = DumpedObject.new(self, id, bytes,
+ @layouts[layout],
+ Reference.new(klass),
+ Reference.new(ivar_ref), ivars)
+ when ?l
+ id, sz = ints(2)
+ syms = ints(sz)
+ @layouts[id] = syms.map { |x| @symbols[x] }
+ when ?- # footer
+ @object = @objects[int].as_module
+ @klass = @objects[int].as_module
+ @module = @objects[int].as_module
+ else
+ raise "invalid code #{cmd}"
+ end
+ end
+ end
+
+ @included_module = @object.find_class("IncludedModule")
+ @f = nil
+ end
+
+ def Object
+ @object
+ end
+
+ def Class
+ @klass
+ end
+
+ def Module
+ @module
+ end
+
+ def all_objects
+ return Objects.new(@objects)
+ end
+
+ def find_references(id)
+ out = []
+ @objects.each do |o|
+ out << o if o.references?(id)
+ end
+
+ return out
+ end
+ end
+
+ end
+end
View
30 lib/heap_dump/diff.rb
@@ -0,0 +1,30 @@
+module Rubinius
+ module HeapDump
+ class Diff
+ def initialize(before, after)
+ @before = before
+ @after = after
+ end
+
+ def histogram
+ b = Histogram.by_class_name @before.all_objects.array
+ a = Histogram.by_class_name @after.all_objects.array
+
+ c = {}
+
+ a.each do |k,e|
+ if prev = b[k]
+ n = e - prev
+ if n.objects != 0
+ c[k] = n
+ end
+ else
+ c[k] = e
+ end
+ end
+
+ Histogram.new(c)
+ end
+ end
+ end
+end
View
26 lib/heap_dump/dumped_lookup_table.rb
@@ -0,0 +1,26 @@
+require 'heap_dump/specializer'
+
+module Rubinius
+ module HeapDump
+ class DumpedLookupTable < Specializer
+ def each
+ @object["@values"].data.objects.each do |ref|
+ if ref
+ obj = @object.decoder.deref(ref)
+ while obj
+ yield obj["@key"], obj["@value"]
+
+ obj = obj["@next"]
+ end
+ end
+ end
+ end
+
+ include Enumerable
+
+ def keys
+ map { |k,v| k.data }
+ end
+ end
+ end
+end
View
26 lib/heap_dump/dumped_method_table.rb
@@ -0,0 +1,26 @@
+require 'heap_dump/specializer'
+
+module Rubinius
+ module HeapDump
+ class DumpedMethodTable < Specializer
+ def each
+ @object["@values"].data.objects.each do |ref|
+ if ref
+ obj = @object.decoder.deref(ref)
+ while obj
+ yield obj["@name"], obj["@method"], obj["@visibility"]
+
+ obj = obj["@next"]
+ end
+ end
+ end
+ end
+
+ include Enumerable
+
+ def keys
+ map { |k,v| k.data }
+ end
+ end
+ end
+end
View
75 lib/heap_dump/dumped_module.rb
@@ -0,0 +1,75 @@
+require 'heap_dump/specializer'
+require 'heap_dump/dumped_lookup_table'
+require 'heap_dump/dumped_method_table'
+
+module Rubinius
+ module HeapDump
+ class DumpedModule < Specializer
+ def name
+ if sym = @object["@module_name"]
+ sym.data
+ end
+ end
+
+ def superclass
+ if obj = @object["@superclass"]
+ return obj.as_module
+ end
+
+ return nil
+ end
+
+ def constant_table
+ @constants ||= DumpedLookupTable.new(@object["@constant_table"])
+ end
+
+ def constants
+ constant_table.map do |key, val|
+ key.data
+ end
+ end
+
+ def method_table
+ @methods ||= DumpedMethodTable.new(@object["@method_table"])
+ end
+
+ def find_class(name)
+ name = name.to_s
+ constant_table.map do |key, val|
+ return val.as_module if key.data == name
+ end
+
+ nil
+ end
+
+ def constant(name)
+ name = name.to_s
+ constant_table.map do |key, val|
+ if key.data == name
+ if val.dump_kind_of?(@object.decoder.Module)
+ return val.as_module
+ else
+ return val
+ end
+ end
+ end
+
+ nil
+ end
+
+ def all_instances
+ ary = []
+ @object.decoder.objects.each do |obj|
+ ary << obj if obj.class_object.id == @object.id
+ end
+
+ return Objects.new(ary)
+ end
+
+ def metaclass?
+ !@object["@attached_instance"].nil?
+ end
+ end
+
+ end
+end
View
111 lib/heap_dump/dumped_object.rb
@@ -0,0 +1,111 @@
+require 'heap_dump/dumped_module'
+
+module Rubinius
+ module HeapDump
+ class DumpedObject
+ def initialize(decoder, id, bytes, layout, klass, ivar_ref, ivars)
+ @decoder = decoder
+ @id = id
+ @bytes = bytes
+ @layout = layout
+ @ivars = ivars
+ @ivar_ref = ivar_ref
+ @klass = klass
+ end
+
+ attr_reader :decoder
+ attr_reader :id, :bytes, :layout, :ivars, :klass, :ivar_ref
+
+ def raw_ivar(name)
+ idx = @layout.index(name)
+ return nil unless idx
+
+ return @ivars[idx]
+ end
+
+ def [](name)
+ ref = raw_ivar(name)
+ if ref.kind_of? Reference
+ return @decoder.objects[ref.id]
+ end
+
+ return ref
+ end
+
+ def to_hash
+ hsh = {}
+ @layout.each_with_index do |name, idx|
+ hsh[name] = @ivars[idx]
+ end
+
+ hsh
+ end
+
+ def each_ivar
+ @layout.each_with_index do |name, idx|
+ yield name, @ivars[idx]
+ end
+ end
+
+ def direct_class_object
+ @klass_object ||= @decoder.objects[@klass.id].as_module
+ end
+
+ def dump_kind_of?(cls)
+ start = direct_class_object
+
+ while start
+ return true if start.object == cls.object
+ start = start.superclass
+ end
+
+ return false
+ end
+
+ def class_object
+ start = direct_class_object
+ return start unless start.metaclass?
+
+ start = start.superclass
+ cls = @decoder.Class
+
+ while start
+ return start if start.object.dump_kind_of?(cls) and !start.metaclass?
+ start = start.superclass
+ end
+
+ raise "Unable to figure out class"
+ end
+
+ def as_module
+ @as_module ||= DumpedModule.new(self)
+ end
+
+ def data
+ @ivars[@layout.size]
+ end
+
+ def inspect
+ "#<DumpedObject id=#{@id} klass=#{@klass} #{to_hash.inspect}>"
+ end
+
+ def referers
+ @referers ||= Objects.new(@decoder.find_references(@id))
+ end
+
+ def references?(other)
+ @ivars.each do |i|
+ return true if i.kind_of? Reference and i.id == other
+ end
+
+ if d = data and d.kind_of?(HeapDump::Decoder::Tuple)
+ d.objects.each do |i|
+ return true if i.kind_of? Reference and i.id == other
+ end
+ end
+
+ return false
+ end
+ end
+ end
+end
View
86 lib/heap_dump/histogram.rb
@@ -0,0 +1,86 @@
+module Rubinius
+ module HeapDump
+ class Histogram
+ class Entry
+ def initialize(klass, objects=0, bytes=0)
+ @klass = klass
+ @objects = objects
+ @bytes = bytes
+ end
+
+ attr_reader :objects, :bytes
+
+ def inc(object)
+ @objects += 1
+ @bytes += object.bytes
+ end
+
+ def <=>(other)
+ @objects <=> other.objects
+ end
+
+ def -(other)
+ objects = @objects - other.objects
+ bytes = @bytes - other.bytes
+
+ Entry.new(@klass, objects, bytes)
+ end
+ end
+
+ def initialize(data)
+ @data = data
+ end
+
+ def self.by_class(objects)
+ histogram = Hash.new { |h,k| h[k] = Entry.new(k) }
+
+ objects.each do |o|
+ klass = o.class_object
+
+ if klass.name
+ histogram[klass].inc(o)
+ end
+ end
+
+ return Histogram.new(histogram)
+ end
+
+ def self.by_class_name(objects)
+ histogram = Hash.new { |h,k| h[k] = Entry.new(k) }
+
+ objects.each do |o|
+ klass = o.class_object
+
+ if n = klass.name
+ histogram[n].inc(o)
+ end
+ end
+
+ return Histogram.new(histogram)
+ end
+
+ def [](cls)
+ @data[cls]
+ end
+
+ def to_text
+ each_sorted do |klass, entry|
+ puts "%10d %s (%d bytes)" % [entry.objects, klass.name, entry.bytes]
+ end
+ end
+
+ def each
+ @data.each do |k,e|
+ yield k, e
+ end
+ end
+
+ def each_sorted
+ sorted = @data.to_a.sort_by { |x| x[1] }
+ sorted.reverse_each do |klass, entry|
+ yield klass, entry
+ end
+ end
+ end
+ end
+end
View
54 lib/heap_dump/objects.rb
@@ -0,0 +1,54 @@
+require 'heap_dump/histogram'
+
+module Rubinius
+ module HeapDump
+ class Objects
+ def initialize(ary)
+ @array = ary
+ end
+
+ attr_reader :array
+
+ def histogram
+ Histogram.by_class(@array)
+ end
+
+ def referers
+ ary = []
+
+ # Use a hash to speed up checking. Doing a #include? check on @array
+ # is pretty slow.
+ hsh = {}
+
+ @array.each do |obj|
+ hsh[obj.id] = true
+ end
+
+ @array[0].decoder.objects.each do |obj|
+ obj.ivars.each do |iv|
+ ary << obj if iv.kind_of?(Reference) && hsh[iv.id]
+ end
+ end
+
+ return Objects.new(ary.uniq)
+ end
+
+ def size
+ @array.size
+ end
+
+ def byte_size
+ @bytes ||= @array.inject(0) { |acc,o| acc + o.bytes }
+ end
+
+ def [](obj)
+ @array[obj]
+ end
+
+ def inspect
+ "#<#{self.class} size=#{size}>"
+ end
+ end
+
+ end
+end
View
27 lib/heap_dump/specializer.rb
@@ -0,0 +1,27 @@
+module Rubinius
+ module HeapDump
+ class Specializer
+ def initialize(obj)
+ @object = obj
+ end
+
+ attr_reader :object
+
+ def ==(obj)
+ @object == obj || super
+ end
+
+ def method_missing(msg, *args)
+ if @object.respond_to?(msg)
+ @object.__send__(msg, *args)
+ else
+ super
+ end
+ end
+
+ def id
+ @object.id
+ end
+ end
+ end
+end
View
530 lib/read_dump.rb
@@ -1,532 +1,12 @@
+require 'heap_dump/decoder'
+
module Rubinius
module HeapDump
+ def self.open(file)
+ dec = Decoder.new
- class DumpedObject
- def initialize(decoder, id, bytes, layout, klass, ivar_ref, ivars)
- @decoder = decoder
- @id = id
- @bytes = bytes
- @layout = layout
- @ivars = ivars
- @ivar_ref = ivar_ref
- @klass = klass
- end
-
- attr_reader :decoder
- attr_reader :id, :bytes, :layout, :ivars, :klass, :ivar_ref
-
- def raw_ivar(name)
- idx = @layout.index(name)
- return nil unless idx
-
- return @ivars[idx]
- end
-
- def [](name)
- ref = raw_ivar(name)
- if ref.kind_of? Reference
- return @decoder.objects[ref.id]
- end
-
- return ref
- end
-
- def to_hash
- hsh = {}
- @layout.each_with_index do |name, idx|
- hsh[name] = @ivars[idx]
- end
-
- hsh
- end
-
- def each_ivar
- @layout.each_with_index do |name, idx|
- yield name, @ivars[idx]
- end
- end
-
- def direct_class_object
- @klass_object ||= @decoder.objects[@klass.id].as_module
- end
-
- def dump_kind_of?(cls)
- start = direct_class_object
-
- while start
- return true if start.object == cls.object
- start = start.superclass
- end
-
- return false
- end
-
- def class_object
- start = direct_class_object
- return start unless start.metaclass?
-
- start = start.superclass
- cls = @decoder.Class
-
- while start
- return start if start.object.dump_kind_of?(cls) and !start.metaclass?
- start = start.superclass
- end
-
- raise "Unable to figure out class"
- end
-
- def as_module
- @as_module ||= DumpedModule.new(self)
- end
-
- def data
- @ivars[@layout.size]
- end
-
- def inspect
- "#<DumpedObject id=#{@id} klass=#{@klass} #{to_hash.inspect}>"
- end
-
- def referers
- @referers ||= Objects.new(@decoder.find_references(@id))
- end
-
- def references?(other)
- @ivars.each do |i|
- return true if i.kind_of? Reference and i.id == other
- end
-
- if d = data and d.kind_of?(HeapDump::Decoder::Tuple)
- d.objects.each do |i|
- return true if i.kind_of? Reference and i.id == other
- end
- end
-
- return false
- end
- end
-
- class Specializer
- def initialize(obj)
- @object = obj
- end
-
- attr_reader :object
-
- def ==(obj)
- @object == obj || super
- end
-
- def method_missing(msg, *args)
- if @object.respond_to?(msg)
- @object.__send__(msg, *args)
- else
- super
- end
- end
-
- def id
- @object.id
- end
- end
-
- class DumpedLookupTable < Specializer
- def each
- @object["@values"].data.objects.each do |ref|
- if ref
- obj = @object.decoder.deref(ref)
- while obj
- yield obj["@key"], obj["@value"]
-
- obj = obj["@next"]
- end
- end
- end
- end
-
- include Enumerable
-
- def keys
- map { |k,v| k.data }
- end
- end
-
- class DumpedMethodTable < Specializer
- def each
- @object["@values"].data.objects.each do |ref|
- if ref
- obj = @object.decoder.deref(ref)
- while obj
- yield obj["@name"], obj["@method"], obj["@visibility"]
-
- obj = obj["@next"]
- end
- end
- end
- end
-
- include Enumerable
-
- def keys
- map { |k,v| k.data }
- end
- end
-
- class DumpedModule < Specializer
- def name
- if sym = @object["@module_name"]
- sym.data
- end
- end
-
- def superclass
- if obj = @object["@superclass"]
- return obj.as_module
- end
-
- return nil
- end
-
- def constant_table
- @constants ||= DumpedLookupTable.new(@object["@constants"])
- end
-
- def constants
- constant_table.map do |key, val|
- key.data
- end
- end
-
- def method_table
- @methods ||= DumpedMethodTable.new(@object["@method_table"])
- end
-
- def find_class(name)
- name = name.to_s
- constant_table.map do |key, val|
- return val.as_module if key.data == name
- end
-
- nil
- end
-
- def constant(name)
- name = name.to_s
- constant_table.map do |key, val|
- if key.data == name
- if val.dump_kind_of?(@object.decoder.Module)
- return val.as_module
- else
- return val
- end
- end
- end
-
- nil
- end
-
- def all_instances
- ary = []
- @object.decoder.objects.each do |obj|
- ary << obj if obj.class_object.id == @object.id
- end
-
- return Objects.new(ary)
- end
-
- def metaclass?
- !@object["@attached_instance"].nil?
- end
- end
-
- Reference = Struct.new(:id)
- Symbol = Struct.new(:id, :data)
-
- class Symbol
- def ==(other)
- case other
- when String
- data == other
- else
- super
- end
- end
-
- def <=>(other)
- data <=> other.data
- end
-
- def to_s
- data
- end
- end
-
- class Decoder
-
- Tuple = Struct.new(:objects)
- Bytes = Struct.new(:data)
-
- def initialize
- @symbols = []
- @objects = []
- @layouts = []
- end
-
- attr_reader :symbols, :objects, :layouts
-
- def deref(id)
- if id.kind_of? Reference
- @objects[id.id]
- else
- @objects[id]
- end
- end
-
- def int
- @f.read(4).unpack("N").first
- end
-
- def ints(n)
- @f.read(4*n).unpack("N#{n}")
- end
-
- def short
- @f.read(2).unpack("n").first
- end
-
- def char
- @f.read(1)[0]
- end
-
- def decode_reference
- subcmd = @f.read(1)[0]
- case subcmd
- when ?r
- obj = Reference.new(int)
- when ?x
- ref = int
- obj = @symbols[ref]
- when ?s
- id, sz = ints(2)
- obj = Symbol.new(id, @f.read(sz))
- @symbols[id] = obj
- when ?f
- obj = int
- when ?t
- obj = []
- sz = int
- sz.times do
- obj << decode_reference
- end
-
- obj = Tuple.new(obj)
- when ?b
- obj = Bytes.new(@f.read(int))
- when ?i
- obj = case char
- when 0
- false
- when 1
- true
- when 2
- nil
- end
- else
- raise "invalid sub code - #{subcmd}, #{subcmd.chr}"
- end
-
- return obj
- end
-
- def decode(file)
- File.open(file) do |f|
- magic = f.read(12)
- if magic != "RBXHEAPDUMP\0"
- raise "Invalid file format"
- end
-
- @f = f
-
- version = int
-
- unless version == 1
- raise "Invalid version - #{version}"
- end
-
- while true
- str = @f.read(1)
- break unless str
- cmd = str[0]
-
- case cmd
- when ?s
- id, sz = ints(2)
- str = @f.read(sz)
- @symbols[id] = str
- when ?o
- id, bytes, layout, klass = ints(4)
- ivar_ref = decode_reference
- syms = short # , syms = @f.read(18).unpack("NNNNn")
- ivars = []
-
- syms.times do
- ivars << decode_reference
- end
-
- if @objects[id]
- raise "redefined object #{id}"
- end
-
- @objects[id] = DumpedObject.new(self, id, bytes,
- @layouts[layout],
- Reference.new(klass),
- Reference.new(ivar_ref), ivars)
- when ?l
- id, sz = ints(2)
- syms = ints(sz)
- @layouts[id] = syms.map { |x| @symbols[x] }
- when ?- # footer
- @object = @objects[int].as_module
- @klass = @objects[int].as_module
- @module = @objects[int].as_module
- else
- raise "invalid code #{cmd}"
- end
- end
- end
-
- @included_module = @object.find_class("IncludedModule")
- @f = nil
- end
-
- def Object
- @object
- end
-
- def Class
- @klass
- end
-
- def Module
- @module
- end
-
- def all_objects
- return Objects.new(@objects)
- end
-
- def find_references(id)
- out = []
- @objects.each do |o|
- out << o if o.references?(id)
- end
-
- return out
- end
- end
-
- class Histogram
- def initialize(data)
- @data = data
- end
-
- class Entry
- def initialize(klass)
- @klass = klass
- @objects = 0
- @bytes = 0
- end
-
- attr_reader :objects, :bytes
-
- def inc(object)
- @objects += 1
- @bytes += object.bytes
- end
-
- def <=>(other)
- @objects <=> other.objects
- end
- end
-
- def self.by_class(objects)
- histogram = Hash.new { |h,k| h[k] = Entry.new(k) }
-
- objects.each do |o|
- klass = o.class_object
-
- if klass.name
- histogram[klass].inc(o)
- end
- end
-
- return Histogram.new(histogram)
- end
-
- def [](cls)
- @data[cls]
- end
-
- def to_text
- each_sorted do |klass, entry|
- puts "%10d %s (%d bytes)" % [entry.objects, klass.name, entry.bytes]
- end
- end
-
- def each_sorted
- sorted = @data.to_a.sort_by { |x| x[1] }
- sorted.reverse_each do |klass, entry|
- yield klass, entry
- end
- end
- end
-
- class Objects
- def initialize(ary)
- @array = ary
- end
-
- attr_reader :array
-
- def histogram
- Histogram.by_class(@array)
- end
-
- def referers
- ary = []
-
- # Use a hash to speed up checking. Doing a #include? check on @array
- # is pretty slow.
- hsh = {}
-
- @array.each do |obj|
- hsh[obj.id] = true
- end
-
- @array[0].decoder.objects.each do |obj|
- obj.ivars.each do |iv|
- ary << obj if iv.kind_of?(Reference) && hsh[iv.id]
- end
- end
-
- return Objects.new(ary.uniq)
- end
-
- def size
- @array.size
- end
-
- def byte_size
- @bytes ||= @array.inject(0) { |acc,o| acc + o.bytes }
- end
-
- def [](obj)
- @array[obj]
- end
+ File.open(file) do |f|
- def inspect
- "#<#{self.class} size=#{size}>"
end
end
end
View
279 public/index.html
@@ -1,279 +0,0 @@
-<!DOCTYPE html>
-<html>
- <head>
- <title>Ruby on Rails: Welcome aboard</title>
- <style type="text/css" media="screen">
- body {
- margin: 0;
- margin-bottom: 25px;
- padding: 0;
- background-color: #f0f0f0;
- font-family: "Lucida Grande", "Bitstream Vera Sans", "Verdana";
- font-size: 13px;
- color: #333;
- }
-
- h1 {
- font-size: 28px;
- color: #000;
- }
-
- a {color: #03c}
- a:hover {
- background-color: #03c;
- color: white;
- text-decoration: none;
- }
-
-
- #page {
- background-color: #f0f0f0;
- width: 750px;
- margin: 0;
- margin-left: auto;
- margin-right: auto;
- }
-
- #content {
- float: left;
- background-color: white;
- border: 3px solid #aaa;
- border-top: none;
- padding: 25px;
- width: 500px;
- }
-
- #sidebar {
- float: right;
- width: 175px;
- }
-
- #footer {
- clear: both;
- }
-
-
- #header, #about, #getting-started {
- padding-left: 75px;
- padding-right: 30px;
- }
-
-
- #header {
- background-image: url("images/rails.png");
- background-repeat: no-repeat;
- background-position: top left;
- height: 64px;
- }
- #header h1, #header h2 {margin: 0}
- #header h2 {
- color: #888;
- font-weight: normal;
- font-size: 16px;
- }
-
-
- #about h3 {
- margin: 0;
- margin-bottom: 10px;
- font-size: 14px;
- }
-
- #about-content {
- background-color: #ffd;
- border: 1px solid #fc0;
- margin-left: -55px;
- margin-right: -10px;
- }
- #about-content table {
- margin-top: 10px;
- margin-bottom: 10px;
- font-size: 11px;
- border-collapse: collapse;
- }
- #about-content td {
- padding: 10px;
- padding-top: 3px;
- padding-bottom: 3px;
- }
- #about-content td.name {color: #555}
- #about-content td.value {color: #000}
-
- #about-content ul {
- padding: 0;
- list-style-type: none;
- }
-
- #about-content.failure {
- background-color: #fcc;
- border: 1px solid #f00;
- }
- #about-content.failure p {
- margin: 0;
- padding: 10px;
- }
-
-
- #getting-started {
- border-top: 1px solid #ccc;
- margin-top: 25px;
- padding-top: 15px;
- }
- #getting-started h1 {
- margin: 0;
- font-size: 20px;
- }
- #getting-started h2 {
- margin: 0;
- font-size: 14px;
- font-weight: normal;
- color: #333;
- margin-bottom: 25px;
- }
- #getting-started ol {
- margin-left: 0;
- padding-left: 0;
- }
- #getting-started li {
- font-size: 18px;
- color: #888;
- margin-bottom: 25px;
- }
- #getting-started li h2 {
- margin: 0;
- font-weight: normal;
- font-size: 18px;
- color: #333;
- }
- #getting-started li p {
- color: #555;
- font-size: 13px;
- }
-
-
- #search {
- margin: 0;
- padding-top: 10px;
- padding-bottom: 10px;
- font-size: 11px;
- }
- #search input {
- font-size: 11px;
- margin: 2px;
- }
- #search-text {width: 170px}
-
-
- #sidebar ul {
- margin-left: 0;
- padding-left: 0;
- }
- #sidebar ul h3 {
- margin-top: 25px;
- font-size: 16px;
- padding-bottom: 10px;
- border-bottom: 1px solid #ccc;
- }
- #sidebar li {
- list-style-type: none;
- }
- #sidebar ul.links li {
- margin-bottom: 5px;
- }
-
- </style>
- <script type="text/javascript">
- function about() {
- info = document.getElementById('about-content');
- if (window.XMLHttpRequest)
- { xhr = new XMLHttpRequest(); }
- else
- { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
- xhr.open("GET","rails/info/properties",false);
- xhr.send("");
- info.innerHTML = xhr.responseText;
- info.style.display = 'block'
- }
-
- function prepend() {
- search = document.getElementById('search-text');
- text = search.value;
- search.value = 'site:rubyonrails.org ' + text;
- }
-
- window.onload = function() {
- document.getElementById('search-text').value = '';
- }
- </script>
- </head>
- <body>
- <div id="page">
- <div id="sidebar">
- <ul id="sidebar-items">
- <li>
- <form id="search" action="http://www.google.com/search" method="get" onSubmit="prepend();">
- <input type="hidden" name="hl" value="en" />
- <input type="text" id="search-text" name="q" value="site:rubyonrails.org " />
- <input type="submit" value="Search" /> the Rails site
- </form>
- </li>
-
- <li>
- <h3>Join the community</h3>
- <ul class="links">
- <li><a href="http://www.rubyonrails.org/">Ruby on Rails</a></li>
- <li><a href="http://weblog.rubyonrails.org/">Official weblog</a></li>
- <li><a href="http://wiki.rubyonrails.org/">Wiki</a></li>
- </ul>
- </li>
-
- <li>
- <h3>Browse the documentation</h3>
- <ul class="links">
- <li><a href="http://api.rubyonrails.org/">Rails API</a></li>
- <li><a href="http://stdlib.rubyonrails.org/">Ruby standard library</a></li>
- <li><a href="http://corelib.rubyonrails.org/">Ruby core</a></li>
- <li><a href="http://guides.rubyonrails.org/">Rails Guides</a></li>
- </ul>
- </li>
- </ul>
- </div>
-
- <div id="content">
- <div id="header">
- <h1>Welcome aboard</h1>
- <h2>You&rsquo;re riding Ruby on Rails!</h2>
- </div>
-
- <div id="about">
- <h3><a href="rails/info/properties" onclick="about(); return false">About your application&rsquo;s environment</a></h3>
- <div id="about-content" style="display: none"></div>
- </div>
-
- <div id="getting-started">
- <h1>Getting started</h1>
- <h2>Here&rsquo;s how to get rolling:</h2>
-
- <ol>
- <li>
- <h2>Use <code>rails generate</code> to create your models and controllers</h2>
- <p>To see all available options, run it without parameters.</p>
- </li>
-
- <li>
- <h2>Set up a default route and remove or rename this file</h2>
- <p>Routes are set up in config/routes.rb.</p>
- </li>
-
- <li>
- <h2>Create your database</h2>
- <p>Run <code>rake db:migrate</code> to create your database. If you're not using SQLite (the default), edit <code>config/database.yml</code> with your username and password.</p>
- </li>
- </ol>
- </div>
- </div>
-
- <div id="footer">&nbsp;</div>
- </div>
- </body>
-</html>

0 comments on commit 933e0ac

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