Skip to content

Commit

Permalink
helpers: cpumask: add helper to convert a cpumask to string of cpu ra…
Browse files Browse the repository at this point in the history
…nges.

In some cases (for example getting affinity of an irq), it is better
to have an easily understandable list of cpus corresonding to a given
cpumask.
This helper converts a given cpumask to string, such that the string
represents the range of CPUs that are present in the given mask.

Signed-off-by: Imran Khan <imran.f.khan@oracle.com>
  • Loading branch information
imran-kn committed Oct 15, 2023
1 parent 185a5bf commit d1944ea
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions drgn/helpers/linux/cpumask.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,50 @@ def for_each_possible_cpu(prog: Program) -> Iterator[int]:
def for_each_present_cpu(prog: Program) -> Iterator[int]:
"""Iterate over all present CPUs."""
return _for_each_cpu_mask(prog, "__cpu_present_mask")


def cpumask_to_cpulist(cpumask: Object) -> str:
"""
Get list of CPUs, present in a cpumask.
:param cpumask: ``struct cpumask*``
:returns: list of CPUs as string
"""
start = 0
end = 0
count = 1
cpu_range = str()
all_cpu_ranges = str()
cpulist = [cpu for cpu in for_each_cpu(cpumask)]

if len(cpulist) == 1: # Just one CPU in mask
return str(cpulist[0])

for index, value in enumerate(cpulist):
if index < len(cpulist) - 1:
if cpulist[index + 1] > value + 1:
end = index
if count > 1:
cpu_range = str(cpulist[start]) + "-" + str(cpulist[end])
else:
cpu_range = str(cpulist[start])
start = end + 1
count = 1
if not len(all_cpu_ranges):
all_cpu_ranges += cpu_range
else:
all_cpu_ranges += ", " + cpu_range
else:
count += 1
else:
if count == index + 1: # only one range
all_cpu_ranges = str(cpulist[start]) + "-" + str(cpulist[index])
else:
if cpulist[start] == cpulist[index]: # only one element in last range
all_cpu_ranges += ", " + str(cpulist[index])
else: # more than 1 element in last range
all_cpu_ranges += (
", " + str(cpulist[start]) + "-" + str(cpulist[index])
)

return all_cpu_ranges

0 comments on commit d1944ea

Please sign in to comment.