• X
    sched/cputime: use sched idle time accounting · f7b617be
    Xie XiuQi 提交于
    hulk inclusion
    category: bugfix
    bugzilla: 13257
    CVE: NA
    
    -------------------------------------------------
    
    Interduce "use-sched-idle-time" option to enable/disable
    sched idle time accounting instead of tick based time accounting.
    
    nohz mode: use get_idle_time;
    nohz=off mode: use idle time accounting in sched_info_switch function.
    
    And you can also use "use-sched-idle-time=force" force to use
    idle time accountint in sched_info_switch function.
    
    guarantee:
    cpu_clock(cpu) - idle == utime + stime
    
    We use this time in /proc/stat.
    
    We use get_idle_time instead of collecting idle time when
    context switch. And get_idle_time is available when nohz is
    enabled. So do not collect idle time when nohz is enabled.
    
    Issue 1:
    1) boot euleros
    2) bind a kernel thread to a cpu (eg. cpu1), run a long time (more than 10mins)
    3) stop this kernel thread
    4) run a spin loop process in userspace
    5) then, the utime is 0, but the stime is ~100%
    
    for example:
    stime = 10000; utime = 1; rtime = 100001;
    Run 10s in userspace now, then
    
    rtime = clock - idle , (10001 + 10 = 10011)
    stime = 10 * 10000/(1 + 10 + 10000) ≈ 10
    utime = 10 * (1 + 10)/(1 + 10 + 10000) ≈ 0
    
    That's the problem.
    
    We just adjust the delta time to reduce the accumulated:
    error of stime/utime:
    delta = rtime - (utime + stime)
    stime += delta * delta_of_stat_stime / (delta_of_stat_stime + delta_of_stat_utime)
    utime = rtime - stime
    
    Issue 2:
    When cpu is offline we don't need to adjust the user and system time.
    Only cpu is online we could asume:
    
    utime% + stime% + idle% = 100%.
    
    Issue 3:
    When nohz=off, we must add 'now - last_entry_idle_time' to sum_idle_time.
    Tested-by: NXie Zhipeng <xiezhipeng1@huawei.com>
    Signed-off-by: NXie XiuQi <xiexiuqi@huawei.com>
    Signed-off-by: NYang Yingliang <yangyingliang@huawei.com>
    f7b617be
stat.c 5.9 KB