summaryrefslogtreecommitdiff
path: root/ext/probeprofiler/probeprofiler.c
blob: 9ffb203673967465caadf69fb1594816dab706e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <ruby.h>
#include <yarvcore.h>

static void
hash_inc(VALUE data, VALUE key)
{
    VALUE num = INT2FIX(0);

    if (num = rb_hash_aref(data, key)) {
	num = INT2FIX(FIX2INT(num) + 1);
    }

    rb_hash_aset(data, key, num);
}

static void
pprof_hook(rb_event_flag_t flag, VALUE data,
	   VALUE dmyid, VALUE dmyklass)
{
    rb_thread_t *th = GET_THREAD();
    VALUE sig = rb_thread_current_status(th);
    hash_inc(data, sig);
}

static VALUE
pprof_data(VALUE mod)
{
    return rb_const_get_at(mod, rb_intern("#pprof_data"));
}

static VALUE
pprof_start(VALUE self)
{
    VALUE data = pprof_data(self);
    rb_add_event_hook(pprof_hook, RUBY_EVENT_SWITCH, data);
    return Qnil;
}

static VALUE
pprof_stop(VALUE self)
{
    rb_remove_event_hook(pprof_hook);
    return Qnil;
}

static int
hash_to_ary_i(VALUE key, VALUE value, VALUE ary)
{
    rb_ary_push(ary, rb_ary_new3(2, value, key));
    return ST_CONTINUE;
}

Init_probeprofiler(void)
{
    VALUE mPProf;

    mPProf = rb_define_module("ProbeProfiler");
    rb_const_set(mPProf, rb_intern("#pprof_data"), rb_hash_new());
    rb_define_module_function(mPProf, "start_profile", pprof_start, 0);
    rb_define_module_function(mPProf, "stop_profile", pprof_stop, 0);
    rb_define_module_function(mPProf, "profile_data", pprof_data, 0);
}