Skip to content

Commit

Permalink
Add setting to show only active CPUs
Browse files Browse the repository at this point in the history
Closes: htop-dev#757
  • Loading branch information
cgzones committed Aug 21, 2021
1 parent a9ddacc commit f65f726
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
46 changes: 41 additions & 5 deletions CPUMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static const int CPUMeter_attributes[] = {

typedef struct CPUMeterData_ {
unsigned int cpus;
unsigned int adjustActive;
Meter** meters;
} CPUMeterData;

Expand Down Expand Up @@ -188,6 +189,7 @@ static void CPUMeter_display(const Object* cast, RichString* out) {
static void AllCPUsMeter_getRange(const Meter* this, int* start, int* count) {
const CPUMeterData* data = this->meterData;
unsigned int cpus = data->cpus;
unsigned int adjustActive = data->adjustActive;
switch(Meter_name(this)[0]) {
default:
case 'A': // All
Expand All @@ -199,7 +201,7 @@ static void AllCPUsMeter_getRange(const Meter* this, int* start, int* count) {
*count = (cpus+1) / 2;
break;
case 'R': // Second Half
*start = (cpus+1) / 2;
*start = (cpus+1) / 2 + adjustActive;
*count = cpus / 2;
break;
}
Expand All @@ -214,20 +216,54 @@ static void AllCPUsMeter_updateValues(Meter* this) {
Meter_updateValues(meters[i]);
}

static void AllCPUsMeter_done(Meter* this);

static void CPUMeterCommonInit(Meter* this, int ncol) {
unsigned int cpus = this->pl->existingCPUs;
unsigned int cpus = this->pl->settings->showOnlyActiveCPUs ? this->pl->activeCPUs : this->pl->existingCPUs;
CPUMeterData* data = this->meterData;
if (!data) {
if (!data || data->cpus != cpus) {
if (data)
AllCPUsMeter_done(this);

data = this->meterData = xMalloc(sizeof(CPUMeterData));
data->cpus = cpus;
data->meters = xCalloc(cpus, sizeof(Meter*));
if (this->pl->settings->showOnlyActiveCPUs && (Meter_name(this)[0] == 'L' || Meter_name(this)[0] == 'R')) {
/* If there are more inactive CPUs in the first half then there are in the second half,
* shift the second half to begin at a later ID to cover the last CPU if online. */
unsigned int countL = 0, countR = 0;
for (unsigned int i = 0; i < this->pl->existingCPUs / 2; i++)
countL += ProcessList_isCPUonline(this->pl, i) ? 1 : 0;
for (unsigned int i = this->pl->existingCPUs / 2; i < this->pl->existingCPUs; i++)
countR += ProcessList_isCPUonline(this->pl, i) ? 1 : 0;
data->adjustActive = countR > countL ? countR - countL : 0;
} else
data->adjustActive = 0;
}

Meter** meters = data->meters;
int start, count;
AllCPUsMeter_getRange(this, &start, &count);
for (int i = 0; i < count; i++) {

int i = 0;

for (unsigned int j = start; j < this->pl->existingCPUs && i < count; j++) {
if (this->pl->settings->showOnlyActiveCPUs && !ProcessList_isCPUonline(this->pl, j))
continue;

if (!meters[i])
meters[i] = Meter_new(this->pl, j + 1, (const MeterClass*) Class(CPUMeter));

Meter_init(meters[i]);

i++;
}

/* Initialize any remaining items to the first CPU (there should however be none). */
assert(i == count);
for (; i < count; i++) {
if (!meters[i])
meters[i] = Meter_new(this->pl, start + i + 1, (const MeterClass*) Class(CPUMeter));
meters[i] = Meter_new(this->pl, 1, (const MeterClass*) Class(CPUMeter));

Meter_init(meters[i]);
}
Expand Down
1 change: 1 addition & 0 deletions DisplayOptionsPanel.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ DisplayOptionsPanel* DisplayOptionsPanel_new(Settings* settings, ScreenManager*
Panel_add(super, (Object*) CheckItem_newByRef("Leave a margin around header", &(settings->headerMargin)));
Panel_add(super, (Object*) CheckItem_newByRef("Detailed CPU time (System/IO-Wait/Hard-IRQ/Soft-IRQ/Steal/Guest)", &(settings->detailedCPUTime)));
Panel_add(super, (Object*) CheckItem_newByRef("Count CPUs from 1 instead of 0", &(settings->countCPUsFromOne)));
Panel_add(super, (Object*) CheckItem_newByRef("Show only active CPUs", &(settings->showOnlyActiveCPUs)));
Panel_add(super, (Object*) CheckItem_newByRef("Update process names on every refresh", &(settings->updateProcessNames)));
Panel_add(super, (Object*) CheckItem_newByRef("Add guest time in CPU meter percentage", &(settings->accountGuestInCPUMeter)));
Panel_add(super, (Object*) CheckItem_newByRef("Also show CPU percentage numerically", &(settings->showCPUUsage)));
Expand Down
4 changes: 4 additions & 0 deletions Settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ static bool Settings_read(Settings* this, const char* fileName, unsigned int ini
} else if (String_eq(option[0], "cpu_count_from_zero")) {
// old (inverted) naming also supported for backwards compatibility
this->countCPUsFromOne = !atoi(option[1]);
} else if (String_eq(option[0], "show_only_active_cpu")) {
this->showOnlyActiveCPUs = atoi(option[1]);
} else if (String_eq(option[0], "show_cpu_usage")) {
this->showCPUUsage = atoi(option[1]);
} else if (String_eq(option[0], "show_cpu_frequency")) {
Expand Down Expand Up @@ -352,6 +354,7 @@ int Settings_write(const Settings* this, bool onCrash) {
fprintf(fd, "header_margin=%d\n", (int) this->headerMargin);
fprintf(fd, "detailed_cpu_time=%d\n", (int) this->detailedCPUTime);
fprintf(fd, "cpu_count_from_one=%d\n", (int) this->countCPUsFromOne);
fprintf(fd, "show_only_active_cpu=%d\n", (int) this->showOnlyActiveCPUs);
fprintf(fd, "show_cpu_usage=%d\n", (int) this->showCPUUsage);
fprintf(fd, "show_cpu_frequency=%d\n", (int) this->showCPUFrequency);
#ifdef BUILD_WITH_CPU_TEMP
Expand Down Expand Up @@ -407,6 +410,7 @@ Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicColumns)
this->highlightMegabytes = true;
this->detailedCPUTime = false;
this->countCPUsFromOne = false;
this->showOnlyActiveCPUs = false;
this->showCPUUsage = true;
this->showCPUFrequency = false;
#ifdef BUILD_WITH_CPU_TEMP
Expand Down
1 change: 1 addition & 0 deletions Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ typedef struct Settings_ {
ProcessField treeSortKey;

bool countCPUsFromOne;
bool showOnlyActiveCPUs;
bool detailedCPUTime;
bool showCPUUsage;
bool showCPUFrequency;
Expand Down

1 comment on commit f65f726

@mator
Copy link

@mator mator commented on f65f726 Aug 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just tested this branch, does not fix the issue
Screenshot 2021-08-22 at 12 34 09

Please sign in to comment.