-1

Is the process scheduler "visible" at user space? Can I see it with ps? Maybe is it one of the kernel threads? If yes, how is it called? How can I "see" it?

It doesn’t exist as a separate thread, or module, it’s implemented as a function

Ok, but how and where and in which way is this function run? Is there a way to track it and see it?

Allexj
  • 265

1 Answers1

1

As explained in What's the process scheduler in Linux? the scheduler is a kernel function, __schedule; it doesn’t appear as a separate thread or process.

The function comment explains how it is run:

 * The main means of driving the scheduler and thus entering this function are:
 *
 *   1. Explicit blocking: mutex, semaphore, waitqueue, etc.
 *
 *   2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
 *      paths. For example, see arch/x86/entry_64.S.
 *
 *      To drive preemption between tasks, the scheduler sets the flag in timer
 *      interrupt handler scheduler_tick().
 *
 *   3. Wakeups don't really cause entry into schedule(). They add a
 *      task to the run-queue and that's it.
 *
 *      Now, if the new task added to the run-queue preempts the current
 *      task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
 *      called on the nearest possible occasion:
 *
 *       - If the kernel is preemptible (CONFIG_PREEMPTION=y):
 *
 *         - in syscall or exception context, at the next outmost
 *           preempt_enable(). (this might be as soon as the wake_up()'s
 *           spin_unlock()!)
 *
 *         - in IRQ context, return from interrupt-handler to
 *           preemptible context
 *
 *       - If the kernel is not preemptible (CONFIG_PREEMPTION is not set)
 *         then at the next:
 *
 *          - cond_resched() call
 *          - explicit schedule() call
 *          - return from syscall or exception to user-space
 *          - return from interrupt-handler to user-space

The function can’t be traced; its instrumentation is disabled (see the notrace entry in its declaration).

Stephen Kitt
  • 434,908
  • if it is a kernel function, the kernel is "invisible"...? Does it fight against other processes/threads for cpu time? How is being executed? – Allexj Apr 16 '22 at 14:24
  • 1
    The kernel isn’t invisible, it’s the “system” part of CPU time (e.g. in top). Kernel code is executed whenever a process calls it (through a system call) or whenever an interrupt occurs. It doesn’t fight with processes for CPU time, it enables processes to do their work. – Stephen Kitt Apr 16 '22 at 14:28
  • thanks for answer. it’s the “system” part of CPU time (e.g. in top). what do you mean? – Allexj Apr 16 '22 at 14:37
  • 1
    Run top, and look at the “sy” value in the “%Cpu(s)” line — that’s the system time, “sy, system: time running kernel processes” (in the top man page). You’d also need to add “hi” and “si”, the time spent servicing interrupts. IIRC this doesn’t count the time spent handling system calls; that’s done in service of the calling process and counted against that process. You can see the time spent in system calls by running time. – Stephen Kitt Apr 16 '22 at 14:40
  • thanks a lot!! time always show me 0 though... anyway, one question: why do you say that kernel doesn't fight with processes for CPU time? this sy value actually shows that this is time that processor execute the kernel code, not user program code... so in some way kernel also fights for cpu time... another question: are these sy things invisible at user space? they are not threads/processes so they are not "visible" for the user, right? – Allexj Apr 16 '22 at 14:53
  • 1
    OK, so yes, the time spent running the kernel is time not available to run user processes, but it’s handling essential work so I don’t really see it as a fight; in other words, the system can’t choose not to run the kernel when it needs to, otherwise nothing else happens. sy covers time spent in kernel threads, which are visible as processes; they are all children of the kthreadd process (pid 2). – Stephen Kitt Apr 16 '22 at 15:37
  • thanks a lot for your help... but you said that process scheduler is not a thread... and then you said that kernel can be seen in sy section with top... but now you say that sy shows kernel threads...... so I am confused... – Allexj Apr 16 '22 at 16:17
  • 1
    The scheduler is a small part of the kernel. It doesn’t show up as a kernel thread, but other tasks in the kernel are handled as threads. My comment about the kernel not being invisible was about the kernel as a whole. – Stephen Kitt Apr 16 '22 at 16:22
  • Thanks again. So sy does not cover all kernel actions (for example, not the scheduler), am I wrong? – Allexj Apr 19 '22 at 08:41