blob: 4e1c537620a718fd072067858d6628ad2538f522 [file] [log] [blame]
Amaury Denoyellef75c6402021-04-14 15:03:51 +02001#define _GNU_SOURCE
2#include <sched.h>
3
4#include <haproxy/compat.h>
5#include <haproxy/cpuset.h>
6#include <haproxy/intops.h>
7
Willy Tarreau5b093412022-07-08 09:38:30 +02008struct cpu_map cpu_map[MAX_TGROUPS];
Amaury Denoyellefc6ac532021-04-27 10:46:36 +02009
Amaury Denoyellef75c6402021-04-14 15:03:51 +020010void 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
20int 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
35int 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
Willy Tarreaua65b4932022-01-28 09:10:52 +010050void ha_cpuset_and(struct hap_cpuset *dst, struct hap_cpuset *src)
Amaury Denoyellef75c6402021-04-14 15:03:51 +020051{
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
63int 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
73int 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
Willy Tarreaua65b4932022-01-28 09:10:52 +010097void ha_cpuset_assign(struct hap_cpuset *dst, struct hap_cpuset *src)
Amaury Denoyellef75c6402021-04-14 15:03:51 +020098{
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
111int ha_cpuset_size()
112{
Amaury Denoyelle697cfde2021-10-15 15:22:31 +0200113#if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET)
Amaury Denoyellef75c6402021-04-14 15:03:51 +0200114 return CPU_SETSIZE;
115
Amaury Denoyellef75c6402021-04-14 15:03:51 +0200116#elif defined(CPUSET_USE_ULONG)
117 return LONGBITS;
118
119#endif
120}
Willy Tarreaub8ee9312023-07-18 17:14:10 +0200121
122/* Returns true if at least one cpu-map directive was configured, otherwise
123 * false.
124 */
125int cpu_map_configured(void)
126{
127 int grp, thr;
128
129 for (grp = 0; grp < MAX_TGROUPS; grp++) {
Willy Tarreaub8ee9312023-07-18 17:14:10 +0200130 for (thr = 0; thr < MAX_THREADS_PER_GROUP; thr++)
131 if (ha_cpuset_count(&cpu_map[grp].thread[thr]))
132 return 1;
133 }
134 return 0;
135}