是否保证在每次切换进程时调用kernel / sched.c / context_switch()?

时间:2021-08-17 02:16:45

I want to alter the Linux kernel so that every time the current PID changes - i.e., a new process is switched in - some diagnostic code is executed (detailed explanation below, if curious). I did some digging around, and it seems that every time the scheduler chooses a new process, the function context_switch() is called, which makes sense (this is just from a cursory analysis of sched.c/schedule() ).

我想改变Linux内核,以便每次当前PID改变 - 即,接入新进程 - 执行一些诊断代码(下面的详细解释,如果好奇的话)。我做了一些挖掘,似乎每次调度程序选择一个新进程时,都会调用函数context_switch(),这是有意义的(这只是对sched.c / schedule()的粗略分析)。

The problem is, the Linux scheduler is basically black magic to me right now, so I'd like to know if that assumption is correct. Is it guaranteed that, every time a new process is selected to get some time on the CPU, the context_switch() function is called? Or are there other places in the kernel source where scheduling could be handled in other situations? (Or am I totally misunderstanding all this?)

问题是,Linux调度程序现在对我来说基本上是黑魔法,所以我想知道这个假设是否正确。是否保证每次选择新进程以在CPU上获得一些时间时,都会调用context_switch()函数?或者内核源中是否有其他位置可以在其他情况下处理调度? (或者我完全误解了这一切?)

To give some context, I'm working with the MARSS x86 simulator trying to do some instrumentation and measurement of certain programs. The problem is that my instrumentation needs to know which executing process certain code events correspond to, in order to avoid misinterpreting the data. The idea is to use some built-in message passing systems in MARSS to pass the PID of the new process on every context switch, so it always knows what PID is currently in execution. If anyone can think of a simpler way to accomplish that, that would also be greatly appreciated.

为了给出一些背景信息,我正在使用MARSS x86模拟器来尝试对某些程序进行一些检测和测量。问题是我的仪器需要知道某些代码事件对应于哪个执行过程,以避免误解数据。我们的想法是使用MARSS中的一些内置消息传递系统在每个上下文切换时传递新进程的PID,因此它始终知道当前正在执行的PID。如果有人能想到一种更简单的方法来实现这一点,那也将非常感激。

1 个解决方案

#1


4  

Yes, you are correct.

是的,你是对的。

The schedule() will call context_switch() which is responsible for switching from one task to another when the new process has been selected by schedule().

schedule()将调用context_switch(),它负责在schedule()选择新进程时从一个任务切换到另一个任务。

context_switch() basically does two things. It calls switch_mm() and switch_to().

context_switch()基本上做了两件事。它调用switch_mm()和switch_to()。

switch_mm() - switch to the virtual memory mapping for the new process

switch_mm() - 切换到新进程的虚拟内存映射

switch_to() - switch the processor state from the previous process to the new process (save/restore registers, stack info and other architecture specific things)

switch_to() - 将处理器状态从上一个进程切换到新进程(保存/恢复寄存器,堆栈信息和其他特定于体系结构的东西)

As for your approach, I guess it's fine. It's important to keep things nice and clean when working with the kernel, and try to keep it relatively easy until you gain more knowledge.

至于你的方法,我想这很好。在使用内核时保持良好和干净是很重要的,并且在获得更多知识之前,尽量保持相对简单。

#1


4  

Yes, you are correct.

是的,你是对的。

The schedule() will call context_switch() which is responsible for switching from one task to another when the new process has been selected by schedule().

schedule()将调用context_switch(),它负责在schedule()选择新进程时从一个任务切换到另一个任务。

context_switch() basically does two things. It calls switch_mm() and switch_to().

context_switch()基本上做了两件事。它调用switch_mm()和switch_to()。

switch_mm() - switch to the virtual memory mapping for the new process

switch_mm() - 切换到新进程的虚拟内存映射

switch_to() - switch the processor state from the previous process to the new process (save/restore registers, stack info and other architecture specific things)

switch_to() - 将处理器状态从上一个进程切换到新进程(保存/恢复寄存器,堆栈信息和其他特定于体系结构的东西)

As for your approach, I guess it's fine. It's important to keep things nice and clean when working with the kernel, and try to keep it relatively easy until you gain more knowledge.

至于你的方法,我想这很好。在使用内核时保持良好和干净是很重要的,并且在获得更多知识之前,尽量保持相对简单。