Module: StackTracy

Extended by:
StackTracy
Included in:
StackTracy
Defined in:
lib/stack_tracy/version.rb,
lib/stack_tracy.rb,
lib/stack_tracy/cli.rb,
lib/stack_tracy/sinatra.rb,
lib/stack_tracy/event_info.rb

Overview

encoding: UTF-8

Defined Under Namespace

Classes: CLI, Error, EventInfo, Sinatra

Constant Summary collapse

PRESETS =
{
  :core => "Array #{"BasicObject " unless RUBY_VERSION == "1.8.7"}Enumerable Fixnum Float Hash IO Integer Kernel Module #{"Mutex " unless RUBY_VERSION == "1.8.7"}Numeric Object Rational String Symbol Thread Time",
  :active_record => "ActiveRecord::Base",
  :data_mapper => "DataMapper::Resource"
}
MAJOR =
0
MINOR =
1
TINY =
9
VERSION =
[MAJOR, MINOR, TINY].join(".")

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._start(only_names, exclude_names) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'ext/stack_tracy/stack_tracy.c', line 139

VALUE stack_tracy_start(VALUE self, VALUE only_names, VALUE exclude_names) {
  char *token;

  token = strtok((char *) RSTRING_PTR(only_names), " ");
  only_size = 0;

  while (token != NULL) {
    only_size++;
    only = (RubyClass *) realloc (only, only_size * sizeof(RubyClass));

    only[only_size - 1].name = (char *) token;
    only[only_size - 1].klass = (VALUE *) rb_path2class((char *) token);

    token = strtok(NULL, " ");
  }

  token = strtok((char *) RSTRING_PTR(exclude_names), " ");
  exclude_size = 0;

  while (token != NULL) {
    exclude_size++;
    exclude = (RubyClass *) realloc (exclude, exclude_size * sizeof(RubyClass));

    exclude[exclude_size - 1].name = (char *) token;
    exclude[exclude_size - 1].klass = (VALUE *) rb_path2class((char *) token);

    token = strtok(NULL, " ");
  }

  #if defined(RB_EVENT_HOOKS_HAVE_CALLBACK_DATA) || defined(RUBY_EVENT_VM)
    rb_add_event_hook(stack_tracy_trap, RUBY_EVENT_CALL | RUBY_EVENT_C_CALL | RUBY_EVENT_RETURN | RUBY_EVENT_C_RETURN, 0);
  #else
    rb_add_event_hook(stack_tracy_trap, RUBY_EVENT_CALL | RUBY_EVENT_C_CALL | RUBY_EVENT_RETURN | RUBY_EVENT_C_RETURN);
  #endif

  stack_size = 0, trace = false;

  return Qnil;
}

._stopObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'ext/stack_tracy/stack_tracy.c', line 179

VALUE stack_tracy_stop(VALUE self) {
  VALUE events, event;
  const char *method;
  int i;

  rb_remove_event_hook(stack_tracy_trap);

  events = rb_ary_new();

  for (i = 0; i < stack_size; i++) {
    method = rb_id2name((ID) stack[i].method);
    if (method != NULL) {
      event = rb_funcall(cEventInfo, rb_intern("new"), 0);
      rb_iv_set(event, "@event", rb_str_new2(event_name(stack[i].event)));
      rb_iv_set(event, "@file", rb_str_new2(stack[i].file));
      rb_iv_set(event, "@line", rb_int_new(stack[i].line));
      rb_iv_set(event, "@singleton", stack[i].singleton);
      rb_iv_set(event, "@object", (VALUE) stack[i].object);
      rb_iv_set(event, "@method", rb_str_new2(method));
      rb_iv_set(event, "@nsec", rb_float_new(stack[i].nsec));
      rb_ary_push(events, event);
    }
  }

  rb_iv_set(mStackTracy, "@stack_trace", events);

  return Qnil;
}

Instance Method Details

#config {|@options| ... } ⇒ Object

Yields:

  • (@options)


25
26
27
# File 'lib/stack_tracy.rb', line 25

def config
  yield @options
end

#dump(path = nil, dump_source_location = nil, *only) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/stack_tracy.rb', line 70

def dump(path = nil, dump_source_location = nil, *only)
  unless path && path.match(/\.csv$/)
    path = File.join [path || @options.dump_dir, "stack_events-#{SecureRandom.hex(3)}.csv"].compact
  end
  File.expand_path(path).tap do |path|
    bool = dump_source_location.nil? ? @options[:dump_source_location] : dump_source_location
    keys = [:event, (:file if bool), (:line if bool), :singleton, :object, :method, :nsec, :time, :call, :depth, :duration]
    (RUBY_VERSION == "1.8.7" ? FasterCSV : CSV).open(path, "w", :col_sep => ";") do |file|
      file << keys
      select(only).each do |event|
        file << event.values_at(*keys)
      end
    end
    yield path if block_given?
    stack_trace.clear
  end
end

#open(path = nil, use_current_stack_trace = false, options = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/stack_tracy.rb', line 88

def open(path = nil, use_current_stack_trace = false, options = {})
  if use_current_stack_trace
    file = File.expand_path(path) if path
  else
    unless path && path.match(/\.csv$/)
      path   = Dir[File.join(path || @options.dump_dir, "stack_events-*.csv")].sort_by{|f| File.mtime(f)}.last
      path ||= Dir[File.join(".", "stack_events-*.csv")].sort_by{|f| File.mtime(f)}.last
      path ||= Dir[File.join(Dir::tmpdir, "stack_events-*.csv")].sort_by{|f| File.mtime(f)}.last
    end
    if path
      file = File.expand_path(path)
    else
      raise Error, "Could not locate StackTracy file"
    end
  end

  index = ui("index.html")

  if use_current_stack_trace || (file && File.exists?(file))
    threshold = options["threshold"] || options[:threshold] || @options[:threshold]
    limit = options["limit"] || options[:limit] || @options[:limit]
    messages_only = [options["messages_only"], options[:messages_only], @options[:messages_only]].detect{|x| !x.nil?}
    slows_only = [options["slows_only"], options[:slows_only], @options[:slows_only]].detect{|x| !x.nil?}
    events = use_current_stack_trace ? select : StackTracy::EventInfo.to_hashes(File.read(file))
    erb = ERB.new File.new(ui("index.html.erb")).read
    File.open(index, "w"){|f| f.write erb.result(binding)}
  elsif path && path.match(/\.csv$/)
    raise Error, "Could not locate StackTracy file at #{file}"
  end

  if File.exists?(index)
    if RbConfig::CONFIG["host_os"].match(/(mswin|mingw)/) # I know, don't say it!
      `start file://#{index}`
    else
      Launchy.open("file://#{index}")
    end
    nil
  else
    raise Error, "Could not locate StackTracy file"
  end
end


62
63
64
65
66
67
68
# File 'lib/stack_tracy.rb', line 62

def print(*only)
  puts select(only).collect{ |event|
    line = "   " * event[:depth]
    line << event[:call]
    line << " <#{"%.6f" % event[:duration]}>" if event[:duration]
  }
end

#select(*only) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/stack_tracy.rb', line 44

def select(*only)
  [].tap do |lines|
    first, call_stack, only = nil, [], only.flatten.collect{|x| x.split(" ")}.flatten
    stack_trace.each do |event_info|
      next unless process?(event_info, only)
      first ||= event_info
      if event_info.call?
        lines << event_info.to_hash(first).merge!(:depth => call_stack.size)
        call_stack << [lines.size - 1, event_info]
      elsif event_info.return? && call_stack.last && event_info.matches?(call_stack.last.last)
        call_stack.pop.tap do |(line, match)|
          lines[line][:duration] = event_info - match
        end
      end
    end
  end
end

#stack_traceObject



40
41
42
# File 'lib/stack_tracy.rb', line 40

def stack_trace
  @stack_trace || []
end

#start(options = {}) ⇒ Object



29
30
31
32
33
# File 'lib/stack_tracy.rb', line 29

def start(options = {})
  opts = merge_options(options)
  _start mod_names(opts[:only]), mod_names(opts[:exclude])
  nil
end

#stopObject



35
36
37
38
# File 'lib/stack_tracy.rb', line 35

def stop
  _stop
  nil
end