Skip to content

Commit

Permalink
[perforator] Do use use stale timestamps for reused thread ids
Browse files Browse the repository at this point in the history
commit_hash:d6d901e269fb94704f3e4988810b502812ab49ce
  • Loading branch information
BigRedEye committed Feb 18, 2025
1 parent 3a19ade commit 58252a2
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions perforator/agent/collector/progs/unwinder/unwinder.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,20 @@ struct profiler_config {
_Static_assert(SIGRTMIN < signal_mask_bits, "Unsupported signal set size");
};

struct thread_key {
u32 pid;
u32 tid;
u64 starttime;
};

// Heap. BPF stack is limited (512 bytes).
BPF_MAP(profiler_state, BPF_MAP_TYPE_PERCPU_ARRAY, u32, struct profiler_state, 1);
BPF_MAP(profiler_config, BPF_MAP_TYPE_ARRAY, u32, struct profiler_config, 1);
BPF_MAP(process_info, BPF_MAP_TYPE_HASH, u32, struct process_info, PROCESS_MAP_SIZE);
BPF_MAP(default_process_info, BPF_MAP_TYPE_ARRAY, u32, struct process_info, 1);
BPF_MAP(process_discovery, BPF_MAP_TYPE_LRU_HASH, u32, u8, PROCESS_MAP_SIZE);
BPF_MAP(perf_event_values, BPF_MAP_TYPE_HASH, u64, struct bpf_perf_event_value, 4096);
BPF_MAP(thread_last_sample_time, BPF_MAP_TYPE_LRU_HASH, u32, u64, 1024 * 1024);
BPF_MAP(thread_last_sample_time, BPF_MAP_TYPE_LRU_HASH, struct thread_key, u64, 256 * 1024);
BPF_MAP(percpu_user_regs, BPF_MAP_TYPE_PERCPU_ARRAY, u32, struct user_regs, 1);

static ALWAYS_INLINE void* map_lookup_zero(void* map) {
Expand Down Expand Up @@ -301,7 +307,13 @@ static ALWAYS_INLINE void record_thread_walltime(struct profiler_config* config,
return;
}

u64* last = bpf_map_lookup_elem(&thread_last_sample_time, &state->sample.tid);
struct thread_key key = {
.pid = state->sample.pid,
.tid = state->sample.tid,
.starttime = state->sample.starttime,
};

u64* last = bpf_map_lookup_elem(&thread_last_sample_time, &key);
if (last) {
i64 delta = (i64)state->prog_starttime - (i64)*last;
BPF_TRACE("calculated thread %d timedelta: %lld ns\n", state->sample.tid, delta);
Expand All @@ -317,7 +329,7 @@ static ALWAYS_INLINE void record_thread_walltime(struct profiler_config* config,
state->sample.timedelta *= config->sched_sample_modulo;
}

int err = bpf_map_update_elem(&thread_last_sample_time, &state->sample.tid, &state->prog_starttime, 0);
int err = bpf_map_update_elem(&thread_last_sample_time, &key, &state->prog_starttime, 0);
if (err != 0) {
BPF_TRACE("failed to set thread %d sample time: %d\n", state->sample.tid, err);
}
Expand Down

0 comments on commit 58252a2

Please sign in to comment.