2323#include < unistd.h>
2424#include < utility>
2525#include < vector>
26+ #include < thread>
2627#include " bpftimeruntime.h"
2728
2829BpfTimeRuntime::BpfTimeRuntime (pid_t tid, std::string program_location)
@@ -35,31 +36,43 @@ BpfTimeRuntime::BpfTimeRuntime(pid_t tid, std::string program_location)
3536}
3637
3738BpfTimeRuntime::~BpfTimeRuntime () { bpftime_remove_global_shm (); }
38-
3939int BpfTimeRuntime::read (CXLController *controller, BPFTimeRuntimeElem *elem) {
4040 mem_stats stats;
4141 proc_info proc_info1;
4242 proc_info thread_info1;
43+
4344 for (int i = 6 ; i < 11 ; i++) {
44- int key = 0 ;
45- int key1 = 0 ;
46- bpftime_map_get_next_key (i, &key1, &key); // process map
47- auto item2 = bpftime_map_lookup_elem (i, &key); // allocs map
48- SPDLOG_DEBUG (" Process map key: {} {} {}" , key1, key, tid);
49- if (i == 6 && item2 != nullptr ) {
50- stats = *((mem_stats *)item2);
51- controller->set_stats (stats);
52- elem->total ++;
53- }
54- if (i == 9 && item2 != nullptr ) {
55- proc_info1 = *((proc_info *)item2);
56- controller->set_process_info (proc_info1);
57- elem->total ++;
58- }
59- if (i == 10 && item2 != nullptr ) {
60- thread_info1 = *((proc_info *)item2);
61- controller->set_thread_info (thread_info1);
62- elem->total ++;
45+ // 修改数据类型匹配 bpftime_map_get_next_key 预期的大小
46+ uint64_t key = 0 ; // 改为8字节
47+ uint64_t key1 = 0 ; // 改为8字节
48+ void *item2 = (void *)1 ;
49+ while (item2){
50+ int ret = bpftime_map_get_next_key (i, &key1, &key); // 获取key
51+ if (ret != 0 ) {
52+ SPDLOG_DEBUG (" Failed to get next key for map {}" , i);
53+ break ;
54+ }
55+
56+ item2 = (void *)bpftime_map_lookup_elem (i, &key);
57+ SPDLOG_DEBUG (" Process map key: {} {} thread_id:{}" , key1, key,
58+ std::this_thread::get_id ()); // 使用std::this_thread获取当前线程ID
59+
60+ if (i == 6 && item2 != nullptr ) {
61+ stats = *((mem_stats *)item2);
62+ controller->set_stats (stats);
63+ elem->total ++;
64+ }
65+ if (i == 9 && item2 != nullptr ) {
66+ proc_info1 = *((proc_info *)item2);
67+ controller->set_process_info (proc_info1);
68+ elem->total ++;
69+ }
70+ if (i == 10 && item2 != nullptr ) {
71+ thread_info1 = *((proc_info *)item2);
72+ controller->set_thread_info (thread_info1);
73+ elem->total ++;
74+ }
75+ key1 = key; // 更新key1为当前key
6376 }
6477 }
6578 return 0 ;
0 commit comments