I want to measure time intervals in my kernel. I know nanoseconds are not really precise but I need more precision than milliseconds.

There are many many functions to get a time. Jiffies, time of day, time without sleeping, time with sleeping... There is ktime_get, clock_gettime, sys_clock_gettime and others.
I looked into the functions and feel like some enormous machinery is started in them. I don't need conversion to time of day and I also want to avoid converting values back and forth from and to several time-keeping structs.

For a start without respecting performance I thought I am fine with

struct compat_timespec cts;
compat_sys_clock_gettime(CLOCK_MONOTONIC, &cts);

It worked until I wanted to use it in a file open function of a virtual file. Now compat_sys_clock_gettime returns an error code. What can cause an error reading a time?

So my question is embarrassingly basic.

How can I performantly just get the nanoseconds since booting the PC...

  • without subtracting sleep times or any other special conditions,
  • without a possibility of failure?

My simple imagination is there should be a function that reads a ticker value from somewhere and multiplies / divides this value to get ns out of it. I'd also appreciate to learn why it's not so easy.

  • You could find a hardware source (such as the powerpc time base register - see this) - but sounds to me like using jiffies would be good enough - you can get a timespec from jiffies using jiffies_to_timespec(). Have a read of this. – Murray Jensen Jun 25 at 2:33
  • @MurrayJensen Thanks for this hint but jiffies is too coarse. This is on a small device, I don't know the exact value right now but the jiffies rate is far below 1000. This resolution is larger than a millisecond but must be more exact. The function in my example would be great if it didn't return an error in some cases. Strange to see reading a simple ticker is such a rocket science. – ThreadGuy Jun 25 at 4:58
  • You are making a simple thing complicated by calling a user process system call in your kernel code (generally speaking, things that start with sys_ are often system calls - haven't seen compat_sys_ before, but probably the same ... it is, see this). If jiffies are too coarse for you, you will have to read a hardware time base register (which makes your code platform dependent). Maybe there is a platform independent way to do this (I would like to know). – Murray Jensen Jun 25 at 9:49
  • @MurrayJensen Oh a good point, I didn't thinkk about sys_. I wrote an answer how I do this now.. – ThreadGuy Jun 25 at 13:18

As of lately I am using ktime_get.

I have no idea if there is something better or if this function is so common it's a good idea to use it. But this function seems to return a small resolution and is at least not incredibly slow.
Also it has no return value which implies it shouldn't fail but reliably return a time.

  • Thanks for the pointer - this looks to me like the right thing to use (read this) – Murray Jensen Jun 25 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.