Skip to content

Commit 4647264

Browse files
committed
Extract sample capture to its own function
1 parent bfa4309 commit 4647264

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

ext/pf2c/sample.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdbool.h>
2+
#include <time.h>
3+
4+
#include <ruby.h>
5+
#include <ruby/debug.h>
6+
7+
#include "sample.h"
8+
9+
// Capture a sample from the current thread.
10+
bool
11+
pf2_sample_capture(struct pf2_sample *sample)
12+
{
13+
// Record the current time
14+
struct timespec now;
15+
clock_gettime(CLOCK_MONOTONIC, &now);
16+
sample->timestamp_ns = (uint64_t)now.tv_sec * 1000000000ULL + (uint64_t)now.tv_nsec;
17+
18+
// Obtain the current stack from Ruby
19+
sample->depth = rb_profile_frames(0, 200, sample->cmes, sample->linenos);
20+
21+
return true;
22+
}

ext/pf2c/sample.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ struct pf2_sample {
1111
uint64_t timestamp_ns;
1212
};
1313

14+
bool pf2_sample_capture(struct pf2_sample *sample);
15+
1416
#endif // PF2_SAMPLE_H

ext/pf2c/session.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <ruby.h>
1111
#include <ruby/debug.h>
1212

13+
#include "sample.h"
1314
#include "session.h"
1415
#include "serializer.h"
1516

@@ -120,19 +121,20 @@ sigprof_handler(int sig, siginfo_t *info, void *ucontext)
120121

121122
struct pf2_sample sample = { 0 };
122123

123-
// Record the current time
124-
struct timespec now;
125-
clock_gettime(CLOCK_MONOTONIC, &now);
126-
sample.timestamp_ns = (uint64_t)now.tv_sec * 1000000000ULL + (uint64_t)now.tv_nsec;
124+
if (pf2_sample_capture(&sample) == false) {
125+
#ifdef PF2_DEBUG
126+
printf("Dropping sample: Failed to capture sample\n");
127+
#endif
128+
return;
129+
}
127130

128-
// Obtain the current stack from Ruby
129-
sample.depth = rb_profile_frames(0, 200, sample.cmes, sample.linenos);
130-
// Copy the sample to the ringbuffer.
131+
// Copy the sample to the ringbuffer
131132
if (pf2_ringbuffer_push(session->rbuf, &sample) == false) {
132133
// Copy failed. The sample buffer is full.
133134
#ifdef PF2_DEBUG
134135
printf("Dropping sample: Sample buffer is full\n");
135136
#endif
137+
return;
136138
}
137139

138140
#ifdef PF2_DEBUG

0 commit comments

Comments
 (0)