Date: Wed, 14 Dec 2016 21:18:31 -0700 From: Ian Lepore <ian@freebsd.org> To: Goran =?iso-8859-2?Q?Meki=E6?= <meka@tilda.center> Cc: freebsd-hackers@freebsd.org Subject: Re: How to use sem_timedwait? Message-ID: <1481775511.1889.450.camel@freebsd.org> In-Reply-To: <20161215002906.mllorgvvuovbdtze@hal9000.meka.no-ip.org> References: <20161214074228.zh6q5zya2gszw4g6@hal9000.meka.no-ip.org> <1481748960.1889.398.camel@freebsd.org> <20161215002906.mllorgvvuovbdtze@hal9000.meka.no-ip.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 2016-12-15 at 01:29 +0100, Goran Mekiĉ wrote: > On Wed, Dec 14, 2016 at 01:56:00PM -0700, Ian Lepore wrote: > > > > On Wed, 2016-12-14 at 08:42 +0100, Goran Mekiĉ wrote: > > One of the things you did with that code is measured how much time > > it > > took to format and print the "It should have ended..." line. If > > you > > want to capture how long you were asleep you need the > > clock_gettime() > > to be the next thing you call after sem_timedwait(). Even the time > > to > > access errno may be non-trivial if it's actually a thread-local > > variable. > > > > If you want to get better sleep-timing performance (at the expense > > of > > less power-saving efficiency), try setting > > > > sysctl kern.timecounter.alloweddeviation=0 > > > > That will prevent the aggregation of timeouts scheduled near each > > other > > to all happen at the same time, resulting in more wakeups, each of > > which is more accurate (more wakeups = more power used). > > > > -- Ian > > > This is exactly what I've needed. Thank you so much. If you can tell > me any other tip to make the difference even smaller, I'd be > grateful. Currently, it goes from 0.1ms to 1ms. Great improvement! > Once more, thank you very much! > > Regards, > meka Do you actually need semaphore behavior along with the precise sleeping, or are you just using it because it is a convenient function that takes a timespec/nanosecs wait time? Making a guess here: Is your actual goal to wake up periodically with the period between wakeups as accurate as possible? If so, a better mechanism might be to use kqueue(2)/kevent(2) with EVFILT_TIMER events. With EVFILT_TIMER events, the wakeups are scheduled as if aligned to a grid -- even if one wakeup is a bit late due to system workload, the next wakeup after it will still be properly aligned to the original grid. For example, if you ask for a wakeup once per millisecond and some wake happens after 1.2mS, the next wakeup will be .8mS after that; the phase of the wakeups doesn't shift over time. -- Ian
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1481775511.1889.450.camel>