Skip to content

Commit

Permalink
numactl.c: Refactor print_node_cpus to display CPU ranges
Browse files Browse the repository at this point in the history

This commit enhances the readability and conciseness of the output produced by the print_node_cpus function. Previously, the function listed each available CPU individually, which, especially for systems with a high number of CPUs, resulted in extensive and somewhat cluttered output.

The key changes include the introduction of logic to identify contiguous ranges of CPUs, thereby allowing these sequences to be printed as hyphen-separated ranges (e.g., 1-4) instead of itemized lists (e.g., 1 2 3 4). This change significantly streamlines the output, making it more digestible and easier to interpret, particularly for systems with large numbers of CPUs.

By tracking the start and end of each CPU range and only printing once a discontinuity (or the end of the list) is encountered, the function can represent the same information more compactly and clearly. This update respects the original structure and logic of the function, altering only the output strategy to improve clarity without affecting the core functionality.

It's worth noting that the function's error-handling and interaction with the NUMA library remain unchanged, preserving the original behavior in scenarios where system queries might fail or return unexpected results.

Signed-off-by: Vishal Chourasia vishalc@linux.ibm.com
  • Loading branch information
vishalc-ibm authored and andikleen committed Jan 6, 2024
1 parent 21ac663 commit b4054df
Showing 1 changed file with 31 additions and 40 deletions.
71 changes: 31 additions & 40 deletions numactl.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,62 +229,53 @@ static void print_distances(int maxnode)
}
}

static void print_node_cpus(int node) {
int i, err, start_range, end_range, first_print;
static void print_node_cpus(int node)
{
int i = 0, err, start, segment = 0;
struct bitmask *cpus;

cpus = numa_allocate_cpumask();
err = numa_node_to_cpus(node, cpus);
if (err < 0) {
numa_free_cpumask(cpus);
return;
goto out; // If there's an error, we jump to cleanup.
}

start_range = -1;
end_range = -1;
first_print = 1; // Flag to check if we need to print a comma

for (i = 0; i < cpus->size; i++) {
if (numa_bitmask_isbitset(cpus, i)) {
if (start_range == -1) {
// Start of a new range
start_range = i;
}
end_range = i; // Extend the range
} else {
if (start_range != -1) {
// We've reached the end of a range, let's print it
if (!first_print) {
printf(",");
}
if (start_range == end_range) {
// Range consists of a single CPU
printf(" %d", start_range);
} else {
// Range includes multiple CPUs
printf(" %d-%d", start_range, end_range);
}
first_print = 0; // We've printed, so clear the flag
start_range = -1; // Reset the range
}
// Main loop over each CPU in the bitmask.
while (i < cpus->size) {
start = -1;

// If the bit is set, we're in a range of available CPUs.
while (i < cpus->size && numa_bitmask_isbitset(cpus, i)) {
if (start == -1) start = i; // We've found the start of a range.
i++;
}
}

// Handle the last range, if it hasn't been printed yet
if (start_range != -1) {
if (!first_print) {
// If start is still -1, the current bit wasn't set, and we move to the next iteration.
if (start == -1) {
i++;
continue;
}

// Formatting the output: we put a comma before all segments, except the first.
if (segment) {
printf(",");
}
if (start_range == end_range) {
printf(" %d", start_range);

int end = i - 1; // The end of the current range.
if (start == end) {
// If it's a single CPU (no range), we print it directly.
printf(" %d", start);
} else {
printf(" %d-%d", start_range, end_range);
// Otherwise, we print the range.
printf(" %d-%d", start, end);
}
segment++; // Update the count of printed segments.
}

putchar('\n');
putchar('\n'); // Newline for cleaner output formatting.

numa_free_cpumask(cpus);
out:
numa_free_cpumask(cpus); // Cleanup the allocated bitmask.
}

static void hardware(void)
Expand Down

0 comments on commit b4054df

Please sign in to comment.