performance - Unexpected task switch on Linux despite of real time and nice -20 -


i have program needs execute 100% performance see paused more 20 usec. i've struggled while , can't find reason/explanation.

so question is: why program "paused"/"stalled" 20 usec every , then?

to investigate wrote following small program:

#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; } 

the program test how long time takes call function "get_time_in_ns". program 5 times in row. program tracks longest time measured.

normally takes 30 ns call function takes long 20000 ns. don't understand.

a little part of program output is:

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 

the output shows normal call time approx. 30ns (column 2 5) largest time 20000ns (column 6 9).

i start program this:

chrt -f 99 nice -n -20 myprogram 

any ideas why call takes 20000ns when takes 30ns?

the program executed on dual xeon (8 cores each) machine.

i connect using ssh.

top shows:

 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 

even lowest value of niceness not real time priority — still in policy sched_other, round-robin time-sharing policy. need switch real time scheduling policy sched_setscheduler(), either sched_fifo or sched_rr required.

note that still not give absolute 100% cpu if isn't task running. if run task without interruption, linux still grant few percent of cpu time non-real time tasks runaway rt task not hang machine. of course, real time task needing 100% cpu time unlikely perform correctly.

edit: given process runs rt scheduler (nice values relevant sched_other, it's pointless set in addition) pointed out, rest of answer still applies how , why other tasks still being run (remember there number kernel tasks).

the way better dedicating 1 cpu core task out of it. works on multi-core cpus. there question related here: whole 1 core dedicated single process


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -