我在哪里可以找到" sched_getcpu()"的代码?

时间:2016-08-07 18:49:45

标签: linux header-files scheduling cpu-usage scheduler

最近我在Linux上使用头文件sched_getcpu()中的函数sched.h

但是,我想知道在哪里可以找到此功能的源代码?

感谢。

1 个答案:

答案 0 :(得分:3)

在Linux下,sched_getcpu()函数是sys_getcpu()系统调用的glibc包装器,它是特定于体系结构的。

对于x86_64架构,它在arch/x86/include/asm/vgtod.h下定义为__getcpu()(树4.x):

#ifdef CONFIG_X86_64

#define VGETCPU_CPU_MASK 0xfff

static inline unsigned int __getcpu(void)
{
        unsigned int p;

        /*
         * Load per CPU data from GDT.  LSL is faster than RDTSCP and
         * works on all CPUs.  This is volatile so that it orders
         * correctly wrt barrier() and to keep gcc from cleverly
         * hoisting it out of the calling function.
         */
        asm volatile ("lsl %1,%0" : "=r" (p) : "r" (__PER_CPU_SEG));

        return p;
}

#endif /* CONFIG_X86_64 */

__vdso_getcpu()中声明的arch/entry/vdso/vgetcpu.c调用此函数:

notrace long
__vdso_getcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *unused)
{
        unsigned int p;

        p = __getcpu();

        if (cpu)
                *cpu = p & VGETCPU_CPU_MASK;
        if (node)
                *node = p >> 12;
        return 0;
}

(有关vdso前缀是什么的详细信息,请参阅vDSO

编辑1:(回复行李代码位置)

ARM代码位置

可以在arch/arm/include/asm/thread_info.h文件中找到:

static inline struct thread_info *current_thread_info(void)
{
        return (struct thread_info *)
                (current_stack_pointer & ~(THREAD_SIZE - 1));
}

raw_smp_processor_id()使用此函数,该文件在arch/arm/include/asm/smp.h文件中定义为:

#define raw_smp_processor_id() (current_thread_info()->cpu)

由文件getcpu中声明的kernel/sys.c系统调用调用它:

SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep, struct getcpu_cache __user *, unused)
{
        int err = 0;
        int cpu = raw_smp_processor_id();

        if (cpup)
                err |= put_user(cpu, cpup);
        if (nodep)
                err |= put_user(cpu_to_node(cpu), nodep);
        return err ? -EFAULT : 0;
}