Module: StackProfx

Defined in:
ext/stackprofx.c

Class Method Summary collapse

Class Method Details

.results(*args) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'ext/stackprofx.c', line 267

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

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

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

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

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

    if (_stackprofx.raw && _stackprofx.raw_samples_len) {
	size_t len, n, o;
	VALUE raw_samples = rb_ary_new_capa(_stackprofx.raw_samples_len);

	for (n = 0; n < _stackprofx.raw_samples_len; n++) {
	    len = (size_t)_stackprofx.raw_samples[n];
	    rb_ary_push(raw_samples, SIZET2NUM(len));

	    for (o = 0, n++; o < len; n++, o++)
		rb_ary_push(raw_samples, rb_obj_id(_stackprofx.raw_samples[n]));
	    rb_ary_push(raw_samples, SIZET2NUM((size_t)_stackprofx.raw_samples[n]));
	}

	free(_stackprofx.raw_samples);
	_stackprofx.raw_samples = NULL;
	_stackprofx.raw_samples_len = 0;
	_stackprofx.raw_samples_capa = 0;
	_stackprofx.raw_sample_index = 0;
	_stackprofx.raw = 0;

	rb_hash_aset(results, sym_raw, raw_samples);
    }

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

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

.run(*args) ⇒ Object



332
333
334
335
336
337
338
339
# File 'ext/stackprofx.c', line 332

static VALUE
stackprofx_run(int argc, VALUE *argv, VALUE self)
{
    rb_need_block();
    stackprofx_start(argc, argv, self);
    rb_ensure(rb_yield, Qundef, stackprofx_stop, self);
    return stackprofx_results(0, 0, self);
}

.running?Boolean

Returns:

  • (Boolean)


341
342
343
344
345
# File 'ext/stackprofx.c', line 341

static VALUE
stackprofx_running_p(VALUE self)
{
    return _stackprofx.running ? Qtrue : Qfalse;
}

.sampleObject



526
527
528
529
530
531
532
533
534
535
# File 'ext/stackprofx.c', line 526

static VALUE
stackprofx_sample(VALUE self)
{
    if (!_stackprofx.running)
	return Qfalse;

    _stackprofx.overall_signals++;
    stackprofx_job_handler(0);
    return Qtrue;
}

.start(*args) ⇒ Object



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
115
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'ext/stackprofx.c', line 81

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

    if (_stackprofx.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);
	threads = rb_hash_aref(opts, sym_threads);

	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 (RTEST(threads))
  {
    _stackprofx.threads = st_init_numtable();
    for (int i = 0; i < RARRAY_LEN(threads); i++)
    {
        VALUE thr = rb_ary_entry(threads, i);
        st_add_direct(_stackprofx.threads, thr, 0);
        rb_gc_mark(thr);
    }
  }
  else
  {
      _stackprofx.threads = 0;
  }

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

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

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

	sa.sa_sigaction = stackprofx_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");
    }

    _stackprofx.running = 1;
    _stackprofx.raw = raw;
    _stackprofx.aggregate = aggregate;
    _stackprofx.mode = mode;
    _stackprofx.interval = interval;
    _stackprofx.out = out;

    return Qtrue;
}

.stopObject



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
# File 'ext/stackprofx.c', line 163

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

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

    if (_stackprofx.threads)
    {
        st_free_table(_stackprofx.threads);
        _stackprofx.threads = 0;
    }

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

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

    return Qtrue;
}