| 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 <stdio.h> | ||
| 10 | #include <stdlib.h> | ||
| 11 | #include <pthread.h> | ||
| 12 | #include <iostream> | ||
| 13 | #include "pin_thread_to_core.h" | ||
| 14 | |||
| 15 | #define handle_error_en(en, msg) \ | ||
| 16 | do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) | ||
| 17 | |||
| 18 | using namespace std; | ||
| 19 | |||
| 20 | ///Pins the current thread to the current core | ||
| 21 | /** @return true on success, false otherwise | ||
| 22 | */ | ||
| 23 | 40 | bool pinThreadToCore(){ | |
| 24 | #ifndef __APPLE__ | ||
| 25 | int s, j; | ||
| 26 | cpu_set_t cpuset; | ||
| 27 | pthread_t thread; | ||
| 28 | |||
| 29 | 40 | thread = pthread_self(); | |
| 30 | |||
| 31 | /* Set affinity mask to include CPUs 0 to 7 */ | ||
| 32 | |||
| 33 | 40 | CPU_ZERO(&cpuset); | |
| 34 |
2/2✓ Branch 0 (7→3) taken 320 times.
✓ Branch 1 (7→8) taken 40 times.
|
360 | for (j = 0; j < 8; j++) |
| 35 |
1/2✓ Branch 0 (3→4) taken 320 times.
✗ Branch 1 (3→5) not taken.
|
320 | CPU_SET(j, &cpuset); |
| 36 | |||
| 37 | 40 | s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset); | |
| 38 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→12) taken 40 times.
|
40 | if (s != 0){ |
| 39 | ✗ | handle_error_en(s, "pthread_setaffinity_np"); | |
| 40 | return false; | ||
| 41 | } | ||
| 42 | /* Check the actual affinity mask assigned to the thread */ | ||
| 43 | |||
| 44 | 40 | s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset); | |
| 45 |
1/2✗ Branch 0 (13→14) not taken.
✓ Branch 1 (13→16) taken 40 times.
|
40 | if (s != 0){ |
| 46 | ✗ | handle_error_en(s, "pthread_getaffinity_np"); | |
| 47 | return false; | ||
| 48 | } | ||
| 49 | #endif | ||
| 50 | 40 | return true; | |
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 |