start_kernel——setup_nr_cpu_ids

原创 2015年07月06日 18:58:18

kernel/smp.c

/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
void __init setup_nr_cpu_ids(void)
{
    nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
}

include/linux/cpumask.h

#if NR_CPUS == 1
#define nr_cpu_ids      1
#else
extern int nr_cpu_ids;
#endif

NR_CPUS不是1,所以编译extern int nr_cpu_ids;,我在源代码中没有找到nr_cpu_ids的定义,资料上说nr_cpu_ids全局变量被声明为__read_mostly属性。
nr_cpu_ids保存的是所有可处于联机状态的CPU总数。
nr_cpu_ids具有当前系统能具备的CPU数的信息,默认值为NR_CPUS值,NR_CPUS是编译时用户可设置的常量值。NR_CPUS并非当前系统内存在的CPU的数值,而是Linux内核能支持的最大CPU数的最大值。
UP(Uni-Processor)中是1,32位SMP中具有2~32的值,64位内核中具有2~64位的值。
kernel/cpu.c

#ifdef CONFIG_INIT_ALL_POSSIBLE
static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
    = CPU_BITS_ALL;
#else
static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
#endif

没有定义CONFIG_INIT_ALL_POSSIBLE,所以编译

static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;

DECLARE_BITMAP我们在http://blog.csdn.net/yin262/article/details/46774041已经分析过了,这句定义的最终结果是生车功能一个unsigned long 类型的数组,数组名为cpu_possible_bits,用位图来管理cpu个数,每一位对应一个cpu。

关于利用位图来管理cpu变量,还有以下:
include/linux/cpumask.h

/*
 * The following particular system cpumasks and operations manage
 * possible, present, active and online cpus.
 *
 *     cpu_possible_mask- has bit 'cpu' set iff cpu is populatable
 *     cpu_present_mask - has bit 'cpu' set iff cpu is populated
 *     cpu_online_mask  - has bit 'cpu' set iff cpu available to scheduler
 *     cpu_active_mask  - has bit 'cpu' set iff cpu available to migration
 *
 *  If !CONFIG_HOTPLUG_CPU, present == possible, and active == online.
 *
 *  The cpu_possible_mask is fixed at boot time, as the set of CPU id's
 *  that it is possible might ever be plugged in at anytime during the
 *  life of that system boot.  The cpu_present_mask is dynamic(*),
 *  representing which CPUs are currently plugged in.  And
 *  cpu_online_mask is the dynamic subset of cpu_present_mask,
 *  indicating those CPUs available for scheduling.
 *
 *  If HOTPLUG is enabled, then cpu_possible_mask is forced to have
 *  all NR_CPUS bits set, otherwise it is just the set of CPUs that
 *  ACPI reports present at boot.
 *
 *  If HOTPLUG is enabled, then cpu_present_mask varies dynamically,
 *  depending on what ACPI reports as currently plugged in, otherwise
 *  cpu_present_mask is just a copy of cpu_possible_mask.
 *
 *  (*) Well, cpu_present_mask is dynamic in the hotplug case.  If not
 *      hotplug, it's a copy of cpu_possible_mask, hence fixed at boot.
 *
 * Subtleties:
 * 1) UP arch's (NR_CPUS == 1, CONFIG_SMP not defined) hardcode
 *    assumption that their single CPU is online.  The UP
 *    cpu_{online,possible,present}_masks are placebos.  Changing them
 *    will have no useful affect on the following num_*_cpus()
 *    and cpu_*() macros in the UP case.  This ugliness is a UP
 *    optimization - don't waste any instructions or memory references
 *    asking if you're online or how many CPUs there are if there is
 *    only one CPU.
 */

extern const struct cpumask *const cpu_possible_mask;
extern const struct cpumask *const cpu_online_mask;
extern const struct cpumask *const cpu_present_mask;
extern const struct cpumask *const cpu_active_mask;

cpu_possible_mask:系统内可安装的最多CPU的位图
cpu_online_mask:系统内安装的CPU中,正在使用的CPU的位图
cpu_present_mask:系统内安装的CPU的位图
cpu_active_mask:处于联机状态且可以动的(migration)的CPU的位图

以上都是结构体指针类型,由相应的bitmap转化而来:
kernel/cpu.c

#ifdef CONFIG_INIT_ALL_POSSIBLE
static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
    = CPU_BITS_ALL;
#else
static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
#endif
const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
EXPORT_SYMBOL(cpu_possible_mask);

static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
EXPORT_SYMBOL(cpu_online_mask);

static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
EXPORT_SYMBOL(cpu_present_mask);

static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
EXPORT_SYMBOL(cpu_active_mask);

转换的过程,在之前的文章里也分析了。

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

设置每CPU环境

5.3 设置每CPU环境回到start_kernel,563行调用mm_init_owner函数,将init_mm的owner字段指回init_task。这个函数可以说进入start_kernel以来...

(二)start_kernel分析二---之setup_arch()函数分析

start_kernel分析 如果以为到了c代码可以松一口气的话,就大错特措了,linux的c也不比汇编好懂多少,相反到掩盖了汇编的一些和机器相关的部分,有时候更难懂。其实作为编写操作系统的c代码,...

start_kernel->setup_arch->paging_init->memtable_init-->create_mapping

/* * Create the page directory entries and any necessary * page tables for the mapping specif...

start_kernel->setup_arch->paging_init->bootmem_init() 之 4

/* * Reserve the various regions of node 0 */ static __init void reserve_node_zero(unsigne...

start_kernel->setup_arch->paging_init->memtable_init

/* * Setup initial mappings.  We use the page we allocated for zero page to hold * the mappin...

per_cpu_pageset 之一(init/main.c start_kernel初始化)

pcp 初始化(之一)         所谓的pcp是每cpu页框高速缓冲,由数据结构struct per_cpu_pageset描述,包含在内存域struct zone中。在内核中,系统会经常请求和...

ARM Linux启动流程分析——start_kernel前启动阶段(汇编部分)

本文整理了ARM Linxu启动流程的第二阶段——start_kernel前启动阶段(汇编部分),内核版本为3.12.35。我以手上的树莓派b(ARM11)为平台示例来分析Linux内核在自解压后到跳...

<Linux>Linux内核启动分析(二)——start_kernel

在前一篇对head.S的分析中,我们知道内核启动的第一阶段的是处理u-boot传进来的机器id,由此来判断内核是否支持这个CPU以及该单板。而head.S最后跳到start_kernel这函数中作进一...

[kernel 启动流程] (第七章)第一阶段之——跳转到start_kernel

零、说明本文是《[kernel 启动流程] (第一章)概述》的延伸, 阅读本文前建议先阅读《[kernel 启动流程] (第一章)概述》1、kernel启动流程第一阶段简单说明arch/arm/ke...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)