歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> uclinux-2008R1-RC8(bf561)到VDSP5的移植(27):cpumask_t

uclinux-2008R1-RC8(bf561)到VDSP5的移植(27):cpumask_t

日期:2017/3/3 16:43:38   编辑:關於Linux

uclinux使用一個稱之為cpumask_t的結構體來保存各個CPU的信息。其定義在include/linux/cpumask.h中,如下所示:

typedef struct { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t;

其中DECLARE_BITMAP的定義為:

#define BITS_TO_LONGS(bits) /
(((bits)+BITS_PER_LONG-1)/BITS_PER_LONG)
#define DECLARE_BITMAP(name,bits) /
unsigned long name[BITS_TO_LONGS(bits)]

因此對於bf561來講,cpumask_t的定義相當於:

typedef struct { unsigned long bits[1]; } cpumask_t;

每個CPU的信息用一個整數中的一位來表示。在cpumask.h中定義了與此掩碼相關的一些基本操作:

/* The available cpumask operations are:
 *
 * void cpu_set(cpu, mask)       turn on bit 'cpu' in mask
 * void cpu_clear(cpu, mask)         turn off bit 'cpu' in mask
 * void cpus_setall(mask)        set all bits
 * void cpus_clear(mask)         clear all bits
 * int cpu_isset(cpu, mask)      true iff bit 'cpu' set in mask
 * int cpu_test_and_set(cpu, mask)   test and set bit 'cpu' in mask
 *
 * void cpus_and(dst, src1, src2)    dst = src1 & src2 [intersection]
 * void cpus_or(dst, src1, src2) dst = src1 | src2 [union]
 * void cpus_xor(dst, src1, src2)    dst = src1 ^ src2
 * void cpus_andnot(dst, src1, src2) dst = src1 & ~src2
 * void cpus_complement(dst, src)    dst = ~src
 *
 * int cpus_equal(mask1, mask2)      Does mask1 == mask2?
 * int cpus_intersects(mask1, mask2) Do mask1 and mask2 intersect?
 * int cpus_subset(mask1, mask2) Is mask1 a subset of mask2?
 * int cpus_empty(mask)          Is mask empty (no bits sets)?
 * int cpus_full(mask)           Is mask full (all bits sets)?
 * int cpus_weight(mask)         Hamming weigh - number of set bits
 *
 * void cpus_shift_right(dst, src, n) Shift right
 * void cpus_shift_left(dst, src, n) Shift left
 *
 * int first_cpu(mask)           Number lowest set bit, or NR_CPUS
 * int next_cpu(cpu, mask)       Next cpu past 'cpu', or NR_CPUS
 *
 * cpumask_t cpumask_of_cpu(cpu) Return cpumask with bit 'cpu' set
 * CPU_MASK_ALL                  Initializer - all bits set
 * CPU_MASK_NONE            Initializer - no bits set
 * unsigned long *cpus_addr(mask)    Array of unsigned long's in mask
 *
 * int cpumask_scnprintf(buf, len, mask) Format cpumask for printing
 * int cpumask_parse_user(ubuf, ulen, mask)    Parse ascii string as cpumask
 * int cpulist_scnprintf(buf, len, mask) Format cpumask as list for printing
 * int cpulist_parse(buf, map)       Parse ascii string as cpulist
 * int cpu_remap(oldbit, old, new)   newbit = map(old, new)(oldbit)
 * int cpus_remap(dst, src, old, new) *dst = map(old, new)(src)
 *
 * for_each_cpu_mask(cpu, mask)      for-loop cpu over mask
 *
 * int num_online_cpus()         Number of online CPUs
 * int num_possible_cpus()       Number of all possible CPUs
 * int num_present_cpus()        Number of present CPUs
 *
 * int cpu_online(cpu)           Is some cpu online?
 * int cpu_possible(cpu)         Is some cpu possible?
 * int cpu_present(cpu)          Is some cpu present (can schedule)?
 *
 * int any_online_cpu(mask)      First online cpu in mask
 *
 * for_each_possible_cpu(cpu)        for-loop cpu over cpu_possible_map
 * for_each_online_cpu(cpu)      for-loop cpu over cpu_online_map
 * for_each_present_cpu(cpu)         for-loop cpu over cpu_present_map
 *
 * Subtlety:
 * 1) The 'type-checked' form of cpu_isset() causes gcc (3.3.2, anyway)
 *    to generate slightly worse code. Note for example the additional
 *    40 lines of assembly code compiling the "for each possible cpu"
 *    loops buried in the disk_stat_read() macros calls when compiling
 *    drivers/block/genhd.c (arch i386, CONFIG_SMP=y). So use a simple
 *    one-line #define for cpu_isset(), instead of wrapping an inline
 *    inside a macro, the way we do the other calls.
 */

在uclinux內核的一開始,就設置了啟動內核的CPU的信息,如下所示(init/main.c):

/*
 *   Activate the first processor.
 */

static void __init boot_cpu_init(void)
{
     int cpu = smp_processor_id();
     /* Mark the boot cpu "present", "online" etc for SMP and UP case */
     cpu_set(cpu, cpu_online_map);
     cpu_set(cpu, cpu_present_map);
     cpu_set(cpu, cpu_possible_map);
}

這裡用到了cpu_online_map,cpu_present_map和cpu_possible_map三個變量,在uclinux內核文檔中解釋了它們的用途(cpu-hotplug.txt):

cpu_possible_map: Bitmap of possible CPUs that can ever be available in the
system. This is used to allocate some boot time memory for per_cpu variables
that aren't designed to grow/shrink as CPUs are made available or removed.
Once set during boot time discovery phase, the map is static, i.e no bits
are added or removed anytime. Trimming it accurately for your system needs
upfront can save some boot time memory. See below for how we use heuristics
in x86_64 case to keep this under check.

cpu_online_map: Bitmap of all CPUs currently online. Its set in __cpu_up()
after a cpu is available for kernel scheduling and ready to receive
interrupts from devices. Its cleared when a cpu is brought down using
__cpu_disable(), before which all OS services including interrupts are
migrated to another target CPU.

cpu_present_map: Bitmap of CPUs currently present in the system. Not all
of them may be online. When physical hotplug is processed by the relevant
subsystem (e.g ACPI) can change and new bit either be added or removed
from the map depending on the event is hot-add/hot-remove. There are currently
no locking rules as of now. Typical usage is to init topology during boot,
at which time hotplug is disabled.

You really dont need to manipulate any of the system cpu maps. They should
be read-only for most use. When setting up per-cpu resources almost always use
cpu_possible_map/for_each_possible_cpu() to iterate.

在kernel/shed.c中可以發現它們的定義:

/*
 * Represents all cpu's present in the system
 * In systems capable of hotplug, this map could dynamically grow
 * as new cpu's are detected in the system via any platform specific
 * method, such as ACPI for e.g.
 */

cpumask_t cpu_present_map __read_mostly;
EXPORT_SYMBOL(cpu_present_map);

#ifndef CONFIG_SMP
cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
EXPORT_SYMBOL(cpu_online_map);

cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
EXPORT_SYMBOL(cpu_possible_map);
#endif

可以看到,當使用了SMP的時候cpu_online_map和cpu_possible_map是沒有定義的,因為這兩個量的使用與體系結構相關,它們的定義是放在不同arch的kernel下面的,而uclinux並不支持SMP,因此我們需要在arch/blackfin/kernel下面添加一個smp.c,並在其中加上這兩個量的定義:

/*
 * bitmask of present and online CPUs.
 * The present bitmask indicates that the CPU is physically present.
 * The online bitmask indicates that the CPU is up and running.
 */
cpumask_t cpu_possible_map;
EXPORT_SYMBOL(cpu_possible_map);
cpumask_t cpu_online_map;
EXPORT_SYMBOL(cpu_online_map);

Copyright © Linux教程網 All Rights Reserved