编写系统调用来计算进程的上下文切换

发布于 2025-01-06 00:32:49 字数 94 浏览 5 评论 0原文

我必须做一个系统调用来计算自愿的人数和人数。进程的非自愿上下文切换。我已经知道向 Linux 内核添加新系统调用的步骤,但我不知道应该从哪里开始上下文切换功能。有什么想法吗?

I have to do a system call to count the voluntary & involuntary context switches of a process. I already know the steps to add a new system call to a linux kernel but i have no clue of where i should start for the context-switch function. Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花海 2025-01-13 00:32:49

如果您的系统调用只应报告统计信息,则可以使用内核中已有的上下文切换计数代码。

wait3 系统调用getrusage syscall 已经在 struct rusage 字段中报告上下文切换计数:

struct rusage {
 ...
    long   ru_nvcsw;         /* voluntary context switches */
    long   ru_nivcsw;        /* involuntary context switches */
};

您可以通过运行来尝试:

$ /usr/bin/time -v /bin/ls -R
....
    Voluntary context switches: 1669
    Involuntary context switches: 207

where "/bin/ls -R”是任何程序。

通过在内核源代码中搜索“struct rusage”,您可以找到 kernel/sys.c 中的这个 accumulate_thread_rusage 会更新 rusage 结构体。它从 struct task_struct *t 读取;字段 t->nvcsw;t->nivcsw;

1477  static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1478  {
1479        r->ru_nvcsw += t->nvcsw;    // <<=== here
1480        r->ru_nivcsw += t->nivcsw;
1481        r->ru_minflt += t->min_flt;
1482        r->ru_majflt += t->maj_flt;

那么您应该搜索 nvcswnivcsw 在内核文件夹中查找内核如何更新它们。

asmlinkage void __sched Schedule(void)

4124     if (likely(prev != next)) {         // <= if we are switching between different tasks
4125            sched_info_switch(prev, next);
4126            perf_event_task_sched_out(prev, next);
4127
4128            rq->nr_switches++;          
4129            rq->curr = next;
4130            ++*switch_count;     // <= increment nvcsw or nivcsw via pointer
4131
4132            context_switch(rq, prev, next); /* unlocks the rq */

指针 < code>switch_count 来自 第 4091 行行[第 4111 章]

PS:来自 perreal 的链接很棒: http://oreilly.com/catalog/linuxkernel/chapter /ch10.html(搜索context_swtch

If your syscall should only report statistics, you can use context switch counting code that is already in the kernel.

wait3 syscall or getrusage syscall already reports context switch count in struct rusage fields:

struct rusage {
 ...
    long   ru_nvcsw;         /* voluntary context switches */
    long   ru_nivcsw;        /* involuntary context switches */
};

You can try it by running:

$ /usr/bin/time -v /bin/ls -R
....
    Voluntary context switches: 1669
    Involuntary context switches: 207

where "/bin/ls -R" is any program.

By searching an "struct rusage" in kernel sources, you can find this accumulate_thread_rusage in kernel/sys.c, which updates rusage struct. It reads from struct task_struct *t; the fields t->nvcsw; and t->nivcsw;:

1477  static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1478  {
1479        r->ru_nvcsw += t->nvcsw;    // <<=== here
1480        r->ru_nivcsw += t->nivcsw;
1481        r->ru_minflt += t->min_flt;
1482        r->ru_majflt += t->maj_flt;

Then you should search nvcsw and nivcsw in kernel folder to find how they are updated by kernel.

asmlinkage void __sched schedule(void):

4124     if (likely(prev != next)) {         // <= if we are switching between different tasks
4125            sched_info_switch(prev, next);
4126            perf_event_task_sched_out(prev, next);
4127
4128            rq->nr_switches++;          
4129            rq->curr = next;
4130            ++*switch_count;     // <= increment nvcsw or nivcsw via pointer
4131
4132            context_switch(rq, prev, next); /* unlocks the rq */

Pointer switch_count is from line 4091 or line 4111 of the same file.

PS: Link from perreal is great: http://oreilly.com/catalog/linuxkernel/chapter/ch10.html (search context_swtch)

↙温凉少女 2025-01-13 00:32:49

这已经存在:虚拟文件 /proc/NNNN/status (其中 NNNN 是您想要了解的进程的十进制进程 ID)包含自愿和非自愿上下文的计数等开关。与 getrusage 不同,它允许您了解任何进程(而不仅仅是子进程)的上下文切换计数。请参阅proc(5) 联机帮助页了解更多详细信息。

This already exists: the virtual file /proc/NNNN/status (where NNNN is the decimal process ID of the process you want to know about) contains, among other things, counts of both voluntary and involuntary context switches. Unlike getrusage this allows you to learn the context switch counts for any process, not just children. See the proc(5) manpage for more details.

仙气飘飘 2025-01-13 00:32:49

进程会在阻塞、时间片到期或中断等情况下进行上下文切换。最终调用 Schedule() 函数。由于您想单独计算每个进程的数量,因此必须为每个进程保留一个新变量来计算上下文切换的数量。您可以在当前进程的 Schedule fun 中每次更新此变量。使用系统调用您可以读取该值。这是 pintos 的调度函数的片段,

static void
schedule (void) 
{
  struct thread *cur = running_thread ();
  struct thread *next = next_thread_to_run ();
  struct thread *prev = NULL;

  ASSERT (intr_get_level () == INTR_OFF);
  ASSERT (cur->status != THREAD_RUNNING);
  ASSERT (is_thread (next));<br/>

  if (cur != next)
    prev = switch_threads (cur, next);  <== here you can update count of "cur"   
  thread_schedule_tail (prev);
}  

A process will make a context switch in case of blocking, time quantum expiring or for interrupts etc. Eventually schedule() function is called. Since you want to count it for each process separately you have to keep a new variable for each process for counting the no of context switches. And you can update this variable each time in schedule fun for current process. Using your system call you can read this value. Here is a snippet of the schedule function of pintos,

static void
schedule (void) 
{
  struct thread *cur = running_thread ();
  struct thread *next = next_thread_to_run ();
  struct thread *prev = NULL;

  ASSERT (intr_get_level () == INTR_OFF);
  ASSERT (cur->status != THREAD_RUNNING);
  ASSERT (is_thread (next));<br/>

  if (cur != next)
    prev = switch_threads (cur, next);  <== here you can update count of "cur"   
  thread_schedule_tail (prev);
}  
不念旧人 2025-01-13 00:32:49

上下文切换总数

cat /proc/PID/sched|grep nr_switches

自愿上下文切换

cat /proc/PID/sched | grep nr_known_switches

非自愿上下文切换

cat /proc/PID/sched|grep nr_involent_switches

其中 PID 是您希望监视的进程的进程 ID。

但是,如果您想通过修补(创建挂钩)Linux 源代码来获取这些统计信息,则与调度相关的代码位于

kernel/sched/

源代码树的 文件夹中。
特别是

kernel/sched/core.c包含schedule()函数,它是linux调度程序的代码。
CFS(完全公平调度程序)的代码是 Linux 中存在的几种调度程序之一,最常用的代码位于

/kernel/sched/fair.c

中,当设置 TIF_NEED_RESCHED 标志时,就会执行 Scheduler() ,因此找出设置此标志的所有位置(在 Linux 源代码上使用 cscope),这将使您深入了解进程发生的上下文切换的类型。

Total number of context switches

cat /proc/PID/sched|grep nr_switches

Voluntary context switches

cat /proc/PID/sched | grep nr_voluntary_switches

Involuntary context switches

cat /proc/PID/sched|grep nr_involuntary_switches

where PID is process ID of the process you wish to monitor.

However if you want to get these statistics by patching (creating a hook) linux source, the code related to scheduling is present in

kernel/sched/

folder of the source tree.
In particular

kernel/sched/core.c contains the schedule() function, which is the code of linux scheduler.
The code of CFS (completely fair scheduler), which is one of the several schedulers present in Linux, and is most commonly used is present in

/kernel/sched/fair.c

scheduler() is executed when ever TIF_NEED_RESCHED flag is set, so find out from which all places this flag is being set (use cscope on linux source) which will give you an insight intsight into the types of context switches occurring for a process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文