set current thread cpu affinity to cpu list, this function works like * taskset command (actually cpulist parsing logic reference to util-linux). * example of this function: "0,2,3", "0,2-3", "0-20:2". */
| 71 | * taskset command (actually cpulist parsing logic reference to util-linux). |
| 72 | * example of this function: "0,2,3", "0,2-3", "0-20:2". */ |
| 73 | void setcpuaffinity(const char *cpulist) { |
| 74 | const char *p, *q; |
| 75 | char *end = NULL; |
| 76 | #ifdef __linux__ |
| 77 | cpu_set_t cpuset; |
| 78 | #endif |
| 79 | #if defined (__FreeBSD__) || defined(__DragonFly__) |
| 80 | cpuset_t cpuset; |
| 81 | #endif |
| 82 | #ifdef __NetBSD__ |
| 83 | cpuset_t *cpuset; |
| 84 | #endif |
| 85 | |
| 86 | if (!cpulist) |
| 87 | return; |
| 88 | |
| 89 | #ifndef __NetBSD__ |
| 90 | CPU_ZERO(&cpuset); |
| 91 | #else |
| 92 | cpuset = cpuset_create(); |
| 93 | #endif |
| 94 | |
| 95 | q = cpulist; |
| 96 | while (p = q, q = next_token(q, ','), p) { |
| 97 | int a, b, s; |
| 98 | const char *c1, *c2; |
| 99 | |
| 100 | if (next_num(p, &end, &a) != 0) |
| 101 | return; |
| 102 | |
| 103 | b = a; |
| 104 | s = 1; |
| 105 | p = end; |
| 106 | |
| 107 | c1 = next_token(p, '-'); |
| 108 | c2 = next_token(p, ','); |
| 109 | |
| 110 | if (c1 != NULL && (c2 == NULL || c1 < c2)) { |
| 111 | if (next_num(c1, &end, &b) != 0) |
| 112 | return; |
| 113 | |
| 114 | c1 = end && *end ? next_token(end, ':') : NULL; |
| 115 | if (c1 != NULL && (c2 == NULL || c1 < c2)) { |
| 116 | if (next_num(c1, &end, &s) != 0) |
| 117 | return; |
| 118 | |
| 119 | if (s == 0) |
| 120 | return; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if ((a > b)) |
| 125 | return; |
| 126 | |
| 127 | while (a <= b) { |
| 128 | #ifndef __NetBSD__ |
| 129 | CPU_SET(a, &cpuset); |
| 130 | #else |
no test coverage detected