calleree 0.1.0

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
+ SHA256:
3
+ metadata.gz: fbb6acebd17bc68a5547ccd29c14e41a359aec189a08dbdeca7ab19088a00a8e
4
+ data.tar.gz: 7e364534c22b4d4d6184fba2dc472939bb578b379fe508d1292e538f27391383
5
+ SHA512:
6
+ metadata.gz: 0a1be7bce4337c46c1bb6e45c1dc62d8801379a07167b0784dfa5c36e0595e5fd2bbcf939b27ca7e6ba41f006f6a6b3a31807f52a5d67861fb9ad314903ec209
7
+ data.tar.gz: c5c6695fe51447599341bb541a903964380e1b6f1579856101739d14a6e6443fb04f4accd7859ef7d12ca2b8146989507475984ae7bd428cc27d05427b65a4fb
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /lib/calleree/calleree.so
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ test.rb
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.3
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in calleree.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rake-compiler"
8
+ gem "minitest", "~> 5.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Koichi Sasada
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Calleree
2
+
3
+ Calleree helps to analyze Ruby's caller-callee relationships.
4
+ Note that this tool consumes memory and introduces additional overhead because of dynamic analysis.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'calleree'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install calleree
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ 1
26
+ 2 require 'calleree'
27
+ 3
28
+ 4 def foo
29
+ 5 :foo
30
+ 6 end
31
+ 7
32
+ 8 def bar
33
+ 9 foo
34
+ 10 end
35
+ 11
36
+ 12 Calleree.start
37
+ 13
38
+ 14 foo
39
+ 15
40
+ 16 pp Calleree.result
41
+ ```
42
+
43
+ And this program shows:
44
+
45
+ ```
46
+ [[["test.rb", 14], ["test.rb", 5], 1],
47
+ [["test.rb", 16], ["/mnt/c/ko1/src/rb/calleree/lib/calleree.rb", 24], 1]]
48
+ ...
49
+
50
+ The `Calleree.result` method returns an array of arrays which contains `[[caller_path, caller_line], [callee_path, callee_line], called_caount]`.
51
+
52
+ In this case, `foo` at `["test.rb", 5]` is called at `["test.rb", 14]` only once.
53
+
54
+ * You can stop the analisys with `Calleree.stop`. `Calleree.start` will continue the analysis.
55
+ * You can use block with `Calleree.start do ... end`.
56
+ * You can clear the result if `Calleree.result(clear: true)` is passed.
57
+
58
+ ## Development
59
+
60
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
61
+
62
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ko1/calleree.
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ require "rake/extensiontask"
11
+
12
+ task :build => :compile
13
+
14
+ Rake::ExtensionTask.new("calleree") do |ext|
15
+ ext.lib_dir = "lib/calleree"
16
+ end
17
+
18
+ task :default => [:clobber, :compile, :test] do
19
+
20
+ end
21
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "calleree"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/calleree.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ require_relative 'lib/calleree/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "calleree"
5
+ spec.version = Calleree::VERSION
6
+ spec.authors = ["Koichi Sasada"]
7
+ spec.email = ["ko1@atdot.net"]
8
+
9
+ spec.summary = %q{Calleree helps to analyze Ruby's caller-callee relationships.}
10
+ spec.description = %q{Calleree helps to analyze Ruby's caller-callee relationships.}
11
+ spec.homepage = "https://github.com/ko1/calleree"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/ko1/calleree"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+ spec.extensions = ["ext/calleree/extconf.rb"]
27
+ end
@@ -0,0 +1,112 @@
1
+ #include "ruby/ruby.h"
2
+ #include "ruby/debug.h"
3
+ #include "ruby/st.h"
4
+
5
+ VALUE rb_mCalleree;
6
+
7
+ struct eree_data {
8
+ VALUE tp; // TracePoint.new(:call)
9
+ VALUE files; // name -> [ {[fname, 1], ... } ]
10
+ struct st_table* caller_callee; // [posnum | posnum] -> cnt
11
+ int last_posnum;
12
+ } eree_data;
13
+
14
+ static int
15
+ posnum(struct eree_data *data, VALUE path, int line)
16
+ {
17
+ VALUE lines, posnumv;
18
+
19
+ if (NIL_P(lines = rb_hash_aref(data->files, path))) {
20
+ rb_hash_aset(data->files, path, lines = rb_ary_new());
21
+ }
22
+ if (NIL_P(posnumv = rb_ary_entry(lines, line))) {
23
+ rb_ary_store(lines, line, posnumv = INT2FIX(data->last_posnum++));
24
+ }
25
+ return FIX2INT(posnumv);
26
+ }
27
+
28
+ static int
29
+ increment_i(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
30
+ {
31
+ *value = (existing ? *value : 0) + 1;
32
+ return ST_CONTINUE;
33
+ }
34
+
35
+ static void
36
+ erree_called(VALUE tpval, void *ptr)
37
+ {
38
+ struct eree_data *data = ptr;
39
+ VALUE frames[2];
40
+ int lines[2];
41
+ // int start, int limit, VALUE *buff, int *lines);
42
+ rb_profile_frames(1, 2, frames, lines);
43
+ int callee_posnum = posnum(data, rb_profile_frame_path(frames[0]), lines[0]);
44
+ int caller_posnum = posnum(data, rb_profile_frame_path(frames[1]), lines[1]);
45
+ // rb_p(rb_profile_frame_path(frames[1]));
46
+ st_data_t eree_pair = (((VALUE)caller_posnum << 32) | ((VALUE)callee_posnum));
47
+
48
+ st_update(data->caller_callee, eree_pair, increment_i, 0);
49
+ }
50
+
51
+ static VALUE
52
+ eree_start(VALUE self)
53
+ {
54
+ if (!eree_data.tp) {
55
+ eree_data.tp = rb_tracepoint_new(0, RUBY_EVENT_CALL, erree_called, &eree_data);
56
+ eree_data.caller_callee = st_init_numtable();
57
+ }
58
+ rb_tracepoint_enable(eree_data.tp);
59
+ return self;
60
+ }
61
+
62
+ static VALUE
63
+ eree_stop(VALUE self)
64
+ {
65
+ if (eree_data.tp) {
66
+ rb_tracepoint_disable(eree_data.tp);
67
+ }
68
+ return self;
69
+ }
70
+
71
+ static int
72
+ raw_result_i(st_data_t key, st_data_t val, st_data_t data)
73
+ {
74
+ VALUE ary = (VALUE)data;
75
+ int callee_posnum = (int)(key & 0xffffffff);
76
+ int caller_posnum = (int)(key >> 32);
77
+ rb_ary_push(ary, rb_ary_new_from_args(3, INT2FIX(caller_posnum), INT2FIX(callee_posnum), INT2FIX((int)val)));
78
+ return ST_CONTINUE;
79
+ }
80
+
81
+ static VALUE
82
+ eree_raw_result(VALUE self, VALUE clear_p)
83
+ {
84
+ VALUE ary = rb_ary_new();
85
+ if (eree_data.tp) {
86
+ st_foreach(eree_data.caller_callee, raw_result_i, (st_data_t)ary);
87
+
88
+ if (RTEST(clear_p)) {
89
+ st_clear(eree_data.caller_callee);
90
+ }
91
+ }
92
+ return ary;
93
+ }
94
+
95
+ static VALUE
96
+ eree_raw_posmap(VALUE self)
97
+ {
98
+ return eree_data.files;
99
+ }
100
+
101
+ void
102
+ Init_calleree(void)
103
+ {
104
+ eree_data.files = rb_hash_new();
105
+ rb_gc_register_mark_object(eree_data.files);
106
+
107
+ rb_mCalleree = rb_define_module("Calleree");
108
+ rb_define_singleton_method(rb_mCalleree, "_start", eree_start, 0);
109
+ rb_define_singleton_method(rb_mCalleree, "_stop", eree_stop, 0);
110
+ rb_define_singleton_method(rb_mCalleree, "raw_result", eree_raw_result, 1);
111
+ rb_define_singleton_method(rb_mCalleree, "raw_posmap", eree_raw_posmap, 0);
112
+ }
@@ -0,0 +1,6 @@
1
+ #ifndef CALLEREE_H
2
+ #define CALLEREE_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ #endif /* CALLEREE_H */
@@ -0,0 +1,8 @@
1
+ require "mkmf"
2
+ require 'rbconfig/sizeof'
3
+
4
+ unless RbConfig::LIMITS['SIZE_MAX'] == 2**64 - 1
5
+ raise "This library is only work on 64bit CPU."
6
+ end
7
+
8
+ create_makefile("calleree/calleree")
data/lib/calleree.rb ADDED
@@ -0,0 +1,38 @@
1
+ require "calleree/version"
2
+ require "calleree/calleree"
3
+
4
+ module Calleree
5
+ def self.start
6
+ if block_given?
7
+ begin
8
+ Calleree._start
9
+ yield
10
+ ensure
11
+ Calleree._stop
12
+ end
13
+ else
14
+ Calleree._start
15
+ end
16
+ end
17
+
18
+ def self.stop
19
+ Calleree._stop
20
+ end
21
+
22
+ def self.result clear: false
23
+ posnum_set = []
24
+ Calleree.raw_posmap.each{|file, lines|
25
+ lines.each_with_index{|posnum, line|
26
+ posnum_set[posnum] = [file, line] if posnum
27
+ }
28
+ }
29
+ # pp posnum_set
30
+ rs = []
31
+ Calleree.raw_result(clear).each{|i, j, cnt|
32
+ er = posnum_set[i]
33
+ ee = posnum_set[j]
34
+ rs << [er, ee, cnt] if er && ee
35
+ }
36
+ rs
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module Calleree
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: calleree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Koichi Sasada
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Calleree helps to analyze Ruby's caller-callee relationships.
14
+ email:
15
+ - ko1@atdot.net
16
+ executables: []
17
+ extensions:
18
+ - ext/calleree/extconf.rb
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".travis.yml"
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/console
28
+ - bin/setup
29
+ - calleree.gemspec
30
+ - ext/calleree/calleree.c
31
+ - ext/calleree/calleree.h
32
+ - ext/calleree/extconf.rb
33
+ - lib/calleree.rb
34
+ - lib/calleree/version.rb
35
+ homepage: https://github.com/ko1/calleree
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ homepage_uri: https://github.com/ko1/calleree
40
+ source_code_uri: https://github.com/ko1/calleree
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.3.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.1.6
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Calleree helps to analyze Ruby's caller-callee relationships.
60
+ test_files: []