Directory: | ./ |
---|---|
File: | src/pin_thread_to_core.cpp |
Date: | 2025-05-16 18:34:22 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 10 | 10 | 100.0% |
Branches: | 5 | 12 | 41.7% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | |||
2 | /*************************************** | ||
3 | Auteur : Pierre Aubert | ||
4 | Mail : pierre.aubert@lapp.in2p3.fr | ||
5 | Licence : CeCILL-C | ||
6 | ****************************************/ | ||
7 | |||
8 | #include <errno.h> | ||
9 | #include <pthread.h> | ||
10 | #include <iostream> | ||
11 | #include "pin_thread_to_core.h" | ||
12 | |||
13 | #define handle_error_en(en, msg) \ | ||
14 | do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) | ||
15 | |||
16 | ///Pins the current thread to the current core | ||
17 | /** @return true on success, false otherwise | ||
18 | */ | ||
19 | 1 | bool pinThreadToCore(){ | |
20 | #ifndef __APPLE__ | ||
21 | cpu_set_t cpuset; | ||
22 | 1 | pthread_t thread = pthread_self(); | |
23 | /* Set affinity mask to include CPUs 0 to 7 */ | ||
24 | |||
25 | 1 | CPU_ZERO(&cpuset); | |
26 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
|
9 | for(int j = 0; j < 8; j++){ |
27 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | CPU_SET(j, &cpuset); |
28 | } | ||
29 | 1 | int s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); | |
30 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | if(s != 0){handle_error_en(s, "pthread_setaffinity_np");return false;} |
31 | /* Check the actual affinity mask assigned to the thread */ | ||
32 | 1 | s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset); | |
33 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | if(s != 0){handle_error_en(s, "pthread_getaffinity_np");return false;} |
34 | #endif | ||
35 | 1 | return true; | |
36 | } | ||
37 | |||
38 | |||
39 |