| 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 (7→3) taken 8 times.
✓ Branch 1 (7→8) taken 1 times.
|
9 | for(int j = 0; j < 8; j++){ |
| 27 |
1/2✓ Branch 0 (3→4) taken 8 times.
✗ Branch 1 (3→5) not taken.
|
8 | CPU_SET(j, &cpuset); |
| 28 | } | ||
| 29 | 1 | int s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); | |
| 30 |
1/3✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 1 times.
✗ Branch 2 (10→11) 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/3✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→16) taken 1 times.
✗ Branch 2 (14→15) 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 |