gpt4 book ai didi

linux - Linux 上的意外任务切换,尽管实时且不错 -20

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:13 25 4
gpt4 key购买 nike

我有一个程序需要以 100% 的性能执行,但我发现它有时会暂停超过 20 微秒。我为此苦苦挣扎了一段时间,找不到原因/解释。

所以我的问题是:为什么我的程序时不时地“暂停”/“停止”20 uSec?

为了对此进行调查,我编写了以下小程序:

#include <string.h>
#include <iostream>
#include <signal.h>

using namespace std;

unsigned long long get_time_in_ns(void)
{
struct timespec tmp;
if (clock_gettime(CLOCK_MONOTONIC, &tmp) == 0)
{
return tmp.tv_sec * 1000000000 + tmp.tv_nsec;
}
else
{
exit(0);
}
}

bool go_on = true;

static void Sig(int sig)
{
(void)sig;
go_on = false;
}

int main()
{
unsigned long long t1=0;
unsigned long long t2=0;
unsigned long long t3=0;
unsigned long long t4=0;
unsigned long long t5=0;
unsigned long long t2saved=0;
unsigned long long t3saved=0;
unsigned long long t4saved=0;
unsigned long long t5saved=0;

struct sigaction sig;
memset(&sig, 0, sizeof(sig));
sig.sa_handler = Sig;
if (sigaction(SIGINT, &sig, 0) < 0)
{
cout << "sigaction failed" << endl;
return 0;
}

while (go_on)
{
t1 = get_time_in_ns();
t2 = get_time_in_ns();
t3 = get_time_in_ns();
t4 = get_time_in_ns();
t5 = get_time_in_ns();
if ((t2-t1)>t2saved) t2saved = t2-t1;
if ((t3-t2)>t3saved) t3saved = t3-t2;
if ((t4-t3)>t4saved) t4saved = t4-t3;
if ((t5-t4)>t5saved) t5saved = t5-t4;
cout <<
t1 << " " <<
t2-t1 << " " <<
t3-t2 << " " <<
t4-t3 << " " <<
t5-t4 << " " <<
t2saved << " " <<
t3saved << " " <<
t4saved << " " <<
t5saved << endl;
}

cout << endl << "Closing..." << endl;

return 0;
}

程序只是测试调用函数“get_time_in_ns”需要多长时间。该程序连续执行 5 次。该程序还跟踪测量的最长时间。

通常调用该函数需要 30 ns,但有时需要长达 20000 ns。我不明白。

程序输出的一小部分是:

8909078678739 37 29 28 28 17334 17164 17458 18083
8909078680355 36 30 29 28 17334 17164 17458 18083
8909078681947 38 28 28 27 17334 17164 17458 18083
8909078683521 37 29 28 27 17334 17164 17458 18083
8909078685096 39 27 28 29 17334 17164 17458 18083
8909078686665 37 29 28 28 17334 17164 17458 18083
8909078688256 37 29 28 28 17334 17164 17458 18083
8909078689827 37 27 28 28 17334 17164 17458 18083

输出显示正常调用时间约为。 30ns(第2至5列),但最大时间接近20000ns(第6至9列)。

我这样启动程序:

chrt -f 99 nice -n -20 myprogram

知道为什么调用有时需要 20000 纳秒,而通常需要 30 纳秒吗?

程序在双 Xeon(每台 8 核)机器上执行。

我使用 SSH 连接。

热门节目:

 PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
8107 root rt -20 16788 1448 1292 S 3.0 0.0 0:00.88 myprogram
2327 root 20 0 69848 7552 5056 S 1.3 0.0 0:37.07 sshd

最佳答案

即使是最低的 niceness 值也不是实时优先级 — 它仍在策略 SCHED_OTHER 中,这是一种循环分时策略。您需要使用 sched_setscheduler() 切换到实时调度策略,根据需要可以是 SCHED_FIFOSCHED_RR

请注意,如果它不是唯一运行的任务,它仍然不会为您提供绝对 100% 的 CPU。如果您不间断地运行任务,Linux 仍会将百分之几的 CPU 时间分配给非实时任务,这样失控的 RT 任务就不会有效地挂起机器。当然,需要 100% CPU 时间的实时任务不太可能正确执行。

编辑:鉴于该进程已经使用 RT 调度程序运行(不错的值仅与 SCHED_OTHER 相关,因此另外设置这些值毫无意义)正如所指出的,我的其余答案仍然适用于其他任务仍在运行的方式和原因(请记住,还有许多内核任务)。

唯一比这更好的方法可能是将一个 CPU 核心专用于任务以充分利用它。显然这只适用于多核 CPU。这里有一个与此相关的问题:Whole one core dedicated to single process

关于linux - Linux 上的意外任务切换,尽管实时且不错 -20,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30243153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com