EM-ODP  3.7.0
Event Machine on ODP
event_machine_hw_specific.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2023, 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 HW specific functions and other additions.
35  *
36  */
37 #include "em_include.h"
38 
39 /*
40  * core mask manipulation prototypes or inlined functions
41  */
42 
44 {
45  odp_cpumask_zero(&mask->odp_cpumask);
46 }
47 
48 void em_core_mask_set(int core, em_core_mask_t *mask)
49 {
50  odp_cpumask_set(&mask->odp_cpumask, core);
51 }
52 
53 void em_core_mask_clr(int core, em_core_mask_t *mask)
54 {
55  odp_cpumask_clr(&mask->odp_cpumask, core);
56 }
57 
58 int em_core_mask_isset(int core, const em_core_mask_t *mask)
59 {
60  return odp_cpumask_isset(&mask->odp_cpumask, core);
61 }
62 
64 {
65  odp_cpumask_t zero_mask;
66 
67  odp_cpumask_zero(&zero_mask);
68  return odp_cpumask_equal(&zero_mask, &mask->odp_cpumask);
69 }
70 
71 int em_core_mask_equal(const em_core_mask_t *mask1, const em_core_mask_t *mask2)
72 {
73  return odp_cpumask_equal(&mask1->odp_cpumask, &mask2->odp_cpumask);
74 }
75 
76 void em_core_mask_set_count(int count, em_core_mask_t *mask)
77 {
78  for (int i = 0; i < count; i++)
79  odp_cpumask_set(&mask->odp_cpumask, i);
80 }
81 
83 {
84  odp_cpumask_copy(&dst->odp_cpumask, &src->odp_cpumask);
85 }
86 
88 {
89  return odp_cpumask_count(&mask->odp_cpumask);
90 }
91 
92 void em_core_mask_set_bits(const uint64_t bits[], int len, em_core_mask_t *mask)
93 {
94  const int maxlen = (ODP_CPUMASK_SIZE + 63) / 64;
95  const int maxcpu = ODP_CPUMASK_SIZE - 1;
96  int cpu;
97 
98  len = len > maxlen ? maxlen : len;
99 
100  for (int i = 0; i < len; i++) {
101  uint64_t mask64 = bits[i];
102 
103  for (int j = 0; mask64 && j < 64; j++) {
104  cpu = i * 64 + j;
105  if (unlikely(cpu > maxcpu))
106  return;
107  if (mask64 & ((uint64_t)1 << j)) {
108  odp_cpumask_set(&mask->odp_cpumask, cpu);
109  mask64 &= mask64 - 1; /*clear lowest set bit*/
110  }
111  }
112  }
113 }
114 
115 int em_core_mask_get_bits(uint64_t bits[/*out*/], int len,
116  const em_core_mask_t *mask)
117 {
118  int u64s_set; /* return value */
119  int maxcpu = ODP_CPUMASK_SIZE - 1;
120  int i;
121  int j;
122  int cpu;
123 
124  if (unlikely(len < 1))
125  return 0;
126 
127  if (maxcpu >= len * 64)
128  maxcpu = len * 64 - 1;
129 
130  /* zero out the bits[] array*/
131  for (i = 0; i < len; i++)
132  bits[i] = 0;
133 
134  i = -1;
135  cpu = odp_cpumask_first(&mask->odp_cpumask);
136  while (cpu >= 0 && cpu <= maxcpu) {
137  i = cpu / 64;
138  j = cpu % 64;
139  bits[i] |= (uint64_t)1 << j;
140  cpu = odp_cpumask_next(&mask->odp_cpumask, cpu);
141  }
142  u64s_set = i + 1; /* >= 0 */
143  return u64s_set;
144 }
145 
146 int em_core_mask_set_str(const char *mask_str, em_core_mask_t *mask)
147 {
148  odp_cpumask_t str_mask;
149 
150  odp_cpumask_from_str(&str_mask, mask_str);
151  odp_cpumask_or(&mask->odp_cpumask, &mask->odp_cpumask, &str_mask);
152 
153  return 0;
154 }
155 
156 void em_core_mask_tostr(char *mask_str, int len, const em_core_mask_t *mask)
157 {
158  int32_t ret = odp_cpumask_to_str(&mask->odp_cpumask, mask_str, len);
159 
160  if (unlikely(ret <= 0 && len > 0))
161  mask_str[0] = '\0';
162 }
163 
164 int em_core_mask_idx(int n, const em_core_mask_t *mask)
165 {
166  if (unlikely((unsigned int)(n - 1) >= EM_MAX_CORES))
167  return -1;
168 
169  int i = 1;
170  int cpu = odp_cpumask_first(&mask->odp_cpumask);
171 
172  while (cpu >= 0 && i < n) {
173  cpu = odp_cpumask_next(&mask->odp_cpumask, cpu);
174  i++;
175  }
176 
177  /* cpu >=0 only if odp_cpumask_first/next successful */
178  return cpu;
179 }
180 
182  const em_core_mask_t *src2)
183 {
184  odp_cpumask_and(&dst->odp_cpumask,
185  &src1->odp_cpumask, &src2->odp_cpumask);
186 }
187 
189  const em_core_mask_t *src2)
190 {
191  odp_cpumask_or(&dst->odp_cpumask,
192  &src1->odp_cpumask, &src2->odp_cpumask);
193 }
194 
196  const em_core_mask_t *src2)
197 {
198  odp_cpumask_xor(&dst->odp_cpumask,
199  &src1->odp_cpumask, &src2->odp_cpumask);
200 }
em_core_mask_set_bits
void em_core_mask_set_bits(const uint64_t bits[], int len, em_core_mask_t *mask)
Definition: event_machine_hw_specific.c:92
em_core_mask_idx
int em_core_mask_idx(int n, const em_core_mask_t *mask)
Definition: event_machine_hw_specific.c:164
em_core_mask_isset
int em_core_mask_isset(int core, const em_core_mask_t *mask)
Definition: event_machine_hw_specific.c:58
em_core_mask_count
int em_core_mask_count(const em_core_mask_t *mask)
Definition: event_machine_hw_specific.c:87
em_core_mask_set_count
void em_core_mask_set_count(int count, em_core_mask_t *mask)
Definition: event_machine_hw_specific.c:76
em_core_mask_copy
void em_core_mask_copy(em_core_mask_t *dst, const em_core_mask_t *src)
Definition: event_machine_hw_specific.c:82
em_core_mask_t
Definition: event_machine_hw_types.h:242
em_core_mask_or
void em_core_mask_or(em_core_mask_t *dst, const em_core_mask_t *src1, const em_core_mask_t *src2)
Definition: event_machine_hw_specific.c:188
em_core_mask_set_str
int em_core_mask_set_str(const char *mask_str, em_core_mask_t *mask)
Definition: event_machine_hw_specific.c:146
em_core_mask_zero
void em_core_mask_zero(em_core_mask_t *mask)
Definition: event_machine_hw_specific.c:43
em_core_mask_tostr
void em_core_mask_tostr(char *mask_str, int len, const em_core_mask_t *mask)
Definition: event_machine_hw_specific.c:156
em_include.h
em_core_mask_set
void em_core_mask_set(int core, em_core_mask_t *mask)
Definition: event_machine_hw_specific.c:48
em_core_mask_equal
int em_core_mask_equal(const em_core_mask_t *mask1, const em_core_mask_t *mask2)
Definition: event_machine_hw_specific.c:71
em_core_mask_clr
void em_core_mask_clr(int core, em_core_mask_t *mask)
Definition: event_machine_hw_specific.c:53
em_core_mask_xor
void em_core_mask_xor(em_core_mask_t *dst, const em_core_mask_t *src1, const em_core_mask_t *src2)
Definition: event_machine_hw_specific.c:195
em_core_mask_get_bits
int em_core_mask_get_bits(uint64_t bits[], int len, const em_core_mask_t *mask)
Definition: event_machine_hw_specific.c:115
em_core_mask_iszero
int em_core_mask_iszero(const em_core_mask_t *mask)
Definition: event_machine_hw_specific.c:63
em_core_mask_and
void em_core_mask_and(em_core_mask_t *dst, const em_core_mask_t *src1, const em_core_mask_t *src2)
Definition: event_machine_hw_specific.c:181