-
-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Report the correct number of CPUs and calculate the per-CPU load. Start implementing a common BSD interface, as all BSD implementations use *slightly* different functions to achieve the same goal, and maintaining them in one place would be easier. Fixes #2097.
- Loading branch information
Showing
6 changed files
with
235 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* | ||
* Conky, a system monitor, based on torsmo | ||
* | ||
* Please see COPYING for details | ||
* | ||
* Copyright (c) 2005-2024 Brenden Matthews, Philip Kovacs, et. al. | ||
* (see AUTHORS) | ||
* All rights reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#include "bsdcommon.h" | ||
#include "logging.h" | ||
|
||
#include <kvm.h> | ||
|
||
#include <sys/sysctl.h> | ||
|
||
static kvm_t *kd = nullptr; | ||
static bool kvm_initialised = false; | ||
static bool cpu_initialised = false; | ||
|
||
static struct bsdcommon::cpu_load *cpu_loads = nullptr; | ||
|
||
bool bsdcommon::init_kvm() { | ||
if (kvm_initialised) { | ||
return true; | ||
} | ||
|
||
kd = kvm_open(nullptr, nullptr, nullptr, KVM_NO_FILES, nullptr); | ||
if (kd == nullptr) { | ||
NORM_ERR("opening kvm"); | ||
return false; | ||
} | ||
|
||
kvm_initialised = true; | ||
return false; | ||
} | ||
|
||
void bsdcommon::deinit_kvm() { | ||
if (!kvm_initialised || kd == nullptr) { | ||
return; | ||
} | ||
|
||
kvm_close(kd); | ||
} | ||
|
||
void bsdcommon::get_cpu_count(float **cpu_usage, unsigned int *cpu_count) { | ||
int ncpu = 1; | ||
int mib[2] = {CTL_HW, HW_NCPU}; | ||
size_t len = sizeof(ncpu); | ||
|
||
if (sysctl(mib, 2, &ncpu, &len, nullptr, 0) != 0) { | ||
NORM_ERR("error getting kern.ncpu, defaulting to 1"); | ||
ncpu = 1; | ||
} | ||
|
||
if (*cpu_count != ncpu) { | ||
*cpu_count = ncpu; | ||
|
||
if (*cpu_usage != nullptr) { | ||
free(*cpu_usage); | ||
*cpu_usage = nullptr; | ||
} | ||
|
||
if (cpu_loads != nullptr) { | ||
free(cpu_loads); | ||
cpu_loads = nullptr; | ||
} | ||
} | ||
|
||
if (*cpu_usage == nullptr) { | ||
// [0] - Total CPU | ||
// [1, 2, ... ] - CPU1, CPU2, ... | ||
*cpu_usage = (float*)calloc(ncpu + 1, sizeof(float)); | ||
if (*cpu_usage == nullptr) { | ||
CRIT_ERR("calloc of cpu_usage"); | ||
} | ||
} | ||
|
||
if (cpu_loads == nullptr) { | ||
cpu_loads = (struct cpu_load*)calloc(ncpu + 1, sizeof(struct cpu_load)); | ||
if (cpu_loads == nullptr) { | ||
CRIT_ERR("calloc of cpu_loads"); | ||
} | ||
} | ||
} | ||
|
||
void bsdcommon::update_cpu_usage(float **cpu_usage, unsigned int *cpu_count) { | ||
uint64_t cp_time0[CPUSTATES]; | ||
int mib_cpu0[2] = {CTL_KERN, KERN_CP_TIME}; | ||
uint64_t cp_timen[CPUSTATES]; | ||
int mib_cpun[3] = {CTL_KERN, KERN_CP_TIME, 0}; | ||
size_t size = 0; | ||
u_int64_t used = 0, total = 0; | ||
|
||
if (!cpu_initialised) { | ||
get_cpu_count(cpu_usage, cpu_count); | ||
cpu_initialised = true; | ||
} | ||
|
||
size = sizeof(cp_time0); | ||
if (sysctl(mib_cpu0, 2, &cp_time0, &size, nullptr, 0) != 0) { | ||
NORM_ERR("unable to get kern.cp_time for cpu0"); | ||
return; | ||
} | ||
|
||
for (int j = 0; j < CPUSTATES; ++j) { | ||
total += cp_time0[j]; | ||
} | ||
used = total - cp_time0[CP_IDLE]; | ||
|
||
if ((total - cpu_loads[0].old_total) != 0) { | ||
const float diff_used = (float)(used - cpu_loads[0].old_used); | ||
const float diff_total = (float)(total - cpu_loads[0].old_total); | ||
(*cpu_usage)[0] = diff_used / diff_total; | ||
} else { | ||
(*cpu_usage)[0] = 0; | ||
} | ||
cpu_loads[0].old_used = used; | ||
cpu_loads[0].old_total = total; | ||
|
||
for (int i = 0; i < *cpu_count; ++i) { | ||
mib_cpun[2] = i; | ||
size = sizeof(cp_timen); | ||
if (sysctl(mib_cpun, 3, &cp_timen, &size, nullptr, 0) != 0) { | ||
NORM_ERR("unable to get kern.cp_time for cpu%d", i); | ||
return; | ||
} | ||
|
||
total = 0; | ||
used = 0; | ||
for (int j = 0; j < CPUSTATES; ++j) { | ||
total += cp_timen[j]; | ||
} | ||
used = total - cp_timen[CP_IDLE]; | ||
|
||
const int n = i + 1; // [0] is the total CPU, must shift by 1 | ||
if ((total - cpu_loads[n].old_total) != 0) { | ||
const float diff_used = (float)(used - cpu_loads[n].old_used); | ||
const float diff_total = (float)(total - cpu_loads[n].old_total); | ||
(*cpu_usage)[n] = diff_used / diff_total; | ||
} else { | ||
(*cpu_usage)[n] = 0; | ||
} | ||
|
||
cpu_loads[n].old_used = used; | ||
cpu_loads[n].old_total = total; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* | ||
* Conky, a system monitor, based on torsmo | ||
* | ||
* Please see COPYING for details | ||
* | ||
* Copyright (c) 2005-2024 Brenden Matthews, Philip Kovacs, et. al. | ||
* (see AUTHORS) | ||
* All rights reserved. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
/* | ||
* Shared or very similar code across BSDs. | ||
*/ | ||
|
||
#ifndef BSDCOMMON_H_ | ||
#define BSDCOMMON_H_ | ||
|
||
#define BSD_COMMON | ||
|
||
#include <stdint.h> | ||
|
||
namespace bsdcommon { | ||
struct cpu_load { | ||
uint64_t old_used; | ||
uint64_t old_total; | ||
}; | ||
|
||
bool init_kvm(); | ||
void deinit_kvm(); | ||
|
||
void get_cpu_count(float **cpu_usage, unsigned int *cpu_count); | ||
void update_cpu_usage(float **cpu_usage, unsigned int *cpu_count); | ||
}; | ||
|
||
#endif /*BSDCOMMON_H_*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters