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.