Class: GVLTiming::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/gvl_timing.rb,
ext/gvl_timing/gvl_timing.c

Constant Summary collapse

NANOSECONDS_PER_SECOND_F =
1000000000.0

Instance Method Summary collapse

Instance Method Details

#cpu_duration_nsObject



27
28
29
# File 'lib/gvl_timing.rb', line 27

def cpu_duration_ns
  cputime_stop_ns - cputime_start_ns
end

#cputime_start_nsObject



133
134
135
# File 'ext/gvl_timing/gvl_timing.c', line 133

VALUE gvl_timer_cputime_start(VALUE obj) {
    return ULL2NUM(get_timer(obj)->cputime_start);
}

#cputime_stop_nsObject



137
138
139
# File 'ext/gvl_timing/gvl_timing.c', line 137

VALUE gvl_timer_cputime_stop(VALUE obj) {
    return ULL2NUM(get_timer(obj)->cputime_stop);
}

#duration_nsObject



23
24
25
# File 'lib/gvl_timing.rb', line 23

def duration_ns
  monotonic_stop_ns - monotonic_start_ns
end

#idle_duration_nsObject



149
150
151
# File 'ext/gvl_timing/gvl_timing.c', line 149

VALUE gvl_timer_idle_duration(VALUE obj) {
    return ULL2NUM(get_timer(obj)->timings[GVL_STATE_IDLE]);
}

#inspectObject



45
46
47
48
49
50
51
52
# File 'lib/gvl_timing.rb', line 45

def inspect
  "#<#{self.class} total=%.2fs running=%.2fs idle=%.2fs stalled=%.2fs>" % [
    duration,
    running_duration,
    idle_duration,
    stalled_duration,
  ]
end

#monotonic_start_nsObject



125
126
127
# File 'ext/gvl_timing/gvl_timing.c', line 125

VALUE gvl_timer_monotonic_start(VALUE obj) {
    return ULL2NUM(get_timer(obj)->monotonic_start);
}

#monotonic_stop_nsObject



129
130
131
# File 'ext/gvl_timing/gvl_timing.c', line 129

VALUE gvl_timer_monotonic_stop(VALUE obj) {
    return ULL2NUM(get_timer(obj)->monotonic_stop);
}

#running_duration_nsObject



141
142
143
# File 'ext/gvl_timing/gvl_timing.c', line 141

VALUE gvl_timer_running_duration(VALUE obj) {
    return ULL2NUM(get_timer(obj)->timings[GVL_STATE_RUNNING]);
}

#stalled_duration_nsObject



145
146
147
# File 'ext/gvl_timing/gvl_timing.c', line 145

VALUE gvl_timer_stalled_duration(VALUE obj) {
    return ULL2NUM(get_timer(obj)->timings[GVL_STATE_STALLED]);
}

#startObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'ext/gvl_timing/gvl_timing.c', line 96

VALUE gvl_timer_start(VALUE obj) {
    struct gvl_timer *timer = get_timer(obj);
    timer->monotonic_start = clock_gettime_ns(CLOCK_MONOTONIC);
    timer->cputime_start   = clock_gettime_ns(CLOCK_THREAD_CPUTIME_ID);

    timer->thread = rb_thread_current();
    timer->running = true;
    timer->event_hook = rb_internal_thread_add_event_hook(internal_thread_event_cb, RUBY_INTERNAL_THREAD_EVENT_MASK, timer);

    timer->prev_state = GVL_STATE_RUNNING;
    timer->prev_timestamp = clock_gettime_ns(CLOCK_MONOTONIC);

    return obj;
}

#stopObject



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'ext/gvl_timing/gvl_timing.c', line 111

VALUE gvl_timer_stop(VALUE obj) {
    struct gvl_timer *timer = get_timer(obj);

    // Record last interval
    record_timing(timer, GVL_STATE_RUNNING);

    rb_internal_thread_remove_event_hook(timer->event_hook);

    timer->monotonic_stop = clock_gettime_ns(CLOCK_MONOTONIC);
    timer->cputime_stop   = clock_gettime_ns(CLOCK_THREAD_CPUTIME_ID);
    timer->running = false;
    return obj;
}