Module: Corkscrews::Sampler

Defined in:
ext/corkscrews/sampler.c

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
# File 'ext/corkscrews/sampler.c', line 31

static VALUE
sampler_available_p(VALUE self)
{
    (void)self;
    return Qtrue;
}

.record_line(line_value) ⇒ Object



75
76
77
78
79
80
81
# File 'ext/corkscrews/sampler.c', line 75

static VALUE
sampler_record_line(VALUE self, VALUE line_value)
{
    (void)self;
    record_line_hit(0, NUM2UINT(line_value));
    return Qnil;
}

.record_site(site_key_value, line_value) ⇒ Object



83
84
85
86
87
88
89
# File 'ext/corkscrews/sampler.c', line 83

static VALUE
sampler_record_site(VALUE self, VALUE site_key_value, VALUE line_value)
{
    (void)self;
    record_line_hit((uintptr_t)NUM2ULL(site_key_value), NUM2UINT(line_value));
    return Qnil;
}

.sample_currentObject



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
64
65
66
67
68
69
70
71
72
73
# File 'ext/corkscrews/sampler.c', line 38

static VALUE
sampler_sample_current(VALUE self)
{
    (void)self;
    VALUE frames[CS_MAX_DEPTH];
    int lines[CS_MAX_DEPTH];
    int n = rb_profile_frames(0, CS_MAX_DEPTH, frames, lines);
    VALUE ary = rb_ary_new_capa(n);

    atomic_fetch_add(&cs_global.samples, 1);
    if (n > 0) {
        uint8_t hit = 0;
        uint64_t delay = 0;
        uint32_t speedup = atomic_load(&cs_global.speedup_pct);
        uint32_t kind = atomic_load(&cs_global.target_kind);
        uint32_t target_line = atomic_load(&cs_global.target_line);
        uintptr_t target_key = atomic_load(&cs_global.target_iseq);
        if (kind == 1 && speedup > 0 && target_key == 0 && (uint32_t)lines[0] == target_line) {
            uint64_t period = atomic_load(&cs_global.sample_period_ns);
            delay = period * speedup / 100;
            hit = 1;
            atomic_fetch_add(&cs_global.target_hits, 1);
            cs_delay_issue_for_current(delay);
            cs_delay_request_settle();
        }
        cs_record_line_sample(0, (uint32_t)lines[0], hit, delay);
    }

    for (int i = 0; i < n; i++) {
        VALUE frame = rb_hash_new();
        rb_hash_aset(frame, ID2SYM(rb_intern("frame")), frames[i]);
        rb_hash_aset(frame, ID2SYM(rb_intern("line")), INT2NUM(lines[i]));
        rb_ary_push(ary, frame);
    }
    return ary;
}