return 0;
struct val_t val = {};
- __u32 pid = bpf_get_current_pid_tgid() >> 32;
+ __u64 pid_tgid = bpf_get_current_pid_tgid();
+ __u32 pid = pid_tgid >> 32;
+ __u32 tid = (__u32)pid_tgid;
if (targ_tgid && targ_tgid != pid)
return 0;
(void *)PT_REGS_PARM1(ctx));
val.pid = pid;
val.time = bpf_ktime_get_ns();
- bpf_map_update_elem(&start, &pid, &val, BPF_ANY);
+ bpf_map_update_elem(&start, &tid, &val, BPF_ANY);
}
return 0;
static __always_inline
int probe_return(struct pt_regs *ctx) {
struct val_t *valp;
- __u32 pid = bpf_get_current_pid_tgid() >> 32;
+ __u64 pid_tgid = bpf_get_current_pid_tgid();
+ __u32 pid = pid_tgid >> 32;
+ __u32 tid = (__u32)pid_tgid;
__u64 now = bpf_ktime_get_ns();
- valp = bpf_map_lookup_elem(&start, &pid);
+ valp = bpf_map_lookup_elem(&start, &tid);
if (!valp)
return 0;
valp->time = now - valp->time;
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, valp,
sizeof(*valp));
- bpf_map_delete_elem(&start, &pid);
+ bpf_map_delete_elem(&start, &tid);
return 0;
}
#define PERF_POLL_TIMEOUT_MS 100
#define warn(...) fprintf(stderr, __VA_ARGS__)
-volatile sig_atomic_t canceled = 0;
-pid_t traced_pid = 0;
+static volatile sig_atomic_t exiting = 0;
+static pid_t traced_pid = 0;
const char *argp_program_version = "gethostlatency 0.1";
const char *argp_program_bug_address =
" gethostlatency -p 1216 # only trace PID 1216\n";
static const struct argp_option opts[] = {
- {"pid", 'p', "PID", 0, "Process ID to trace"},
- {NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help"},
+ { "pid", 'p', "PID", 0, "Process ID to trace" },
+ { NULL, 'h', NULL, OPTION_HIDDEN, "Show the full help" },
{},
};
static void sig_int(int signo)
{
- canceled = 1;
+ exiting = 1;
}
static const char *strftime_now(char *s, size_t max, const char *format)
return s;
}
-void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
+static void handle_event(void *ctx, int cpu, void *data, __u32 data_sz)
{
const struct val_t *e = data;
char s[16] = {};
now, e->pid, e->comm, (double)e->time/1000000, e->host);
}
-void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
+static void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt)
{
warn("lost %llu events on CPU #%d\n", lost_cnt, cpu);
}
while (1) {
if ((err = perf_buffer__poll(pb, PERF_POLL_TIMEOUT_MS)) < 0)
break;
- if (canceled)
+ if (exiting)
goto cleanup;
}
warn("error polling perf buffer: %d\n", err);