xref: /spdk/include/spdk/cpuset.h (revision cdeecfb8d22e5a40290b01972ba74744b2120a29)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2017 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 /**
7  * \file
8  * CPU set management functions
9  */
10 
11 #ifndef SPDK_CPUSET_H
12 #define SPDK_CPUSET_H
13 
14 #include "spdk/stdinc.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 #define SPDK_CPUSET_SIZE 1024
21 
22 /**
23  * List of CPUs.
24  */
25 struct spdk_cpuset {
26 	char str[SPDK_CPUSET_SIZE / 4 + 1];
27 	uint8_t cpus[SPDK_CPUSET_SIZE / 8];
28 };
29 
30 /**
31  * Allocate CPU set object.
32  *
33  * \return a pointer to the allocated zeroed cpuset on success, or NULL on failure.
34  */
35 struct spdk_cpuset *spdk_cpuset_alloc(void);
36 
37 /**
38  * Free allocated CPU set.
39  *
40  * \param set CPU set to be freed.
41  */
42 void spdk_cpuset_free(struct spdk_cpuset *set);
43 
44 /**
45  * Compare two CPU sets.
46  *
47  * \param set1 CPU set1.
48  * \param set2 CPU set2.
49  *
50  * \return true if both CPU sets are equal.
51  */
52 bool spdk_cpuset_equal(const struct spdk_cpuset *set1, const struct spdk_cpuset *set2);
53 
54 /**
55  * Copy the content of CPU set to another.
56  *
57  * \param dst Destination CPU set
58  * \param src Source CPU set
59  */
60 void spdk_cpuset_copy(struct spdk_cpuset *dst, const struct spdk_cpuset *src);
61 
62 /**
63  * Perform AND operation on two CPU sets. The result is stored in dst.
64  *
65  * \param dst First argument of operation. This value also stores the result of operation.
66  * \param src Second argument of operation.
67  */
68 void spdk_cpuset_and(struct spdk_cpuset *dst, const struct spdk_cpuset *src);
69 
70 /**
71  * Perform OR operation on two CPU sets. The result is stored in dst.
72  *
73  * \param dst First argument of operation. This value also stores the result of operation.
74  * \param src Second argument of operation.
75  */
76 void spdk_cpuset_or(struct spdk_cpuset *dst, const struct spdk_cpuset *src);
77 
78 /**
79  * Perform XOR operation on two CPU sets. The result is stored in dst.
80  *
81  * \param dst First argument of operation. This value also stores the result of operation.
82  * \param src Second argument of operation.
83  */
84 void spdk_cpuset_xor(struct spdk_cpuset *dst, const struct spdk_cpuset *src);
85 
86 /**
87  * Negate all CPUs in CPU set.
88  *
89  * \param set CPU set to be negated. This value also stores the result of operation.
90  */
91 void spdk_cpuset_negate(struct spdk_cpuset *set);
92 
93 /**
94  * Clear all CPUs in CPU set.
95  *
96  * \param set CPU set to be cleared.
97  */
98 void spdk_cpuset_zero(struct spdk_cpuset *set);
99 
100 /**
101  * Set or clear CPU state in CPU set.
102  *
103  * \param set CPU set object.
104  * \param cpu CPU index to be set or cleared.
105  * \param state *true* to set cpu, *false* to clear.
106  */
107 void spdk_cpuset_set_cpu(struct spdk_cpuset *set, uint32_t cpu, bool state);
108 
109 /**
110  * Get the state of CPU in CPU set.
111  *
112  * \param set CPU set object.
113  * \param cpu CPU index.
114  *
115  * \return the state of selected CPU.
116  */
117 bool spdk_cpuset_get_cpu(const struct spdk_cpuset *set, uint32_t cpu);
118 
119 /** Call the specified function for each set cpu in the specified cpuset.
120  *
121  * \param set The cpuset to iterate
122  * \param fn The function to call for each set cpu
123  * \param ctx Context pointer to pass to fn
124  */
125 void spdk_cpuset_for_each_cpu(const struct spdk_cpuset *set,
126 			      void (*fn)(void *ctx, uint32_t cpu), void *ctx);
127 
128 /**
129  * Get the number of CPUs that are set in CPU set.
130  *
131  * \param set CPU set object.
132  *
133  * \return the number of CPUs.
134  */
135 uint32_t spdk_cpuset_count(const struct spdk_cpuset *set);
136 
137 /**
138  * Convert a CPU set to hex string.
139  *
140  * \param set CPU set.
141  *
142  * \return a pointer to hexadecimal representation of CPU set. Buffer to store a
143  * string is dynamically allocated internally and freed with CPU set object.
144  * Memory returned by this function might be changed after subsequent calls to
145  * this function so string should be copied by user.
146  */
147 const char *spdk_cpuset_fmt(struct spdk_cpuset *set);
148 
149 /**
150  * Convert a string containing a CPU core mask into a CPU set.
151  *
152  * \param set CPU set.
153  * \param mask String defining CPU set. By default hexadecimal value is used or
154  * as CPU list enclosed in square brackets defined as: 'c1[-c2][,c3[-c4],...]'.
155  * When hexadecimal value is passed, any commas will be ignored, to allow using
156  * this function with masks generated by Linux kernel in sysfs.
157  *
158  * \return zero if success, non zero if fails.
159  */
160 int spdk_cpuset_parse(struct spdk_cpuset *set, const char *mask);
161 
162 #ifdef __cplusplus
163 }
164 #endif
165 #endif /* SPDK_CPUSET_H */
166