Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NetBSD: Fix CPU usage and top info. #2101

Merged
merged 6 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ if(OS_SOLARIS)
endif(OS_SOLARIS)

if(OS_NETBSD)
set(netbsd netbsd.cc netbsd.h)
set(netbsd netbsd.cc netbsd.h bsdcommon.cc bsdcommon.h)
set(optional_sources ${optional_sources} ${netbsd})
endif(OS_NETBSD)

Expand Down
319 changes: 319 additions & 0 deletions src/bsdcommon.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
/*
*
* 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/param.h>
#include <sys/sysctl.h>

#include <string.h>

#include "top.h"

static kvm_t *kd = nullptr;
static char kvm_errbuf[_POSIX2_LINE_MAX];
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, kvm_errbuf);
if (kd == nullptr) {
NORM_ERR("opening kvm :%s", kvm_errbuf);
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, ... ] - CPU0, CPU1, ...
*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;
}
}

BSD_COMMON_PROC_STRUCT *bsdcommon::get_processes(short unsigned int *procs) {
if (!init_kvm()) {
return nullptr;
}

int n_processes = 0;
BSD_COMMON_PROC_STRUCT *ki = kvm_getproc2(kd, KERN_PROC_ALL, 0,
sizeof(BSD_COMMON_PROC_STRUCT),
&n_processes);
if (ki == nullptr) {
NORM_ERR("kvm_getproc2() failed");
return nullptr;
}

*procs = n_processes;
return ki;
}

static bool is_process_running(BSD_COMMON_PROC_STRUCT *p) {
#if defined(__NetBSD__)
return p->p_stat == LSRUN || p->p_stat == LSIDL || p->p_stat == LSONPROC;
#else
#error Not supported BSD system
#endif
}

void bsdcommon::get_number_of_running_processes(short unsigned int *run_procs) {
if (!init_kvm()) {
return;
}

short unsigned int nprocs = 0;
BSD_COMMON_PROC_STRUCT* ps = get_processes(&nprocs);
if (ps == nullptr) {
return;
}

short unsigned int ctr = 0;
for (int i = 0; i < nprocs; ++i) {
if (is_process_running(&ps[i])) {
++ctr;
}
}

*run_procs = ctr;
}

static bool is_top_process(BSD_COMMON_PROC_STRUCT *p) {
#if defined(__NetBSD__)
return !((p->p_flag & P_SYSTEM)) && p->p_comm[0] != 0;
#else
#error Not supported BSD system
#endif
}

static int32_t get_pd(BSD_COMMON_PROC_STRUCT *p) {
#if defined(__NetBSD__)
return p->p_pid;
#else
#error Not supported BSD system
#endif
}

// conky uses time in hundredths of seconds (centiseconds)
static unsigned long to_conky_time(u_int32_t sec, u_int32_t usec) {
return sec * 100 + (unsigned long)(usec * 0.0001);
}

static void proc_from_bsdproc(struct process *proc, BSD_COMMON_PROC_STRUCT *p) {
free_and_zero(proc->name);
free_and_zero(proc->basename);

unsigned long user_time = 0;
unsigned long kernel_time = 0;

#if defined(__NetBSD__)
// https://github.com/netbsd/src/blob/trunk/sys/sys/sysctl.h
proc->time_stamp = g_time;
proc->user_time = to_conky_time(p->p_uutime_sec, p->p_uutime_usec);
proc->kernel_time = to_conky_time(p->p_ustime_sec, p->p_ustime_usec);
proc->uid = p->p_uid;
proc->name = strndup(p->p_comm, text_buffer_size.get(*::state));
proc->basename = strndup(p->p_comm, text_buffer_size.get(*::state));
proc->amount = 100.0 * p->p_pctcpu / FSCALE;
proc->vsize = p->p_vm_vsize * getpagesize();
proc->rss = p->p_vm_rssize * getpagesize();
proc->total_cpu_time = to_conky_time(p->p_rtime_sec, p->p_rtime_usec);
#else
#error Not supported BSD system
#endif

if (proc->previous_user_time == ULONG_MAX) {
proc->previous_user_time = proc->user_time;
}

if (proc->previous_kernel_time == ULONG_MAX) {
proc->previous_kernel_time = proc->kernel_time;
}

/* strangely, the values aren't monotonous (from Linux) */
if (proc->previous_user_time > proc->user_time) {
proc->previous_user_time = proc->user_time;
}

if (proc->previous_kernel_time > proc->kernel_time) {
proc->previous_kernel_time = proc->kernel_time;
}

/* store the difference of the user_time */
user_time = proc->user_time - proc->previous_user_time;
kernel_time = proc->kernel_time - proc->previous_kernel_time;

/* backup the process->user_time for next time around */
proc->previous_user_time = proc->user_time;
proc->previous_kernel_time = proc->kernel_time;

/* store only the difference of the user_time here... */
proc->user_time = user_time;
proc->kernel_time = kernel_time;
}

void bsdcommon::update_top_info() {
if (!init_kvm()) {
return;
}

struct process *proc = nullptr;
short unsigned int nprocs = 0;

BSD_COMMON_PROC_STRUCT *ps = get_processes(&nprocs);
if (ps == nullptr) {
return;
}

for (int i = 0; i < nprocs; ++i) {
BSD_COMMON_PROC_STRUCT *p = &ps[i];

if (!is_top_process(p)) {
continue;
}

proc = get_process(get_pd(p));
if (!proc) {
continue;
}

proc_from_bsdproc(proc, p);
}
}
61 changes: 61 additions & 0 deletions src/bsdcommon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
*
* 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_
Copy link
Owner

Choose a reason for hiding this comment

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

Nitpick: you can just use #pragma once in the future.

#define BSDCOMMON_H_

#define BSD_COMMON

#if defined(__NetBSD__)
#include "sys/sysctl.h"
#define BSD_COMMON_PROC_STRUCT struct kinfo_proc2
#else
#error Not supported BSD system
#endif

#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);

BSD_COMMON_PROC_STRUCT* get_processes(short unsigned int *procs);

void get_number_of_running_processes(short unsigned int *run_procs);
void update_top_info();
};

#endif /*BSDCOMMON_H_*/
Loading
Loading