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
8 struct cpu_map cpu_map;
9
ha_cpuset_zero(struct hap_cpuset * set)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
ha_cpuset_set(struct hap_cpuset * set,int cpu)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
ha_cpuset_clr(struct hap_cpuset * set,int cpu)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
ha_cpuset_and(struct hap_cpuset * dst,const struct hap_cpuset * src)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 #if defined(CPU_ALLOC)
57 CPU_AND(&dst->cpuset, &dst->cpuset, &src->cpuset);
58 #else
59 CPU_AND(&dst->cpuset, &src->cpuset);
60 #endif
61
62 #elif defined(CPUSET_USE_ULONG)
63 dst->cpuset &= src->cpuset;
64 #endif
65 }
66
ha_cpuset_count(const struct hap_cpuset * set)67 int ha_cpuset_count(const struct hap_cpuset *set)
68 {
69 #if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET)
70 return CPU_COUNT(&set->cpuset);
71
72 #elif defined(CPUSET_USE_ULONG)
73 return my_popcountl(set->cpuset);
74 #endif
75 }
76
ha_cpuset_ffs(const struct hap_cpuset * set)77 int ha_cpuset_ffs(const struct hap_cpuset *set)
78 {
79 #if defined(CPUSET_USE_CPUSET)
80 int n;
81
82 if (!CPU_COUNT(&set->cpuset))
83 return 0;
84
85 for (n = 0; !CPU_ISSET(n, &set->cpuset); ++n)
86 ;
87
88 return n + 1;
89
90 #elif defined(CPUSET_USE_FREEBSD_CPUSET)
91 return CPU_FFS(&set->cpuset);
92
93 #elif defined(CPUSET_USE_ULONG)
94 if (!set->cpuset)
95 return 0;
96
97 return my_ffsl(set->cpuset);
98 #endif
99 }
100
ha_cpuset_assign(struct hap_cpuset * dst,const struct hap_cpuset * src)101 void ha_cpuset_assign(struct hap_cpuset *dst, const struct hap_cpuset *src)
102 {
103 #if defined(CPUSET_USE_CPUSET)
104 CPU_ZERO(&dst->cpuset);
105 CPU_OR(&dst->cpuset, &dst->cpuset, &src->cpuset);
106
107 #elif defined(CPUSET_USE_FREEBSD_CPUSET)
108 CPU_COPY(&src->cpuset, &dst->cpuset);
109
110 #elif defined(CPUSET_USE_ULONG)
111 dst->cpuset = src->cpuset;
112 #endif
113 }
114
ha_cpuset_size()115 int ha_cpuset_size()
116 {
117 #if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET)
118 return CPU_SETSIZE;
119
120 #elif defined(CPUSET_USE_ULONG)
121 return LONGBITS;
122
123 #endif
124 }
125