如何进行积极的睡眠?
我正在运行一些分析测试,usleep 是一个有用的函数。 但是,当我的程序处于睡眠状态时,这次不会出现在配置文件中。
例如。 如果我有一个功能:
void f1() {
for (i = 0; i < 1000; i++)
usleep(1000);
}
使用 gprof 等配置工具,f1 似乎不会消耗任何时间。
我正在寻找一种比空 while 循环更好的方法来进行主动睡眠,例如:
while (1) {
if (gettime() == whatiwant)
break;
}
I am running some profiling tests, and usleep is an useful function. But while my program is sleeping, this time does not appear in the profile.
eg. if I have a function as :
void f1() {
for (i = 0; i < 1000; i++)
usleep(1000);
}
With profile tools as gprof, f1 does not seems to consume any time.
What I am looking is a method nicer than an empty while loop for doing an active sleep, like:
while (1) {
if (gettime() == whatiwant)
break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您使用的是哪种系统? 在类 UNIX 系统中,您可以使用 setitimer() 在指定时间段后向进程发送信号。 这是实现您正在寻找的“主动睡眠”类型所需的设施。
设置计时器,然后循环,直到收到信号。
What kind of a system are you on? In UNIX-like systems you can use setitimer() to send a signal to a process after a specified period of time. This is the facility you would need to implement the type of "active sleep" you're looking for.
Set the timer, then loop until you receive the signal.
因为当你调用 usleep 时,CPU 会被用来处理其他事情 1 秒钟。 因此当前线程不使用任何处理器资源,这是一个非常聪明的做法。
积极的睡眠是绝对要避免的,因为它浪费资源(最终通过将电能转化为热量来破坏环境;))。
无论如何,如果你真的想这样做,你必须给处理器做一些实际的工作,这些工作不会被编译器优化排除。 例如
Because when you call usleep the CPU is put to work to something else for 1 second. So the current thread does not use any processor resources, and that's a very clever thing to do.
An active sleep is something to absolutely avoid because it's a waste of resources (ultimately damaging the environment by converting electricity to heat ;) ).
Anyway if you really want to do that you must give some real work to do to the processor, something that will not be factored out by compiler optimizations. For example
我假设您想找出 f1() 所花费的总时间(挂钟时间、现实世界时间、您坐着观看应用程序运行的时间),而不是 CPU 时间。 我会调查 gprof 是否可以为您提供挂钟时间而不是处理时间。
我想这取决于您的操作系统,但是您在配置文件中没有看到 usleep 占用任何进程时间的原因是因为从技术上讲,它在这段时间内没有使用任何进程 - 其他正在运行的进程是(假设它在*上运行) nix 平台)。
I assume you want to find out the total amount of time (wall-clock time, real-world time, the time you are sitting watching your app run) f1() is taking, as opposed to CPU time. I'd investigate to see if gprof can give you a wall-clock-time instead of a processing-time.
I imagine it depends upon your OS, but the reason you aren't seeing usleep as taking any process time in the profile is because it technically isn't using any during that time - other running processes are (assuming this is running on a *nix platform).
“睡眠”功能的全部要点是您的应用程序没有运行。 它被放入睡眠队列中,操作系统将控制权转移给另一个进程。 如果您希望应用程序运行但不执行任何操作,空循环是一个简单的解决方案。 但是你失去了睡眠的所有好处(让其他应用程序运行,节省CPU使用/功耗)
所以你的要求没有意义。 您不能让应用程序处于睡眠状态,但仍处于运行状态。
The entire point in "sleep" functions is that your application is not running. It is put in a sleep queue, and the OS transfers control to another process. If you want your application to run, but do nothing, an empty loop is a simple solution. But you lose all the benefits of sleep (letting other applications run, saving CPU usage/power consumption)
So what you're asking makes no sense. You can't have your application sleep, but still be running.
AFAIK 唯一的选择是做一个 while 循环。 操作系统通常假设,如果您想等待一段时间,您将希望屈服于操作系统。
能够获得微秒级精确的计时器也是一个潜在的问题。 AFAIK 没有跨平台的计时方法(请有人纠正我,因为我喜欢跨平台的亚微秒计时器!:D)。 在 Win32 下,您可以使用一些 QueryPerformanceCounter 调用来包围一个循环,以计算出您何时在循环中花费了足够的时间,然后退出。
例如
AFAIK the only option is to do a while loop. The operating system generally assumes that if you want to wait for a period of time that you will want to be yielding to the operating system.
Being able to get a microsecond accurate timer is also a potential issue. AFAIK there isn't a cross-platform way of doing timing (please someone correct me on this because i'd love a cross-platform sub-microsecond timer! :D). Under Win32, You could surround a loop with some QueryPerformanceCounter calls to work out when you have spent enough time in the loop and then exit.
e.g
这就是为什么在分析时查看“Switched Out %”时间很重要。 基本上,虽然您的函数的独占时间可能很少,但如果它执行 I/O、DB 等,等待外部资源,那么“Switched Out %”是需要注意的指标。
That's why it's important when profiling to look at the "Switched Out %" time. Basically, while your function's exclusive time may be little, if it performs e.g. I/O, DB, etc, waiting for external resources, then "Switched Out %" is the metric to watch out.
这就是您在 gprof 中遇到的混乱,因为您关心的是挂钟时间。 我使用这个。
This is the kind of confusion you get with gprof, since what you care about is wall-clock time. I use this.