|
|
@@ -37,6 +37,20 @@ struct {
|
|
|
__uint(max_entries, 32768);
|
|
|
} thread_trace_info_heap SEC(".maps");
|
|
|
|
|
|
+struct {
|
|
|
+ __uint(type, BPF_MAP_TYPE_LRU_HASH);
|
|
|
+ __uint(key_size, sizeof(struct goid_trace_key_t));
|
|
|
+ __uint(value_size, sizeof(struct apm_trace_info_t));
|
|
|
+ __uint(max_entries, 32768);
|
|
|
+} goid_trace_info_heap SEC(".maps");
|
|
|
+
|
|
|
+struct {
|
|
|
+ __uint(type, BPF_MAP_TYPE_HASH);
|
|
|
+ __uint(key_size, sizeof(struct goid_trace_key_t));
|
|
|
+ __uint(value_size, sizeof(__u64));
|
|
|
+ __uint(max_entries, 32768);
|
|
|
+} goid_trace_info_heap2 SEC(".maps");
|
|
|
+
|
|
|
struct {
|
|
|
__uint(type, BPF_MAP_TYPE_HASH);
|
|
|
__uint(key_size, sizeof(__u64));
|
|
|
@@ -71,6 +85,13 @@ struct apm_trace_key_t get_apm_trace_key(__u64 timeout, bool is_socket_io) {
|
|
|
return key;
|
|
|
}
|
|
|
|
|
|
+static __inline __attribute__((__always_inline__))
|
|
|
+struct thread_trace_key_t get_thread_trace_key(__u64 id) {
|
|
|
+ struct thread_trace_key_t trace_key = {};
|
|
|
+ trace_key.pid_tgid = id;
|
|
|
+ return trace_key;
|
|
|
+}
|
|
|
+
|
|
|
static __inline __attribute__((__always_inline__))
|
|
|
__u64 get_apm_trace_id(__u32 pid, __u32 tid) {
|
|
|
struct apm_trace_key_t trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
|
|
|
@@ -83,6 +104,257 @@ __u64 get_apm_trace_id(__u32 pid, __u32 tid) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
+//
|
|
|
+//static __inline __u64 get_trace_id_from_runq_map(__u32 tgid, __u64 goid) {
|
|
|
+//
|
|
|
+// __u64 ancestor = goid;
|
|
|
+// int idx = 0;
|
|
|
+//#pragma unroll
|
|
|
+// for (idx = 0; idx < 6; ++idx) {
|
|
|
+// bpf_printk("[start][%d] goid------>%llu -----", idx, ancestor);
|
|
|
+// struct go_key key = {.tgid = tgid, .goid = ancestor};
|
|
|
+// __u64 *newancestor =
|
|
|
+// bpf_map_lookup_elem(&go_ancerstor_by_runq_map, &key);
|
|
|
+// if (!newancestor) {
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// ancestor = *newancestor;
|
|
|
+// struct apm_trace_key_t trace_key = {.tgid = tgid, .goid = ancestor};
|
|
|
+// struct apm_trace_info_t *trace_info = bpf_map_lookup_elem(&trace_info_heap, &trace_key);
|
|
|
+// if (trace_info) {
|
|
|
+// bpf_printk("---------get_trace_id_from_runq_map info_trace_id:%llu", trace_info->trace_id);
|
|
|
+// return trace_info->trace_id;
|
|
|
+// }
|
|
|
+// bpf_printk("[end][%d] goid------>%llu -----\n", idx, ancestor);
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+// return 0;
|
|
|
+//}
|
|
|
+
|
|
|
+// find Parent from go_ancerstor_by_runq_map and find in go_ancerstor_map == 0
|
|
|
+// find by tgid_pid == 0
|
|
|
+// find by tgid_goid == 0
|
|
|
+// any idea?
|
|
|
+static __inline __attribute__((__always_inline__))
|
|
|
+__u64 get_apm_trace_id_v2(__u64 tgid_pid, __u32 tgid, __u32 pid) {
|
|
|
+ struct apm_trace_key_t trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
|
|
|
+ __u64 trace_id = 0;
|
|
|
+ struct apm_trace_info_t *trace_info = bpf_map_lookup_elem(&trace_info_heap, &trace_key);
|
|
|
+ if (trace_info) {
|
|
|
+ cw_bpf_debug("info_trace_id:%llu", trace_info->trace_id);
|
|
|
+ trace_id = trace_info->trace_id;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (trace_id == 0) {
|
|
|
+ __u64 goid = get_current_goroutine();
|
|
|
+// __u32 tgid = (__u32) (bpf_get_current_pid_tgid() >> 32);
|
|
|
+ struct go_key key = {.tgid = tgid, .goid = goid};
|
|
|
+ __u64 *goid_in_runq_map =
|
|
|
+ bpf_map_lookup_elem(&go_ancerstor_by_runq_map, &key);
|
|
|
+ if (goid_in_runq_map) {
|
|
|
+// bpf_printk("-----[Kernel Enter][HTTP]:has 1 goid_in_runq_map1 :%llu", *goid_in_runq_map);
|
|
|
+ struct go_key goid_in_runq_map_key = {.tgid = tgid, .goid = *goid_in_runq_map};
|
|
|
+
|
|
|
+// bpf_printk("-----[Kernel Enter][HTTP]: only one in go_ancerstor_by_runq_map. Now find it from go_ancerstor_map");
|
|
|
+ __u64 *newancestor3 = bpf_map_lookup_elem(&go_ancerstor_map, &goid_in_runq_map_key);
|
|
|
+ if (newancestor3) {
|
|
|
+// bpf_printk("-----[Kernel Enter][HTTP]:has a newancestor in go_ancerstor_map :%llu", *newancestor3);
|
|
|
+ struct apm_trace_key_t trace_key2 = {.tgid = tgid, .goid = *newancestor3};
|
|
|
+ trace_info = bpf_map_lookup_elem(&trace_info_heap, &trace_key2);
|
|
|
+ if (trace_info) {
|
|
|
+ bpf_printk("[get_apm_trace_id_v2] [find goid in go_ancerstor_map] [key:%d-%llu] -->%llu", tgid,
|
|
|
+ goid, trace_info->trace_id);
|
|
|
+ struct goid_trace_key_t k = {.tgid = tgid, .goid = get_current_goroutine()};
|
|
|
+ if (trace_info->routine_reuse == true) {
|
|
|
+ bpf_printk("[get_apm_trace_id_v2] [routine_reuse already set] [key:%d-%llu] -->%llu", tgid,
|
|
|
+ goid, trace_info->trace_id);
|
|
|
+ }
|
|
|
+ if (trace_info->routine_reuse == false) {
|
|
|
+ trace_id = trace_info->trace_id;
|
|
|
+ trace_info->routine_reuse = true;
|
|
|
+// bpf_map_update_elem(&trace_info_heap, &key, trace_info, BPF_EXIST);
|
|
|
+ bpf_printk(
|
|
|
+ "[get_apm_trace_id_v2] [save routine_reuse into trace_info_heap] [key:%d-%llu] -->%llu",
|
|
|
+ tgid, goid, trace_info->trace_id);
|
|
|
+ }
|
|
|
+
|
|
|
+// trace_id = trace_info->trace_id;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// long aaa = bpf_map_update_elem(&goid_trace_info_heap, &k, &trace_id, BPF_ANY);
|
|
|
+// if (aaa==0){
|
|
|
+// bpf_printk("save ok");
|
|
|
+// __u64 *watch_trace_id = bpf_map_lookup_elem(&goid_trace_info_heap, &k);
|
|
|
+// if (watch_trace_id) {
|
|
|
+// bpf_printk("watch [key:%d-%llu] -> trace_id:%llu",k.tgid ,k.goid,*watch_trace_id);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// todo 保存 gocache
|
|
|
+// bpf_printk("ok tid:%d-goid:%d-%llu",key.tgid,key.goid, trace_info2->trace_id);
|
|
|
+// struct goid_trace_key_t go_key = {.tgid = tgid, .goid = goid};
|
|
|
+// bpf_printk("111find by goid_trace_key_t tgid:%d-goid:%llu", k.tgid, k.goid);
|
|
|
+//
|
|
|
+// struct apm_trace_info_t *aa = bpf_map_lookup_elem(&goid_trace_info_heap, &k);
|
|
|
+// if (aa) {
|
|
|
+// bpf_printk("111get[%llu]", aa->trace_id);
|
|
|
+// }
|
|
|
+// return trace_id;
|
|
|
+ }
|
|
|
+ }
|
|
|
+// todo 保存 gocache
|
|
|
+// trace_id = get_trace_id_from_runq_map(tgid, goid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// find by tgid_pid
|
|
|
+// if (trace_id == 0) {
|
|
|
+// bpf_printk("find by tgid_pid");
|
|
|
+// struct thread_trace_key_t t_key = get_thread_trace_key(tgid_pid);
|
|
|
+// struct apm_trace_info_t *trace_info = bpf_map_lookup_elem(&thread_trace_info_heap, &t_key);
|
|
|
+// if (trace_info) {
|
|
|
+//
|
|
|
+// // todo 保存 gocache
|
|
|
+//// struct goid_trace_key_t key = {.tgid = tgid, .goid = get_current_goroutine()};
|
|
|
+//// bpf_map_update_elem(&goid_trace_info_heap, &key, trace_info, BPF_ANY);
|
|
|
+//
|
|
|
+// trace_id = trace_info->trace_id;
|
|
|
+// bpf_printk("ttttttttttttt trace_id:%llu", trace_info->trace_id);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ // save
|
|
|
+
|
|
|
+
|
|
|
+// if (trace_id == 0) {
|
|
|
+// struct goid_trace_key_t go_key = {.tgid = tgid, .goid = get_current_goroutine()};
|
|
|
+// bpf_printk("find by goid_trace_key_t tgid:%d-goid:%llu", go_key.tgid, go_key.goid);
|
|
|
+// struct apm_trace_info_t *trace_info = bpf_map_lookup_elem(&goid_trace_info_heap, &go_key);
|
|
|
+// if (trace_info) {
|
|
|
+// bpf_printk("get trace_id from goid_trace_key_t pid:[%d]|trace_info:[%llu]", pid, trace_info->trace_id);
|
|
|
+// trace_id = trace_info->trace_id;
|
|
|
+// bpf_printk("---------[from] goid_trace_key_t trace_id:%llu", trace_info->trace_id);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ return trace_id;
|
|
|
+}
|
|
|
+
|
|
|
+static __inline __attribute__((__always_inline__))
|
|
|
+struct apm_trace_info_t *get_apm_trace_info_v2(__u64 tgid_pid, __u32 tgid, __u32 pid) {
|
|
|
+ struct apm_trace_key_t trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
|
|
|
+// __u64 trace_id = 0;
|
|
|
+ struct apm_trace_info_t *trace_info = bpf_map_lookup_elem(&trace_info_heap, &trace_key);
|
|
|
+ if (trace_info) {
|
|
|
+ bpf_printk("info_trace_id:%llu", trace_info->trace_id);
|
|
|
+// return trace_info;
|
|
|
+ return trace_info;
|
|
|
+ }
|
|
|
+
|
|
|
+ __u64 goid = get_current_goroutine();
|
|
|
+ struct go_key key = {.tgid = tgid, .goid = goid};
|
|
|
+ __u64 *goid_in_runq_map =
|
|
|
+ bpf_map_lookup_elem(&go_ancerstor_by_runq_map, &key);
|
|
|
+ if (goid_in_runq_map) {
|
|
|
+// bpf_printk("-----[Kernel Enter][HTTP]:has 1 goid_in_runq_map1 :%llu", *goid_in_runq_map);
|
|
|
+ struct go_key goid_in_runq_map_key = {.tgid = tgid, .goid = *goid_in_runq_map};
|
|
|
+
|
|
|
+// bpf_printk("-----[Kernel Enter][HTTP]: only one in go_ancerstor_by_runq_map. Now find it from go_ancerstor_map");
|
|
|
+ __u64 *newancestor3 = bpf_map_lookup_elem(&go_ancerstor_map, &goid_in_runq_map_key);
|
|
|
+ if (newancestor3) {
|
|
|
+// bpf_printk("-----[Kernel Enter][HTTP]:has a newancestor in go_ancerstor_map :%llu", *newancestor3);
|
|
|
+ struct apm_trace_key_t trace_key2 = {.tgid = tgid, .goid = *newancestor3};
|
|
|
+ trace_info = bpf_map_lookup_elem(&trace_info_heap, &trace_key2);
|
|
|
+ if (trace_info) {
|
|
|
+ bpf_printk("[get_apm_trace_info_v2] [find goid in go_ancerstor_map] [key:%d-%llu] -->%llu", tgid, goid,
|
|
|
+ trace_info->trace_id);
|
|
|
+
|
|
|
+ struct goid_trace_key_t k = {.tgid = tgid, .goid = get_current_goroutine()};
|
|
|
+ if (trace_info->routine_reuse == true) {
|
|
|
+ bpf_printk("[get_apm_trace_info_v2] [routine_reuse already set] [key:%d-%llu] -->%llu", tgid, goid,
|
|
|
+ trace_info->trace_id);
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+ if (trace_info->routine_reuse == false) {
|
|
|
+// trace_id = trace_info->trace_id;
|
|
|
+ trace_info->routine_reuse = true;
|
|
|
+// bpf_map_update_elem(&trace_info_heap, &key, trace_info, BPF_EXIST);
|
|
|
+ bpf_printk("[sssset routine_reuse true] [key:%d-%llu] -->%llu", tgid, goid, trace_info->trace_id);
|
|
|
+ return trace_info;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+// todo 保存 gocache
|
|
|
+// trace_id = get_trace_id_from_runq_map(tgid, goid);
|
|
|
+ }
|
|
|
+// }
|
|
|
+
|
|
|
+ return NULL;
|
|
|
+}
|
|
|
+
|
|
|
+static __inline __attribute__((__always_inline__))
|
|
|
+struct apm_trace_info_t *get_apm_trace_info_v3(__u64 tgid_pid, __u32 tgid, __u32 pid) {
|
|
|
+ struct apm_trace_info_t *trace_info = get_apm_trace_info_v2(tgid_pid, tgid, pid);
|
|
|
+
|
|
|
+ struct goid_trace_key_t goid_trace_key = {.tgid = tgid, .goid = get_current_goroutine()};
|
|
|
+
|
|
|
+ if (trace_info != NULL) {
|
|
|
+ trace_info->type = 1;
|
|
|
+// trace_info = get_apm_trace_info_v2(id, pid, tid);
|
|
|
+// trace_id = trace_info->trace_id;
|
|
|
+ bpf_map_update_elem(&goid_trace_info_heap, &goid_trace_key, trace_info, BPF_ANY);
|
|
|
+// if (trace_info!=NULL)
|
|
|
+// bpf_printk("%llu",trace_info->trace_id);
|
|
|
+ bpf_printk("[get_apm_trace_info_v3 save] [key:%d-%llu] -> trace_id:%llu",goid_trace_key.tgid,goid_trace_key.goid ,trace_info->trace_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (trace_info == NULL) {
|
|
|
+ bpf_printk("[get_apm_trace_info_v3] find by goid_trace_info_heap2 ");
|
|
|
+ //find
|
|
|
+ struct apm_trace_info_t *goid_trace_info = bpf_map_lookup_elem(&goid_trace_info_heap, &goid_trace_key);
|
|
|
+ if (goid_trace_info != NULL) {
|
|
|
+// trace_id = *ttt;
|
|
|
+ bpf_printk(" [get_apm_trace_info_v3 find ooooooooooook] [key:%d-%llu] -> trace_id:%llu", goid_trace_key.tgid,
|
|
|
+ goid_trace_key.goid, goid_trace_info->trace_id);
|
|
|
+ // 将key保存在trace_info用于后续清除
|
|
|
+
|
|
|
+ goid_trace_info->goid_trace_key = goid_trace_key;
|
|
|
+ trace_info = bpf_map_lookup_elem(&trace_info_heap, &goid_trace_info->trace_key);
|
|
|
+ if (trace_info) {
|
|
|
+ trace_info->goid_trace_key = goid_trace_key;
|
|
|
+ trace_info->type = 2;
|
|
|
+ }
|
|
|
+// bpf_printk(" v2v2v2v2v2 [key:%d-%llu] -> trace_id:%llu",.tgid,kkk.goid ,*val2);
|
|
|
+
|
|
|
+
|
|
|
+// trace_info->goid_trace_key = go_id_key;
|
|
|
+
|
|
|
+// bpf_printk(" v2v2v2v2v2 [key:%d-%llu] -> trace_id:%llu",kkk.tgid,kkk.goid ,*val2);
|
|
|
+
|
|
|
+// trace_id = *val2;
|
|
|
+// type = 2;
|
|
|
+// trace_id = goid_trace_info->trace_id;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (trace_info == NULL) {
|
|
|
+ bpf_printk("[get_apm_trace_info_v3] find by tgid_pid");
|
|
|
+ struct thread_trace_key_t t_key = get_thread_trace_key(tgid_pid);
|
|
|
+ trace_info = bpf_map_lookup_elem(&thread_trace_info_heap, &t_key);
|
|
|
+ if (trace_info != NULL) {
|
|
|
+ bpf_printk("ttttttttttttt trace_id:%llu", trace_info->trace_id);
|
|
|
+ trace_info->type = 3;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return trace_info;
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
static __inline __attribute__((__always_inline__))
|
|
|
struct apm_trace_info_t *get_apm_trace_info(__u32 pid, __u32 tid) {
|
|
|
struct apm_trace_key_t trace_key = get_apm_trace_key(120 * NS_PER_SEC, true);
|
|
|
@@ -90,7 +362,7 @@ struct apm_trace_info_t *get_apm_trace_info(__u32 pid, __u32 tid) {
|
|
|
if (trace_info) {
|
|
|
return trace_info;
|
|
|
}
|
|
|
- return 0;
|
|
|
+ return NULL;
|
|
|
}
|
|
|
|
|
|
static __inline __attribute__((__always_inline__))
|
|
|
@@ -101,13 +373,6 @@ struct fd_trace_key_t get_fd_trace_key(__u32 pid, __u64 fd) {
|
|
|
return trace_key;
|
|
|
}
|
|
|
|
|
|
-static __inline __attribute__((__always_inline__))
|
|
|
-struct thread_trace_key_t get_thread_trace_key(__u64 id) {
|
|
|
- struct thread_trace_key_t trace_key = {};
|
|
|
- trace_key.pid_tgid = id;
|
|
|
- return trace_key;
|
|
|
-}
|
|
|
-
|
|
|
|
|
|
static __inline __attribute__((__always_inline__))
|
|
|
__u64 get_fd_trace_id(__u32 pid, __u32 fd) {
|
|
|
@@ -192,6 +457,13 @@ __u64 cw_clear_trace(__u32 tgid, __u32 pid, __u32 fd) {
|
|
|
struct apm_trace_info_t *trace_info = bpf_map_lookup_elem(&trace_info_heap, &trace_key);
|
|
|
if (trace_info) {
|
|
|
bpf_map_delete_elem(&thread_trace_info_heap, &trace_info->thread_trace_key);
|
|
|
+
|
|
|
+// bpf_map_delete_elem(&trace_info_heap, &trace_info_by_fd->trace_key);
|
|
|
+// bpf_map_delete_elem(&goid_trace_info_heap, &trace_info_by_fd->goid_trace_key);
|
|
|
+// bpf_printk("clean trace_info_heap ok %d",trace_info_by_fd->trace_key.goid);
|
|
|
+
|
|
|
+ bpf_map_delete_elem(&goid_trace_info_heap, &trace_info->goid_trace_key);
|
|
|
+ bpf_printk("clean goid_trace_info_heap ok %d", trace_info->goid_trace_key.goid);
|
|
|
}
|
|
|
// 清除trace信息
|
|
|
bpf_map_delete_elem(&trace_info_heap, &trace_key);
|
|
|
@@ -259,7 +531,7 @@ __u32 cw_add_event_count(__u64 trace_id) {
|
|
|
static __inline __attribute__((__always_inline__))
|
|
|
__u32 cw_get_event_count(__u64 trace_id) {
|
|
|
__u32 *event_count = bpf_map_lookup_elem(&trace_event_count_heap, &trace_id);
|
|
|
- if (event_count != NULL){
|
|
|
+ if (event_count != NULL) {
|
|
|
return *event_count;
|
|
|
}
|
|
|
return 0;
|
|
|
@@ -290,6 +562,13 @@ struct apm_trace_info_t cw_save_trace_info(__u64 id, __u32 pid, __u64 fd) {
|
|
|
bpf_map_update_elem(&trace_info_heap, &trace_info.trace_key, &trace_info, BPF_NOEXIST);
|
|
|
bpf_map_update_elem(&fd_trace_info_heap, &trace_info.fd_trace_key, &trace_info, BPF_NOEXIST);
|
|
|
bpf_map_update_elem(&thread_trace_info_heap, &trace_info.thread_trace_key, &trace_info, BPF_NOEXIST);
|
|
|
+
|
|
|
+
|
|
|
+//
|
|
|
+// struct goid_trace_key_t key = {.tgid = id, .goid = get_current_goroutine()};
|
|
|
+// bpf_map_update_elem(&goid_trace_info_heap, &key, &trace_info, BPF_NOEXIST);
|
|
|
+
|
|
|
+
|
|
|
// 事件个数
|
|
|
__u32 event_count = 0;
|
|
|
bpf_map_update_elem(&trace_event_count_heap, &trace_info.trace_id, &event_count, BPF_NOEXIST);
|