skaes-ruby-prof 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. data/CHANGES +202 -0
  2. data/LICENSE +23 -0
  3. data/README +436 -0
  4. data/Rakefile +129 -0
  5. data/bin/ruby-prof +207 -0
  6. data/examples/flat.txt +55 -0
  7. data/examples/graph.html +823 -0
  8. data/examples/graph.txt +170 -0
  9. data/ext/extconf.rb +34 -0
  10. data/ext/measure_allocations.h +58 -0
  11. data/ext/measure_cpu_time.h +152 -0
  12. data/ext/measure_gc_runs.h +76 -0
  13. data/ext/measure_gc_time.h +57 -0
  14. data/ext/measure_memory.h +101 -0
  15. data/ext/measure_process_time.h +52 -0
  16. data/ext/measure_wall_time.h +53 -0
  17. data/ext/mingw/Rakefile +23 -0
  18. data/ext/mingw/build.rake +38 -0
  19. data/ext/ruby_prof.c +1747 -0
  20. data/ext/ruby_prof.h +185 -0
  21. data/ext/vc/ruby_prof.sln +20 -0
  22. data/ext/vc/ruby_prof.vcproj +241 -0
  23. data/ext/version.h +4 -0
  24. data/lib/ruby-prof.rb +51 -0
  25. data/lib/ruby-prof/abstract_printer.rb +41 -0
  26. data/lib/ruby-prof/aggregate_call_info.rb +68 -0
  27. data/lib/ruby-prof/call_info.rb +112 -0
  28. data/lib/ruby-prof/call_stack_printer.rb +746 -0
  29. data/lib/ruby-prof/call_tree_printer.rb +84 -0
  30. data/lib/ruby-prof/empty.png +0 -0
  31. data/lib/ruby-prof/flat_printer.rb +79 -0
  32. data/lib/ruby-prof/graph_html_printer.rb +272 -0
  33. data/lib/ruby-prof/graph_printer.rb +164 -0
  34. data/lib/ruby-prof/method_info.rb +131 -0
  35. data/lib/ruby-prof/minus.png +0 -0
  36. data/lib/ruby-prof/multi_printer.rb +55 -0
  37. data/lib/ruby-prof/plus.png +0 -0
  38. data/lib/ruby-prof/result.rb +70 -0
  39. data/lib/ruby-prof/task.rb +146 -0
  40. data/lib/ruby-prof/test.rb +148 -0
  41. data/lib/unprof.rb +8 -0
  42. data/rails/environment/profile.rb +24 -0
  43. data/rails/example/example_test.rb +9 -0
  44. data/rails/profile_test_helper.rb +21 -0
  45. data/test/aggregate_test.rb +136 -0
  46. data/test/basic_test.rb +283 -0
  47. data/test/duplicate_names_test.rb +32 -0
  48. data/test/exceptions_test.rb +15 -0
  49. data/test/exclude_threads_test.rb +54 -0
  50. data/test/line_number_test.rb +73 -0
  51. data/test/measurement_test.rb +121 -0
  52. data/test/method_elimination_test.rb +74 -0
  53. data/test/module_test.rb +54 -0
  54. data/test/multi_printer_test.rb +81 -0
  55. data/test/no_method_class_test.rb +13 -0
  56. data/test/prime.rb +58 -0
  57. data/test/prime_test.rb +13 -0
  58. data/test/printers_test.rb +73 -0
  59. data/test/recursive_test.rb +215 -0
  60. data/test/singleton_test.rb +38 -0
  61. data/test/stack_printer_test.rb +74 -0
  62. data/test/stack_test.rb +138 -0
  63. data/test/start_stop_test.rb +95 -0
  64. data/test/test_suite.rb +26 -0
  65. data/test/thread_test.rb +159 -0
  66. data/test/unique_call_path_test.rb +206 -0
  67. metadata +128 -0
@@ -0,0 +1,57 @@
1
+ /* :nodoc:
2
+ * Copyright (C) 2008 Shugo Maeda <shugo@ruby-lang.org>
3
+ * Charlie Savage <cfis@savagexi.com>
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions
8
+ * are met:
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ *
15
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
+ * SUCH DAMAGE. */
26
+
27
+ #if defined(HAVE_RB_GC_TIME)
28
+ #define MEASURE_GC_TIME 6
29
+
30
+ static prof_measure_t
31
+ measure_gc_time()
32
+ {
33
+ #if HAVE_LONG_LONG
34
+ return NUM2LL(rb_gc_time());
35
+ #else
36
+ return NUM2LONG(rb_gc_time());
37
+ #endif
38
+ }
39
+
40
+ static double
41
+ convert_gc_time(prof_measure_t c)
42
+ {
43
+ return (double) c / 1000000;
44
+ }
45
+
46
+ /* Document-method: prof_measure_gc_time
47
+ call-seq:
48
+ gc_time -> Integer
49
+
50
+ Returns the time spent doing garbage collections in microseconds.*/
51
+ static VALUE
52
+ prof_measure_gc_time(VALUE self)
53
+ {
54
+ return rb_gc_time();
55
+ }
56
+
57
+ #endif
@@ -0,0 +1,101 @@
1
+ /* :nodoc:
2
+ * Copyright (C) 2008 Alexander Dymo <adymo@pluron.com>
3
+ *
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions
8
+ * are met:
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ *
15
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
+ * SUCH DAMAGE. */
26
+
27
+
28
+ #if defined(HAVE_RB_GC_ALLOCATED_SIZE)
29
+ #define MEASURE_MEMORY 4
30
+ #define TOGGLE_GC_STATS 1
31
+
32
+ static prof_measure_t
33
+ measure_memory()
34
+ {
35
+ #if defined(HAVE_LONG_LONG)
36
+ return NUM2LL(rb_gc_allocated_size());
37
+ #else
38
+ return NUM2ULONG(rb_gc_allocated_size());
39
+ #endif
40
+ }
41
+
42
+ static double
43
+ convert_memory(prof_measure_t c)
44
+ {
45
+ return (double) c / 1024;
46
+ }
47
+
48
+ /* Document-method: prof_measure_memory
49
+ call-seq:
50
+ measure_memory -> int
51
+
52
+ Returns total allocated memory in bytes.*/
53
+ static VALUE
54
+ prof_measure_memory(VALUE self)
55
+ {
56
+ return rb_gc_allocated_size();
57
+ }
58
+
59
+ #elif defined(HAVE_RB_GC_MALLOC_ALLOCATED_SIZE)
60
+ #define MEASURE_MEMORY 4
61
+
62
+ static prof_measure_t
63
+ measure_memory()
64
+ {
65
+ return rb_gc_malloc_allocated_size();
66
+ }
67
+
68
+ static double
69
+ convert_memory(prof_measure_t c)
70
+ {
71
+ return (double) c / 1024;
72
+ }
73
+
74
+ static VALUE
75
+ prof_measure_memory(VALUE self)
76
+ {
77
+ return UINT2NUM(rb_gc_malloc_allocated_size());
78
+ }
79
+
80
+ #elif defined(HAVE_RB_HEAP_TOTAL_MEM)
81
+ #define MEASURE_MEMORY 4
82
+
83
+ static prof_measure_t
84
+ measure_memory()
85
+ {
86
+ return rb_heap_total_mem();
87
+ }
88
+
89
+ static double
90
+ convert_memory(prof_measure_t c)
91
+ {
92
+ return (double) c / 1024;
93
+ }
94
+
95
+ static VALUE
96
+ prof_measure_memory(VALUE self)
97
+ {
98
+ return ULONG2NUM(rb_heap_total_mem());
99
+ }
100
+
101
+ #endif
@@ -0,0 +1,52 @@
1
+ /*
2
+ * Copyright (C) 2008 Shugo Maeda <shugo@ruby-lang.org>
3
+ * Charlie Savage <cfis@savagexi.com>
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions
8
+ * are met:
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ *
15
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
+ * SUCH DAMAGE. */
26
+
27
+ #include <time.h>
28
+
29
+ #define MEASURE_PROCESS_TIME 0
30
+
31
+ static prof_measure_t
32
+ measure_process_time()
33
+ {
34
+ return clock();
35
+ }
36
+
37
+ static double
38
+ convert_process_time(prof_measure_t c)
39
+ {
40
+ return (double) c / CLOCKS_PER_SEC;
41
+ }
42
+
43
+ /* Document-method: measure_process_time
44
+ call-seq:
45
+ measure_process_time -> float
46
+
47
+ Returns the process time.*/
48
+ static VALUE
49
+ prof_measure_process_time(VALUE self)
50
+ {
51
+ return rb_float_new(convert_process_time(measure_process_time()));
52
+ }
@@ -0,0 +1,53 @@
1
+ /* :nodoc:
2
+ * Copyright (C) 2008 Shugo Maeda <shugo@ruby-lang.org>
3
+ * Charlie Savage <cfis@savagexi.com>
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions
8
+ * are met:
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ *
15
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
+ * SUCH DAMAGE. */
26
+
27
+
28
+ #define MEASURE_WALL_TIME 1
29
+
30
+ static prof_measure_t
31
+ measure_wall_time()
32
+ {
33
+ struct timeval tv;
34
+ gettimeofday(&tv, NULL);
35
+ return tv.tv_sec * 1000000 + tv.tv_usec;
36
+ }
37
+
38
+ static double
39
+ convert_wall_time(prof_measure_t c)
40
+ {
41
+ return (double) c / 1000000;
42
+ }
43
+
44
+ /* Document-method: prof_measure_wall_time
45
+ call-seq:
46
+ measure_wall_time -> float
47
+
48
+ Returns the wall time.*/
49
+ static VALUE
50
+ prof_measure_wall_time(VALUE self)
51
+ {
52
+ return rb_float_new(convert_wall_time(measure_wall_time()));
53
+ }
@@ -0,0 +1,23 @@
1
+ # We can't use Ruby's standard build procedures
2
+ # on Windows because the Ruby executable is
3
+ # built with VC++ while here we want to build
4
+ # with MingW. So just roll our own...
5
+
6
+ require 'fileutils'
7
+ require 'rbconfig'
8
+
9
+ EXTENSION_NAME = "ruby_prof.#{Config::CONFIG["DLEXT"]}"
10
+
11
+ # This is called when the Windows GEM is installed!
12
+ task :install do
13
+ # Gems will pass these two environment variables:
14
+ # RUBYARCHDIR=#{dest_path}
15
+ # RUBYLIBDIR=#{dest_path}
16
+
17
+ dest_path = ENV['RUBYLIBDIR']
18
+
19
+ # Copy the extension
20
+ cp(EXTENSION_NAME, dest_path)
21
+ end
22
+
23
+ task :default => :install
@@ -0,0 +1,38 @@
1
+ # We can't use Ruby's standard build procedures
2
+ # on Windows because the Ruby executable is
3
+ # built with VC++ while here we want to build
4
+ # with MingW. So just roll our own...
5
+
6
+ require 'rake/clean'
7
+ require 'rbconfig'
8
+
9
+ RUBY_INCLUDE_DIR = Config::CONFIG["archdir"]
10
+ RUBY_BIN_DIR = Config::CONFIG["bindir"]
11
+ RUBY_LIB_DIR = Config::CONFIG["libdir"]
12
+ RUBY_SHARED_LIB = Config::CONFIG["LIBRUBY"]
13
+ RUBY_SHARED_DLL = RUBY_SHARED_LIB.gsub(/lib$/, 'dll')
14
+
15
+ EXTENSION_NAME = "ruby_prof.#{Config::CONFIG["DLEXT"]}"
16
+
17
+ CLEAN.include('*.o')
18
+ CLOBBER.include(EXTENSION_NAME)
19
+
20
+ task :default => "ruby_prof"
21
+
22
+ SRC = FileList['../*.c']
23
+ OBJ = SRC.collect do |file_name|
24
+ File.basename(file_name).ext('o')
25
+ end
26
+
27
+ SRC.each do |srcfile|
28
+ objfile = File.basename(srcfile).ext('o')
29
+ file objfile => srcfile do
30
+ command = "gcc -c -fPIC -O2 -Wall -o #{objfile} -I/usr/local/include #{srcfile} -I#{RUBY_INCLUDE_DIR}"
31
+ sh "sh -c '#{command}'"
32
+ end
33
+ end
34
+
35
+ file "ruby_prof" => OBJ do
36
+ command = "gcc -shared -o #{EXTENSION_NAME} -L/usr/local/lib #{OBJ} #{RUBY_BIN_DIR}/#{RUBY_SHARED_DLL}"
37
+ sh "sh -c '#{command}'"
38
+ end
@@ -0,0 +1,1747 @@
1
+ /*
2
+ * Copyright (C) 2008 Shugo Maeda <shugo@ruby-lang.org>
3
+ * Charlie Savage <cfis@savagexi.com>
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions
8
+ * are met:
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * 2. Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ *
15
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
+ * SUCH DAMAGE.
26
+ */
27
+
28
+ /* ruby-prof tracks the time spent executing every method in ruby programming.
29
+ The main players are:
30
+
31
+ prof_result_t - Its one field, values, contains the overall results
32
+ thread_data_t - Stores data about a single thread.
33
+ prof_stack_t - The method call stack in a particular thread
34
+ prof_method_t - Profiling information for each method
35
+ prof_call_info_t - Keeps track a method's callers and callees.
36
+
37
+ The final resulut is a hash table of thread_data_t, keyed on the thread
38
+ id. Each thread has an hash a table of prof_method_t, keyed on the
39
+ method id. A hash table is used for quick look up when doing a profile.
40
+ However, it is exposed to Ruby as an array.
41
+
42
+ Each prof_method_t has two hash tables, parent and children, of prof_call_info_t.
43
+ These objects keep track of a method's callers (who called the method) and its
44
+ callees (who the method called). These are keyed the method id, but once again,
45
+ are exposed to Ruby as arrays. Each prof_call_into_t maintains a pointer to the
46
+ caller or callee method, thereby making it easy to navigate through the call
47
+ hierarchy in ruby - which is very helpful for creating call graphs.
48
+ */
49
+
50
+ #include "ruby_prof.h"
51
+
52
+ /* ================ Helper Functions =================*/
53
+ static VALUE
54
+ figure_singleton_name(VALUE klass)
55
+ {
56
+ VALUE result = Qnil;
57
+
58
+ /* We have come across a singleton object. First
59
+ figure out what it is attached to.*/
60
+ VALUE attached = rb_iv_get(klass, "__attached__");
61
+
62
+ /* Is this a singleton class acting as a metaclass? */
63
+ if (BUILTIN_TYPE(attached) == T_CLASS)
64
+ {
65
+ result = rb_str_new2("<Class::");
66
+ rb_str_append(result, rb_inspect(attached));
67
+ rb_str_cat2(result, ">");
68
+ }
69
+
70
+ /* Is this for singleton methods on a module? */
71
+ else if (BUILTIN_TYPE(attached) == T_MODULE)
72
+ {
73
+ result = rb_str_new2("<Module::");
74
+ rb_str_append(result, rb_inspect(attached));
75
+ rb_str_cat2(result, ">");
76
+ }
77
+
78
+ /* Is this for singleton methods on an object? */
79
+ else if (BUILTIN_TYPE(attached) == T_OBJECT)
80
+ {
81
+ /* Make sure to get the super class so that we don't
82
+ mistakenly grab a T_ICLASS which would lead to
83
+ unknown method errors. */
84
+ #ifdef RCLASS_SUPER
85
+ VALUE super = rb_class_real(RCLASS_SUPER(klass));
86
+ #else
87
+ VALUE super = rb_class_real(RCLASS(klass)->super);
88
+ #endif
89
+ result = rb_str_new2("<Object::");
90
+ rb_str_append(result, rb_inspect(super));
91
+ rb_str_cat2(result, ">");
92
+ }
93
+
94
+ /* Ok, this could be other things like an array made put onto
95
+ a singleton object (yeah, it happens, see the singleton
96
+ objects test case). */
97
+ else
98
+ {
99
+ result = rb_inspect(klass);
100
+ }
101
+
102
+ return result;
103
+ }
104
+
105
+ static VALUE
106
+ klass_name(VALUE klass)
107
+ {
108
+ VALUE result = Qnil;
109
+
110
+ if (klass == 0 || klass == Qnil)
111
+ {
112
+ result = rb_str_new2("Global");
113
+ }
114
+ else if (BUILTIN_TYPE(klass) == T_MODULE)
115
+ {
116
+ result = rb_inspect(klass);
117
+ }
118
+ else if (BUILTIN_TYPE(klass) == T_CLASS && FL_TEST(klass, FL_SINGLETON))
119
+ {
120
+ result = figure_singleton_name(klass);
121
+ }
122
+ else if (BUILTIN_TYPE(klass) == T_CLASS)
123
+ {
124
+ result = rb_inspect(klass);
125
+ }
126
+ else
127
+ {
128
+ /* Should never happen. */
129
+ result = rb_str_new2("Unknown");
130
+ }
131
+
132
+ return result;
133
+ }
134
+
135
+ static VALUE
136
+ method_name(ID mid)
137
+ {
138
+ VALUE result;
139
+
140
+ if (mid == ID_ALLOCATOR)
141
+ result = rb_str_new2("allocate");
142
+ else if (mid == 0)
143
+ result = rb_str_new2("[No method]");
144
+ else
145
+ result = rb_String(ID2SYM(mid));
146
+
147
+ return result;
148
+ }
149
+
150
+ static VALUE
151
+ full_name(VALUE klass, ID mid)
152
+ {
153
+ VALUE result = klass_name(klass);
154
+ rb_str_cat2(result, "#");
155
+ rb_str_append(result, method_name(mid));
156
+ return result;
157
+ }
158
+
159
+ /* ================ Stack Handling =================*/
160
+ /* Creates a stack of prof_frame_t to keep track
161
+ of timings for active methods. */
162
+ static prof_stack_t *
163
+ stack_create()
164
+ {
165
+ prof_stack_t *stack = ALLOC(prof_stack_t);
166
+ stack->start = ALLOC_N(prof_frame_t, INITIAL_STACK_SIZE);
167
+ stack->ptr = stack->start;
168
+ stack->end = stack->start + INITIAL_STACK_SIZE;
169
+ return stack;
170
+ }
171
+
172
+ static void
173
+ stack_free(prof_stack_t *stack)
174
+ {
175
+ xfree(stack->start);
176
+ xfree(stack);
177
+ }
178
+
179
+ static prof_frame_t *
180
+ stack_push(prof_stack_t *stack)
181
+ {
182
+ /* Is there space on the stack? If not, double
183
+ its size. */
184
+ if (stack->ptr == stack->end)
185
+ {
186
+ size_t len = stack->ptr - stack->start;
187
+ size_t new_capacity = (stack->end - stack->start) * 2;
188
+ REALLOC_N(stack->start, prof_frame_t, new_capacity);
189
+ stack->ptr = stack->start + len;
190
+ stack->end = stack->start + new_capacity;
191
+ }
192
+ return stack->ptr++;
193
+ }
194
+
195
+ static prof_frame_t *
196
+ stack_pop(prof_stack_t *stack)
197
+ {
198
+ if (stack->ptr == stack->start)
199
+ return NULL;
200
+ else
201
+ return --stack->ptr;
202
+ }
203
+
204
+ static prof_frame_t *
205
+ stack_peek(prof_stack_t *stack)
206
+ {
207
+ if (stack->ptr == stack->start)
208
+ return NULL;
209
+ else
210
+ return stack->ptr - 1;
211
+ }
212
+
213
+ /* ================ Method Key =================*/
214
+ static int
215
+ method_table_cmp(prof_method_key_t *key1, prof_method_key_t *key2)
216
+ {
217
+ return (key1->klass != key2->klass) || (key1->mid != key2->mid);
218
+ }
219
+
220
+ static int
221
+ method_table_hash(prof_method_key_t *key)
222
+ {
223
+ return key->key;
224
+ }
225
+
226
+ static struct st_hash_type type_method_hash = {
227
+ method_table_cmp,
228
+ method_table_hash
229
+ };
230
+
231
+ static void
232
+ method_key(prof_method_key_t* key, VALUE klass, ID mid)
233
+ {
234
+ key->klass = klass;
235
+ key->mid = mid;
236
+ key->key = (klass << 4) + (mid << 2);
237
+ }
238
+
239
+
240
+ /* ================ Call Info =================*/
241
+ static st_table *
242
+ call_info_table_create()
243
+ {
244
+ return st_init_table(&type_method_hash);
245
+ }
246
+
247
+ static size_t
248
+ call_info_table_insert(st_table *table, const prof_method_key_t *key, prof_call_info_t *val)
249
+ {
250
+ return st_insert(table, (st_data_t) key, (st_data_t) val);
251
+ }
252
+
253
+ static prof_call_info_t *
254
+ call_info_table_lookup(st_table *table, const prof_method_key_t *key)
255
+ {
256
+ st_data_t val;
257
+ if (st_lookup(table, (st_data_t) key, &val))
258
+ {
259
+ return (prof_call_info_t *) val;
260
+ }
261
+ else
262
+ {
263
+ return NULL;
264
+ }
265
+ }
266
+
267
+ static void
268
+ call_info_table_free(st_table *table)
269
+ {
270
+ st_free_table(table);
271
+ }
272
+
273
+ /* Document-class: RubyProf::CallInfo
274
+ RubyProf::CallInfo is a helper class used by RubyProf::MethodInfo
275
+ to keep track of which child methods were called and how long
276
+ they took to execute. */
277
+
278
+ /* :nodoc: */
279
+ static prof_call_info_t *
280
+ prof_call_info_create(prof_method_t* method, prof_call_info_t* parent)
281
+ {
282
+ prof_call_info_t *result = ALLOC(prof_call_info_t);
283
+ result->object = Qnil;
284
+ result->target = method;
285
+ result->parent = parent;
286
+ result->call_infos = call_info_table_create();
287
+ result->children = Qnil;
288
+
289
+ result->called = 0;
290
+ result->total_time = 0;
291
+ result->self_time = 0;
292
+ result->wait_time = 0;
293
+ result->line = 0;
294
+ return result;
295
+ }
296
+
297
+ static void
298
+ prof_call_info_mark(prof_call_info_t *call_info)
299
+ {
300
+ rb_gc_mark(prof_method_wrap(call_info->target));
301
+ rb_gc_mark(call_info->children);
302
+ if (call_info->parent)
303
+ rb_gc_mark(prof_call_info_wrap(call_info->parent));
304
+ }
305
+
306
+ static void
307
+ prof_call_info_free(prof_call_info_t *call_info)
308
+ {
309
+ call_info_table_free(call_info->call_infos);
310
+ xfree(call_info);
311
+ }
312
+
313
+ static VALUE
314
+ prof_call_info_wrap(prof_call_info_t *call_info)
315
+ {
316
+ if (call_info->object == Qnil)
317
+ {
318
+ call_info->object = Data_Wrap_Struct(cCallInfo, prof_call_info_mark, prof_call_info_free, call_info);
319
+ }
320
+ return call_info->object;
321
+ }
322
+
323
+ static prof_call_info_t *
324
+ prof_get_call_info_result(VALUE obj)
325
+ {
326
+ if (BUILTIN_TYPE(obj) != T_DATA)
327
+ {
328
+ /* Should never happen */
329
+ rb_raise(rb_eTypeError, "Not a call info object");
330
+ }
331
+ return (prof_call_info_t *) DATA_PTR(obj);
332
+ }
333
+
334
+
335
+ /* call-seq:
336
+ called -> MethodInfo
337
+
338
+ Returns the target method. */
339
+ static VALUE
340
+ prof_call_info_target(VALUE self)
341
+ {
342
+ /* Target is a pointer to a method_info - so we have to be careful
343
+ about the GC. We will wrap the method_info but provide no
344
+ free method so the underlying object is not freed twice! */
345
+
346
+ prof_call_info_t *result = prof_get_call_info_result(self);
347
+ return prof_method_wrap(result->target);
348
+ }
349
+
350
+ /* call-seq:
351
+ called -> int
352
+
353
+ Returns the total amount of time this method was called. */
354
+ static VALUE
355
+ prof_call_info_called(VALUE self)
356
+ {
357
+ prof_call_info_t *result = prof_get_call_info_result(self);
358
+ return INT2NUM(result->called);
359
+ }
360
+
361
+ /* call-seq:
362
+ called=n -> n
363
+
364
+ Sets the call count to n. */
365
+ static VALUE
366
+ prof_call_info_set_called(VALUE self, VALUE called)
367
+ {
368
+ prof_call_info_t *result = prof_get_call_info_result(self);
369
+ result->called = NUM2INT(called);
370
+ return called;
371
+ }
372
+
373
+ /* call-seq:
374
+ line_no -> int
375
+
376
+ returns the line number of the method */
377
+ static VALUE
378
+ prof_call_info_line(VALUE self)
379
+ {
380
+ prof_call_info_t *result = prof_get_call_info_result(self);
381
+ return rb_int_new(result->line);
382
+ }
383
+
384
+ /* call-seq:
385
+ total_time -> float
386
+
387
+ Returns the total amount of time spent in this method and its children. */
388
+ static VALUE
389
+ prof_call_info_total_time(VALUE self)
390
+ {
391
+ prof_call_info_t *result = prof_get_call_info_result(self);
392
+ return rb_float_new(convert_measurement(result->total_time));
393
+ }
394
+
395
+ /* call-seq:
396
+ add_total_time(call_info) -> nil
397
+
398
+ adds total time time from call_info to self. */
399
+ static VALUE
400
+ prof_call_info_add_total_time(VALUE self, VALUE other)
401
+ {
402
+ prof_call_info_t *result = prof_get_call_info_result(self);
403
+ prof_call_info_t *other_info = prof_get_call_info_result(other);
404
+
405
+ result->total_time += other_info->total_time;
406
+ return Qnil;
407
+ }
408
+
409
+ /* call-seq:
410
+ self_time -> float
411
+
412
+ Returns the total amount of time spent in this method. */
413
+ static VALUE
414
+ prof_call_info_self_time(VALUE self)
415
+ {
416
+ prof_call_info_t *result = prof_get_call_info_result(self);
417
+
418
+ return rb_float_new(convert_measurement(result->self_time));
419
+ }
420
+
421
+ /* call-seq:
422
+ add_self_time(call_info) -> nil
423
+
424
+ adds self time from call_info to self. */
425
+ static VALUE
426
+ prof_call_info_add_self_time(VALUE self, VALUE other)
427
+ {
428
+ prof_call_info_t *result = prof_get_call_info_result(self);
429
+ prof_call_info_t *other_info = prof_get_call_info_result(other);
430
+
431
+ result->self_time += other_info->self_time;
432
+ return Qnil;
433
+ }
434
+
435
+ /* call-seq:
436
+ wait_time -> float
437
+
438
+ Returns the total amount of time this method waited for other threads. */
439
+ static VALUE
440
+ prof_call_info_wait_time(VALUE self)
441
+ {
442
+ prof_call_info_t *result = prof_get_call_info_result(self);
443
+
444
+ return rb_float_new(convert_measurement(result->wait_time));
445
+ }
446
+
447
+ /* call-seq:
448
+ add_wait_time(call_info) -> nil
449
+
450
+ adds wait time from call_info to self. */
451
+
452
+ static VALUE
453
+ prof_call_info_add_wait_time(VALUE self, VALUE other)
454
+ {
455
+ prof_call_info_t *result = prof_get_call_info_result(self);
456
+ prof_call_info_t *other_info = prof_get_call_info_result(other);
457
+
458
+ result->wait_time += other_info->wait_time;
459
+ return Qnil;
460
+ }
461
+
462
+ /* call-seq:
463
+ parent -> call_info
464
+
465
+ Returns the call_infos parent call_info object (the method that called this method).*/
466
+ static VALUE
467
+ prof_call_info_parent(VALUE self)
468
+ {
469
+ prof_call_info_t *result = prof_get_call_info_result(self);
470
+ if (result->parent)
471
+ return prof_call_info_wrap(result->parent);
472
+ else
473
+ return Qnil;
474
+ }
475
+
476
+ /* call-seq:
477
+ parent=new_parent -> new_parent
478
+
479
+ Changes the parent of self to new_parent and returns it.*/
480
+ static VALUE
481
+ prof_call_info_set_parent(VALUE self, VALUE new_parent)
482
+ {
483
+ prof_call_info_t *result = prof_get_call_info_result(self);
484
+ if (new_parent == Qnil)
485
+ result->parent = NULL;
486
+ else
487
+ result->parent = prof_get_call_info_result(new_parent);
488
+ return prof_call_info_parent(self);
489
+ }
490
+
491
+ static int
492
+ prof_call_info_collect_children(st_data_t key, st_data_t value, st_data_t result)
493
+ {
494
+ prof_call_info_t *call_info = (prof_call_info_t *) value;
495
+ VALUE arr = (VALUE) result;
496
+ rb_ary_push(arr, prof_call_info_wrap(call_info));
497
+ return ST_CONTINUE;
498
+ }
499
+
500
+ /* call-seq:
501
+ children -> hash
502
+
503
+ Returns an array of call info objects of methods that this method
504
+ called (ie, children).*/
505
+ static VALUE
506
+ prof_call_info_children(VALUE self)
507
+ {
508
+ prof_call_info_t *call_info = prof_get_call_info_result(self);
509
+ if (call_info->children == Qnil)
510
+ {
511
+ call_info->children = rb_ary_new();
512
+ st_foreach(call_info->call_infos, prof_call_info_collect_children, call_info->children);
513
+ }
514
+ return call_info->children;
515
+ }
516
+
517
+ /* ================ Call Infos =================*/
518
+ static prof_call_infos_t*
519
+ prof_call_infos_create()
520
+ {
521
+ prof_call_infos_t *result = ALLOC(prof_call_infos_t);
522
+ result->start = ALLOC_N(prof_call_info_t*, INITIAL_CALL_INFOS_SIZE);
523
+ result->end = result->start + INITIAL_CALL_INFOS_SIZE;
524
+ result->ptr = result->start;
525
+ result->object = Qnil;
526
+ return result;
527
+ }
528
+
529
+ static void
530
+ prof_call_infos_free(prof_call_infos_t *call_infos)
531
+ {
532
+ xfree(call_infos->start);
533
+ xfree(call_infos);
534
+ }
535
+
536
+ static void
537
+ prof_add_call_info(prof_call_infos_t *call_infos, prof_call_info_t *call_info)
538
+ {
539
+ if (call_infos->ptr == call_infos->end)
540
+ {
541
+ size_t len = call_infos->ptr - call_infos->start;
542
+ size_t new_capacity = (call_infos->end - call_infos->start) * 2;
543
+ REALLOC_N(call_infos->start, prof_call_info_t*, new_capacity);
544
+ call_infos->ptr = call_infos->start + len;
545
+ call_infos->end = call_infos->start + new_capacity;
546
+ }
547
+ *call_infos->ptr = call_info;
548
+ call_infos->ptr++;
549
+ }
550
+
551
+ static VALUE
552
+ prof_call_infos_wrap(prof_call_infos_t *call_infos)
553
+ {
554
+ if (call_infos->object == Qnil)
555
+ {
556
+ prof_call_info_t **i;
557
+ call_infos->object = rb_ary_new();
558
+ for(i=call_infos->start; i<call_infos->ptr; i++)
559
+ {
560
+ VALUE call_info = prof_call_info_wrap(*i);
561
+ rb_ary_push(call_infos->object, call_info);
562
+ }
563
+ }
564
+ return call_infos->object;
565
+ }
566
+
567
+
568
+ /* ================ Method Info =================*/
569
+ /* Document-class: RubyProf::MethodInfo
570
+ The RubyProf::MethodInfo class stores profiling data for a method.
571
+ One instance of the RubyProf::MethodInfo class is created per method
572
+ called per thread. Thus, if a method is called in two different
573
+ thread then there will be two RubyProf::MethodInfo objects
574
+ created. RubyProf::MethodInfo objects can be accessed via
575
+ the RubyProf::Result object.
576
+ */
577
+
578
+ static prof_method_t*
579
+ prof_method_create(prof_method_key_t *key, const char* source_file, int line)
580
+ {
581
+ prof_method_t *result = ALLOC(prof_method_t);
582
+ result->object = Qnil;
583
+ result->key = ALLOC(prof_method_key_t);
584
+ method_key(result->key, key->klass, key->mid);
585
+
586
+ result->call_infos = prof_call_infos_create();
587
+
588
+ if (source_file != NULL)
589
+ {
590
+ int len = strlen(source_file) + 1;
591
+ char *buffer = ALLOC_N(char, len);
592
+
593
+ MEMCPY(buffer, source_file, char, len);
594
+ result->source_file = buffer;
595
+ }
596
+ else
597
+ {
598
+ result->source_file = source_file;
599
+ }
600
+ result->line = line;
601
+
602
+ return result;
603
+ }
604
+
605
+ static void
606
+ prof_method_mark(prof_method_t *method)
607
+ {
608
+ rb_gc_mark(method->call_infos->object);
609
+ rb_gc_mark(method->key->klass);
610
+ }
611
+
612
+ static void
613
+ prof_method_free(prof_method_t *method)
614
+ {
615
+ if (method->source_file)
616
+ {
617
+ xfree((char*)method->source_file);
618
+ }
619
+
620
+ prof_call_infos_free(method->call_infos);
621
+ xfree(method->key);
622
+ xfree(method);
623
+ }
624
+
625
+ static VALUE
626
+ prof_method_wrap(prof_method_t *result)
627
+ {
628
+ if (result->object == Qnil)
629
+ {
630
+ result->object = Data_Wrap_Struct(cMethodInfo, prof_method_mark, prof_method_free, result);
631
+ }
632
+ return result->object;
633
+ }
634
+
635
+ static prof_method_t *
636
+ get_prof_method(VALUE obj)
637
+ {
638
+ return (prof_method_t *) DATA_PTR(obj);
639
+ }
640
+
641
+ /* call-seq:
642
+ line_no -> int
643
+
644
+ returns the line number of the method */
645
+ static VALUE
646
+ prof_method_line(VALUE self)
647
+ {
648
+ return rb_int_new(get_prof_method(self)->line);
649
+ }
650
+
651
+ /* call-seq:
652
+ source_file => string
653
+
654
+ return the source file of the method
655
+ */
656
+ static VALUE prof_method_source_file(VALUE self)
657
+ {
658
+ const char* sf = get_prof_method(self)->source_file;
659
+ if(!sf)
660
+ {
661
+ return rb_str_new2("ruby_runtime");
662
+ }
663
+ else
664
+ {
665
+ return rb_str_new2(sf);
666
+ }
667
+ }
668
+
669
+
670
+ /* call-seq:
671
+ method_class -> klass
672
+
673
+ Returns the Ruby klass that owns this method. */
674
+ static VALUE
675
+ prof_method_klass(VALUE self)
676
+ {
677
+ prof_method_t *result = get_prof_method(self);
678
+ return result->key->klass;
679
+ }
680
+
681
+ /* call-seq:
682
+ method_id -> ID
683
+
684
+ Returns the id of this method. */
685
+ static VALUE
686
+ prof_method_id(VALUE self)
687
+ {
688
+ prof_method_t *result = get_prof_method(self);
689
+ return ID2SYM(result->key->mid);
690
+ }
691
+
692
+ /* call-seq:
693
+ klass_name -> string
694
+
695
+ Returns the name of this method's class. Singleton classes
696
+ will have the form <Object::Object>. */
697
+
698
+ static VALUE
699
+ prof_klass_name(VALUE self)
700
+ {
701
+ prof_method_t *method = get_prof_method(self);
702
+ return klass_name(method->key->klass);
703
+ }
704
+
705
+ /* call-seq:
706
+ method_name -> string
707
+
708
+ Returns the name of this method in the format Object#method. Singletons
709
+ methods will be returned in the format <Object::Object>#method.*/
710
+
711
+ static VALUE
712
+ prof_method_name(VALUE self)
713
+ {
714
+ prof_method_t *method = get_prof_method(self);
715
+ return method_name(method->key->mid);
716
+ }
717
+
718
+ /* call-seq:
719
+ full_name -> string
720
+
721
+ Returns the full name of this method in the format Object#method.*/
722
+
723
+ static VALUE
724
+ prof_full_name(VALUE self)
725
+ {
726
+ prof_method_t *method = get_prof_method(self);
727
+ return full_name(method->key->klass, method->key->mid);
728
+ }
729
+
730
+ /* call-seq:
731
+ call_infos -> Array of call_info
732
+
733
+ Returns an array of call info objects that contain profiling information
734
+ about the current method.*/
735
+ static VALUE
736
+ prof_method_call_infos(VALUE self)
737
+ {
738
+ prof_method_t *method = get_prof_method(self);
739
+ return prof_call_infos_wrap(method->call_infos);
740
+ }
741
+
742
+ static int
743
+ collect_methods(st_data_t key, st_data_t value, st_data_t result)
744
+ {
745
+ /* Called for each method stored in a thread's method table.
746
+ We want to store the method info information into an array.*/
747
+ VALUE methods = (VALUE) result;
748
+ prof_method_t *method = (prof_method_t *) value;
749
+ rb_ary_push(methods, prof_method_wrap(method));
750
+
751
+ /* Wrap call info objects */
752
+ prof_call_infos_wrap(method->call_infos);
753
+
754
+ return ST_CONTINUE;
755
+ }
756
+
757
+ /* ================ Method Table =================*/
758
+ static st_table *
759
+ method_table_create()
760
+ {
761
+ return st_init_table(&type_method_hash);
762
+ }
763
+
764
+ static size_t
765
+ method_table_insert(st_table *table, const prof_method_key_t *key, prof_method_t *val)
766
+ {
767
+ return st_insert(table, (st_data_t) key, (st_data_t) val);
768
+ }
769
+
770
+ static prof_method_t *
771
+ method_table_lookup(st_table *table, const prof_method_key_t* key)
772
+ {
773
+ st_data_t val;
774
+ if (st_lookup(table, (st_data_t)key, &val))
775
+ {
776
+ return (prof_method_t *) val;
777
+ }
778
+ else
779
+ {
780
+ return NULL;
781
+ }
782
+ }
783
+
784
+
785
+ static void
786
+ method_table_free(st_table *table)
787
+ {
788
+ /* Don't free the contents since they are wrapped by
789
+ Ruby objects! */
790
+ st_free_table(table);
791
+ }
792
+
793
+
794
+ /* ================ Thread Handling =================*/
795
+
796
+ /* ---- Keeps track of thread's stack and methods ---- */
797
+ static thread_data_t*
798
+ thread_data_create()
799
+ {
800
+ thread_data_t* result = ALLOC(thread_data_t);
801
+ result->stack = stack_create();
802
+ result->method_table = method_table_create();
803
+ result->last_switch = get_measurement();
804
+ return result;
805
+ }
806
+
807
+ static void
808
+ thread_data_free(thread_data_t* thread_data)
809
+ {
810
+ method_table_free(thread_data->method_table);
811
+ stack_free(thread_data->stack);
812
+ xfree(thread_data);
813
+ }
814
+
815
+ /* ---- Hash, keyed on thread, that stores thread's stack
816
+ and methods---- */
817
+
818
+ static st_table *
819
+ threads_table_create()
820
+ {
821
+ return st_init_numtable();
822
+ }
823
+
824
+ static size_t
825
+ threads_table_insert(st_table *table, VALUE thread, thread_data_t *thread_data)
826
+ {
827
+ /* Its too slow to key on the real thread id so just typecast thread instead. */
828
+ return st_insert(table, (st_data_t) thread, (st_data_t) thread_data);
829
+ }
830
+
831
+ static thread_data_t *
832
+ threads_table_lookup(st_table *table, VALUE thread_id)
833
+ {
834
+ thread_data_t* result;
835
+ st_data_t val;
836
+
837
+ /* Its too slow to key on the real thread id so just typecast thread instead. */
838
+ if (st_lookup(table, (st_data_t) thread_id, &val))
839
+ {
840
+ result = (thread_data_t *) val;
841
+ }
842
+ else
843
+ {
844
+ result = thread_data_create();
845
+ result->thread_id = thread_id;
846
+
847
+ /* Insert the table */
848
+ threads_table_insert(threads_tbl, thread_id, result);
849
+ }
850
+ return result;
851
+ }
852
+
853
+ static int
854
+ free_thread_data(st_data_t key, st_data_t value, st_data_t dummy)
855
+ {
856
+ thread_data_free((thread_data_t*)value);
857
+ return ST_CONTINUE;
858
+ }
859
+
860
+
861
+ static void
862
+ threads_table_free(st_table *table)
863
+ {
864
+ st_foreach(table, free_thread_data, 0);
865
+ st_free_table(table);
866
+ }
867
+
868
+
869
+ static int
870
+ collect_threads(st_data_t key, st_data_t value, st_data_t result)
871
+ {
872
+ /* Although threads are keyed on an id, that is actually a
873
+ pointer to the VALUE object of the thread. So its bogus.
874
+ However, in thread_data is the real thread id stored
875
+ as an int. */
876
+ thread_data_t* thread_data = (thread_data_t*) value;
877
+ VALUE threads_hash = (VALUE) result;
878
+
879
+ VALUE methods = rb_ary_new();
880
+
881
+ /* Now collect an array of all the called methods */
882
+ st_table* method_table = thread_data->method_table;
883
+ st_foreach(method_table, collect_methods, methods);
884
+
885
+ /* Store the results in the threads hash keyed on the thread id. */
886
+ rb_hash_aset(threads_hash, thread_data->thread_id, methods);
887
+
888
+ return ST_CONTINUE;
889
+ }
890
+
891
+
892
+ /* ================ Profiling =================*/
893
+
894
+ /* support tracing ruby events from ruby-prof. useful for getting at
895
+ what actually happens inside the ruby interpreter (and ruby-prof).
896
+ set environment variable RUBY_PROF_TRACE to filename you want to
897
+ find the trace in.
898
+ */
899
+ static FILE* trace_file = NULL;
900
+
901
+ /* Copied from eval.c */
902
+ static char *
903
+ get_event_name(rb_event_flag_t event)
904
+ {
905
+ switch (event) {
906
+ case RUBY_EVENT_LINE:
907
+ return "line";
908
+ case RUBY_EVENT_CLASS:
909
+ return "class";
910
+ case RUBY_EVENT_END:
911
+ return "end";
912
+ case RUBY_EVENT_CALL:
913
+ return "call";
914
+ case RUBY_EVENT_RETURN:
915
+ return "return";
916
+ case RUBY_EVENT_C_CALL:
917
+ return "c-call";
918
+ case RUBY_EVENT_C_RETURN:
919
+ return "c-return";
920
+ case RUBY_EVENT_RAISE:
921
+ return "raise";
922
+ default:
923
+ return "unknown";
924
+ }
925
+ }
926
+
927
+
928
+ static prof_method_t*
929
+ get_method(rb_event_flag_t event, NODE *node, VALUE klass, ID mid, st_table* method_table)
930
+ {
931
+ prof_method_key_t key;
932
+ prof_method_t *method = NULL;
933
+
934
+ method_key(&key, klass, mid);
935
+ method = method_table_lookup(method_table, &key);
936
+
937
+ if (!method)
938
+ {
939
+ const char* source_file = rb_sourcefile();
940
+ int line = rb_sourceline();
941
+
942
+ /* Line numbers are not accurate for c method calls */
943
+ if (event == RUBY_EVENT_C_CALL)
944
+ {
945
+ line = 0;
946
+ source_file = NULL;
947
+ }
948
+
949
+ method = prof_method_create(&key, source_file, line);
950
+ method_table_insert(method_table, method->key, method);
951
+ }
952
+ return method;
953
+ }
954
+
955
+ static void
956
+ update_result(prof_measure_t total_time,
957
+ prof_frame_t *parent_frame,
958
+ prof_frame_t *frame)
959
+ {
960
+ prof_measure_t self_time = total_time - frame->child_time - frame->wait_time;
961
+
962
+ prof_call_info_t *call_info = frame->call_info;
963
+
964
+ /* Update information about the current method */
965
+ call_info->called++;
966
+ call_info->total_time += total_time;
967
+ call_info->self_time += self_time;
968
+ call_info->wait_time += frame->wait_time;
969
+
970
+ /* Note where the current method was called from */
971
+ if (parent_frame)
972
+ call_info->line = parent_frame->line;
973
+ }
974
+
975
+ static thread_data_t *
976
+ switch_thread(VALUE thread_id, prof_measure_t now)
977
+ {
978
+ prof_frame_t *frame = NULL;
979
+ prof_measure_t wait_time = 0;
980
+
981
+ /* Get new thread information. */
982
+ thread_data_t *thread_data = threads_table_lookup(threads_tbl, thread_id);
983
+
984
+ /* How long has this thread been waiting? */
985
+ wait_time = now - thread_data->last_switch;
986
+ thread_data->last_switch = 0;
987
+
988
+ /* Get the frame at the top of the stack. This may represent
989
+ the current method (EVENT_LINE, EVENT_RETURN) or the
990
+ previous method (EVENT_CALL).*/
991
+ frame = stack_peek(thread_data->stack);
992
+
993
+ if (frame)
994
+ frame->wait_time += wait_time;
995
+
996
+ /* Save on the last thread the time of the context switch
997
+ and reset this thread's last context switch to 0.*/
998
+ if (last_thread_data)
999
+ last_thread_data->last_switch = now;
1000
+
1001
+ last_thread_data = thread_data;
1002
+ return thread_data;
1003
+ }
1004
+
1005
+ static prof_frame_t*
1006
+ pop_frame(thread_data_t *thread_data, prof_measure_t now)
1007
+ {
1008
+ prof_frame_t *frame = NULL;
1009
+ prof_frame_t* parent_frame = NULL;
1010
+ prof_measure_t total_time;
1011
+
1012
+ frame = stack_pop(thread_data->stack);
1013
+
1014
+ /* Frame can be null. This can happen if RubProf.start is called from
1015
+ a method that exits. And it can happen if an exception is raised
1016
+ in code that is being profiled and the stack unwinds (RubProf is
1017
+ not notified of that by the ruby runtime. */
1018
+ if (frame == NULL) return NULL;
1019
+
1020
+ /* Calculate the total time this method took */
1021
+ total_time = now - frame->start_time;
1022
+
1023
+ parent_frame = stack_peek(thread_data->stack);
1024
+ if (parent_frame)
1025
+ {
1026
+ parent_frame->child_time += total_time;
1027
+ }
1028
+
1029
+ update_result(total_time, parent_frame, frame);
1030
+ return frame;
1031
+ }
1032
+
1033
+ static int
1034
+ pop_frames(st_data_t key, st_data_t value, st_data_t now_arg)
1035
+ {
1036
+ VALUE thread_id = (VALUE)key;
1037
+ thread_data_t* thread_data = (thread_data_t *) value;
1038
+ prof_measure_t now = *(prof_measure_t *) now_arg;
1039
+
1040
+ if (!last_thread_data || last_thread_data->thread_id != thread_id)
1041
+ thread_data = switch_thread(thread_id, now);
1042
+ else
1043
+ thread_data = last_thread_data;
1044
+
1045
+ while (pop_frame(thread_data, now))
1046
+ {
1047
+ }
1048
+
1049
+ return ST_CONTINUE;
1050
+ }
1051
+
1052
+ static void
1053
+ prof_pop_threads()
1054
+ {
1055
+ /* Get current measurement*/
1056
+ prof_measure_t now = get_measurement();
1057
+ st_foreach(threads_tbl, pop_frames, (st_data_t) &now);
1058
+ }
1059
+
1060
+ #ifdef RUBY_VM
1061
+ static void
1062
+ prof_event_hook(rb_event_flag_t event, NODE *node, VALUE data, VALUE self, ID mid, VALUE klass)
1063
+ #else
1064
+ static void
1065
+ prof_event_hook(rb_event_flag_t event, NODE *node, VALUE self, ID mid, VALUE klass)
1066
+ #endif
1067
+ {
1068
+
1069
+ VALUE thread = Qnil;
1070
+ VALUE thread_id = Qnil;
1071
+ prof_measure_t now = 0;
1072
+ thread_data_t* thread_data = NULL;
1073
+ prof_frame_t *frame = NULL;
1074
+
1075
+
1076
+ #ifdef RUBY_VM
1077
+
1078
+ if (event != RUBY_EVENT_C_CALL && event != RUBY_EVENT_C_RETURN) {
1079
+ rb_frame_method_id_and_class(&mid, &klass);
1080
+ }
1081
+ #endif
1082
+
1083
+ if (trace_file!=NULL) {
1084
+ char* key = 0;
1085
+ static VALUE last_thread_id = Qnil;
1086
+
1087
+ VALUE thread = rb_thread_current();
1088
+ VALUE thread_id = rb_obj_id(thread);
1089
+ char* class_name = NULL;
1090
+ char* method_name = rb_id2name(mid);
1091
+ char* source_file = rb_sourcefile();
1092
+ unsigned int source_line = rb_sourceline();
1093
+
1094
+ char* event_name = get_event_name(event);
1095
+
1096
+ if (klass != 0)
1097
+ klass = (BUILTIN_TYPE(klass) == T_ICLASS ? RBASIC(klass)->klass : klass);
1098
+
1099
+ class_name = rb_class2name(klass);
1100
+
1101
+ if (last_thread_id != thread_id)
1102
+ fprintf(trace_file, "\n");
1103
+
1104
+ fprintf(trace_file, "%2u: %-8s :%2d %s#%s\n",
1105
+ thread_id, event_name, source_line, class_name, method_name);
1106
+
1107
+ last_thread_id = thread_id;
1108
+ }
1109
+
1110
+ /* Special case - skip any methods from the mProf
1111
+ module, such as Prof.stop, since they clutter
1112
+ the results but aren't important to them results. */
1113
+ if (self == mProf) return;
1114
+
1115
+ /* Get current measurement*/
1116
+ now = get_measurement();
1117
+
1118
+ /* Get the current thread information. */
1119
+ thread = rb_thread_current();
1120
+ thread_id = rb_obj_id(thread);
1121
+
1122
+ if (exclude_threads_tbl &&
1123
+ st_lookup(exclude_threads_tbl, (st_data_t) thread_id, 0))
1124
+ {
1125
+ return;
1126
+ }
1127
+
1128
+ /* Was there a context switch? */
1129
+ if (!last_thread_data || last_thread_data->thread_id != thread_id)
1130
+ thread_data = switch_thread(thread_id, now);
1131
+ else
1132
+ thread_data = last_thread_data;
1133
+
1134
+ /* Get the current frame for the current thread. */
1135
+ frame = stack_peek(thread_data->stack);
1136
+
1137
+ switch (event) {
1138
+ case RUBY_EVENT_LINE:
1139
+ {
1140
+ /* Keep track of the current line number in this method. When
1141
+ a new method is called, we know what line number it was
1142
+ called from. */
1143
+ if (frame)
1144
+ {
1145
+ frame->line = rb_sourceline();
1146
+ break;
1147
+ }
1148
+
1149
+ /* If we get here there was no frame, which means this is
1150
+ the first method seen for this thread, so fall through
1151
+ to below to create it. */
1152
+ }
1153
+ case RUBY_EVENT_CALL:
1154
+ case RUBY_EVENT_C_CALL:
1155
+ {
1156
+ prof_call_info_t *call_info = NULL;
1157
+ prof_method_t *method = NULL;
1158
+
1159
+ /* Is this an include for a module? If so get the actual
1160
+ module class since we want to combine all profiling
1161
+ results for that module. */
1162
+
1163
+ if (klass != 0)
1164
+ klass = (BUILTIN_TYPE(klass) == T_ICLASS ? RBASIC(klass)->klass : klass);
1165
+
1166
+ method = get_method(event, node, klass, mid, thread_data->method_table);
1167
+
1168
+ if (!frame)
1169
+ {
1170
+ call_info = prof_call_info_create(method, NULL);
1171
+ prof_add_call_info(method->call_infos, call_info);
1172
+ }
1173
+ else
1174
+ {
1175
+ call_info = call_info_table_lookup(frame->call_info->call_infos, method->key);
1176
+
1177
+ if (!call_info)
1178
+ {
1179
+ call_info = prof_call_info_create(method, frame->call_info);
1180
+ call_info_table_insert(frame->call_info->call_infos, method->key, call_info);
1181
+ prof_add_call_info(method->call_infos, call_info);
1182
+ }
1183
+ }
1184
+
1185
+ /* Push a new frame onto the stack */
1186
+ frame = stack_push(thread_data->stack);
1187
+ frame->call_info = call_info;
1188
+ frame->start_time = now;
1189
+ frame->wait_time = 0;
1190
+ frame->child_time = 0;
1191
+ frame->line = rb_sourceline();
1192
+
1193
+ break;
1194
+ }
1195
+ case RUBY_EVENT_RETURN:
1196
+ case RUBY_EVENT_C_RETURN:
1197
+ {
1198
+ pop_frame(thread_data, now);
1199
+ break;
1200
+ }
1201
+ }
1202
+ }
1203
+
1204
+
1205
+ /* ======== ProfResult ============== */
1206
+
1207
+ /* Document-class: RubyProf::Result
1208
+ The RubyProf::Result class is used to store the results of a
1209
+ profiling run. And instace of the class is returned from
1210
+ the methods RubyProf#stop and RubyProf#profile.
1211
+
1212
+ RubyProf::Result has one field, called threads, which is a hash
1213
+ table keyed on thread ID. For each thread id, the hash table
1214
+ stores another hash table that contains profiling information
1215
+ for each method called during the threads execution. That
1216
+ hash table is keyed on method name and contains
1217
+ RubyProf::MethodInfo objects. */
1218
+
1219
+
1220
+ static void
1221
+ prof_result_mark(prof_result_t *prof_result)
1222
+ {
1223
+ VALUE threads = prof_result->threads;
1224
+ rb_gc_mark(threads);
1225
+ }
1226
+
1227
+ static void
1228
+ prof_result_free(prof_result_t *prof_result)
1229
+ {
1230
+ prof_result->threads = Qnil;
1231
+ xfree(prof_result);
1232
+ }
1233
+
1234
+ static VALUE
1235
+ prof_result_new()
1236
+ {
1237
+ prof_result_t *prof_result = ALLOC(prof_result_t);
1238
+
1239
+ /* Wrap threads in Ruby regular Ruby hash table. */
1240
+ prof_result->threads = rb_hash_new();
1241
+ st_foreach(threads_tbl, collect_threads, prof_result->threads);
1242
+
1243
+ return Data_Wrap_Struct(cResult, prof_result_mark, prof_result_free, prof_result);
1244
+ }
1245
+
1246
+
1247
+ static prof_result_t *
1248
+ get_prof_result(VALUE obj)
1249
+ {
1250
+ if (BUILTIN_TYPE(obj) != T_DATA ||
1251
+ RDATA(obj)->dfree != (RUBY_DATA_FUNC) prof_result_free)
1252
+ {
1253
+ /* Should never happen */
1254
+ rb_raise(rb_eTypeError, "wrong result object");
1255
+ }
1256
+ return (prof_result_t *) DATA_PTR(obj);
1257
+ }
1258
+
1259
+ /* call-seq:
1260
+ threads -> Hash
1261
+
1262
+ Returns a hash table keyed on thread ID. For each thread id,
1263
+ the hash table stores another hash table that contains profiling
1264
+ information for each method called during the threads execution.
1265
+ That hash table is keyed on method name and contains
1266
+ RubyProf::MethodInfo objects. */
1267
+ static VALUE
1268
+ prof_result_threads(VALUE self)
1269
+ {
1270
+ prof_result_t *prof_result = get_prof_result(self);
1271
+ return prof_result->threads;
1272
+ }
1273
+
1274
+
1275
+
1276
+ /* call-seq:
1277
+ measure_mode -> measure_mode
1278
+
1279
+ Returns what ruby-prof is measuring. Valid values include:
1280
+
1281
+ *RubyProf::PROCESS_TIME - Measure process time. This is default. It is implemented using the clock functions in the C Runtime library.
1282
+ *RubyProf::WALL_TIME - Measure wall time using gettimeofday on Linx and GetLocalTime on Windows
1283
+ *RubyProf::CPU_TIME - Measure time using the CPU clock counter. This mode is only supported on Pentium or PowerPC platforms.
1284
+ *RubyProf::ALLOCATIONS - Measure object allocations. This requires a patched Ruby interpreter.
1285
+ *RubyProf::MEMORY - Measure memory size. This requires a patched Ruby interpreter.
1286
+ *RubyProf::GC_RUNS - Measure number of garbage collections. This requires a patched Ruby interpreter.
1287
+ *RubyProf::GC_TIME - Measure time spent doing garbage collection. This requires a patched Ruby interpreter.*/
1288
+ static VALUE
1289
+ prof_get_measure_mode(VALUE self)
1290
+ {
1291
+ return INT2NUM(measure_mode);
1292
+ }
1293
+
1294
+ /* call-seq:
1295
+ measure_mode=value -> void
1296
+
1297
+ Specifies what ruby-prof should measure. Valid values include:
1298
+
1299
+ *RubyProf::PROCESS_TIME - Measure process time. This is default. It is implemented using the clock functions in the C Runtime library.
1300
+ *RubyProf::WALL_TIME - Measure wall time using gettimeofday on Linx and GetLocalTime on Windows
1301
+ *RubyProf::CPU_TIME - Measure time using the CPU clock counter. This mode is only supported on Pentium or PowerPC platforms.
1302
+ *RubyProf::ALLOCATIONS - Measure object allocations. This requires a patched Ruby interpreter.
1303
+ *RubyProf::MEMORY - Measure memory size. This requires a patched Ruby interpreter.
1304
+ *RubyProf::GC_RUNS - Measure number of garbage collections. This requires a patched Ruby interpreter.
1305
+ *RubyProf::GC_TIME - Measure time spent doing garbage collection. This requires a patched Ruby interpreter.*/
1306
+ static VALUE
1307
+ prof_set_measure_mode(VALUE self, VALUE val)
1308
+ {
1309
+ long mode = NUM2LONG(val);
1310
+
1311
+ if (threads_tbl)
1312
+ {
1313
+ rb_raise(rb_eRuntimeError, "can't set measure_mode while profiling");
1314
+ }
1315
+
1316
+ switch (mode) {
1317
+ case MEASURE_PROCESS_TIME:
1318
+ get_measurement = measure_process_time;
1319
+ convert_measurement = convert_process_time;
1320
+ break;
1321
+
1322
+ case MEASURE_WALL_TIME:
1323
+ get_measurement = measure_wall_time;
1324
+ convert_measurement = convert_wall_time;
1325
+ break;
1326
+
1327
+ #if defined(MEASURE_CPU_TIME)
1328
+ case MEASURE_CPU_TIME:
1329
+ if (cpu_frequency == 0)
1330
+ cpu_frequency = get_cpu_frequency();
1331
+ get_measurement = measure_cpu_time;
1332
+ convert_measurement = convert_cpu_time;
1333
+ break;
1334
+ #endif
1335
+
1336
+ #if defined(MEASURE_ALLOCATIONS)
1337
+ case MEASURE_ALLOCATIONS:
1338
+ get_measurement = measure_allocations;
1339
+ convert_measurement = convert_allocations;
1340
+ break;
1341
+ #endif
1342
+
1343
+ #if defined(MEASURE_MEMORY)
1344
+ case MEASURE_MEMORY:
1345
+ get_measurement = measure_memory;
1346
+ convert_measurement = convert_memory;
1347
+ break;
1348
+ #endif
1349
+
1350
+ #if defined(MEASURE_GC_RUNS)
1351
+ case MEASURE_GC_RUNS:
1352
+ get_measurement = measure_gc_runs;
1353
+ convert_measurement = convert_gc_runs;
1354
+ break;
1355
+ #endif
1356
+
1357
+ #if defined(MEASURE_GC_TIME)
1358
+ case MEASURE_GC_TIME:
1359
+ get_measurement = measure_gc_time;
1360
+ convert_measurement = convert_gc_time;
1361
+ break;
1362
+ #endif
1363
+
1364
+ default:
1365
+ rb_raise(rb_eArgError, "invalid mode: %ld", mode);
1366
+ break;
1367
+ }
1368
+
1369
+ measure_mode = mode;
1370
+ return val;
1371
+ }
1372
+
1373
+ /* call-seq:
1374
+ exclude_threads= -> void
1375
+
1376
+ Specifies what threads ruby-prof should exclude from profiling */
1377
+ static VALUE
1378
+ prof_set_exclude_threads(VALUE self, VALUE threads)
1379
+ {
1380
+ int i;
1381
+
1382
+ if (threads_tbl != NULL)
1383
+ {
1384
+ rb_raise(rb_eRuntimeError, "can't set exclude_threads while profiling");
1385
+ }
1386
+
1387
+ /* Stay simple, first free the old hash table */
1388
+ if (exclude_threads_tbl)
1389
+ {
1390
+ st_free_table(exclude_threads_tbl);
1391
+ exclude_threads_tbl = NULL;
1392
+ }
1393
+
1394
+ /* Now create a new one if the user passed in any threads */
1395
+ if (threads != Qnil)
1396
+ {
1397
+ Check_Type(threads, T_ARRAY);
1398
+ exclude_threads_tbl = st_init_numtable();
1399
+
1400
+ for (i=0; i < RARRAY_LEN(threads); ++i)
1401
+ {
1402
+ VALUE thread = rb_ary_entry(threads, i);
1403
+ st_insert(exclude_threads_tbl, (st_data_t) rb_obj_id(thread), 0);
1404
+ }
1405
+ }
1406
+ return threads;
1407
+ }
1408
+
1409
+
1410
+ /* ========= Profiling ============= */
1411
+ void
1412
+ prof_install_hook()
1413
+ {
1414
+ #ifdef RUBY_VM
1415
+ rb_add_event_hook(prof_event_hook,
1416
+ RUBY_EVENT_CALL | RUBY_EVENT_RETURN |
1417
+ RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN
1418
+ | RUBY_EVENT_LINE, Qnil);
1419
+ #else
1420
+ rb_add_event_hook(prof_event_hook,
1421
+ RUBY_EVENT_CALL | RUBY_EVENT_RETURN |
1422
+ RUBY_EVENT_C_CALL | RUBY_EVENT_C_RETURN
1423
+ | RUBY_EVENT_LINE);
1424
+ #endif
1425
+
1426
+ #if defined(TOGGLE_GC_STATS)
1427
+ rb_gc_enable_stats();
1428
+ #endif
1429
+ }
1430
+
1431
+ void
1432
+ prof_remove_hook()
1433
+ {
1434
+ #if defined(TOGGLE_GC_STATS)
1435
+ rb_gc_disable_stats();
1436
+ #endif
1437
+
1438
+ /* Now unregister from event */
1439
+ rb_remove_event_hook(prof_event_hook);
1440
+ }
1441
+
1442
+
1443
+
1444
+ /* call-seq:
1445
+ running? -> boolean
1446
+
1447
+ Returns whether a profile is currently running.*/
1448
+ static VALUE
1449
+ prof_running(VALUE self)
1450
+ {
1451
+ if (threads_tbl != NULL)
1452
+ return Qtrue;
1453
+ else
1454
+ return Qfalse;
1455
+ }
1456
+
1457
+ /* call-seq:
1458
+ start -> RubyProf
1459
+
1460
+ Starts recording profile data.*/
1461
+ static VALUE
1462
+ prof_start(VALUE self)
1463
+ {
1464
+ if (threads_tbl != NULL)
1465
+ {
1466
+ rb_raise(rb_eRuntimeError, "RubyProf.start was already called");
1467
+ }
1468
+
1469
+ /* Setup globals */
1470
+ last_thread_data = NULL;
1471
+ threads_tbl = threads_table_create();
1472
+
1473
+ /* open trace file if environment wants it */
1474
+ char* trace_file_name = getenv("RUBY_PROF_TRACE");
1475
+ if (trace_file_name != NULL) {
1476
+ if (0==strcmp(trace_file_name, "stdout")) {
1477
+ trace_file = stdout;
1478
+ } else if (0==strcmp(trace_file_name, "stderr")) {
1479
+ trace_file = stderr;
1480
+ } else {
1481
+ trace_file = fopen(trace_file_name, "a");
1482
+ }
1483
+ }
1484
+
1485
+ prof_install_hook();
1486
+ return self;
1487
+ }
1488
+
1489
+ /* call-seq:
1490
+ pause -> RubyProf
1491
+
1492
+ Pauses collecting profile data. */
1493
+ static VALUE
1494
+ prof_pause(VALUE self)
1495
+ {
1496
+ if (threads_tbl == NULL)
1497
+ {
1498
+ rb_raise(rb_eRuntimeError, "RubyProf is not running.");
1499
+ }
1500
+
1501
+ prof_remove_hook();
1502
+ return self;
1503
+ }
1504
+
1505
+ /* call-seq:
1506
+ resume {block} -> RubyProf
1507
+
1508
+ Resumes recording profile data.*/
1509
+ static VALUE
1510
+ prof_resume(VALUE self)
1511
+ {
1512
+ if (threads_tbl == NULL)
1513
+ {
1514
+ prof_start(self);
1515
+ }
1516
+ else
1517
+ {
1518
+ prof_install_hook();
1519
+ }
1520
+
1521
+ if (rb_block_given_p())
1522
+ {
1523
+ rb_ensure(rb_yield, self, prof_pause, self);
1524
+ }
1525
+
1526
+ return self;
1527
+ }
1528
+
1529
+ /* call-seq:
1530
+ stop -> RubyProf::Result
1531
+
1532
+ Stops collecting profile data and returns a RubyProf::Result object. */
1533
+ static VALUE
1534
+ prof_stop(VALUE self)
1535
+ {
1536
+ VALUE result = Qnil;
1537
+
1538
+ /* close trace file if open */
1539
+ if (trace_file != NULL) {
1540
+ if (trace_file!=stderr && trace_file!=stdout)
1541
+ fclose(trace_file);
1542
+ trace_file = NULL;
1543
+ }
1544
+
1545
+ prof_remove_hook();
1546
+
1547
+ prof_pop_threads();
1548
+
1549
+ /* Create the result */
1550
+ result = prof_result_new();
1551
+
1552
+ /* Unset the last_thread_data (very important!)
1553
+ and the threads table */
1554
+ last_thread_data = NULL;
1555
+ threads_table_free(threads_tbl);
1556
+ threads_tbl = NULL;
1557
+
1558
+ /* compute minimality of call_infos */
1559
+ rb_funcall(result, rb_intern("compute_minimality") , 0);
1560
+
1561
+ return result;
1562
+ }
1563
+
1564
+ /* call-seq:
1565
+ profile {block} -> RubyProf::Result
1566
+
1567
+ Profiles the specified block and returns a RubyProf::Result object. */
1568
+ static VALUE
1569
+ prof_profile(VALUE self)
1570
+ {
1571
+ int result;
1572
+
1573
+ if (!rb_block_given_p())
1574
+ {
1575
+ rb_raise(rb_eArgError, "A block must be provided to the profile method.");
1576
+ }
1577
+
1578
+ prof_start(self);
1579
+ rb_protect(rb_yield, self, &result);
1580
+ return prof_stop(self);
1581
+ }
1582
+
1583
+ /* Get arround annoying limitations in RDOC */
1584
+
1585
+ /* Document-method: measure_process_time
1586
+ call-seq:
1587
+ measure_process_time -> float
1588
+
1589
+ Returns the process time.*/
1590
+
1591
+ /* Document-method: measure_wall_time
1592
+ call-seq:
1593
+ measure_wall_time -> float
1594
+
1595
+ Returns the wall time.*/
1596
+
1597
+ /* Document-method: measure_cpu_time
1598
+ call-seq:
1599
+ measure_cpu_time -> float
1600
+
1601
+ Returns the cpu time.*/
1602
+
1603
+ /* Document-method: get_cpu_frequency
1604
+ call-seq:
1605
+ cpu_frequency -> int
1606
+
1607
+ Returns the cpu's frequency. This value is needed when
1608
+ RubyProf::measure_mode is set to CPU_TIME. */
1609
+
1610
+ /* Document-method: cpu_frequency
1611
+ call-seq:
1612
+ cpu_frequency -> int
1613
+
1614
+ Returns the cpu's frequency. This value is needed when
1615
+ RubyProf::measure_mode is set to CPU_TIME. */
1616
+
1617
+ /* Document-method: cpu_frequency=
1618
+ call-seq:
1619
+ cpu_frequency = frequency
1620
+
1621
+ Sets the cpu's frequency. This value is needed when
1622
+ RubyProf::measure_mode is set to CPU_TIME. */
1623
+
1624
+ /* Document-method: measure_allocations
1625
+ call-seq:
1626
+ measure_allocations -> int
1627
+
1628
+ Returns the total number of object allocations since Ruby started.*/
1629
+
1630
+ /* Document-method: measure_memory
1631
+ call-seq:
1632
+ measure_memory -> int
1633
+
1634
+ Returns total allocated memory in bytes.*/
1635
+
1636
+ /* Document-method: measure_gc_runs
1637
+ call-seq:
1638
+ gc_runs -> Integer
1639
+
1640
+ Returns the total number of garbage collections.*/
1641
+
1642
+ /* Document-method: measure_gc_time
1643
+ call-seq:
1644
+ gc_time -> Integer
1645
+
1646
+ Returns the time spent doing garbage collections in microseconds.*/
1647
+
1648
+
1649
+ #if defined(_WIN32)
1650
+ __declspec(dllexport)
1651
+ #endif
1652
+ void
1653
+
1654
+ Init_ruby_prof()
1655
+ {
1656
+ mProf = rb_define_module("RubyProf");
1657
+ rb_define_const(mProf, "VERSION", rb_str_new2(RUBY_PROF_VERSION));
1658
+ rb_define_module_function(mProf, "start", prof_start, 0);
1659
+ rb_define_module_function(mProf, "stop", prof_stop, 0);
1660
+ rb_define_module_function(mProf, "resume", prof_resume, 0);
1661
+ rb_define_module_function(mProf, "pause", prof_pause, 0);
1662
+ rb_define_module_function(mProf, "running?", prof_running, 0);
1663
+ rb_define_module_function(mProf, "profile", prof_profile, 0);
1664
+
1665
+ rb_define_singleton_method(mProf, "exclude_threads=", prof_set_exclude_threads, 1);
1666
+ rb_define_singleton_method(mProf, "measure_mode", prof_get_measure_mode, 0);
1667
+ rb_define_singleton_method(mProf, "measure_mode=", prof_set_measure_mode, 1);
1668
+
1669
+ rb_define_const(mProf, "CLOCKS_PER_SEC", INT2NUM(CLOCKS_PER_SEC));
1670
+ rb_define_const(mProf, "PROCESS_TIME", INT2NUM(MEASURE_PROCESS_TIME));
1671
+ rb_define_singleton_method(mProf, "measure_process_time", prof_measure_process_time, 0); /* in measure_process_time.h */
1672
+ rb_define_const(mProf, "WALL_TIME", INT2NUM(MEASURE_WALL_TIME));
1673
+ rb_define_singleton_method(mProf, "measure_wall_time", prof_measure_wall_time, 0); /* in measure_wall_time.h */
1674
+
1675
+ #ifndef MEASURE_CPU_TIME
1676
+ rb_define_const(mProf, "CPU_TIME", Qnil);
1677
+ #else
1678
+ rb_define_const(mProf, "CPU_TIME", INT2NUM(MEASURE_CPU_TIME));
1679
+ rb_define_singleton_method(mProf, "measure_cpu_time", prof_measure_cpu_time, 0); /* in measure_cpu_time.h */
1680
+ rb_define_singleton_method(mProf, "cpu_frequency", prof_get_cpu_frequency, 0); /* in measure_cpu_time.h */
1681
+ rb_define_singleton_method(mProf, "cpu_frequency=", prof_set_cpu_frequency, 1); /* in measure_cpu_time.h */
1682
+ #endif
1683
+
1684
+ #ifndef MEASURE_ALLOCATIONS
1685
+ rb_define_const(mProf, "ALLOCATIONS", Qnil);
1686
+ #else
1687
+ rb_define_const(mProf, "ALLOCATIONS", INT2NUM(MEASURE_ALLOCATIONS));
1688
+ rb_define_singleton_method(mProf, "measure_allocations", prof_measure_allocations, 0); /* in measure_allocations.h */
1689
+ #endif
1690
+
1691
+ #ifndef MEASURE_MEMORY
1692
+ rb_define_const(mProf, "MEMORY", Qnil);
1693
+ #else
1694
+ rb_define_const(mProf, "MEMORY", INT2NUM(MEASURE_MEMORY));
1695
+ rb_define_singleton_method(mProf, "measure_memory", prof_measure_memory, 0); /* in measure_memory.h */
1696
+ #endif
1697
+
1698
+ #ifndef MEASURE_GC_RUNS
1699
+ rb_define_const(mProf, "GC_RUNS", Qnil);
1700
+ #else
1701
+ rb_define_const(mProf, "GC_RUNS", INT2NUM(MEASURE_GC_RUNS));
1702
+ rb_define_singleton_method(mProf, "measure_gc_runs", prof_measure_gc_runs, 0); /* in measure_gc_runs.h */
1703
+ #endif
1704
+
1705
+ #ifndef MEASURE_GC_TIME
1706
+ rb_define_const(mProf, "GC_TIME", Qnil);
1707
+ #else
1708
+ rb_define_const(mProf, "GC_TIME", INT2NUM(MEASURE_GC_TIME));
1709
+ rb_define_singleton_method(mProf, "measure_gc_time", prof_measure_gc_time, 0); /* in measure_gc_time.h */
1710
+ #endif
1711
+
1712
+ cResult = rb_define_class_under(mProf, "Result", rb_cObject);
1713
+ rb_undef_method(CLASS_OF(cMethodInfo), "new");
1714
+ rb_define_method(cResult, "threads", prof_result_threads, 0);
1715
+
1716
+ /* MethodInfo */
1717
+ cMethodInfo = rb_define_class_under(mProf, "MethodInfo", rb_cObject);
1718
+ rb_undef_method(CLASS_OF(cMethodInfo), "new");
1719
+
1720
+ rb_define_method(cMethodInfo, "klass", prof_method_klass, 0);
1721
+ rb_define_method(cMethodInfo, "klass_name", prof_klass_name, 0);
1722
+ rb_define_method(cMethodInfo, "method_name", prof_method_name, 0);
1723
+ rb_define_method(cMethodInfo, "full_name", prof_full_name, 0);
1724
+ rb_define_method(cMethodInfo, "method_id", prof_method_id, 0);
1725
+
1726
+ rb_define_method(cMethodInfo, "source_file", prof_method_source_file,0);
1727
+ rb_define_method(cMethodInfo, "line", prof_method_line, 0);
1728
+
1729
+ rb_define_method(cMethodInfo, "call_infos", prof_method_call_infos, 0);
1730
+
1731
+ /* CallInfo */
1732
+ cCallInfo = rb_define_class_under(mProf, "CallInfo", rb_cObject);
1733
+ rb_undef_method(CLASS_OF(cCallInfo), "new");
1734
+ rb_define_method(cCallInfo, "parent", prof_call_info_parent, 0);
1735
+ rb_define_method(cCallInfo, "parent=", prof_call_info_set_parent, 1);
1736
+ rb_define_method(cCallInfo, "children", prof_call_info_children, 0);
1737
+ rb_define_method(cCallInfo, "target", prof_call_info_target, 0);
1738
+ rb_define_method(cCallInfo, "called", prof_call_info_called, 0);
1739
+ rb_define_method(cCallInfo, "called=", prof_call_info_set_called, 1);
1740
+ rb_define_method(cCallInfo, "total_time", prof_call_info_total_time, 0);
1741
+ rb_define_method(cCallInfo, "add_total_time", prof_call_info_add_total_time, 1);
1742
+ rb_define_method(cCallInfo, "self_time", prof_call_info_self_time, 0);
1743
+ rb_define_method(cCallInfo, "add_self_time", prof_call_info_add_self_time, 1);
1744
+ rb_define_method(cCallInfo, "wait_time", prof_call_info_wait_time, 0);
1745
+ rb_define_method(cCallInfo, "add_wait_time", prof_call_info_add_wait_time, 1);
1746
+ rb_define_method(cCallInfo, "line", prof_call_info_line, 0);
1747
+ }