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

Invalid NICE value on FreeBSD #1396

Open
vchimishuk opened this issue Feb 10, 2024 · 2 comments
Open

Invalid NICE value on FreeBSD #1396

vchimishuk opened this issue Feb 10, 2024 · 2 comments
Labels
bug 🐛 Something isn't working FreeBSD 👹 FreeBSD related issues
Milestone

Comments

@vchimishuk
Copy link

vchimishuk commented Feb 10, 2024

On FreeBSD htop displays weird nice (NI column) values for some processes.
Here are three rows which looks invalid to me.

  PID USER       PRI  NI  VIRT   RES S  CPU%▽MEM%   TIME+  Command                                                                                             
   11 root       187  52     0    64 R 395.1  0.0 26h53:03 idle                                                                                                
    2 root       -60 -195     0    64 W   0.0  0.0  0:03.39 clock
    5 root       -60 -195     0    16 S   0.0  0.0  0:00.00 busdma

As you can see their nice value out of range.
htop's man page says:

The nice value of a process, from 19 (low priority) to -20 (high priority).

getpriority(2) says almost the same

The prio argument is a value in the range -20 to 20.

So, displayed values are out of range which nice can have.

I've checked htop's source code and can see it does some weird calculations over nice value. Not sure why do we need this code at all, getpriority(2) should return ready to be displayed value.

      if (String_eq("intr", kproc->ki_comm) && (kproc->ki_flag & P_SYSTEM)) {
         proc->nice = 0; //@etosan: intr kernel process (not thread) has weird nice value
      } else if (kproc->ki_pri.pri_class == PRI_TIMESHARE) {
         proc->nice = kproc->ki_nice - NZERO;
      } else if (PRI_IS_REALTIME(kproc->ki_pri.pri_class)) {
         proc->nice = PRIO_MIN - 1 - (PRI_MAX_REALTIME - kproc->ki_pri.pri_level);
      } else {
         proc->nice = PRIO_MAX + 1 + kproc->ki_pri.pri_level - PRI_MIN_IDLE;
      }

https://github.com/htop-dev/htop/blob/main/freebsd/FreeBSDProcessTable.c#L251

top from FreeBSD base displays - in nice column for kernel processes. Should htop follows the same behavior?

@BenBE BenBE added bug 🐛 Something isn't working FreeBSD 👹 FreeBSD related issues labels Feb 12, 2024
@BenBE BenBE added this to the 3.4.0 milestone Feb 12, 2024
@BenBE
Copy link
Member

BenBE commented Feb 12, 2024

I think the values technically make sense: The idle process has the lowest priority (i.e. when nothing else can run) while the kernel processes below it have the highest priorities available to immediately run whenever there's work to do.

Also, why hide them? The wording in the documentation could be refined to include the word "usually" before the range. Furthermore the display of the column could be fixed to avoid breakage with these extreme priorities.

@vchimishuk
Copy link
Author

vchimishuk commented Feb 12, 2024

Also, why hide them?

Because it shows some synthetic priority now -- not nice value. And it is even hard to say what the value is. PRI column should be displaying scheduling priority, but they do not match now.
I haven't dug into details, but it looks like Linux uses nice value internally for kernel threads but FreeBSD doesn't, what makes their top's output different: - on FreeBSD and some -20..19 value on Linux.

The wording in the documentation could be refined to include the word "usually" before the range.

Do you mean htop's documentation? In this case nice in htop and nice in UNIX-wide sense will not match, it will matter completely different things. Because nice in UNIX cannot be out of -20..19 range.

My understanding is:

  • htop tries to display (calculate manually) process' internal priority instead of nice value. It duplicates PRI column in this sense. And it does it only on FreeBSD and only for some processes (kernel threads without nice value).
  • I'd expect values in top and htop to be equal.
  • nice is not applicable for FreeBSD kernel threads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working FreeBSD 👹 FreeBSD related issues
Projects
None yet
Development

No branches or pull requests

2 participants