printing the cpus a task is allowed to run on?

1.3k views Asked by At

I'm trying to printk the cpus that a specific task is allowed to run on.

Inside struct task_struct (which can be find here) there's the cpumask_t cpus_allowed which from what I understand contains exactly what Im looking for . Is that right ?

If so, how do I extract the cpus' numbers that are allowed ?

for example my comp has 8 logical cores - so Im expecting that somewhere inside cpus_allowed I can find those numbers (for example - 0,2,5)

3

There are 3 answers

0
Tsyvarev On

Macro for_each_cpu will iterate over all CPU's, allowed by the given mask:

// Assume `mask` is given.
int cpu;
for_each_cpu(cpu, mask)
{
    printk("Allowed CPU: %d\n", cpu);
}
0
nadavgam On

Ok, I found a function inside the kernel that does exactly what I needed in cpumask.h cpumask_scnprintf:

 /**
 * cpumask_scnprintf - print a cpumask into a string as comma-separated hex
 * @buf: the buffer to sprintf into
 * @len: the length of the buffer
 * @srcp: the cpumask to print
 *
 * If len is zero, returns zero.  Otherwise returns the length of the
 * (nul-terminated) @buf string.
 */
static inline int cpumask_scnprintf(char *buf, int len,
                                    const struct cpumask *srcp)
{
        return bitmap_scnprintf(buf, len, cpumask_bits(srcp), nr_cpumask_bits);
}
0
Claudio On

Use the function cpumask_pr_args() defined inside cpumask.h.

Usage:

 printk("%*pbl\n", cpumask_pr_args(mask));

See here for information about the %*pbl placeholder.