Module: StackProf

Defined in:
lib/stackprof/report.rb,
lib/stackprof/middleware.rb,
ext/stackprof.c

Defined Under Namespace

Classes: Middleware, Report

Class Method Summary collapse

Class Method Details

.results(*args) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'ext/stackprof.c', line 214

static VALUE
stackprof_results(int argc, VALUE *argv, VALUE self)
{
    VALUE results, frames;

    if (!_stackprof.frames || _stackprof.running)
  return Qnil;

    results = rb_hash_new();
    rb_hash_aset(results, sym_version, DBL2NUM(1.1));
    rb_hash_aset(results, sym_mode, _stackprof.mode);
    rb_hash_aset(results, sym_interval, _stackprof.interval);
    rb_hash_aset(results, sym_samples, SIZET2NUM(_stackprof.overall_samples));
    rb_hash_aset(results, sym_gc_samples, SIZET2NUM(_stackprof.during_gc));
    rb_hash_aset(results, sym_missed_samples, SIZET2NUM(_stackprof.overall_signals - _stackprof.overall_samples));

    frames = rb_hash_new();
    rb_hash_aset(results, sym_frames, frames);
    st_foreach(_stackprof.frames, frame_i, (st_data_t)frames);

    st_free_table(_stackprof.frames);
    _stackprof.frames = NULL;

    if (RTEST(_stackprof.raw)) {
  rb_hash_aset(results, sym_raw, _stackprof.raw);
  _stackprof.raw = Qfalse;
    }

    if (argc == 1)
  _stackprof.out = argv[0];

    if (RTEST(_stackprof.out)) {
  VALUE file;
  if (RB_TYPE_P(_stackprof.out, T_STRING)) {
      file = rb_file_open_str(_stackprof.out, "w");
  } else {
      file = rb_io_check_io(_stackprof.out);
  }
  rb_marshal_dump(results, file);
  rb_io_flush(file);
  _stackprof.out = Qnil;
  return file;
    } else {
  return results;
    }
}

.run(*args) ⇒ Object



261
262
263
264
265
266
267
268
# File 'ext/stackprof.c', line 261

static VALUE
stackprof_run(int argc, VALUE *argv, VALUE self)
{
    rb_need_block();
    stackprof_start(argc, argv, self);
    rb_ensure(rb_yield, Qundef, stackprof_stop, self);
    return stackprof_results(0, 0, self);
}

.running?Boolean

Returns:

  • (Boolean)


270
271
272
273
274
# File 'ext/stackprof.c', line 270

static VALUE
stackprof_running_p(VALUE self)
{
    return _stackprof.running ? Qtrue : Qfalse;
}

.sampleObject



409
410
411
412
413
414
415
416
417
418
# File 'ext/stackprof.c', line 409

static VALUE
stackprof_sample(VALUE self)
{
    if (!_stackprof.running)
  return Qfalse;

    _stackprof.overall_signals++;
    stackprof_job_handler(0);
    return Qtrue;
}

.start(*args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
# File 'ext/stackprof.c', line 54

static VALUE
stackprof_start(int argc, VALUE *argv, VALUE self)
{
    struct sigaction sa;
    struct itimerval timer;
    VALUE opts = Qnil, mode = Qnil, interval = Qnil, raw = Qfalse, out = Qfalse;

    if (_stackprof.running)
  return Qfalse;

    rb_scan_args(argc, argv, "0:", &opts);

    if (RTEST(opts)) {
  mode = rb_hash_aref(opts, sym_mode);
  interval = rb_hash_aref(opts, sym_interval);
  out = rb_hash_aref(opts, sym_out);

  if (RTEST(rb_hash_aref(opts, sym_raw)))
      raw = rb_ary_new();
    }
    if (!RTEST(mode)) mode = sym_wall;

    if (!_stackprof.frames) {
  _stackprof.frames = st_init_numtable();
  _stackprof.overall_signals = 0;
  _stackprof.overall_samples = 0;
  _stackprof.during_gc = 0;
    }

    if (mode == sym_object) {
  if (!RTEST(interval)) interval = INT2FIX(1);

  objtracer = rb_tracepoint_new(0, RUBY_INTERNAL_EVENT_NEWOBJ, stackprof_newobj_handler, 0);
  rb_tracepoint_enable(objtracer);
    } else if (mode == sym_wall || mode == sym_cpu) {
  if (!RTEST(interval)) interval = INT2FIX(1000);

  sa.sa_sigaction = stackprof_signal_handler;
  sa.sa_flags = SA_RESTART | SA_SIGINFO;
  sigemptyset(&sa.sa_mask);
  sigaction(mode == sym_wall ? SIGALRM : SIGPROF, &sa, NULL);

  timer.it_interval.tv_sec = 0;
  timer.it_interval.tv_usec = NUM2LONG(interval);
  timer.it_value = timer.it_interval;
  setitimer(mode == sym_wall ? ITIMER_REAL : ITIMER_PROF, &timer, 0);
    } else if (mode == sym_custom) {
  /* sampled manually */
  interval = Qnil;
    } else {
  rb_raise(rb_eArgError, "unknown profiler mode");
    }

    _stackprof.running = 1;
    _stackprof.raw = raw;
    _stackprof.mode = mode;
    _stackprof.interval = interval;
    _stackprof.out = out;

    return Qtrue;
}

.stopObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'ext/stackprof.c', line 116

static VALUE
stackprof_stop(VALUE self)
{
    struct sigaction sa;
    struct itimerval timer;

    if (!_stackprof.running)
  return Qfalse;
    _stackprof.running = 0;

    if (_stackprof.mode == sym_object) {
  rb_tracepoint_disable(objtracer);
    } else if (_stackprof.mode == sym_wall || _stackprof.mode == sym_cpu) {
  memset(&timer, 0, sizeof(timer));
  setitimer(_stackprof.mode == sym_wall ? ITIMER_REAL : ITIMER_PROF, &timer, 0);

  sa.sa_handler = SIG_IGN;
  sa.sa_flags = SA_RESTART;
  sigemptyset(&sa.sa_mask);
  sigaction(_stackprof.mode == sym_wall ? SIGALRM : SIGPROF, &sa, NULL);
    } else if (_stackprof.mode == sym_custom) {
  /* sampled manually */
    } else {
  rb_raise(rb_eArgError, "unknown profiler mode");
    }

    return Qtrue;
}