Module: StackProf
- Defined in:
- lib/stackprof.rb,
lib/stackprof/report.rb,
lib/stackprof/middleware.rb,
ext/stackprof/stackprof.c
Defined Under Namespace
Classes: Middleware, Report
Constant Summary collapse
- VERSION =
'0.2.18'
Class Method Summary collapse
- .results(*args) ⇒ Object
- .run(*args) ⇒ Object
- .running? ⇒ Boolean
- .sample ⇒ Object
- .start(*args) ⇒ Object
- .stop ⇒ Object
Class Method Details
.results(*args) ⇒ Object
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'ext/stackprof/stackprof.c', line 347
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.2));
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));
rb_hash_aset(results, sym_metadata, _stackprof.metadata);
_stackprof.metadata = Qnil;
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 (_stackprof.raw && _stackprof.raw_samples_len) {
size_t len, n, o;
VALUE raw_sample_timestamps, raw_timestamp_deltas;
VALUE raw_samples = rb_ary_new_capa(_stackprof.raw_samples_len);
for (n = 0; n < _stackprof.raw_samples_len; n++) {
len = (size_t)_stackprof.raw_samples[n];
rb_ary_push(raw_samples, SIZET2NUM(len));
for (o = 0, n++; o < len; n++, o++)
rb_ary_push(raw_samples, PTR2NUM(_stackprof.raw_samples[n]));
rb_ary_push(raw_samples, SIZET2NUM((size_t)_stackprof.raw_samples[n]));
}
free(_stackprof.raw_samples);
_stackprof.raw_samples = NULL;
_stackprof.raw_samples_len = 0;
_stackprof.raw_samples_capa = 0;
_stackprof.raw_sample_index = 0;
rb_hash_aset(results, sym_raw, raw_samples);
raw_sample_timestamps = rb_ary_new_capa(_stackprof.raw_sample_times_len);
raw_timestamp_deltas = rb_ary_new_capa(_stackprof.raw_sample_times_len);
for (n = 0; n < _stackprof.raw_sample_times_len; n++) {
rb_ary_push(raw_sample_timestamps, ULL2NUM(_stackprof.raw_sample_times[n].timestamp_usec));
rb_ary_push(raw_timestamp_deltas, LL2NUM(_stackprof.raw_sample_times[n].delta_usec));
}
free(_stackprof.raw_sample_times);
_stackprof.raw_sample_times = NULL;
_stackprof.raw_sample_times_len = 0;
_stackprof.raw_sample_times_capa = 0;
rb_hash_aset(results, sym_raw_sample_timestamps, raw_sample_timestamps);
rb_hash_aset(results, sym_raw_timestamp_deltas, raw_timestamp_deltas);
_stackprof.raw = 0;
}
if (argc == 1)
_stackprof.out = argv[0];
if (RTEST(_stackprof.out)) {
VALUE file;
if (rb_respond_to(_stackprof.out, rb_intern("to_io"))) {
file = rb_io_check_io(_stackprof.out);
} else {
file = rb_file_open_str(_stackprof.out, "w");
}
rb_marshal_dump(results, file);
rb_io_flush(file);
_stackprof.out = Qnil;
return file;
} else {
return results;
}
}
|
.run(*args) ⇒ Object
434 435 436 437 438 439 440 441 |
# File 'ext/stackprof/stackprof.c', line 434
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
443 444 445 446 447 |
# File 'ext/stackprof/stackprof.c', line 443
static VALUE
stackprof_running_p(VALUE self)
{
return _stackprof.running ? Qtrue : Qfalse;
}
|
.sample ⇒ Object
765 766 767 768 769 770 771 772 773 774 |
# File 'ext/stackprof/stackprof.c', line 765
static VALUE
stackprof_sample(VALUE self)
{
if (!_stackprof.running)
return Qfalse;
_stackprof.overall_signals++;
stackprof_sample_and_record();
return Qtrue;
}
|
.start(*args) ⇒ Object
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 178 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'ext/stackprof/stackprof.c', line 147
static VALUE
stackprof_start(int argc, VALUE *argv, VALUE self)
{
struct sigaction sa;
struct itimerval timer;
VALUE opts = Qnil, mode = Qnil, interval = Qnil, metadata = rb_hash_new(), out = Qfalse;
int ignore_gc = 0;
int raw = 0, aggregate = 1;
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_ignore_gc))) {
ignore_gc = 1;
}
VALUE metadata_val = rb_hash_aref(opts, sym_metadata);
if (RTEST(metadata_val)) {
if (!RB_TYPE_P(metadata_val, T_HASH))
rb_raise(rb_eArgError, "metadata should be a hash");
metadata = metadata_val;
}
if (RTEST(rb_hash_aref(opts, sym_raw)))
raw = 1;
if (rb_hash_lookup2(opts, sym_aggregate, Qundef) == Qfalse)
aggregate = 0;
}
if (!RTEST(mode)) mode = sym_wall;
if (!NIL_P(interval) && (NUM2INT(interval) < 1 || NUM2INT(interval) >= MICROSECONDS_IN_SECOND)) {
rb_raise(rb_eArgError, "interval is a number of microseconds between 1 and 1 million");
}
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(Qnil, 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.aggregate = aggregate;
_stackprof.mode = mode;
_stackprof.interval = interval;
_stackprof.ignore_gc = ignore_gc;
_stackprof.metadata = metadata;
_stackprof.out = out;
if (raw) {
capture_timestamp(&_stackprof.last_sample_at);
}
return Qtrue;
}
|
.stop ⇒ Object
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 260 261 262 |
# File 'ext/stackprof/stackprof.c', line 235
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;
}
|