Class: Tacho::Profile

Inherits:
Object
  • Object
show all
Defined in:
ext/tacho/tacho.c

Instance Method Summary collapse

Constructor Details

#initializeObject



129
130
131
132
133
# File 'ext/tacho/tacho.c', line 129

static VALUE tch_profile_initialize(VALUE self) {
    tch_profile* profile = tch_profile_get_profile(self);
    profile->recording = 0;
    return self;
}

Instance Method Details

#start(filename, name, clock_type) ⇒ Object



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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'ext/tacho/tacho.c', line 135

static VALUE tch_profile_start(VALUE self, VALUE filename, VALUE name, VALUE clock_type) {
    tch_profile* profile = tch_profile_get_profile(self);

    struct timespec time;
    uint32_t name_length;
    ID clock_type_id;

    profile->output = fopen(StringValueCStr(filename), "wb");
    profile->buffer_count = 0;

    if (!profile->output) {
        rb_raise(rb_eRuntimeError, "Could not open file %s", StringValueCStr(filename));
        RB_GC_GUARD(filename);
        return Qnil;
    }
    RB_GC_GUARD(filename);

    profile->recording = 1;

    if (NIL_P(clock_type)) {
        profile->clock_type = CLOCK_MONOTONIC;
    } else if (TYPE(clock_type) == T_SYMBOL) {
        clock_type_id = SYM2ID(clock_type);
        if (id_wall == clock_type_id) {
            profile->clock_type = CLOCK_MONOTONIC;
        } else if (id_process == clock_type_id) {
            profile->clock_type = CLOCK_PROCESS_CPUTIME_ID;
        } else if (id_thread == clock_type_id) {
            profile->clock_type = CLOCK_THREAD_CPUTIME_ID;
        } else {
            rb_raise(rb_eTypeError, "not valid value. Must be one of :wall, :process or :thread got :%s", rb_id2name(clock_type_id));
        }
    } else {
        rb_raise(rb_eTypeError, "not valid value. Must be a symbol of one of :wall, :process or :thread");
    }

    clock_gettime(profile->clock_type, &time);
    profile->start_time = (long)time.tv_sec * 1000000000 + (long)time.tv_nsec;
    write_header(profile, 'S', profile->start_time);

    name_length = (uint32_t)RSTRING_LEN(name);
    fwrite(&name_length, sizeof(uint32_t), 1, profile->output);
    fwrite(RSTRING_PTR(name), name_length, 1, profile->output);
    RB_GC_GUARD(name);

    profile->tracepoint = rb_tracepoint_new(Qnil, RUBY_EVENT_CALL | RUBY_EVENT_RETURN, tacho_event, profile);
    rb_tracepoint_enable(profile->tracepoint);

    return Qnil;
}

#stopObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'ext/tacho/tacho.c', line 186

static VALUE tch_profile_stop(VALUE self) {
    tch_profile* profile = tch_profile_get_profile(self);

    struct timespec time;
    uint64_t itime;

    if (profile->recording) {
        clock_gettime(profile->clock_type, &time);
        itime = (long)time.tv_sec * 1000000000 + (long)time.tv_nsec;
        write_header(profile, 'F', itime);

        fflush(profile->output);
        fclose(profile->output);

        rb_tracepoint_disable(profile->tracepoint);

        profile->recording = 0;
    } else {
        rb_raise(rb_eRuntimeError, "Not currently recording");
    }

    return Qnil;
}