EM-ODP 4.4.0
Event Machine on ODP
Loading...
Searching...
No Matches
event_machine_core_mask.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015-2025, Nokia Solutions and Networks
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/**
32 * @file
33 *
34 * Event Machine core mask manipulation functions
35 */
36#ifndef _GNU_SOURCE
37#define _GNU_SOURCE
38#endif
39
40#ifdef HAVE_CONFIG_H
41#include "config.h"
42#endif
43
44#include <stdint.h>
45
46#include <odp_api.h>
47
48#include <event_machine.h>
49#include <event_machine/platform/env/environment.h> /* unlikely() */
50
52{
53 odp_cpumask_zero(&mask->odp_cpumask);
54}
55
56void em_core_mask_set(int core, em_core_mask_t *mask)
57{
58 odp_cpumask_set(&mask->odp_cpumask, core);
59}
60
61void em_core_mask_clr(int core, em_core_mask_t *mask)
62{
63 odp_cpumask_clr(&mask->odp_cpumask, core);
64}
65
66int em_core_mask_isset(int core, const em_core_mask_t *mask)
67{
68 return odp_cpumask_isset(&mask->odp_cpumask, core);
69}
70
72{
73 odp_cpumask_t zero_mask;
74
75 odp_cpumask_zero(&zero_mask);
76 return odp_cpumask_equal(&zero_mask, &mask->odp_cpumask);
77}
78
79int em_core_mask_equal(const em_core_mask_t *mask1, const em_core_mask_t *mask2)
80{
81 return odp_cpumask_equal(&mask1->odp_cpumask, &mask2->odp_cpumask);
82}
83
85{
86 for (int i = 0; i < count; i++)
87 odp_cpumask_set(&mask->odp_cpumask, i);
88}
89
91{
92 odp_cpumask_copy(&dst->odp_cpumask, &src->odp_cpumask);
93}
94
96{
97 return odp_cpumask_count(&mask->odp_cpumask);
98}
99
100void em_core_mask_set_bits(const uint64_t bits[], int len, em_core_mask_t *mask)
101{
102 const int maxlen = (ODP_CPUMASK_SIZE + 63) / 64;
103 const int maxcpu = ODP_CPUMASK_SIZE - 1;
104 int cpu;
105
106 len = len > maxlen ? maxlen : len;
107
108 for (int i = 0; i < len; i++) {
109 uint64_t mask64 = bits[i];
110
111 for (int j = 0; mask64 && j < 64; j++) {
112 cpu = i * 64 + j;
113 if (unlikely(cpu > maxcpu))
114 return;
115 if (mask64 & ((uint64_t)1 << j)) {
116 odp_cpumask_set(&mask->odp_cpumask, cpu);
117 mask64 &= mask64 - 1; /*clear lowest set bit*/
118 }
119 }
120 }
121}
122
123int em_core_mask_bits(uint64_t bits[/*out*/], int len,
124 const em_core_mask_t *mask)
125{
126 int u64s_set; /* return value */
127 int maxcpu = ODP_CPUMASK_SIZE - 1;
128 int i;
129 int j;
130 int cpu;
131
132 if (unlikely(len < 1))
133 return 0;
134
135 if (maxcpu >= len * 64)
136 maxcpu = len * 64 - 1;
137
138 /* zero out the bits[] array*/
139 for (i = 0; i < len; i++)
140 bits[i] = 0;
141
142 i = -1;
143 cpu = odp_cpumask_first(&mask->odp_cpumask);
144 while (cpu >= 0 && cpu <= maxcpu) {
145 i = cpu / 64;
146 j = cpu % 64;
147 bits[i] |= (uint64_t)1 << j;
148 cpu = odp_cpumask_next(&mask->odp_cpumask, cpu);
149 }
150 u64s_set = i + 1; /* >= 0 */
151 return u64s_set;
152}
153
154int em_core_mask_set_str(const char *mask_str, em_core_mask_t *mask)
155{
156 odp_cpumask_t str_mask;
157
158 odp_cpumask_from_str(&str_mask, mask_str);
159 odp_cpumask_or(&mask->odp_cpumask, &mask->odp_cpumask, &str_mask);
160
161 return 0;
162}
163
164void em_core_mask_tostr(char *mask_str, int len, const em_core_mask_t *mask)
165{
166 int32_t ret = odp_cpumask_to_str(&mask->odp_cpumask, mask_str, len);
167
168 if (unlikely(ret <= 0 && len > 0))
169 mask_str[0] = '\0';
170}
171
172int em_core_mask_idx(int n, const em_core_mask_t *mask)
173{
174 if (unlikely((unsigned int)(n - 1) >= EM_MAX_CORES))
175 return -1;
176
177 int i = 1;
178 int cpu = odp_cpumask_first(&mask->odp_cpumask);
179
180 while (cpu >= 0 && i < n) {
181 cpu = odp_cpumask_next(&mask->odp_cpumask, cpu);
182 i++;
183 }
184
185 /* cpu >=0 only if odp_cpumask_first/next successful */
186 return cpu;
187}
188
190{
191 return odp_cpumask_first(&mask->odp_cpumask);
192}
193
195{
196 return odp_cpumask_last(&mask->odp_cpumask);
197}
198
199int em_core_mask_next(const em_core_mask_t *mask, int idx)
200{
201 return odp_cpumask_next(&mask->odp_cpumask, idx);
202}
203
205 const em_core_mask_t *src2)
206{
207 odp_cpumask_and(&dst->odp_cpumask,
208 &src1->odp_cpumask, &src2->odp_cpumask);
209}
210
212 const em_core_mask_t *src2)
213{
214 odp_cpumask_or(&dst->odp_cpumask,
215 &src1->odp_cpumask, &src2->odp_cpumask);
216}
217
219 const em_core_mask_t *src2)
220{
221 odp_cpumask_xor(&dst->odp_cpumask,
222 &src1->odp_cpumask, &src2->odp_cpumask);
223}
#define EM_MAX_CORES
void em_core_mask_or(em_core_mask_t *dst, const em_core_mask_t *src1, const em_core_mask_t *src2)
int em_core_mask_equal(const em_core_mask_t *mask1, const em_core_mask_t *mask2)
int em_core_mask_count(const em_core_mask_t *mask)
void em_core_mask_tostr(char *mask_str, int len, const em_core_mask_t *mask)
int em_core_mask_first(const em_core_mask_t *mask)
int em_core_mask_iszero(const em_core_mask_t *mask)
void em_core_mask_copy(em_core_mask_t *dst, const em_core_mask_t *src)
int em_core_mask_set_str(const char *mask_str, em_core_mask_t *mask)
void em_core_mask_set(int core, em_core_mask_t *mask)
void em_core_mask_clr(int core, em_core_mask_t *mask)
int em_core_mask_idx(int n, const em_core_mask_t *mask)
int em_core_mask_bits(uint64_t bits[], int len, const em_core_mask_t *mask)
int em_core_mask_isset(int core, const em_core_mask_t *mask)
void em_core_mask_set_count(int count, em_core_mask_t *mask)
int em_core_mask_last(const em_core_mask_t *mask)
void em_core_mask_and(em_core_mask_t *dst, const em_core_mask_t *src1, const em_core_mask_t *src2)
void em_core_mask_set_bits(const uint64_t bits[], int len, em_core_mask_t *mask)
int em_core_mask_next(const em_core_mask_t *mask, int idx)
void em_core_mask_zero(em_core_mask_t *mask)
void em_core_mask_xor(em_core_mask_t *dst, const em_core_mask_t *src1, const em_core_mask_t *src2)