Amaury Denoyelle | f75c640 | 2021-04-14 15:03:51 +0200 | [diff] [blame] | 1 | #define _GNU_SOURCE |
| 2 | #include <sched.h> |
| 3 | |
| 4 | #include <haproxy/compat.h> |
| 5 | #include <haproxy/cpuset.h> |
| 6 | #include <haproxy/intops.h> |
| 7 | |
Amaury Denoyelle | fc6ac53 | 2021-04-27 10:46:36 +0200 | [diff] [blame] | 8 | struct cpu_map cpu_map; |
| 9 | |
Amaury Denoyelle | f75c640 | 2021-04-14 15:03:51 +0200 | [diff] [blame] | 10 | void ha_cpuset_zero(struct hap_cpuset *set) |
| 11 | { |
| 12 | #if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET) |
| 13 | CPU_ZERO(&set->cpuset); |
| 14 | |
| 15 | #elif defined(CPUSET_USE_ULONG) |
| 16 | set->cpuset = 0; |
| 17 | #endif |
| 18 | } |
| 19 | |
| 20 | int ha_cpuset_set(struct hap_cpuset *set, int cpu) |
| 21 | { |
| 22 | if (cpu >= ha_cpuset_size()) |
| 23 | return 1; |
| 24 | |
| 25 | #if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET) |
| 26 | CPU_SET(cpu, &set->cpuset); |
| 27 | return 0; |
| 28 | |
| 29 | #elif defined(CPUSET_USE_ULONG) |
| 30 | set->cpuset |= (0x1 << cpu); |
| 31 | return 0; |
| 32 | #endif |
| 33 | } |
| 34 | |
| 35 | int ha_cpuset_clr(struct hap_cpuset *set, int cpu) |
| 36 | { |
| 37 | if (cpu >= ha_cpuset_size()) |
| 38 | return 1; |
| 39 | |
| 40 | #if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET) |
| 41 | CPU_CLR(cpu, &set->cpuset); |
| 42 | return 0; |
| 43 | |
| 44 | #elif defined(CPUSET_USE_ULONG) |
| 45 | set->cpuset &= ~(0x1 << cpu); |
| 46 | return 0; |
| 47 | #endif |
| 48 | } |
| 49 | |
| 50 | void ha_cpuset_and(struct hap_cpuset *dst, const struct hap_cpuset *src) |
| 51 | { |
| 52 | #if defined(CPUSET_USE_CPUSET) |
| 53 | CPU_AND(&dst->cpuset, &dst->cpuset, &src->cpuset); |
| 54 | |
| 55 | #elif defined(CPUSET_USE_FREEBSD_CPUSET) |
| 56 | CPU_AND(&dst->cpuset, &src->cpuset); |
| 57 | |
| 58 | #elif defined(CPUSET_USE_ULONG) |
| 59 | dst->cpuset &= src->cpuset; |
| 60 | #endif |
| 61 | } |
| 62 | |
| 63 | int ha_cpuset_count(const struct hap_cpuset *set) |
| 64 | { |
| 65 | #if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET) |
| 66 | return CPU_COUNT(&set->cpuset); |
| 67 | |
| 68 | #elif defined(CPUSET_USE_ULONG) |
| 69 | return my_popcountl(set->cpuset); |
| 70 | #endif |
| 71 | } |
| 72 | |
| 73 | int ha_cpuset_ffs(const struct hap_cpuset *set) |
| 74 | { |
| 75 | #if defined(CPUSET_USE_CPUSET) |
| 76 | int n; |
| 77 | |
| 78 | if (!CPU_COUNT(&set->cpuset)) |
| 79 | return 0; |
| 80 | |
| 81 | for (n = 0; !CPU_ISSET(n, &set->cpuset); ++n) |
| 82 | ; |
| 83 | |
| 84 | return n + 1; |
| 85 | |
| 86 | #elif defined(CPUSET_USE_FREEBSD_CPUSET) |
| 87 | return CPU_FFS(&set->cpuset); |
| 88 | |
| 89 | #elif defined(CPUSET_USE_ULONG) |
| 90 | if (!set->cpuset) |
| 91 | return 0; |
| 92 | |
| 93 | return my_ffsl(set->cpuset); |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | void ha_cpuset_assign(struct hap_cpuset *dst, const struct hap_cpuset *src) |
| 98 | { |
| 99 | #if defined(CPUSET_USE_CPUSET) |
| 100 | CPU_ZERO(&dst->cpuset); |
| 101 | CPU_OR(&dst->cpuset, &dst->cpuset, &src->cpuset); |
| 102 | |
| 103 | #elif defined(CPUSET_USE_FREEBSD_CPUSET) |
| 104 | CPU_COPY(&src->cpuset, &dst->cpuset); |
| 105 | |
| 106 | #elif defined(CPUSET_USE_ULONG) |
| 107 | dst->cpuset = src->cpuset; |
| 108 | #endif |
| 109 | } |
| 110 | |
| 111 | int ha_cpuset_size() |
| 112 | { |
| 113 | #if defined(CPUSET_USE_CPUSET) |
| 114 | return CPU_SETSIZE; |
| 115 | |
| 116 | #elif defined(CPUSET_USE_FREEBSD_CPUSET) |
| 117 | return MAXCPU; |
| 118 | |
| 119 | #elif defined(CPUSET_USE_ULONG) |
| 120 | return LONGBITS; |
| 121 | |
| 122 | #endif |
| 123 | } |