-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
212 lines (175 loc) · 6.1 KB
/
Copy pathmain.cpp
File metadata and controls
212 lines (175 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <cstdint>
#include <mach/mach.h>
#include <mach/thread_info.h> // thread_identifier_info_data_t, thread_info()
#include <mach/thread_policy.h> // thread_port_t, thread_policy_set()
#include <pthread.h> // pthread_mach_thread_np(), pthread_threadid_np()
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/sysctl.h>
#if defined(__x86_64__) || defined(__i386__)
#include <cpuid.h>
#endif
#include <cassert>
#include <chrono>
#include <format>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
//
// @Note(impcuong): Abbrev-list
// + np := Non-portable (https://github.com/apple/darwin-libpthread/blob/main/src/pthread.c#L920)
// + cnt := count
// + snd := send
// + rcv := receive
// + ctl := control
// + rm := remove
// + rc := return-code
//
//
// @Docs:
// + https://www.hybridkernel.com/2015/01/18/binding_threads_to_cores_osx.html
// + https://eli.thegreenplace.net/2016/c11-threads-affinity-and-hyperthreading/
//
typedef struct cpu_set
{
uint32_t cnt;
} cpu_set_t;
static inline void CPU_ZERO(cpu_set_t *cpu)
{
cpu->cnt = 0;
}
static inline void CPU_SET(int id, cpu_set_t *cpu)
{
cpu->cnt |= (1 << id);
}
// @Docs: https://linux.die.net/man/3/cpu_isset
// Test to see if CPU cpu is a member of set.
static inline int CPU_ISSET(int id, cpu_set_t *cpu)
{
return cpu->cnt & (1 << id);
}
#define SYSCTL_CORE_COUNT "machdep.cpu.core_count"
int32_t estimate_core_quan()
{
int32_t core_quan = 0;
size_t sz = sizeof(core_quan);
sysctlbyname(SYSCTL_CORE_COUNT, &core_quan, &sz, NULL, 0);
return core_quan;
}
int pthread_setaffinity_np(pthread_t thread, size_t each_cpu_sz, cpu_set_t *cpu)
{
int32_t core_quan = estimate_core_quan();
int core_id = 0;
for (; core_id < core_quan * each_cpu_sz; core_id++)
if (CPU_ISSET(core_id, cpu))
break;
thread_port_t mach_thread_port = pthread_mach_thread_np(thread);
thread_affinity_policy_data_t mach_thread_policy = { .affinity_tag = core_id };
kern_return_t rc = thread_policy_set(mach_thread_port /*thread=*/, THREAD_AFFINITY_POLICY /*flavor=*/,
reinterpret_cast<thread_policy_t>(&mach_thread_policy) /*policy_info=*/, 0 /*policy_infoCnt=*/);
return rc;
}
// @From: https://stackoverflow.com/a/40398183/12535617
#define CPU_ID(info, leaf, subleaf) __cpuid_count(leaf, subleaf, info[0], info[1], info[2], info[3])
void sched_getcpu(int &cpu_id)
{
#if defined(__arm64__) || defined(__aarch64__)
thread_identifier_info_data_t thread_id_info;
mach_msg_type_number_t info_sz = THREAD_IDENTIFIER_INFO_COUNT;
kern_return_t rc = thread_info(mach_thread_self() /*target_act=*/, THREAD_IDENTIFIER_INFO /*flavor=*/,
reinterpret_cast<thread_info_t>(&thread_id_info) /*thread_info_out=*/, &info_sz /*thread_info_outCnt=*/);
uint64_t tid;
pthread_threadid_np(NULL /*pthread_t=*/, &tid);
assert(tid == thread_id_info.thread_id);
if (rc != KERN_SUCCESS)
cpu_id = -1;
else
// @Docs: https://developer.apple.com/documentation/kernel/thread_identifier_info_data_t/1579032-thread_id
cpu_id = static_cast<int>(thread_id_info.thread_id);
#else
uint32_t cpu_info[4];
CPU_ID(cpu_info, 1 /*leaf=*/, 0 /*subleaf=*/);
if ((cpu_info[3] & (1 << 9)) == 0)
cpu_id = -1;
else
cpu_id = static_cast<unsigned>(cpu_info[1] >> 24);
#endif
cpu_id = std::max(cpu_id, 0);
}
// End @From
#define MSG_ALLOWED_TYPE 1
#define MSG_BUF_LEN 1024
// @Docs: https://www.man7.org/linux/man-pages/man3/msgsnd.3p.html
struct thread_shared_msg
{
long type;
char content[MSG_BUF_LEN];
};
int main()
{
int32_t core_quan = estimate_core_quan();
assert(core_quan > 0);
// -----
int cpu_id = -1;
struct thread_shared_msg msg = {0};
msg.type = MSG_ALLOWED_TYPE;
std::string tmp_content = std::format("INFO: Daddy's dsize {}", cpu_id);
std::strncpy(msg.content, tmp_content.c_str(), sizeof(msg.content) - 1);
msg.content[MSG_BUF_LEN - 1] = '\0';
key_t send_key = ftok("." /*path=*/, 'a' /*proj_id=*/); // System V IPC key.
int msg_id = msgget(send_key, IPC_CREAT | 0666);
assert(msg_id != -1);
int msg_send_rc = msgsnd(msg_id, &msg, sizeof(msg.content), 0);
assert(msg_send_rc != -1);
// -----
std::mutex io_mutex; // A mutex ensures orderly access to std::cout from multiple threads.
std::vector<std::thread> thread_pool(core_quan);
for (int tid = 0; tid < core_quan; tid++)
{
thread_pool[tid] = std::thread(
[&msg_id, &cpu_id, &io_mutex, tid]
{
std::this_thread::sleep_for(std::chrono::milliseconds(20));
key_t rcv_key = ftok("." /*path=*/, 'a' /*proj_id=*/);
int rcv_msg_id = msgget(rcv_key, 0666);
struct thread_shared_msg rcv_msg = {0};
rcv_msg.type = MSG_ALLOWED_TYPE;
int msg_rcv_rc = msgrcv(rcv_msg_id, &rcv_msg, sizeof(rcv_msg.content), rcv_msg.type, 0 /*msgflg=*/);
if (msg_rcv_rc == -1)
{
std::lock_guard<std::mutex> lock(io_mutex);
std::cerr << "ERROR: Thread " << tid << " failed to receive: " << strerror(errno) << std::endl;
return;
}
std::atomic<int> tick = 0;
sched_getcpu(cpu_id);
while (true)
{
std::lock_guard<std::mutex> io_lock(io_mutex);
std::cout << "INFO: Thread #" << tid << "\n";
std::cout << " + CPU: " << cpu_id << "\n";
std::cout << " + Received message: " << rcv_msg.content << "\n";
tick++;
if (tick == 15)
break;
}
std::snprintf(rcv_msg.content, MSG_BUF_LEN, "%s + %d", rcv_msg.content, cpu_id);
msgsnd(msg_id, &rcv_msg, sizeof(rcv_msg.content), 0);
std::this_thread::sleep_for(std::chrono::milliseconds(900));
}
);
cpu_set_t cpu; // Object representing a set of CPUs.
CPU_ZERO(&cpu);
CPU_SET(tid /*id=*/, &cpu);
int affinity_thread_rc = pthread_setaffinity_np(thread_pool[tid].native_handle(), sizeof(cpu_set_t), &cpu);
if (affinity_thread_rc != 0)
std::cerr << "ERROR: Affinity thread's RC = " << affinity_thread_rc << "\n";
}
for (auto &t : thread_pool)
if (t.joinable())
t.join();
// -----
msgctl(msg_id, IPC_RMID, nullptr);
return 0;
}