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

FreeBSD: scale CPU% usage on swap time #606

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
9 changes: 8 additions & 1 deletion freebsd/FreeBSDProcessList.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,13 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
return;
}

int ccpu;
size_t size = sizeof(ccpu);
if (sysctlbyname("kern.ccpu", &ccpu, &size, NULL, 0) == -1) {
ccpu = 0;
}
const double decayfactor = log(ccpu / kernelFScale);
Copy link
Member

Choose a reason for hiding this comment

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

kernelFScale might be zero. Not ensured non-zero by the code querying this from the syscall.

Copy link
Member Author

Choose a reason for hiding this comment

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

len = sizeof(kernelFScale);
if (sysctlbyname("kern.fscale", &kernelFScale, &len, NULL, 0) == -1) {
//sane default for kernel provided CPU percentage scaling, at least on x86 machines, in case this sysctl call failed
kernelFScale = 2048;
}

You mean kern.fscale might be 0? Is that possible?

Copy link
Member

Choose a reason for hiding this comment

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

Highly unlikely, given that there's other code already that does a similar division and I'm not aware of any bug reports regarding this. Mostly DiD note.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Actually just looking at the remarked upon code part: ccpu is set to 0, which will cause a math error with the following calculation for decayfactor. I don't think that's right?

Copy link
Contributor

Choose a reason for hiding this comment

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

I've made pull request #1275 that would reject zero kern.fscale value.


int count = 0;
const struct kinfo_proc* kprocs = kvm_getprocs(fpl->kd, KERN_PROC_PROC, 0, &count);

Expand Down Expand Up @@ -504,7 +511,7 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) {
proc->nlwp = kproc->ki_numthreads;
proc->time = (kproc->ki_runtime + 5000) / 10000;

proc->percent_cpu = 100.0 * ((double)kproc->ki_pctcpu / (double)kernelFScale);
proc->percent_cpu = 100.0 * ((double)kproc->ki_pctcpu / (double)kernelFScale) / (1.0 - exp(kproc->ki_swtime * decayfactor));
proc->percent_mem = 100.0 * proc->m_resident / (double)(super->totalMem);

if (kproc->ki_stat == SRUN && kproc->ki_oncpu != NOCPU) {
Expand Down