ruby-prof 1.3.1 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES +37 -0
  3. data/README.rdoc +1 -1
  4. data/Rakefile +2 -14
  5. data/ext/ruby_prof/extconf.rb +13 -27
  6. data/ext/ruby_prof/rp_allocation.c +1 -1
  7. data/ext/ruby_prof/rp_call_trees.c +2 -2
  8. data/ext/ruby_prof/rp_measure_allocations.c +1 -1
  9. data/ext/ruby_prof/rp_measure_process_time.c +1 -1
  10. data/ext/ruby_prof/rp_measure_wall_time.c +1 -1
  11. data/ext/ruby_prof/rp_measurement.c +1 -1
  12. data/ext/ruby_prof/rp_method.c +2 -2
  13. data/ext/ruby_prof/rp_method.h +19 -19
  14. data/ext/ruby_prof/rp_profile.c +12 -37
  15. data/ext/ruby_prof/rp_profile.h +0 -1
  16. data/ext/ruby_prof/rp_stack.c +12 -4
  17. data/ext/ruby_prof/rp_thread.c +2 -2
  18. data/ext/ruby_prof/vc/ruby_prof.sln +8 -0
  19. data/ext/ruby_prof/vc/ruby_prof.vcxproj +15 -4
  20. data/lib/ruby-prof.rb +4 -3
  21. data/lib/ruby-prof/compatibility.rb +0 -10
  22. data/lib/ruby-prof/printers/abstract_printer.rb +2 -2
  23. data/lib/ruby-prof/printers/call_stack_printer.rb +1 -0
  24. data/lib/ruby-prof/printers/graph_printer.rb +1 -1
  25. data/lib/ruby-prof/profile.rb +8 -4
  26. data/lib/ruby-prof/rack.rb +51 -130
  27. data/lib/ruby-prof/version.rb +1 -1
  28. data/ruby-prof.gemspec +1 -1
  29. data/test/fiber_test.rb +55 -187
  30. data/test/gc_test.rb +12 -2
  31. data/test/marshal_test.rb +22 -5
  32. data/test/measure_memory_trace_test.rb +361 -2
  33. data/test/printer_call_stack_test.rb +0 -1
  34. data/test/printer_call_tree_test.rb +0 -1
  35. data/test/printer_flat_test.rb +43 -39
  36. data/test/printer_graph_html_test.rb +0 -1
  37. data/test/printer_graph_test.rb +3 -4
  38. data/test/printers_test.rb +3 -3
  39. data/test/printing_recursive_graph_test.rb +1 -1
  40. data/test/profile_test.rb +16 -0
  41. data/test/rack_test.rb +0 -64
  42. data/test/start_stop_test.rb +4 -4
  43. data/test/temp.rb +20 -0
  44. data/test/test_helper.rb +10 -5
  45. metadata +7 -5
@@ -26,7 +26,6 @@ typedef struct prof_profile_t
26
26
  thread_data_t* last_thread_data;
27
27
  double measurement_at_pause_resume;
28
28
  bool allow_exceptions;
29
- bool merge_fibers;
30
29
  } prof_profile_t;
31
30
 
32
31
  void rp_init_profile(void);
@@ -22,6 +22,14 @@ void prof_stack_free(prof_stack_t* stack)
22
22
  xfree(stack);
23
23
  }
24
24
 
25
+ prof_frame_t* prof_stack_parent(prof_stack_t* stack)
26
+ {
27
+ if (stack->ptr == stack->start || stack->ptr - 1 == stack->start)
28
+ return NULL;
29
+ else
30
+ return stack->ptr - 2;
31
+ }
32
+
25
33
  prof_frame_t* prof_stack_last(prof_stack_t* stack)
26
34
  {
27
35
  if (stack->ptr == stack->start)
@@ -86,8 +94,8 @@ prof_frame_t* prof_frame_current(prof_stack_t* stack)
86
94
 
87
95
  prof_frame_t* prof_frame_push(prof_stack_t* stack, prof_call_tree_t* call_tree, double measurement, bool paused)
88
96
  {
89
- prof_frame_t* parent_frame = prof_stack_last(stack);
90
97
  prof_frame_t* result = prof_stack_push(stack);
98
+ prof_frame_t* parent_frame = prof_stack_parent(stack);
91
99
 
92
100
  result->call_tree = call_tree;
93
101
 
@@ -129,7 +137,7 @@ prof_frame_t* prof_frame_push(prof_stack_t* stack, prof_call_tree_t* call_tree,
129
137
  prof_frame_t* prof_frame_unshift(prof_stack_t* stack, prof_call_tree_t* parent_call_tree, prof_call_tree_t* call_tree, double measurement)
130
138
  {
131
139
  if (prof_stack_last(stack))
132
- rb_raise(rb_eRuntimeError, "Stach unshift can only be called with an empty stack");
140
+ rb_raise(rb_eRuntimeError, "Stack unshift can only be called with an empty stack");
133
141
 
134
142
  parent_call_tree->measurement->total_time = call_tree->measurement->total_time;
135
143
  parent_call_tree->measurement->self_time = 0;
@@ -172,7 +180,7 @@ prof_frame_t* prof_frame_pop(prof_stack_t* stack, double measurement)
172
180
  call_tree->measurement->total_time += total_time;
173
181
 
174
182
  call_tree->visits--;
175
-
183
+
176
184
  prof_frame_t* parent_frame = prof_stack_last(stack);
177
185
  if (parent_frame)
178
186
  {
@@ -187,7 +195,7 @@ prof_frame_t* prof_frame_pop(prof_stack_t* stack, double measurement)
187
195
 
188
196
  prof_method_t* prof_find_method(prof_stack_t* stack, VALUE source_file, int source_line)
189
197
  {
190
- prof_frame_t* frame = stack->ptr;
198
+ prof_frame_t* frame = prof_stack_last(stack);
191
199
  while (frame >= stack->start)
192
200
  {
193
201
  if (!frame->call_tree)
@@ -21,7 +21,7 @@ You cannot create an instance of RubyProf::Thread, instead you access it from a
21
21
 
22
22
  VALUE cRpThread;
23
23
 
24
- // ====== thread_data_t ======
24
+ // ====== thread_data_t ======
25
25
  thread_data_t* thread_data_create(void)
26
26
  {
27
27
  thread_data_t* result = ALLOC(thread_data_t);
@@ -146,7 +146,7 @@ thread_data_t* prof_get_thread(VALUE self)
146
146
  }
147
147
 
148
148
  // ====== Thread Table ======
149
- // The thread table is hash keyed on ruby fiber_id that stores instances of thread_data_t.
149
+ // The thread table is hash keyed on ruby fiber_id that stores instances of thread_data_t.
150
150
 
151
151
  st_table* threads_table_create()
152
152
  {
@@ -7,16 +7,24 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ruby_prof", "ruby_prof.vcxp
7
7
  EndProject
8
8
  Global
9
9
  GlobalSection(SolutionConfigurationPlatforms) = preSolution
10
+ Debug|ARM = Debug|ARM
11
+ Debug|ARM64 = Debug|ARM64
10
12
  Debug|x64 = Debug|x64
11
13
  Debug|x86 = Debug|x86
14
+ Release|ARM = Release|ARM
15
+ Release|ARM64 = Release|ARM64
12
16
  Release|x64 = Release|x64
13
17
  Release|x86 = Release|x86
14
18
  EndGlobalSection
15
19
  GlobalSection(ProjectConfigurationPlatforms) = postSolution
20
+ {6B4978F4-3B5F-4D38-81A8-069EC28CC069}.Debug|ARM.ActiveCfg = Debug|Win32
21
+ {6B4978F4-3B5F-4D38-81A8-069EC28CC069}.Debug|ARM64.ActiveCfg = Debug|Win32
16
22
  {6B4978F4-3B5F-4D38-81A8-069EC28CC069}.Debug|x64.ActiveCfg = Debug|x64
17
23
  {6B4978F4-3B5F-4D38-81A8-069EC28CC069}.Debug|x64.Build.0 = Debug|x64
18
24
  {6B4978F4-3B5F-4D38-81A8-069EC28CC069}.Debug|x86.ActiveCfg = Debug|Win32
19
25
  {6B4978F4-3B5F-4D38-81A8-069EC28CC069}.Debug|x86.Build.0 = Debug|Win32
26
+ {6B4978F4-3B5F-4D38-81A8-069EC28CC069}.Release|ARM.ActiveCfg = Release|Win32
27
+ {6B4978F4-3B5F-4D38-81A8-069EC28CC069}.Release|ARM64.ActiveCfg = Release|Win32
20
28
  {6B4978F4-3B5F-4D38-81A8-069EC28CC069}.Release|x64.ActiveCfg = Release|x64
21
29
  {6B4978F4-3B5F-4D38-81A8-069EC28CC069}.Release|x64.Build.0 = Release|x64
22
30
  {6B4978F4-3B5F-4D38-81A8-069EC28CC069}.Release|x86.ActiveCfg = Release|Win32
@@ -64,7 +64,7 @@
64
64
  </PropertyGroup>
65
65
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
66
66
  <TargetExt>.so</TargetExt>
67
- <OutDir>..\..\..\lib</OutDir>
67
+ <OutDir>..\..\..\lib\</OutDir>
68
68
  </PropertyGroup>
69
69
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
70
70
  <ClCompile>
@@ -102,17 +102,28 @@
102
102
  </ItemDefinitionGroup>
103
103
  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
104
104
  <ClCompile>
105
- <AdditionalIncludeDirectories>C:\msys64\usr\local\ruby-2.7.0vc\include\ruby-2.7.0\x64-mswin64_140;C:\msys64\usr\local\ruby-2.7.0vc\include\ruby-2.7.0;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
105
+ <AdditionalIncludeDirectories>C:\msys64\usr\local\ruby-2.7.2vc\include\ruby-2.7.0\x64-mswin64_140;C:\msys64\usr\local\ruby-2.7.2vc\include\ruby-2.7.0;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
106
106
  <Optimization>Disabled</Optimization>
107
- <PreprocessorDefinitions>HAVE_RB_TRACEARG_CALLEE_ID;%(PreprocessorDefinitions)</PreprocessorDefinitions>
107
+ <PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
108
+ <WarningLevel>Level3</WarningLevel>
108
109
  </ClCompile>
109
110
  <Link>
110
- <AdditionalLibraryDirectories>C:\msys64\usr\local\ruby-2.7.0vc\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
111
+ <AdditionalLibraryDirectories>C:\msys64\usr\local\ruby-2.7.2vc\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
111
112
  <AdditionalDependencies>x64-vcruntime140-ruby270.lib;%(AdditionalDependencies)</AdditionalDependencies>
112
113
  <ModuleDefinitionFile>ruby_prof.def</ModuleDefinitionFile>
113
114
  <SubSystem>Console</SubSystem>
114
115
  </Link>
115
116
  </ItemDefinitionGroup>
117
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
118
+ <ClCompile>
119
+ <AdditionalIncludeDirectories>C:\msys64\usr\local\ruby-2.7.1vc\include\ruby-2.7.0\x64-mswin64_140;C:\msys64\usr\local\ruby-2.7.1vc\include\ruby-2.7.0;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
120
+ </ClCompile>
121
+ <Link>
122
+ <AdditionalLibraryDirectories>C:\msys64\usr\local\ruby-2.7.1vc\lib</AdditionalLibraryDirectories>
123
+ <AdditionalDependencies>x64-vcruntime140-ruby270.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
124
+ <ModuleDefinitionFile>ruby_prof.def</ModuleDefinitionFile>
125
+ </Link>
126
+ </ItemDefinitionGroup>
116
127
  <ItemGroup>
117
128
  <ClInclude Include="..\rp_aggregate_call_tree.h" />
118
129
  <ClInclude Include="..\rp_allocation.h" />
data/lib/ruby-prof.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  # encoding: utf-8
2
+ require 'rubygems/version'
2
3
 
3
4
  # Load the C-based binding.
4
5
  begin
5
- RUBY_VERSION =~ /(\d+\.\d+\.\d+)/
6
- require "#{$1}/ruby_prof.so"
6
+ version = Gem::Version.new(RUBY_VERSION)
7
+ require "#{version.segments[0..1].join('.')}/ruby_prof.so"
7
8
  rescue LoadError
8
- require "ruby_prof.so"
9
+ require_relative "../ext/ruby_prof/ruby_prof.so"
9
10
  end
10
11
 
11
12
  require 'ruby-prof/version'
@@ -81,16 +81,6 @@ module RubyProf
81
81
  Profile.profile(options, &block)
82
82
  end
83
83
 
84
- # :nodoc:
85
- def self.measure_mode_string
86
- case measure_mode
87
- when WALL_TIME then "wall_time"
88
- when PROCESS_TIME then "process_time"
89
- when ALLOCATIONS then "allocations"
90
- when MEMORY then "memory"
91
- end
92
- end
93
-
94
84
  # :nodoc:
95
85
  def self.start_script(script)
96
86
  start
@@ -99,7 +99,7 @@ module RubyProf
99
99
  end
100
100
 
101
101
  def print_header(thread)
102
- @output << "Measure Mode: %s\n" % RubyProf.measure_mode_string
102
+ @output << "Measure Mode: %s\n" % @result.measure_mode_string
103
103
  @output << "Thread ID: %d\n" % thread.id
104
104
  @output << "Fiber ID: %d\n" % thread.fiber_id unless thread.id == thread.fiber_id
105
105
  @output << "Total: %0.6f\n" % thread.total_time
@@ -113,7 +113,7 @@ module RubyProf
113
113
 
114
114
  def print_footer(thread)
115
115
  @output << <<~EOT
116
-
116
+
117
117
  * recursively called methods
118
118
 
119
119
  Columns are:
@@ -4,6 +4,7 @@ require 'erb'
4
4
  require 'fileutils'
5
5
  require 'base64'
6
6
  require 'set'
7
+ require 'stringio'
7
8
 
8
9
  module RubyProf
9
10
  # Prints a HTML visualization of the call tree.
@@ -25,7 +25,7 @@ module RubyProf
25
25
  end
26
26
 
27
27
  def print_header(thread)
28
- @output << "Measure Mode: %s\n" % RubyProf.measure_mode_string
28
+ @output << "Measure Mode: %s\n" % @result.measure_mode_string
29
29
  @output << "Thread ID: #{thread.id}\n"
30
30
  @output << "Fiber ID: #{thread.fiber_id}\n"
31
31
  @output << "Total Time: #{thread.total_time}\n"
@@ -7,10 +7,14 @@ module RubyProf
7
7
  # :nodoc:
8
8
  def measure_mode_string
9
9
  case self.measure_mode
10
- when WALL_TIME then "wall_time"
11
- when PROCESS_TIME then "process_time"
12
- when ALLOCATIONS then "allocations"
13
- when MEMORY then "memory"
10
+ when WALL_TIME
11
+ "wall_time"
12
+ when PROCESS_TIME
13
+ "process_time"
14
+ when ALLOCATIONS
15
+ "allocations"
16
+ when MEMORY
17
+ "memory"
14
18
  end
15
19
  end
16
20
 
@@ -5,43 +5,37 @@ module Rack
5
5
  class RubyProf
6
6
  def initialize(app, options = {})
7
7
  @app = app
8
+ @options = options
9
+ @options[:min_percent] ||= 1
8
10
 
9
- options[:min_percent] ||= 1
11
+ @tmpdir = options[:path] || Dir.tmpdir
12
+ FileUtils.mkdir_p(@tmpdir)
10
13
 
11
- options[:path] ||= Dir.tmpdir
12
- FileUtils.mkdir_p(options[:path])
14
+ @printer_klasses = @options[:printers] || {::RubyProf::FlatPrinter => 'flat.txt',
15
+ ::RubyProf::GraphPrinter => 'graph.txt',
16
+ ::RubyProf::GraphHtmlPrinter => 'graph.html',
17
+ ::RubyProf::CallStackPrinter => 'call_stack.html'}
13
18
 
14
19
  @skip_paths = options[:skip_paths] || [%r{^/assets}, %r{\.(css|js|png|jpeg|jpg|gif)$}]
15
20
  @only_paths = options[:only_paths]
16
-
17
- @max_requests = options[:max_requests]
18
-
19
- @options = options
20
21
  end
21
22
 
22
23
  def call(env)
23
24
  request = Rack::Request.new(env)
24
25
 
25
26
  if should_profile?(request.path)
26
- profiler.resume
27
27
  begin
28
- result = @app.call(env)
29
- ensure
30
- profiler.pause
31
- end
28
+ result = nil
29
+ data = ::RubyProf::Profile.profile(profiling_options) do
30
+ result = @app.call(env)
31
+ end
32
32
 
33
- if profiler.max_requests_reached?
34
- prefix = if aggregate_requests?
35
- nil
36
- else
37
- request.path.gsub('/', '-')[1..-1]
38
- end
33
+ path = request.path.gsub('/', '-')
34
+ path.slice!(0)
39
35
 
40
- profiler.print!(prefix)
41
- delete_profiler!
36
+ print(data, path)
37
+ result
42
38
  end
43
-
44
- result
45
39
  else
46
40
  @app.call(env)
47
41
  end
@@ -49,126 +43,53 @@ module Rack
49
43
 
50
44
  private
51
45
 
52
- class RackProfiler
53
- def initialize(options)
54
- @options = options
55
-
56
- @profile = ::RubyProf::Profile.new(profiling_options)
57
- @profile.start
58
- @profile.pause
59
-
60
- @printer_klasses = options[:printers] || default_printers
61
-
62
- @tmpdir = options[:path]
63
-
64
- @max_requests = options[:max_requests] || 1
65
- @requests_count = 0
46
+ def should_profile?(path)
47
+ return false if paths_match?(path, @skip_paths)
66
48
 
67
- @printed = false
68
- # if running across multiple requests, we want to make sure that the
69
- # ongoing profile is not lost if the process shuts down before the
70
- # max request count is reached
71
- ObjectSpace.define_finalizer(self, proc { print! })
72
- end
49
+ @only_paths ? paths_match?(path, @only_paths) : true
50
+ end
73
51
 
74
- def resume
75
- @profile.resume
76
- end
52
+ def paths_match?(path, paths)
53
+ paths.any? { |skip_path| skip_path =~ path }
54
+ end
77
55
 
78
- def pause
79
- @profile.pause
80
- @requests_count += 1
56
+ def profiling_options
57
+ options = {}
58
+ options[:measure_mode] = ::RubyProf.measure_mode
59
+ options[:exclude_threads] =
60
+ if @options[:ignore_existing_threads]
61
+ Thread.list.select{|t| t != Thread.current}
62
+ else
63
+ ::RubyProf.exclude_threads
64
+ end
65
+ if @options[:request_thread_only]
66
+ options[:include_threads] = [Thread.current]
81
67
  end
82
-
83
- def max_requests_reached?
84
- @requests_count >= @max_requests
68
+ if @options[:merge_fibers]
69
+ options[:merge_fibers] = true
85
70
  end
71
+ options
72
+ end
86
73
 
87
- def print!(prefix = nil)
88
- return false if @printed || @requests_count == 0
89
-
90
- data = @profile.stop
91
-
92
- prefix ||= "multi-requests-#{@requests_count}"
93
-
94
- @printer_klasses.each do |printer_klass, base_name|
95
- printer = printer_klass.new(data)
96
-
97
- if base_name.respond_to?(:call)
98
- base_name = base_name.call
99
- end
74
+ def print(data, path)
75
+ @printer_klasses.each do |printer_klass, base_name|
76
+ printer = printer_klass.new(data)
100
77
 
101
- if printer_klass == ::RubyProf::MultiPrinter \
102
- || printer_klass == ::RubyProf::CallTreePrinter
103
- printer.print(@options.merge(:profile => "#{prefix}-#{base_name}"))
104
- else
105
- file_name = ::File.join(@tmpdir, "#{prefix}-#{base_name}")
106
- ::File.open(file_name, 'wb') do |file|
107
- printer.print(file, @options)
108
- end
109
- end
78
+ if base_name.respond_to?(:call)
79
+ base_name = base_name.call
110
80
  end
111
81
 
112
- @printed = true
113
- end
114
-
115
- private
116
-
117
- def profiling_options
118
- options = {}
119
- options[:measure_mode] = ::RubyProf.measure_mode
120
- options[:exclude_threads] =
121
- if @options[:ignore_existing_threads]
122
- Thread.list.select{|t| t != Thread.current}
123
- else
124
- ::RubyProf.exclude_threads
82
+ if printer_klass == ::RubyProf::MultiPrinter
83
+ printer.print(@options.merge(:profile => "#{path}-#{base_name}"))
84
+ elsif printer_klass == ::RubyProf::CallTreePrinter
85
+ printer.print(@options.merge(:profile => "#{path}-#{base_name}"))
86
+ else
87
+ file_name = ::File.join(@tmpdir, "#{path}-#{base_name}")
88
+ ::File.open(file_name, 'wb') do |file|
89
+ printer.print(file, @options)
125
90
  end
126
- if @options[:request_thread_only]
127
- options[:include_threads] = [Thread.current]
128
91
  end
129
- if @options[:merge_fibers]
130
- options[:merge_fibers] = true
131
- end
132
- options
133
- end
134
-
135
- def default_printers
136
- {::RubyProf::FlatPrinter => 'flat.txt',
137
- ::RubyProf::GraphPrinter => 'graph.txt',
138
- ::RubyProf::GraphHtmlPrinter => 'graph.html',
139
- ::RubyProf::CallStackPrinter => 'call_stack.html'}
140
92
  end
141
93
  end
142
-
143
- def profiler
144
- if aggregate_requests?
145
- @@_shared_profiler ||= RackProfiler.new(@options)
146
- else
147
- @_profiler ||= RackProfiler.new(@options)
148
- end
149
- end
150
-
151
- def delete_profiler!
152
- if aggregate_requests?
153
- @@_shared_profiler.print! if @@_shared_profiler
154
- @@_shared_profiler = nil
155
- else
156
- @_profiler = nil
157
- end
158
- end
159
-
160
- def aggregate_requests?
161
- !@max_requests.nil?
162
- end
163
-
164
- def should_profile?(path)
165
- return false if paths_match?(path, @skip_paths)
166
-
167
- @only_paths ? paths_match?(path, @only_paths) : true
168
- end
169
-
170
- def paths_match?(path, paths)
171
- paths.any? { |skip_path| skip_path =~ path }
172
- end
173
94
  end
174
95
  end