nanosleep高cpu使用率? [英] nanosleep high cpu usage?

查看:593
本文介绍了nanosleep高cpu使用率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到一个名为nanosleep的小型测试程序在内核高于2.6.22的Linux计算机上运行时显示出CPU使用率的巨大差异.

I noticed that a little test program which calls nanosleep is showing a huge difference in CPU usage when run on Linux machines with a kernel newer than 2.6.22.

#include <time.h>
int main (void)
{
    struct timespec sleepTime;
    struct timespec returnTime;
    sleepTime.tv_sec = 0;
    sleepTime.tv_nsec = 1000;
    while (1)
    {
      nanosleep(&sleepTime, &returnTime);
    }
    return 0;
}

(是的,我知道这个程序什么也不做)

(Yes, I realise this program does nothing)

如果我对此进行编译并在openSUSE 10.3计算机(2.6.22.19-0.2-default)上运行,该程序甚至不会显示在"top"生成的进程列表中,这表明我正在使用很少的CPU时间.如果我在openSUSE 11.1机器(2.6.27.23-0.1-default)上运行它,则top显示该程序占用40%的CPU时间.在Fedora 9(2.6.25-14.fc9.i686)和Fedora 10上运行时,在顶部"也显示出相同的高CPU使用率.

If I compile this and run it on an openSUSE 10.3 machine (2.6.22.19-0.2-default), the program does not even show up on the process list generated by "top", indicating to me that it is using very little CPU time. If I run it on an openSUSE 11.1 machine (2.6.27.23-0.1-default), top shows the program taking 40% of the CPU time. Running on Fedora 9 (2.6.25-14.fc9.i686) and Fedora 10 also showed the same high CPU usage in "top".

内核中是否有更改会影响到这一点?

Has there been a change in the kernel that affects this?

推荐答案

这是由于在主线计划程序中引入了NO_HZ.

This is due to the introduction of NO_HZ into the mainline scheduler.

以前,您的1,000 ns睡眠时间通常是整个睡眠时间-1,000,000 ns.现在,当机器处于空闲状态时,它实际上只是按照您的要求进行睡眠.因此,它运行while()循环和syscall的频率大约高出1000倍-因此,CPU使用率更高.如果增加tv_nsec,应该会看到CPU使用率的降低.

Previously, your 1,000 ns sleep was usually sleeping for a whole tick - 1,000,000 ns. Now, when the machine is otherwise idle, it's actually only sleeping for what you asked for. So it's running the while() loop and syscall around 1,000 times more frequently - hence a lot more CPU usage. If you increase tv_nsec you should see a reduction in the CPU usage.

这篇关于nanosleep高cpu使用率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆