linux/include/linux/cpumask.h
<<
>>
Prefs
   1#ifndef __LINUX_CPUMASK_H
   2#define __LINUX_CPUMASK_H
   3
   4/*
   5 * Cpumasks provide a bitmap suitable for representing the
   6 * set of CPU's in a system, one bit position per CPU number.
   7 *
   8 * See detailed comments in the file linux/bitmap.h describing the
   9 * data type on which these cpumasks are based.
  10 *
  11 * For details of cpumask_scnprintf() and cpumask_parse(),
  12 * see bitmap_scnprintf() and bitmap_parse() in lib/bitmap.c.
  13 *
  14 * The available cpumask operations are:
  15 *
  16 * void cpu_set(cpu, mask)              turn on bit 'cpu' in mask
  17 * void cpu_clear(cpu, mask)            turn off bit 'cpu' in mask
  18 * void cpus_setall(mask)               set all bits
  19 * void cpus_clear(mask)                clear all bits
  20 * int cpu_isset(cpu, mask)             true iff bit 'cpu' set in mask
  21 * int cpu_test_and_set(cpu, mask)      test and set bit 'cpu' in mask
  22 *
  23 * void cpus_and(dst, src1, src2)       dst = src1 & src2  [intersection]
  24 * void cpus_or(dst, src1, src2)        dst = src1 | src2  [union]
  25 * void cpus_xor(dst, src1, src2)       dst = src1 ^ src2
  26 * void cpus_andnot(dst, src1, src2)    dst = src1 & ~src2
  27 * void cpus_complement(dst, src)       dst = ~src
  28 *
  29 * int cpus_equal(mask1, mask2)         Does mask1 == mask2?
  30 * int cpus_intersects(mask1, mask2)    Do mask1 and mask2 intersect?
  31 * int cpus_subset(mask1, mask2)        Is mask1 a subset of mask2?
  32 * int cpus_empty(mask)                 Is mask empty (no bits sets)?
  33 * int cpus_full(mask)                  Is mask full (all bits sets)?
  34 * int cpus_weight(mask)                Hamming weigh - number of set bits
  35 *
  36 * void cpus_shift_right(dst, src, n)   Shift right
  37 * void cpus_shift_left(dst, src, n)    Shift left
  38 *
  39 * int first_cpu(mask)                  Number lowest set bit, or NR_CPUS
  40 * int next_cpu(cpu, mask)              Next cpu past 'cpu', or NR_CPUS
  41 *
  42 * cpumask_t cpumask_of_cpu(cpu)        Return cpumask with bit 'cpu' set
  43 * CPU_MASK_ALL                         Initializer - all bits set
  44 * CPU_MASK_NONE                        Initializer - no bits set
  45 * unsigned long *cpus_addr(mask)       Array of unsigned long's in mask
  46 *
  47 * int cpumask_scnprintf(buf, len, mask) Format cpumask for printing
  48 * int cpumask_parse(ubuf, ulen, mask)  Parse ascii string as cpumask
  49 *
  50 * for_each_cpu_mask(cpu, mask)         for-loop cpu over mask
  51 *
  52 * int num_online_cpus()                Number of online CPUs
  53 * int num_possible_cpus()              Number of all possible CPUs
  54 * int num_present_cpus()               Number of present CPUs
  55 *
  56 * int cpu_online(cpu)                  Is some cpu online?
  57 * int cpu_possible(cpu)                Is some cpu possible?
  58 * int cpu_present(cpu)                 Is some cpu present (can schedule)?
  59 *
  60 * int any_online_cpu(mask)             First online cpu in mask
  61 *
  62 * for_each_cpu(cpu)                    for-loop cpu over cpu_possible_map
  63 * for_each_online_cpu(cpu)             for-loop cpu over cpu_online_map
  64 * for_each_present_cpu(cpu)            for-loop cpu over cpu_present_map
  65 *
  66 * Subtlety:
  67 * 1) The 'type-checked' form of cpu_isset() causes gcc (3.3.2, anyway)
  68 *    to generate slightly worse code.  Note for example the additional
  69 *    40 lines of assembly code compiling the "for each possible cpu"
  70 *    loops buried in the disk_stat_read() macros calls when compiling
  71 *    drivers/block/genhd.c (arch i386, CONFIG_SMP=y).  So use a simple
  72 *    one-line #define for cpu_isset(), instead of wrapping an inline
  73 *    inside a macro, the way we do the other calls.
  74 */
  75
  76#include <linux/kernel.h>
  77#include <linux/threads.h>
  78#include <linux/bitmap.h>
  79#include <asm/bug.h>
  80
  81typedef struct { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;
  82extern cpumask_t _unused_cpumask_arg_;
  83
  84#define cpu_set(cpu, dst) __cpu_set((cpu), &(dst))
  85static inline void __cpu_set(int cpu, volatile cpumask_t *dstp)
  86{
  87        set_bit(cpu, dstp->bits);
  88}
  89
  90#define cpu_clear(cpu, dst) __cpu_clear((cpu), &(dst))
  91static inline void __cpu_clear(int cpu, volatile cpumask_t *dstp)
  92{
  93        clear_bit(cpu, dstp->bits);
  94}
  95
  96#define cpus_setall(dst) __cpus_setall(&(dst), NR_CPUS)
  97static inline void __cpus_setall(cpumask_t *dstp, int nbits)
  98{
  99        bitmap_fill(dstp->bits, nbits);
 100}
 101
 102#define cpus_clear(dst) __cpus_clear(&(dst), NR_CPUS)
 103static inline void __cpus_clear(cpumask_t *dstp, int nbits)
 104{
 105        bitmap_zero(dstp->bits, nbits);
 106}
 107
 108/* No static inline type checking - see Subtlety (1) above. */
 109#define cpu_isset(cpu, cpumask) test_bit((cpu), (cpumask).bits)
 110
 111#define cpu_test_and_set(cpu, cpumask) __cpu_test_and_set((cpu), &(cpumask))
 112static inline int __cpu_test_and_set(int cpu, cpumask_t *addr)
 113{
 114        return test_and_set_bit(cpu, addr->bits);
 115}
 116
 117#define cpus_and(dst, src1, src2) __cpus_and(&(dst), &(src1), &(src2), NR_CPUS)
 118static inline void __cpus_and(cpumask_t *dstp, const cpumask_t *src1p,
 119                                        const cpumask_t *src2p, int nbits)
 120{
 121        bitmap_and(dstp->bits, src1p->bits, src2p->bits, nbits);
 122}
 123
 124#define cpus_or(dst, src1, src2) __cpus_or(&(dst), &(src1), &(src2), NR_CPUS)
 125static inline void __cpus_or(cpumask_t *dstp, const cpumask_t *src1p,
 126                                        const cpumask_t *src2p, int nbits)
 127{
 128        bitmap_or(dstp->bits, src1p->bits, src2p->bits, nbits);
 129}
 130
 131#define cpus_xor(dst, src1, src2) __cpus_xor(&(dst), &(src1), &(src2), NR_CPUS)
 132static inline void __cpus_xor(cpumask_t *dstp, const cpumask_t *src1p,
 133                                        const cpumask_t *src2p, int nbits)
 134{
 135        bitmap_xor(dstp->bits, src1p->bits, src2p->bits, nbits);
 136}
 137
 138#define cpus_andnot(dst, src1, src2) \
 139                                __cpus_andnot(&(dst), &(src1), &(src2), NR_CPUS)
 140static inline void __cpus_andnot(cpumask_t *dstp, const cpumask_t *src1p,
 141                                        const cpumask_t *src2p, int nbits)
 142{
 143        bitmap_andnot(dstp->bits, src1p->bits, src2p->bits, nbits);
 144}
 145
 146#define cpus_complement(dst, src) __cpus_complement(&(dst), &(src), NR_CPUS)
 147static inline void __cpus_complement(cpumask_t *dstp,
 148                                        const cpumask_t *srcp, int nbits)
 149{
 150        bitmap_complement(dstp->bits, srcp->bits, nbits);
 151}
 152
 153#define cpus_equal(src1, src2) __cpus_equal(&(src1), &(src2), NR_CPUS)
 154static inline int __cpus_equal(const cpumask_t *src1p,
 155                                        const cpumask_t *src2p, int nbits)
 156{
 157        return bitmap_equal(src1p->bits, src2p->bits, nbits);
 158}
 159
 160#define cpus_intersects(src1, src2) __cpus_intersects(&(src1), &(src2), NR_CPUS)
 161static inline int __cpus_intersects(const cpumask_t *src1p,
 162                                        const cpumask_t *src2p, int nbits)
 163{
 164        return bitmap_intersects(src1p->bits, src2p->bits, nbits);
 165}
 166
 167#define cpus_subset(src1, src2) __cpus_subset(&(src1), &(src2), NR_CPUS)
 168static inline int __cpus_subset(const cpumask_t *src1p,
 169                                        const cpumask_t *src2p, int nbits)
 170{
 171        return bitmap_subset(src1p->bits, src2p->bits, nbits);
 172}
 173
 174#define cpus_empty(src) __cpus_empty(&(src), NR_CPUS)
 175static inline int __cpus_empty(const cpumask_t *srcp, int nbits)
 176{
 177        return bitmap_empty(srcp->bits, nbits);
 178}
 179
 180#define cpus_full(cpumask) __cpus_full(&(cpumask), NR_CPUS)
 181static inline int __cpus_full(const cpumask_t *srcp, int nbits)
 182{
 183        return bitmap_full(srcp->bits, nbits);
 184}
 185
 186#define cpus_weight(cpumask) __cpus_weight(&(cpumask), NR_CPUS)
 187static inline int __cpus_weight(const cpumask_t *srcp, int nbits)
 188{
 189        return bitmap_weight(srcp->bits, nbits);
 190}
 191
 192#define cpus_shift_right(dst, src, n) \
 193                        __cpus_shift_right(&(dst), &(src), (n), NR_CPUS)
 194static inline void __cpus_shift_right(cpumask_t *dstp,
 195                                        const cpumask_t *srcp, int n, int nbits)
 196{
 197        bitmap_shift_right(dstp->bits, srcp->bits, n, nbits);
 198}
 199
 200#define cpus_shift_left(dst, src, n) \
 201                        __cpus_shift_left(&(dst), &(src), (n), NR_CPUS)
 202static inline void __cpus_shift_left(cpumask_t *dstp,
 203                                        const cpumask_t *srcp, int n, int nbits)
 204{
 205        bitmap_shift_left(dstp->bits, srcp->bits, n, nbits);
 206}
 207
 208#define first_cpu(src) __first_cpu(&(src), NR_CPUS)
 209static inline int __first_cpu(const cpumask_t *srcp, int nbits)
 210{
 211        return min_t(int, nbits, find_first_bit(srcp->bits, nbits));
 212}
 213
 214#define next_cpu(n, src) __next_cpu((n), &(src), NR_CPUS)
 215static inline int __next_cpu(int n, const cpumask_t *srcp, int nbits)
 216{
 217        return min_t(int, nbits, find_next_bit(srcp->bits, nbits, n+1));
 218}
 219
 220#define cpumask_of_cpu(cpu)                                             \
 221({                                                                      \
 222        typeof(_unused_cpumask_arg_) m;                                 \
 223        if (sizeof(m) == sizeof(unsigned long)) {                       \
 224                m.bits[0] = 1UL<<(cpu);                                 \
 225        } else {                                                        \
 226                cpus_clear(m);                                          \
 227                cpu_set((cpu), m);                                      \
 228        }                                                               \
 229        m;                                                              \
 230})
 231
 232#define CPU_MASK_LAST_WORD BITMAP_LAST_WORD_MASK(NR_CPUS)
 233
 234#if NR_CPUS <= BITS_PER_LONG
 235
 236#define CPU_MASK_ALL                                                    \
 237(cpumask_t) { {                                                         \
 238        [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD                 \
 239} }
 240
 241#else
 242
 243#define CPU_MASK_ALL                                                    \
 244(cpumask_t) { {                                                         \
 245        [0 ... BITS_TO_LONGS(NR_CPUS)-2] = ~0UL,                        \
 246        [BITS_TO_LONGS(NR_CPUS)-1] = CPU_MASK_LAST_WORD                 \
 247} }
 248
 249#endif
 250
 251#define CPU_MASK_NONE                                                   \
 252(cpumask_t) { {                                                         \
 253        [0 ... BITS_TO_LONGS(NR_CPUS)-1] =  0UL                         \
 254} }
 255
 256#define CPU_MASK_CPU0                                                   \
 257(cpumask_t) { {                                                         \
 258        [0] =  1UL                                                      \
 259} }
 260
 261#define cpus_addr(src) ((src).bits)
 262
 263#define cpumask_scnprintf(buf, len, src) \
 264                        __cpumask_scnprintf((buf), (len), &(src), NR_CPUS)
 265static inline int __cpumask_scnprintf(char *buf, int len,
 266                                        const cpumask_t *srcp, int nbits)
 267{
 268        return bitmap_scnprintf(buf, len, srcp->bits, nbits);
 269}
 270
 271#define cpumask_parse(ubuf, ulen, src) \
 272                        __cpumask_parse((ubuf), (ulen), &(src), NR_CPUS)
 273static inline int __cpumask_parse(const char __user *buf, int len,
 274                                        cpumask_t *dstp, int nbits)
 275{
 276        return bitmap_parse(buf, len, dstp->bits, nbits);
 277}
 278
 279#if NR_CPUS > 1
 280#define for_each_cpu_mask(cpu, mask)            \
 281        for ((cpu) = first_cpu(mask);           \
 282                (cpu) < NR_CPUS;                \
 283                (cpu) = next_cpu((cpu), (mask)))
 284#else /* NR_CPUS == 1 */
 285#define for_each_cpu_mask(cpu, mask) for ((cpu) = 0; (cpu) < 1; (cpu)++)
 286#endif /* NR_CPUS */
 287
 288/*
 289 * The following particular system cpumasks and operations manage
 290 * possible, present and online cpus.  Each of them is a fixed size
 291 * bitmap of size NR_CPUS.
 292 *
 293 *  #ifdef CONFIG_HOTPLUG_CPU
 294 *     cpu_possible_map - all NR_CPUS bits set
 295 *     cpu_present_map  - has bit 'cpu' set iff cpu is populated
 296 *     cpu_online_map   - has bit 'cpu' set iff cpu available to scheduler
 297 *  #else
 298 *     cpu_possible_map - has bit 'cpu' set iff cpu is populated
 299 *     cpu_present_map  - copy of cpu_possible_map
 300 *     cpu_online_map   - has bit 'cpu' set iff cpu available to scheduler
 301 *  #endif
 302 *
 303 *  In either case, NR_CPUS is fixed at compile time, as the static
 304 *  size of these bitmaps.  The cpu_possible_map is fixed at boot
 305 *  time, as the set of CPU id's that it is possible might ever
 306 *  be plugged in at anytime during the life of that system boot.
 307 *  The cpu_present_map is dynamic(*), representing which CPUs
 308 *  are currently plugged in.  And cpu_online_map is the dynamic
 309 *  subset of cpu_present_map, indicating those CPUs available
 310 *  for scheduling.
 311 *
 312 *  If HOTPLUG is enabled, then cpu_possible_map is forced to have
 313 *  all NR_CPUS bits set, otherwise it is just the set of CPUs that
 314 *  ACPI reports present at boot.
 315 *
 316 *  If HOTPLUG is enabled, then cpu_present_map varies dynamically,
 317 *  depending on what ACPI reports as currently plugged in, otherwise
 318 *  cpu_present_map is just a copy of cpu_possible_map.
 319 *
 320 *  (*) Well, cpu_present_map is dynamic in the hotplug case.  If not
 321 *      hotplug, it's a copy of cpu_possible_map, hence fixed at boot.
 322 *
 323 * Subtleties:
 324 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
 325 *    assumption that their single CPU is online.  The UP
 326 *    cpu_{online,possible,present}_maps are placebos.  Changing them
 327 *    will have no useful affect on the following num_*_cpus()
 328 *    and cpu_*() macros in the UP case.  This ugliness is a UP
 329 *    optimization - don't waste any instructions or memory references
 330 *    asking if you're online or how many CPUs there are if there is
 331 *    only one CPU.
 332 * 2) Most SMP arch's #define some of these maps to be some
 333 *    other map specific to that arch.  Therefore, the following
 334 *    must be #define macros, not inlines.  To see why, examine
 335 *    the assembly code produced by the following.  Note that
 336 *    set1() writes phys_x_map, but set2() writes x_map:
 337 *        int x_map, phys_x_map;
 338 *        #define set1(a) x_map = a
 339 *        inline void set2(int a) { x_map = a; }
 340 *        #define x_map phys_x_map
 341 *        main(){ set1(3); set2(5); }
 342 */
 343
 344extern cpumask_t cpu_possible_map;
 345extern cpumask_t cpu_online_map;
 346extern cpumask_t cpu_present_map;
 347
 348#if NR_CPUS > 1
 349#define num_online_cpus()       cpus_weight(cpu_online_map)
 350#define num_possible_cpus()     cpus_weight(cpu_possible_map)
 351#define num_present_cpus()      cpus_weight(cpu_present_map)
 352#define cpu_online(cpu)         cpu_isset((cpu), cpu_online_map)
 353#define cpu_possible(cpu)       cpu_isset((cpu), cpu_possible_map)
 354#define cpu_present(cpu)        cpu_isset((cpu), cpu_present_map)
 355#else
 356#define num_online_cpus()       1
 357#define num_possible_cpus()     1
 358#define num_present_cpus()      1
 359#define cpu_online(cpu)         ((cpu) == 0)
 360#define cpu_possible(cpu)       ((cpu) == 0)
 361#define cpu_present(cpu)        ((cpu) == 0)
 362#endif
 363
 364#define any_online_cpu(mask)                    \
 365({                                              \
 366        int cpu;                                \
 367        for_each_cpu_mask(cpu, (mask))          \
 368                if (cpu_online(cpu))            \
 369                        break;                  \
 370        cpu;                                    \
 371})
 372
 373#define for_each_cpu(cpu)         for_each_cpu_mask((cpu), cpu_possible_map)
 374#define for_each_online_cpu(cpu)  for_each_cpu_mask((cpu), cpu_online_map)
 375#define for_each_present_cpu(cpu) for_each_cpu_mask((cpu), cpu_present_map)
 376
 377#endif /* __LINUX_CPUMASK_H */
 378
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.