sigdump 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/ChangeLog +5 -0
- data/Gemfile +3 -0
- data/LICENSE +20 -0
- data/README.md +22 -0
- data/Rakefile +5 -0
- data/lib/sigdump.rb +102 -0
- data/lib/sigdump/setup.rb +7 -0
- data/sigdump.gemspec +20 -0
- metadata +73 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Sadayuki Furuhashi
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
20
|
+
|
data/README.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# sigdump
|
2
|
+
|
3
|
+
Server applications (like Rails app) cause performance problems, deadlock or memory swapping from time to time. But it's painful to reproduce such kind of problems. If we can get information from a running process without restarting it, and it's really helpful.
|
4
|
+
|
5
|
+
`sigdump` gem installs a signal handler which dumps backtrace of running threads and number of allocated objects per class.
|
6
|
+
|
7
|
+
# Install
|
8
|
+
|
9
|
+
Just install one gem `sigdump` and require `sigdump/setup`:
|
10
|
+
|
11
|
+
gem 'sigdump', :require => 'sigdump/setup'
|
12
|
+
|
13
|
+
# Usage
|
14
|
+
|
15
|
+
Send `SIGCONT` signal to dump backtrace and heap status to `/tmp/sigdump-<pid>.log`:
|
16
|
+
|
17
|
+
$ kill -CONT <pid>
|
18
|
+
|
19
|
+
Set `SIGDUMP_SIGNAL` environment variable to change the signal (default: SIGCONT).
|
20
|
+
|
21
|
+
Set `SIGDUMP_PATH` environment variable to change the output path (default: /tmp/sigdump-\<pid\>.log). You can set "-" here to dump to STDOUT, "+" to dump to STDERR.
|
22
|
+
|
data/Rakefile
ADDED
data/lib/sigdump.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
module Sigdump
|
2
|
+
VERSION = "0.1.0"
|
3
|
+
|
4
|
+
def self.install_thread_dump_handler(signal, path=nil)
|
5
|
+
Kernel.trap(signal) do
|
6
|
+
begin
|
7
|
+
_open_dump_path(path) do |io|
|
8
|
+
io.write "Sigdump at #{Time.now} process #{Process.pid} (#{$0})\n"
|
9
|
+
dump_all_thread_backtrace(io)
|
10
|
+
dump_object_count(io)
|
11
|
+
end
|
12
|
+
rescue
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.dump_all_thread_backtrace(io)
|
18
|
+
Thread.list.each do |thread|
|
19
|
+
dump_backtrace(thread, io)
|
20
|
+
end
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.dump_backtrace(thread, io)
|
25
|
+
status = thread.status
|
26
|
+
if status == nil
|
27
|
+
status = "finished"
|
28
|
+
elsif status == false
|
29
|
+
status = "error"
|
30
|
+
end
|
31
|
+
|
32
|
+
backtrace = thread.backtrace
|
33
|
+
|
34
|
+
io.write " Thread #{thread} status=#{status} priority=#{thread.priority}\n"
|
35
|
+
backtrace.each {|bt|
|
36
|
+
io.write " #{bt}\n"
|
37
|
+
}
|
38
|
+
|
39
|
+
io.flush
|
40
|
+
nil
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.dump_object_count(io)
|
44
|
+
io.write " Built-in objects:\n"
|
45
|
+
ObjectSpace.count_objects.sort_by {|k,v| -v }.each {|k,v|
|
46
|
+
io.write "%10s: %s\n" % [_fn(v), k]
|
47
|
+
}
|
48
|
+
|
49
|
+
string_size = 0
|
50
|
+
array_size = 0
|
51
|
+
hash_size = 0
|
52
|
+
cmap = {}
|
53
|
+
ObjectSpace.each_object {|o|
|
54
|
+
c = o.class
|
55
|
+
cmap[c] = (cmap[c] || 0) + 1
|
56
|
+
if c == String
|
57
|
+
string_size += o.bytesize
|
58
|
+
elsif c == Array
|
59
|
+
array_size = o.size
|
60
|
+
elsif c == Hash
|
61
|
+
hash_size = o.size
|
62
|
+
end
|
63
|
+
}
|
64
|
+
|
65
|
+
io.write " All objects:\n"
|
66
|
+
cmap.sort_by {|k,v| -v }.each {|k,v|
|
67
|
+
io.write "%10s: %s\n" % [_fn(v), k]
|
68
|
+
}
|
69
|
+
|
70
|
+
io.write " String #{_fn(string_size)} bytes\n"
|
71
|
+
io.write " Array #{_fn(array_size)} elements\n"
|
72
|
+
io.write " Hash #{_fn(hash_size)} pairs\n"
|
73
|
+
|
74
|
+
io.flush
|
75
|
+
nil
|
76
|
+
end
|
77
|
+
|
78
|
+
def self._fn(num)
|
79
|
+
s = num.to_s
|
80
|
+
if formatted = s.gsub!(/(\d)(?=(?:\d{3})+(?!\d))/, "\\1,")
|
81
|
+
formatted
|
82
|
+
else
|
83
|
+
s
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def self._open_dump_path(path, &block)
|
88
|
+
case path
|
89
|
+
when nil, ""
|
90
|
+
path = "/tmp/sigdump-#{Process.pid}.log"
|
91
|
+
File.open(path, "a", &block)
|
92
|
+
when IO
|
93
|
+
yield path
|
94
|
+
when "-"
|
95
|
+
yield STDIN
|
96
|
+
when "+"
|
97
|
+
yield STDERR
|
98
|
+
else
|
99
|
+
File.open(path, "a", &block)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
data/sigdump.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'sigdump'
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = "sigdump"
|
7
|
+
gem.description = "Setup signal handler which dumps backtrace of running threads and number of allocated objects per class. Require 'sigdump/setup', send SIGCONT, and see /tmp/sigdump-<pid>.log."
|
8
|
+
gem.homepage = "https://github.com/frsyuki/sigdump"
|
9
|
+
gem.summary = gem.description
|
10
|
+
gem.version = Sigdump::VERSION
|
11
|
+
gem.authors = ["Sadayuki Furuhashi"]
|
12
|
+
gem.email = ["frsyuki@gmail.com"]
|
13
|
+
gem.has_rdoc = false
|
14
|
+
gem.files = `git ls-files`.split("\n")
|
15
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
gem.require_paths = ['lib']
|
18
|
+
|
19
|
+
gem.add_development_dependency "rake", ">= 0.9.2"
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sigdump
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sadayuki Furuhashi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.9.2
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.9.2
|
30
|
+
description: Setup signal handler which dumps backtrace of running threads and number
|
31
|
+
of allocated objects per class. Require 'sigdump/setup', send SIGCONT, and see /tmp/sigdump-<pid>.log.
|
32
|
+
email:
|
33
|
+
- frsyuki@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- ChangeLog
|
40
|
+
- Gemfile
|
41
|
+
- LICENSE
|
42
|
+
- README.md
|
43
|
+
- Rakefile
|
44
|
+
- lib/sigdump.rb
|
45
|
+
- lib/sigdump/setup.rb
|
46
|
+
- sigdump.gemspec
|
47
|
+
homepage: https://github.com/frsyuki/sigdump
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.23
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Setup signal handler which dumps backtrace of running threads and number
|
71
|
+
of allocated objects per class. Require 'sigdump/setup', send SIGCONT, and see /tmp/sigdump-<pid>.log.
|
72
|
+
test_files: []
|
73
|
+
has_rdoc: false
|