您的位置:首页 > 运维架构

cpu topology sys文件系统接口

2017-01-13 15:19 120 查看
在driver/base下面还有一个topology.c 这个文件只要是生成sys文件接口,方便用户同下面的接口查询cpu的拓扑结构.

[root@CentOS sda1]# cd /sys/devices/system/cpu/cpu0/

driver/        firmware_node/ hotplug/       node0/         power/         regs/          subsystem/     topology/

[root@CentOS sda1]# cd /sys/devices/system/cpu/cpu0/topology/

[root@CentOS topology]# ls

core_id  core_siblings  core_siblings_list  physical_package_id  thread_siblings  thread_siblings_list

[root@CentOS topology]# cat core_id

0

[root@CentOS topology]# cat physical_package_id

256

下面详细看看topology.c 的代码。

device_initcall(topology_sysfs_init);

可见是kernel 会自动调用topology_sysfs_init

static int topology_sysfs_init(void)

{

    int cpu;

    int rc = 0;

    cpu_notifier_register_begin();

    for_each_online_cpu(cpu) {

        rc = topology_add_dev(cpu);

        if (rc)

            goto out;

    }

    __hotcpu_notifier(topology_cpu_callback, 0);

out:

    cpu_notifier_register_done();

    return rc;

}

通过for_each_online_cpu 为内个online的调用topology_add_dev.随后注册一个notify。在cpu hotplug的时候更新/sys/devices/system/cpu/

static int topology_cpu_callback(struct notifier_block *nfb,

                 unsigned long action, void *hcpu)

{

    unsigned int cpu = (unsigned long)hcpu;

    int rc = 0;

    switch (action) {

    case CPU_UP_PREPARE:

    case CPU_UP_PREPARE_FROZEN:

        rc = topology_add_dev(cpu);

        break;

    case CPU_UP_CANCELED:

    case CPU_UP_CANCELED_FROZEN:

    case CPU_DEAD:

    case CPU_DEAD_FROZEN:

        topology_remove_dev(cpu);

        break;

    }

    return notifier_from_errno(rc);

}

hotplug的notify函数包含了add cpu 和 remove cpu两种操作.

我们以add为例

/* Add/Remove cpu_topology interface for CPU device */

static int topology_add_dev(unsigned int cpu)

{

    struct device *dev = get_cpu_device(cpu);

    return sysfs_create_group(&dev->kobj, &topology_attr_group);

}

调用sysfs_create_group 增加接口,并绑定处理函数

static struct attribute *default_attrs[] = {

    &dev_attr_physical_package_id.attr,

    &dev_attr_core_id.attr,

    &dev_attr_thread_siblings.attr,

    &dev_attr_thread_siblings_list.attr,

    &dev_attr_core_siblings.attr,

    &dev_attr_core_siblings_list.attr,

#ifdef CONFIG_SCHED_BOOK

    &dev_attr_book_id.attr,

    &dev_attr_book_siblings.attr,

    &dev_attr_book_siblings_list.attr,

#endif

#ifdef CONFIG_SCHED_DRAWER

    &dev_attr_drawer_id.attr,

    &dev_attr_drawer_siblings.attr,

    &dev_attr_drawer_siblings_list.attr,

#endif

    NULL

};

到这一步就很熟悉了。这个attribute会和/sys/devices/system/cpu/ 下面的接口一致

cd /sys/devices/system/cpu

[root@CentOS topology]# ls

core_id        core_siblings_list   thread_siblings

core_siblings  physical_package_id  thread_siblings_list
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: