Skip to content

Commit b105724

Browse files
committed
fix multi-thread
1 parent 15bb195 commit b105724

File tree

14 files changed

+218
-195
lines changed

14 files changed

+218
-195
lines changed

lib/bpftime

microbench/ld.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define STR(x) STR_HELPER(x)
2727

2828
#define MOVE_SIZE 128
29-
#define MAP_SIZE (long)( 1024)
29+
#define MAP_SIZE (long)(1024*1024 * 1024)
3030
#define CACHELINE_SIZE 64
3131

3232
#ifndef FENCE_COUNT

microbench/ld_serial.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#define STR(x) STR_HELPER(x)
2828

2929
#define MOVE_SIZE 128
30-
#define MAP_SIZE (long)(1024)
30+
#define MAP_SIZE (long)(1024*1024 * 1024)
3131
#define CACHELINE_SIZE 64
3232

3333
#ifndef FENCE_COUNT

microbench/st.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#define STR_HELPER(x) #x
1313
#define STR(x) STR_HELPER(x)
1414
#define MOVE_SIZE 128
15-
#define MAP_SIZE (long)(1024)
15+
#define MAP_SIZE (long)(1024*1024 * 1024)
1616
#define CACHELINE_SIZE 64
1717
#ifndef FENCE_COUNT
1818
#define FENCE_COUNT 8

microbench/st_serial.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#define STR_HELPER(x) #x
2121
#define STR(x) STR_HELPER(x)
2222
#define MOVE_SIZE 128
23-
#define MAP_SIZE (long)(1024)
23+
#define MAP_SIZE (long)(1024*1024 * 1024)
2424
#define CACHELINE_SIZE 64
2525
#ifndef FENCE_COUNT
2626
#define FENCE_COUNT 8

microbench/thread.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,20 @@ int main() {
4141
print_thread_id("主线程 ");
4242

4343
// 创建一些普通工作线程
44-
std::vector<std::thread> threads;
44+
std::vector<std::shared_ptr<std::thread>> threads;
4545
for (int i = 0; i < 3; ++i) {
46-
threads.emplace_back(thread_function, i);
46+
std::cout << "english, finally\n";
47+
std::shared_ptr<std::thread> thread = std::make_shared<std::thread>(thread_function, i);
48+
threads.push_back(thread);
4749
}
4850

4951
// 创建将调用exit_group的线程
5052
std::thread exit_thread(exit_group_thread);
5153

5254
// 等待所有工作线程完成
5355
for (auto& t : threads) {
54-
if (t.joinable()) {
55-
t.join();
56+
if (t->joinable()) {
57+
t->join();
5658
}
5759
}
5860

@@ -65,4 +67,4 @@ int main() {
6567
std::cout << "主程序结束" << std::endl;
6668

6769
return 0;
68-
}
70+
}

script/get_all_results.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,13 @@ def main():
296296
# 策略组合参数
297297
parser.add_argument("--run-policy-combinations", action="store_true", help="运行策略组合测试")
298298
parser.add_argument("--allocation-policies", nargs="+", choices=ALLOCATION_POLICIES,
299-
help=f"分配策略选项: {', '.join(ALLOCATION_POLICIES)}")
299+
help=f"分配策略选项: {', '.join(ALLOCATION_POLICIES)}",default=ALLOCATION_POLICIES)
300300
parser.add_argument("--migration-policies", nargs="+", choices=MIGRATION_POLICIES,
301-
help=f"迁移策略选项: {', '.join(MIGRATION_POLICIES)}")
301+
help=f"迁移策略选项: {', '.join(MIGRATION_POLICIES)}",default=MIGRATION_POLICIES)
302302
parser.add_argument("--paging-policies", nargs="+", choices=PAGING_POLICIES,
303-
help=f"分页策略选项: {', '.join(PAGING_POLICIES)}")
303+
help=f"分页策略选项: {', '.join(PAGING_POLICIES)}",default=PAGING_POLICIES)
304304
parser.add_argument("--caching-policies", nargs="+", choices=CACHING_POLICIES,
305-
help=f"缓存策略选项: {', '.join(CACHING_POLICIES)}")
305+
help=f"缓存策略选项: {', '.join(CACHING_POLICIES)}",default=CACHING_POLICIES)
306306

307307
# 错误处理
308308
parser.add_argument("--ignore-errors", action="store_true", help="忽略错误继续运行")

script/run_gromacs.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ wget https://files.rcsb.org/download/5pep.pdb
33
gmx pdb2gmx -f 5pep.pdb -o 5pep.gro -water spc
44
gmx editconf -f 5pep.gro -o 5pep-box.gro -c -d 1.0 -bt cubic
55
gmx solvate -cp 5pep-box.gro -cs spc216.gro -o 5pep-solv.gro -p topol.top
6+
gmx mdrun -v -deffnm 5pep.mdp

src/bpftimeruntime.cpp

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <unistd.h>
2424
#include <utility>
2525
#include <vector>
26+
#include <thread>
2627
#include "bpftimeruntime.h"
2728

2829
BpfTimeRuntime::BpfTimeRuntime(pid_t tid, std::string program_location)
@@ -35,31 +36,43 @@ BpfTimeRuntime::BpfTimeRuntime(pid_t tid, std::string program_location)
3536
}
3637

3738
BpfTimeRuntime::~BpfTimeRuntime() { bpftime_remove_global_shm(); }
38-
3939
int 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;

src/cxlcontroller.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ void CXLController::set_stats(mem_stats stats) {
8181
}
8282

8383
void CXLController::set_process_info(const proc_info &process_info) {
84-
monitors->enable(process_info.current_pid, process_info.current_tid, true, 1000, 0);
84+
monitors->enable(process_info.current_pid, process_info.current_tid, true, 1000, helper.num_of_cpu());
8585
}
8686

8787
void CXLController::set_thread_info(const proc_info &thread_info) {
88-
monitors->enable(thread_info.current_pid, thread_info.current_tid, false, 0, 0);
88+
monitors->enable(thread_info.current_pid, thread_info.current_tid, false, 0, helper.num_of_cpu());
8989
}
9090
void CXLController::perform_migration() {
9191
if (!migration_policy)
@@ -252,6 +252,7 @@ int CXLController::insert(uint64_t timestamp, uint64_t tid, uint64_t phys_addr,
252252
for (int i = last_index; i < index; i++) {
253253
// 更新当前时间戳
254254
current_timestamp += time_step;
255+
// std::cout << std::format("{} {}", current_timestamp, i) << std::endl;
255256

256257
// 首先检查LRU缓存
257258
auto cache_result = access_cache(phys_addr, current_timestamp);

0 commit comments

Comments
 (0)