Skip to content

Commit 5961979

Browse files
committed
working multi-thread
1 parent b69e9eb commit 5961979

File tree

7 files changed

+94
-13
lines changed

7 files changed

+94
-13
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ set(LINUX_SOURCE /lib/modules/${arch}/build/)
1111

1212
set(CMAKE_CXX_STANDARD 26)
1313
add_subdirectory(lib/bpftime)
14-
# add_subdirectory(microbench)
15-
add_subdirectory(workloads)
14+
add_subdirectory(microbench)
15+
#add_subdirectory(workloads)
1616

1717
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})
1818

@@ -27,12 +27,12 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -latomic")
2727
add_executable(CXLMemSim ${SOURCE_FILES} src/main.cc)
2828

2929
include_directories(CXLMemSim include ${cxxopts_INCLUDE_DIR} ${spdlog_INCLUDE_DIR} ${runtime_SOURCE_DIR}/include)
30-
target_link_libraries(CXLMemSim cxxopts::cxxopts bpftime_vm bpftime-object bpftime_base_attach_impl bpftime-agent ${CMAKE_CURRENT_BINARY_DIR}/lib/bpftime/runtime/libruntime.a -lspdlog -lfmt)
30+
target_link_libraries(CXLMemSim cxxopts::cxxopts bpftime_vm bpftime-object bpftime_base_attach_impl bpftime-agent )
3131

3232
add_executable(CXLMemSimRoB ${SOURCE_FILES} src/rob.cc)
3333

3434
include_directories(CXLMemSimRoB include ${cxxopts_INCLUDE_DIR} ${spdlog_INCLUDE_DIR} ${runtime_SOURCE_DIR}/include)
35-
target_link_libraries(CXLMemSimRoB cxxopts::cxxopts bpftime_vm bpftime-object bpftime_base_attach_impl bpftime-agent ${CMAKE_CURRENT_BINARY_DIR}/lib/bpftime/runtime/libruntime.a spdlog fmt)
35+
target_link_libraries(CXLMemSimRoB cxxopts::cxxopts bpftime_vm bpftime-object bpftime_base_attach_impl bpftime-agent)
3636

3737

3838
function(bpf prefix)

include/cxlcontroller.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ class CXLController : public CXLSwitch {
226226
int insert(uint64_t timestamp, uint64_t tid, uint64_t phys_addr, uint64_t virt_addr, int index) override;
227227
void delete_entry(uint64_t addr, uint64_t length) override;
228228
void set_stats(mem_stats stats);
229-
static void set_process_info(const proc_info &process_info);
230-
static void set_thread_info(const proc_info &thread_info);
229+
void set_process_info(const proc_info &process_info);
230+
void set_thread_info(const proc_info &thread_info);
231231
void perform_migration();
232232
// 添加缓存访问方法
233233
std::optional<uint64_t> access_cache(uint64_t addr, uint64_t timestamp) { return lru_cache.get(addr, timestamp); }

microbench/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_executable(ptr-chasing ptr-chasing.cpp)
1111
add_executable(branch_simple branch_simple.c)
1212
add_executable(cache-thrash cache-thrash.c)
1313
add_executable(cache-miss cache-miss.c)
14+
add_executable(thread thread.cpp)
1415

1516
add_executable(ld1 ld.cpp)
1617
target_compile_definitions(ld1 PRIVATE -DFENCE_COUNT=1)

microbench/thread.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#define _GNU_SOURCE
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <unistd.h>
5+
#include <sys/syscall.h>
6+
#include <linux/sched.h>
7+
#include <sys/types.h>
8+
#include <sys/wait.h>
9+
#include <errno.h>
10+
#include <sched.h>
11+
// set_tid_address 系统调用号
12+
#define SYS_set_tid_address 218
13+
14+
// 用于存储线程ID的变量
15+
int *clear_child_tid = NULL;
16+
17+
// 线程函数
18+
int thread_function(void *arg) {
19+
printf("子线程: 我的 TID 是 %ld\n", syscall(SYS_gettid));
20+
printf("子线程: clear_child_tid 地址是 %p\n", clear_child_tid);
21+
22+
// 让线程运行一段时间
23+
sleep(2);
24+
25+
printf("子线程: 退出前 clear_child_tid 的值是 %d\n", *clear_child_tid);
26+
return 0;
27+
}
28+
29+
int main() {
30+
// 分配内存用于存储线程ID
31+
clear_child_tid = (int *)malloc(sizeof(int));
32+
*clear_child_tid = 0;
33+
34+
printf("主线程: clear_child_tid 地址是 %p\n", clear_child_tid);
35+
36+
// 分配线程栈
37+
void *stack = malloc(4096);
38+
if (!stack) {
39+
perror("malloc 失败");
40+
return 1;
41+
}
42+
43+
// 创建子线程
44+
pid_t child_pid = clone(thread_function,
45+
(char *)stack + 4096, // 栈顶
46+
CLONE_VM | CLONE_FS | CLONE_FILES | SIGCHLD,
47+
NULL);
48+
49+
if (child_pid == -1) {
50+
perror("clone 失败");
51+
free(stack);
52+
free(clear_child_tid);
53+
return 1;
54+
}
55+
56+
printf("主线程: 创建的子线程 ID 是 %d\n", child_pid);
57+
58+
// 直接调用 set_tid_address 系统调用
59+
long result = syscall(SYS_set_tid_address, clear_child_tid);
60+
printf("主线程: set_tid_address 返回值是 %ld\n", result);
61+
62+
// 等待子线程结束
63+
waitpid(child_pid, NULL, 0);
64+
65+
printf("主线程: 子线程结束后 clear_child_tid 的值是 %d\n", *clear_child_tid);
66+
67+
// 释放资源
68+
free(stack);
69+
free(clear_child_tid);
70+
71+
return 0;
72+
}

src/bpftimeruntime.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int BpfTimeRuntime::read(CXLController *controller, BPFTimeRuntimeElem *elem) {
5353
SPDLOG_DEBUG("Process map key: {} {} thread_id:{}", key1, key,
5454
std::this_thread::get_id()); // 使用std::this_thread获取当前线程ID
5555

56-
if (i == 10 && item2 != nullptr) {
56+
if (i == 8 && item2 != nullptr) {
5757
stats = *((mem_stats *)item2);
5858
controller->set_stats(stats);
5959
elem->total++;
@@ -63,7 +63,7 @@ int BpfTimeRuntime::read(CXLController *controller, BPFTimeRuntimeElem *elem) {
6363
controller->set_process_info(proc_info1);
6464
elem->total++;
6565
}
66-
if (i == 8 && item2 != nullptr) {
66+
if (i == 7 && item2 != nullptr) {
6767
thread_info1 = *((proc_info *)item2);
6868
controller->set_thread_info(thread_info1);
6969
elem->total++;

src/cxlcontroller.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,15 @@ void CXLController::set_process_info(const proc_info &process_info) {
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+
if (thread_info .parent_pid == monitors->mon[0].tgid) {
89+
monitors->enable(thread_info.current_pid, thread_info.current_tid, false, 0, 0);
90+
std::cout << "set thread info " << thread_info.current_pid << " " << thread_info.current_tid << std::endl;
91+
auto lbr_ = new lbr{.from = 0, .to = 0, .flags = 0};
92+
this->insert_one(thread_map[thread_info.current_tid], *lbr_);
93+
delete lbr_;
94+
}
8995
}
96+
9097
void CXLController::perform_migration() {
9198
if (!migration_policy)
9299
return;

src/main.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Copyright 2025 Regents of the University of California
99
* UC Santa Cruz Sluglab.
1010
*/
11-
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
11+
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_OFF
1212
#include "cxlendpoint.h"
1313
#include "helper.h"
1414
#include "monitor.h"
@@ -228,12 +228,13 @@ int main(int argc, char *argv[]) {
228228
if (t_process == 0) {
229229
sleep(1);
230230
std::vector<const char *> envp;
231-
envp.push_back("LD_PRELOAD=/root/.bpftime/libbpftime-agent.so");
231+
envp.emplace_back("LD_PRELOAD=/root/.bpftime/libbpftime-agent.so");
232+
envp.emplace_back("OMP_NUM_THREADS=24");
232233
while (!env.empty()) {
233-
envp.push_back(env.back().c_str());
234+
envp.emplace_back(env.back().c_str());
234235
env.pop_back();
235236
}
236-
envp.push_back(nullptr);
237+
envp.emplace_back(nullptr);
237238
execve(filename, args, const_cast<char *const *>(envp.data()));
238239
SPDLOG_ERROR("Exec: failed to create target process");
239240
exit(1);

0 commit comments

Comments
 (0)