Skip to content

Commit

Permalink
Refactor print_node_cpus to display CPU ranges instead of individual …
Browse files Browse the repository at this point in the history
…CPUs

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.
  • Loading branch information
vishalc-ibm authored and andikleen committed Jan 6, 2024
1 parent 8f569cd commit 21ac663
Showing 1 changed file with 55 additions and 14 deletions.
69 changes: 55 additions & 14 deletions numactl.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,62 @@ static void print_distances(int maxnode)
}
}

static void print_node_cpus(int node)
{
int i, err;
struct bitmask *cpus;

cpus = numa_allocate_cpumask();
err = numa_node_to_cpus(node, cpus);
if (err >= 0) {
for (i = 0; i < cpus->size; i++)
if (numa_bitmask_isbitset(cpus, i))
printf(" %d", i);
}
putchar('\n');
static void print_node_cpus(int node) {
int i, err, start_range, end_range, first_print;
struct bitmask *cpus;

cpus = numa_allocate_cpumask();
err = numa_node_to_cpus(node, cpus);
if (err < 0) {
numa_free_cpumask(cpus);
return;
}

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
}
}
}

// Handle the last range, if it hasn't been printed yet
if (start_range != -1) {
if (!first_print) {
printf(",");
}
if (start_range == end_range) {
printf(" %d", start_range);
} else {
printf(" %d-%d", start_range, end_range);
}
}

putchar('\n');

numa_free_cpumask(cpus);
numa_free_cpumask(cpus);
}

static void hardware(void)
Expand Down

0 comments on commit 21ac663

Please sign in to comment.