1

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)

Claudio
  • 10,614
  • 4
  • 31
  • 71
nadavgam
  • 2,014
  • 5
  • 20
  • 48
  • Given that it's a `cpumask_t`, I suspect that it is an integer (either 32- or 64-bit) that contains a 1 bit in each position if the corresponding CPU is allowed. In other words, it's not a list of numbers, but you'll have to iterate over the bits and test each one. – twalberg Jan 14 '16 at 19:21

3 Answers3

1

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);
}
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
1

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);
}
Claudio
  • 10,614
  • 4
  • 31
  • 71
nadavgam
  • 2,014
  • 5
  • 20
  • 48
1

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.

Claudio
  • 10,614
  • 4
  • 31
  • 71