Coverage Report

Created: 2025-06-13 06:36

/src/util-linux/include/cpuset.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SPDX-License-Identifier: LGPL-2.1-or-later
3
 *
4
 * This file may be redistributed under the terms of the
5
 * GNU Lesser General Public License.
6
 */
7
#ifndef UTIL_LINUX_CPUSET_H
8
#define UTIL_LINUX_CPUSET_H
9
10
#include <sched.h>
11
12
/*
13
 * Fallback for old or obscure libcs without dynamically allocated cpusets
14
 *
15
 * The following macros are based on code from glibc.
16
 *
17
 * The GNU C Library is free software; you can redistribute it and/or
18
 * modify it under the terms of the GNU Lesser General Public
19
 * License as published by the Free Software Foundation; either
20
 * version 2.1 of the License, or (at your option) any later version.
21
 */
22
#if !HAVE_DECL_CPU_ALLOC
23
24
# define CPU_ZERO_S(setsize, cpusetp) \
25
  do {                        \
26
    size_t __i;                     \
27
    size_t __imax = (setsize) / sizeof (__cpu_mask);            \
28
    __cpu_mask *__bits = (cpusetp)->__bits;             \
29
    for (__i = 0; __i < __imax; ++__i)                \
30
      __bits[__i] = 0;                    \
31
  } while (0)
32
33
# define CPU_SET_S(cpu, setsize, cpusetp) \
34
   ({ size_t __cpu = (cpu);                 \
35
      __cpu < 8 * (setsize)                 \
36
      ? (((__cpu_mask *) ((cpusetp)->__bits))[__CPUELT (__cpu)]         \
37
   |= __CPUMASK (__cpu))                  \
38
      : 0; })
39
40
# define CPU_ISSET_S(cpu, setsize, cpusetp) \
41
   ({ size_t __cpu = (cpu);                 \
42
      __cpu < 8 * (setsize)                 \
43
      ? ((((__cpu_mask *) ((cpusetp)->__bits))[__CPUELT (__cpu)]        \
44
    & __CPUMASK (__cpu))) != 0                \
45
      : 0; })
46
47
# define CPU_EQUAL_S(setsize, cpusetp1, cpusetp2) \
48
   ({ __cpu_mask *__arr1 = (cpusetp1)->__bits;              \
49
      __cpu_mask *__arr2 = (cpusetp2)->__bits;              \
50
      size_t __imax = (setsize) / sizeof (__cpu_mask);            \
51
      size_t __i;                   \
52
      for (__i = 0; __i < __imax; ++__i)              \
53
  if (__arr1[__i] != __arr2[__i])               \
54
    break;                    \
55
      __i == __imax; })
56
57
extern int __cpuset_count_s(size_t setsize, const cpu_set_t *set);
58
# define CPU_COUNT_S(setsize, cpusetp)  __cpuset_count_s(setsize, cpusetp)
59
60
# define CPU_ALLOC_SIZE(count) \
61
    ((((count) + __NCPUBITS - 1) / __NCPUBITS) * sizeof (__cpu_mask))
62
# define CPU_ALLOC(count) (malloc(CPU_ALLOC_SIZE(count)))
63
# define CPU_FREE(cpuset) (free(cpuset))
64
65
#endif /* !HAVE_DECL_CPU_ALLOC */
66
67
68
0
#define cpuset_nbits(setsize) (8 * (setsize))
69
70
/*
71
 * The @idx parameter returns an index of the first mask from @ary array where
72
 * the @cpu is set.
73
 *
74
 * Returns: 0 if found, otherwise 1.
75
 */
76
static inline int cpuset_ary_isset(size_t cpu, cpu_set_t **ary, size_t nmemb,
77
           size_t setsize, size_t *idx)
78
0
{
79
0
  size_t i;
80
0
81
0
  for (i = 0; i < nmemb; i++) {
82
0
    if (CPU_ISSET_S(cpu, setsize, ary[i])) {
83
0
      *idx = i;
84
0
      return 0;
85
0
    }
86
0
  }
87
0
  return 1;
88
0
}
Unexecuted instantiation: probe.c:cpuset_ary_isset
Unexecuted instantiation: verify.c:cpuset_ary_isset
Unexecuted instantiation: partitions.c:cpuset_ary_isset
Unexecuted instantiation: sysfs.c:cpuset_ary_isset
Unexecuted instantiation: path.c:cpuset_ary_isset
Unexecuted instantiation: devname.c:cpuset_ary_isset
Unexecuted instantiation: devno.c:cpuset_ary_isset
Unexecuted instantiation: cpuset.c:cpuset_ary_isset
89
90
extern int get_max_number_of_cpus(void);
91
92
extern cpu_set_t *cpuset_alloc(int ncpus, size_t *setsize, size_t *nbits);
93
extern void cpuset_free(cpu_set_t *set);
94
95
extern char *cpulist_create(char *str, size_t len, cpu_set_t *set, size_t setsize);
96
extern int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail);
97
98
extern char *cpumask_create(char *str, size_t len, cpu_set_t *set, size_t setsize);
99
extern int cpumask_parse(const char *str, cpu_set_t *set, size_t setsize);
100
101
#endif /* UTIL_LINUX_CPUSET_H */