getting that cpu to sleep ...
greetings, I have a question for those linux programmers out there. i'm writing a program on linux (kubuntu to be specific) that requires high-precision timing, somewhere on the order of 100 to 1000 micro seconds. I get very good timing out of a loop written such that a while() loop checks the system clock after every iteration and comparing that to the desired wait time, like so (pseudo_code): while(time_passed < wait_time) { time_passed = get_time() } ... The problem with this approach is that the process uses 99% of the CPU since it just spins the CPU until enough time has expired. My question: is there a way on linux (in C) to have the CPU sleep or send it a no-op such that it's not just eating up cycles until time expires? I have looked at nanosleep and usleep, however they appear to only have precision to 10ms which too latent for my needs. Thanks, --Brad
On Mon, Dec 12, 2005 at 05:48:24PM -0500, brad wrote:
My question: is here a way on linux (in C) to have the CPU sleep or send it a no-op such that it's not just eating up cycles until time expires? I have looked at nanosleep and usleep, however they appear to only have precision to 10ms which too latent for my needs.
you cant unless you tweak the kernel HZ settings read this page: http://kerneltrap.org/node/464 -mike
==> Regarding [Wlug] getting that cpu to sleep ...; brad <maitre@ccs.neu.edu> adds: maitre> greetings, maitre> I have a question for those linux programmers out there. i'm maitre> writing a program on linux (kubuntu to be specific) that requires maitre> high-precision timing, somewhere on the order of 100 to 1000 micro maitre> seconds. I get very good timing out of a loop written such that a maitre> while() loop checks the system clock after every iteration and maitre> comparing that to the desired wait time, like so (pseudo_code): maitre> while(time_passed < wait_time) { time_passed = get_time() } ... maitre> The problem with this approach is that the process uses 99% of the maitre> CPU since it just spins the CPU until enough time has expired. My The other problem with this is that you can be scheduled if your timeslice runs out. maitre> question: is there a way on linux (in C) to have the CPU sleep or maitre> send it a no-op such that it's not just eating up cycles until time maitre> expires? I have looked at nanosleep and usleep, however they appear maitre> to only have precision to 10ms which too latent for my needs. As Mike pointed out, in more recent kernels you can tune the value of HZ to get better granularity from select/poll. What, precisely, are you trying to do and on what kernel? Perhaps with more information we could provide better guidance. -Jeff
On Mon, Dec 12, 2005 at 06:17:35PM -0500, Jeff Moyer wrote:
==> Regarding [Wlug] getting that cpu to sleep ...; brad <maitre@ccs.neu.edu> adds:
maitre> greetings,
maitre> I have a question for those linux programmers out there. i'm maitre> writing a program on linux (kubuntu to be specific) that requires maitre> high-precision timing, somewhere on the order of 100 to 1000 micro maitre> seconds. I get very good timing out of a loop written such that a maitre> while() loop checks the system clock after every iteration and maitre> comparing that to the desired wait time, like so (pseudo_code): maitre> while(time_passed < wait_time) { time_passed = get_time() } ...
maitre> The problem with this approach is that the process uses 99% of the maitre> CPU since it just spins the CPU until enough time has expired. My
The other problem with this is that you can be scheduled if your timeslice runs out.
umm, i didn't think about that one.
maitre> question: is there a way on linux (in C) to have the CPU sleep or maitre> send it a no-op such that it's not just eating up cycles until time maitre> expires? I have looked at nanosleep and usleep, however they appear maitre> to only have precision to 10ms which too latent for my needs.
As Mike pointed out, in more recent kernels you can tune the value of HZ to get better granularity from select/poll.
Actually Mike's response reminded me of an article i bumped into about nanosleep on dragonflyBSD http://www.dragonflybsd.org/docs/nanosleep/
What, precisely, are you trying to do and on what kernel? Perhaps with more information we could provide better guidance.
The immediate task at hand is that i want to get readings from an instrument (a light sensor) at a uniform interval, say every 1000 micro seconds. I have been able to achieve this task by setting up the loop i described above. However the loop above takes 99% of the CPU. I'd like to be able to achieve this without pegging the CPU at 99%. Does this make sense? if not i can try explaining it again, perhaps with some code. The kernel i'm running is 2.6.12. (well uname -r reports '2.6.12-9-686-smp.' its ubuntu's stock smp kernel.) -brad
On Mon, Dec 12, 2005 at 06:17:35PM -0500, Jeff Moyer wrote:
==> Regarding [Wlug] getting that cpu to sleep ...; brad <maitre@ccs.neu.edu> adds:
maitre> greetings,
maitre> I have a question for those linux programmers out there. i'm maitre> writing a program on linux (kubuntu to be specific) that requires maitre> high-precision timing, somewhere on the order of 100 to 1000 micro maitre> seconds. I get very good timing out of a loop written such that a maitre> while() loop checks the system clock after every iteration and maitre> comparing that to the desired wait time, like so (pseudo_code): maitre> while(time_passed < wait_time) { time_passed = get_time() } ...
maitre> The problem with this approach is that the process uses 99% of the maitre> CPU since it just spins the CPU until enough time has expired. My
The other problem with this is that you can be scheduled if your timeslice runs out.
ummm, i didn't think about that one.
maitre> question: is there a way on linux (in C) to have the CPU sleep or maitre> send it a no-op such that it's not just eating up cycles until time maitre> expires? I have looked at nanosleep and usleep, however they appear maitre> to only have precision to 10ms which too latent for my needs.
As Mike pointed out, in more recent kernels you can tune the value of HZ to get better granularity from select/poll.
Actually Mike's response reminded me of an article i bumped into about nanosleep on dragonflyBSD http://www.dragonflybsd.org/docs/nanosleep/
What, precisely, are you trying to do and on what kernel? Perhaps with more information we could provide better guidance.
The immediate task at hand is that i want to get readings from an instrument (a light sensor) at a uniform interval, say every 1000 micro seconds. I have been able to achieve this task by setting up the loop i described above. However the loop above takes 99% of the CPU. I'd like to be able to achieve this without pegging the CPU at 99%. Does this make sense? if not i can try explaining it again, perhaps with some code. The kernel i'm running is 2.6.12. (well uname -r reports '2.6.12-9-686-smp.' its ubuntu's stock smp kernel.) -brad
From: brad <maitre@ccs.neu.edu>
The immediate task at hand is that i want to get readings from an instrument (a light sensor) at a uniform interval, say every 1000 micro seconds. I have been able to achieve this task by setting up the loop i described above. However the loop above takes 99% of the CPU. I'd like to be able to achieve this without pegging the CPU at 99%. Does this make sense? if not i can try explaining it again, perhaps with some code.
The kernel i'm running is 2.6.12. (well uname -r reports '2.6.12-9-686-smp.' its ubuntu's stock smp kernel.)
You are doing something that Linux was not designed to do. But don't despair, you are not the first one. There is RT-Linux, (Real-Time Linux), which consists of a real-time scheduler (meaning that it is designed to schedule tasks with non-negotiable time constraints), which treats the entire Linux kernel as a background task. Assuming your raw processor speed is sufficient (without which you lose no matter what you do), the RT-Linux scheduler will run your special task at guaranteed intervals, while any network, disk I/O, web browsing, etc. that you need to do can use the Linux system in the cracks between. Also the embedded Linux project and uCLinux (microComputer) Linux might be interesting. Give any of these terms to Google and get back more than you can read, but here is a link to get started: http://www.linuxdevices.com/articles/AT3694406595.html -- Keith
I looked into rt-linux. It appears that your real time process doesn't actually run as a process but as a kernel module. Unfortunately this would disrupt the flow of my program, but i'll keep this in the back of my head for the next version. Thanks for the suggestion. --Brad On Mon, Dec 12, 2005 at 09:26:51PM -0500, Keith Wright wrote:
From: brad <maitre@ccs.neu.edu>
The immediate task at hand is that i want to get readings from an instrument (a light sensor) at a uniform interval, say every 1000 micro seconds. I have been able to achieve this task by setting up the loop i described above. However the loop above takes 99% of the CPU. I'd like to be able to achieve this without pegging the CPU at 99%. Does this make sense? if not i can try explaining it again, perhaps with some code.
The kernel i'm running is 2.6.12. (well uname -r reports '2.6.12-9-686-smp.' its ubuntu's stock smp kernel.)
You are doing something that Linux was not designed to do. But don't despair, you are not the first one. There is RT-Linux, (Real-Time Linux), which consists of a real-time scheduler (meaning that it is designed to schedule tasks with non-negotiable time constraints), which treats the entire Linux kernel as a background task.
Assuming your raw processor speed is sufficient (without which you lose no matter what you do), the RT-Linux scheduler will run your special task at guaranteed intervals, while any network, disk I/O, web browsing, etc. that you need to do can use the Linux system in the cracks between.
Also the embedded Linux project and uCLinux (microComputer) Linux might be interesting. Give any of these terms to Google and get back more than you can read, but here is a link to get started:
http://www.linuxdevices.com/articles/AT3694406595.html
-- Keith _______________________________________________ Wlug mailing list Wlug@mail.wlug.org http://mail.wlug.org/mailman/listinfo/wlug
participants (5)
-
brad
-
Bradley Noyes
-
Jeff Moyer
-
Keith Wright
-
Mike Frysinger