Shu Zhong Hua
Shu Zhong Hua

Reputation: 9

Linux posix timer cause high total CPU load

I wrote a simple Linux program (timertest) with 500us POSIX timer and running it in Dual-core ARM Cortex-A9 processor. The process timertest takes about 25% CPU load, that's ok. But sometimes the total CPU load is very high (100%). If I kill timertest process, the total CPU load decreased to 1%. I expect the total CPU load is same as timertest process because there is no other high load process running?

I tried with different Linux timer (e.g., setitimer) and top command (e.g., htop, procps-ng top). But it's same result.

$ timertest
Busy time: 92 us
posix_timer: 0
posix_timer: 1
...
$ htop
    0[####****************************100.0%] Tasks: 13, 0 thr, 45 kthr; 1 running
    1[#                                 0.7%] Load average: 0.76 0.25 0.09
  Mem[|#*@                       15.3M/1003M] Uptime: 00:02:16
  Swp[                                 0K/0K]

  PID USER       PRI  NI  VIRT   RES   SHR S CPU%-MEM%   TIME+  Command
  229 root        20   0  3480  2356  2220 R 25.8  0.2  0:19.73 timertest
  231 root        20   0  3216  2428  1828 R  0.7  0.2  0:00.26 htop
    1 root        20   0  1580  1172  1088 S  0.0  0.1  0:01.68 init [5]
    2 root        20   0     0     0     0 S  0.0  0.0  0:00.00 kthreadd
    3 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 rcu_gp
    4 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 rcu_par_gp
    5 root        20   0     0     0     0 I  0.0  0.0  0:00.00 kworker/0:0-rcu_gp
    6 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 kworker/0:0H-events_highpr
    7 root        20   0     0     0     0 I  0.0  0.0  0:00.00 kworker/u4:0-events_unboun
    8 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 mm_percpu_wq
    9 root        20   0     0     0     0 S  0.0  0.0  0:00.00 rcu_tasks_rude_
   10 root        20   0     0     0     0 S  0.0  0.0  0:00.00 ksoftirqd/0
   11 root        20   0     0     0     0 I  0.0  0.0  0:00.01 rcu_sched
   12 root        RT   0     0     0     0 S  0.0  0.0  0:00.00 migration/0
   13 root        20   0     0     0     0 S  0.0  0.0  0:00.00 cpuhp/0
   14 root        20   0     0     0     0 S  0.0  0.0  0:00.00 cpuhp/1
   15 root        RT   0     0     0     0 S  0.0  0.0  0:00.00 migration/1
   16 root        20   0     0     0     0 S  0.0  0.0  0:00.00 ksoftirqd/1
   17 root        20   0     0     0     0 I  0.0  0.0  0:00.00 kworker/1:0-events
   18 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 kworker/1:0H-events_highpr
   19 root        20   0     0     0     0 S  0.0  0.0  0:00.00 kdevtmpfs
   20 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 netns
   21 root        20   0     0     0     0 I  0.0  0.0  0:00.00 kworker/0:1-events_long
   22 root        20   0     0     0     0 S  0.0  0.0  0:00.00 khungtaskd
   23 root        20   0     0     0     0 S  0.0  0.0  0:00.00 oom_reaper
   24 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 writeback
   25 root        20   0     0     0     0 S  0.0  0.0  0:00.00 kcompactd0
   26 root        20   0     0     0     0 I  0.0  0.0  0:00.05 kworker/u4:1-events_unboun
   31 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 kblockd
   32 root       -51   0     0     0     0 S  0.0  0.0  0:00.00 watchdogd
   33 root        20   0     0     0     0 I  0.0  0.0  0:00.03 kworker/1:1-mm_percpu_wq
   34 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 rpciod
   35 root         0 -20     0     0     0 I  0.0  0.0  0:00.05 kworker/0:1H-kblockd
   36 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 kworker/u5:0
   37 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 xprtiod
   38 root        20   0     0     0     0 S  0.0  0.0  0:00.00 kswapd0
   39 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 nfsiod
   40 root        20   0     0     0     0 I  0.0  0.0  0:00.04 kworker/0:2-mm_percpu_wq
   41 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 stmmac_wq
   43 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 ipv6_addrconf
   44 root         0 -20     0     0     0 I  0.0  0.0  0:00.03 kworker/1:1H-kblockd
   45 root        20   0     0     0     0 I  0.0  0.0  0:00.00 kworker/0:3-events
   46 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 mmc_complete
   47 root        20   0     0     0     0 S  0.0  0.0  0:00.00 jbd2/mmcblk0p2-
   48 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 ext4-rsv-conver
   51 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 kworker/1:2H
   78 root        20   0  2744  1464  1116 S  0.0  0.1  0:00.11 /sbin/udevd -d
   95 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 kworker/0:2H-mmc_complete
   96 root        20   0     0     0     0 I  0.0  0.0  0:00.00 kworker/u4:2-events_unboun
  194 root        20   0  2156  1168  1072 S  0.0  0.1  0:03.01 /usr/sbin/dropbear -r /etc
  202 root        20   0  2344  1492  1408 S  0.0  0.1  0:00.01 /sbin/syslogd -n -O /var/l
  205 root        20   0  2344  1512  1408 S  0.0  0.1  0:00.01 /sbin/klogd -n
  209 root        20   0  2100  1180  1084 S  0.0  0.1  0:00.00 /usr/sbin/vsftpd
  214 root        20   0  2344  1496  1408 S  0.0  0.1  0:00.00 /bin/sh /bin/start_getty 1
  215 root        20   0  2344  1432  1344 S  0.0  0.1  0:00.00 /sbin/getty 38400 tty1
  217 root        20   0  2432  1684  1556 S  0.0  0.2  0:00.02 -sh
  221 root        20   0  2244  1592  1420 S  0.0  0.2  0:00.25 /usr/sbin/dropbear -r /etc
  222 root        20   0  2432  1680  1556 S  0.0  0.2  0:00.01 -sh
#include <csignal>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

namespace {

void busyCpu()
{
    volatile long long i;
    for (i = 0; i < 9000; ++i)
    {
        // Loop 9000: 93 us / CPU 23 %  (Linux arrow-sockit 5.10.100-altera)
    }
}

void posixTimerSignal(int sig, siginfo_t *si, void *pri)
{
    static int i = 0;
    static int count = 0;

    busyCpu();
    if (i++ == 2000)
    {
        ::printf("posix_timer: %d\n", count++);
        i = 0;
    }
}

void posixTimerTest()
{
    timer_t timer = {0};
    struct sigevent sev = {0};
    struct sigaction sa = {0};

    const struct timespec timeVal = {
          .tv_sec = 0,
          .tv_nsec = 500 * 1000 // 500 us
    };

    const struct itimerspec its =
    {
        .it_interval = timeVal,
        .it_value = timeVal
    };

    sev.sigev_notify = SIGEV_SIGNAL;
    sev.sigev_signo = SIGRTMIN;
    sev.sigev_value.sival_ptr = &timer;

    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = posixTimerSignal;

    if(::sigemptyset(&sa.sa_mask) == -1)
    {
        ::perror("sigemptyset error");
    }

    if(::sigaction(SIGRTMIN, &sa, nullptr) == -1)
    {
        ::perror("sigaciton error");
    }

    if(::timer_create(CLOCK_REALTIME, &sev, &timer) != 0)
    {
        ::perror("timer_create error");
    }

    if(::timer_settime(timer, 0, &its, nullptr) != 0)
    {
        ::perror("timer_settime error");
    }

    while (true)
    {
        ::pause();
    }

}

}
int main(int argc, char* argv[])
{
    ::printf("Timer test cpp program started\n");
    posixTimerTest();
    return 0;
}
$ uname -a
Linux arrow-sockit 5.10.100-altera #1 SMP Tue Jun 14 16:39:33 UTC 2022 armv7l GNU/Linux
$ cat /proc/cpuinfo
processor       : 0
model name      : ARMv7 Processor rev 0 (v7l)
BogoMIPS        : 200.00
Features        : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x3
CPU part        : 0xc09
CPU revision    : 0

processor       : 1
model name      : ARMv7 Processor rev 0 (v7l)
BogoMIPS        : 200.00
Features        : half thumb fastmult vfp edsp thumbee neon vfpv3 tls vfpd32
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x3
CPU part        : 0xc09
CPU revision    : 0

Hardware        : Altera SOCFPGA
Revision        : 0000
Serial          : 0000000000000000

Update: If I change the timer interval from 500 us to 499 us, the total CPU load is same as process CPU load (24%).


    0[*                            0.7%] Tasks: 13, 0 thr, 41 kthr; 1 running
    1[#######**                   23.8%] Load average: 0.01 0.01 0.00
  Mem[|#*@                  15.2M/1003M] Uptime: 12:03:14
  Swp[                            0K/0K]

  PID USER       PRI  NI  VIRT   RES   SHR S CPU%-MEM%   TIME+  Command
  437 root        20   0  3480  2356  2220 R 24.5  0.2  0:01.58 ./timertest -p -
  355 root        20   0  3088  2312  1828 R  0.7  0.2  0:31.52 htop
    1 root        20   0  1580  1172  1088 S  0.0  0.1  0:02.24 init [5]
    2 root        20   0     0     0     0 S  0.0  0.0  0:00.01 kthreadd
    3 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 rcu_gp
    4 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 rcu_par_gp
    8 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 mm_percpu_wq
    9 root        20   0     0     0     0 S  0.0  0.0  0:00.00 rcu_tasks_rude_
   10 root        20   0     0     0     0 S  0.0  0.0  0:00.12 ksoftirqd/0
   11 root        20   0     0     0     0 I  0.0  0.0  0:00.88 rcu_sched
   12 root        RT   0     0     0     0 S  0.0  0.0  0:00.00 migration/0
   13 root        20   0     0     0     0 S  0.0  0.0  0:00.00 cpuhp/0
   14 root        20   0     0     0     0 S  0.0  0.0  0:00.00 cpuhp/1
   15 root        RT   0     0     0     0 S  0.0  0.0  0:00.00 migration/1
   16 root        20   0     0     0     0 S  0.0  0.0  0:00.24 ksoftirqd/1
   19 root        20   0     0     0     0 S  0.0  0.0  0:00.00 kdevtmpfs
   20 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 netns
   22 root        20   0     0     0     0 S  0.0  0.0  0:00.01 khungtaskd
   23 root        20   0     0     0     0 S  0.0  0.0  0:00.00 oom_reaper
   24 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 writeback
   25 root        20   0     0     0     0 S  0.0  0.0  0:01.16 kcompactd0
   31 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 kblockd
   32 root       -51   0     0     0     0 S  0.0  0.0  0:00.11 watchdogd
   34 root         0 -20     0     0     0 I  0.0  0.0  0:00.00 rpciod

Upvotes: 0

Views: 83

Answers (0)

Related Questions