我在哪里可以找到“sched_getcpu()”的代码

S.W*_*Wan 1 linux scheduling scheduler cpu-usage header-files

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

但是我想知道在哪里可以找到这个函数的源代码?

谢谢。

pah*_*pah 5

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

对于 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 */
Run Code Online (Sandbox Code Playgroud)

该函数由__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;
}
Run Code Online (Sandbox Code Playgroud)

(有关前缀的详细信息,请参阅vDSOvdso)。

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

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));
}
Run Code Online (Sandbox Code Playgroud)

raw_smp_processor_id()该函数由文件中定义的函数使用arch/arm/include/asm/smp.h

#define raw_smp_processor_id() (current_thread_info()->cpu)
Run Code Online (Sandbox Code Playgroud)

它是由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;
}
Run Code Online (Sandbox Code Playgroud)