ruby-memory-usage-profiler 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 475184e2462d265a1a8c54cb87b471d6f68cf01f
4
+ data.tar.gz: 0a68277dd07cfdddccc01815df9ea812e3a0919c
5
+ SHA512:
6
+ metadata.gz: 8fb74d9aca323224dd24cabda7d513d8138a711b0c7a4e7380dc0a7d2bcd3e357b10079093ec261caf7c65eedec23ac57c790963b96fd066c9f6015b69917cbc
7
+ data.tar.gz: d61a513756a06d51aeb1fa4b22f6abe91a65756fb1330baf40486a06e3a01eab128793f1c34c36ce965ed4f46d3abbf767ca4fbab979aaf7ce9c65bf9d287dbe
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby-memory-usage-profiler.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TAGOMORI Satoshi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Ruby's MemoryUsageProfiler
2
+
3
+ Collect memory usage profiling informations from CRuby runtime and Linux pseudo filesystems.
4
+
5
+ Originally written by @_ko1 on URL below.
6
+ * http://www.atdot.net/sp/view/fd73um
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'ruby-memory-usage-profiler'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install ruby-memory-usage-profiler
21
+
22
+ ## Usage
23
+
24
+ To run profiler itself (without any other ruby code):
25
+
26
+ $ run_ruby_memory_usage_profiler
27
+
28
+ This program writes profiler result in 'memory-profile-result' of current directory, in each 1 second.
29
+
30
+ $ run_ruby_memory_usage_profiler [DURATION sec] [OUTPUT PATH]
31
+
32
+ For stdout, specify '-'
33
+
34
+ $ run_ruby_memory_usage_profiler 5 -
35
+
36
+ ### From your own code
37
+
38
+ Kick `MemoryUsageProfiler#kick` with block how to process results each time you want.
39
+
40
+ each_time_you_want do
41
+ MemoryUsageProfiler.kick('my_program_label') do |result|
42
+ send_to_anywhere_by_myself(result)
43
+ end
44
+ end
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
53
+
54
+ ## Author
55
+
56
+ * @_ko1
57
+ * @tagomoris
58
+
59
+ ## License
60
+
61
+ * This is licensed by the license ruby itself
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'memory_usage_profiler'
4
+
5
+ trap(:INT){ MemoryUsageProfiler.stop_thread }
6
+ trap(:TERM){ MemoryUsageProfiler.stop_thread }
7
+ trap(:HUP){ MemoryUsageProfiler.stop_thread }
8
+
9
+ thread = case ARGV.size
10
+ when 2
11
+ MemoryUsageProfiler.start_thread(ARGV[0].to_i, ARGV[1])
12
+ when 1
13
+ MemoryUsageProfiler.start_thread(ARGV[0].to_i)
14
+ when 0
15
+ MemoryUsageProfiler.start_thread
16
+ end
17
+ thread.join
@@ -0,0 +1,104 @@
1
+ require 'memory_usage_profiler/version'
2
+
3
+ require 'objspace'
4
+
5
+ module MemoryUsageProfiler
6
+
7
+ MEMORY_PROFILE_GC_STAT_HASH = {}
8
+ MEMORY_PROFILE_BANNER = ['name']
9
+ MEMORY_PROFILE_PROCS = []
10
+ MEMORY_PROFILE_DURATION = (ENV['RUBY_MEMORY_PROFILE_DURATION'] || 1).to_i
11
+ MEMORY_PROFILE_OUTPUT_PATH = ENV['RUBY_MEMORY_PROFILE'] || 'memory-profile-result'
12
+
13
+ def self.add(*name, &b)
14
+ MEMORY_PROFILE_BANNER.concat name
15
+ MEMORY_PROFILE_PROCS << b
16
+ end
17
+
18
+ if GC.respond_to?(:malloc_allocated_size)
19
+ add 'malloc_allocated_size', 'malloc_allocations' do |result|
20
+ result.concat [GC.malloc_allocated_size, GC.malloc_allocations]
21
+ end
22
+ end
23
+
24
+ add 'memsize_of_all' do |result|
25
+ result << ObjectSpace.memsize_of_all
26
+ end
27
+
28
+ add(*GC.stat.keys) do |result|
29
+ GC.stat(MEMORY_PROFILE_GC_STAT_HASH)
30
+ result.concat MEMORY_PROFILE_GC_STAT_HASH.values
31
+ end
32
+
33
+ def self.add_proc_meminfo(file, fields)
34
+ return unless FileTest.exist?(file)
35
+ regexp = /(#{fields.join("|")}):\s*(\d+) kB/
36
+ # check = {}; fields.each{|e| check[e] = true}
37
+ add(*fields) do |result|
38
+ text = File.read(file)
39
+ text.scan(regexp){
40
+ # check.delete $1
41
+ result << $2
42
+ ''
43
+ }
44
+ # raise check.inspect unless check.empty?
45
+ end
46
+ end
47
+
48
+ add_proc_meminfo '/proc/meminfo', %w(MemTotal MemFree)
49
+ add_proc_meminfo '/proc/self/status', %w(VmPeak VmSize VmHWM VmRSS)
50
+
51
+ if FileTest.exist?('/proc/self/statm')
52
+ add(*%w(size resident share text lib data dt)) do |result|
53
+ result.concat File.read('/proc/self/statm').split(/\s+/)
54
+ end
55
+ end
56
+
57
+ def self.banner
58
+ MEMORY_PROFILE_BANNER.join("\t")
59
+ end
60
+
61
+ def self.kick(name, &callback)
62
+ result = [name.to_s]
63
+ MEMORY_PROFILE_PROCS.each{|pr|
64
+ pr.call(result)
65
+ }
66
+ callback.call(result.join("\t"))
67
+ end
68
+
69
+ def self.start_thread(duration=MEMORY_PROFILE_DURATION, file=MEMORY_PROFILE_OUTPUT_PATH)
70
+ require 'time'
71
+
72
+ file = if file == '-'
73
+ STDOUT
74
+ else
75
+ open(file, 'w')
76
+ end
77
+ file.sync = true
78
+
79
+ file.puts banner
80
+
81
+ @@thread_running = true
82
+
83
+ Thread.new do
84
+ Thread.current.abort_on_exception = true
85
+ while @@thread_running
86
+ kick(Time.now.iso8601) { |result|
87
+ file.puts result
88
+ sleep duration
89
+ }
90
+ end
91
+ end
92
+ end
93
+
94
+ def self.stop_thread
95
+ @@thread_running = false
96
+ end
97
+ end
98
+ # if __FILE__ == $0
99
+ # MemoryUsageProfiler.periodical_profile
100
+ # 1_000_000_000.times{
101
+ # ''
102
+ # }
103
+ # p :exit
104
+ # end
@@ -0,0 +1,3 @@
1
+ module MemoryUsageProfiler
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'memory_usage_profiler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby-memory-usage-profiler"
8
+ spec.version = MemoryUsageProfiler::VERSION
9
+ spec.authors = ["TAGOMORI Satoshi"]
10
+ spec.email = ["tagomoris@gmail.com"]
11
+ spec.description = %q{Memory usage profiler for debugging of Ruby itself (and/or middlewares)}
12
+ spec.summary = %q{CRuby and linux memory usage profiler by @_ko1}
13
+ spec.homepage = "https://github.com/tagomoris/ruby-memory-usage-profiler"
14
+ spec.license = "Ruby's"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-memory-usage-profiler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - TAGOMORI Satoshi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Memory usage profiler for debugging of Ruby itself (and/or middlewares)
42
+ email:
43
+ - tagomoris@gmail.com
44
+ executables:
45
+ - run_ruby_memory_usage_profiler
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/run_ruby_memory_usage_profiler
55
+ - lib/memory_usage_profiler.rb
56
+ - lib/memory_usage_profiler/version.rb
57
+ - ruby-memory-usage-profiler.gemspec
58
+ homepage: https://github.com/tagomoris/ruby-memory-usage-profiler
59
+ licenses:
60
+ - Ruby's
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.3
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: CRuby and linux memory usage profiler by @_ko1
82
+ test_files: []
83
+ has_rdoc: