You are not logged in.
Is there any api/mechanism to get the absolute time from jiffies value in user space ?
I am using ktime_get_ts() in the kernel to get the absolute time and passing this info to user space app. Not sure if this is the best way to get the timestamp per packet. This api seems to be taking about 168ns per invocation.
The recommended way to get time in userspace is gettimeofday().
If possible, it will use the VDSO to get the current time very quickly.
If that is not possible for whatever reason, it still should be the fastest
way of getting the current time reliably. Multiple cores, CPU sleep
modes, frequency scaling, etc. all can mess up time.
But if you mean the user space equivalent of ktime_get_ts(), then
you should use a posix CLOCK_MONOTONIC timer, that ends up
using ktime_get_ts(). Then use timer_gettime() to check the time.
This might or might not be faster than gettimeofday().
But if you mean if there's a way to get the raw value you get with
ktime_get_ts(), but in user space, then I don't know.
What packets are you using? Perhaps there already is a way to enable
timestamps for them via an existing API (e.g. setsockopt).
Thanks for your response. I cannot use the timestamp functionality available with setsockopt. For my requirement, i need to get the timestamp / jiffies in kernel module... pass this value to user space along with the packet. In user space read this value from the packet.
Currently i am using ktime_get_ts() in kernel module and sending it to user space, which meets the requirement.
Since this is going to be done for every packet (l2/l3) entering the system, i was looking at other mechanisms (if any) like exporting jiffies to user space instead of calling ktime_get_ts() as reading jiffies would be less expensive compared to ktime_get_ts(). But couldn't find any conversion api to retrieve absolute time from jiffies. Jiffies just gives the system up time.
What are your requirements? If you retrieve absolute time from
jiffies you probably end up with something close to ktime_get_ts().
Can't you use jiffies directly?
If you have a very high packet rate it's likely that your NIC
buffers multiple packets. If so, you could call ktime_get_ts()
only once and set it on all packets received with one DMA
transfer. But this might be too low level for your need.
What are you trying to measure? The time packets arrive, or the
time packets are received by user space, or something else?
I have an application in user space, which wants to know the "time" when a packet entered the system. so in my requirement, i have to time stamp each packet as early as possible say in the l2 driver code in the kernel.
so basically i need to have the time stamp per packet stamped on to the packet in the kernel and then pass this value to user space.
is there a better api that i should use or is ktime_get_ts() the better one, is what i need to know. what ever be the mechanism the changes are to be done in the kernel (either getting timestamp / jiffies).
I was wondering if there is a way to avoid the overhead in the kernel by avoiding ktime_get_ts() and shift to a different scheme.
you mentioned that if we try to get absolute time from jiffies, it will be closer to ktime_get_ts(). How can we get the absolute time from jiffies ? From jiffies we can only the get the system uptime and not the absolute / real time right ? If there is way, do let me know.
In that case I'd try the "one timestamp per DMA" approach first,
because that's the closest arrival time of the packet you have,
and could save quite a few calls.
It's still unclear what kind of timestamp you need:
What resolution is required? Jiffies are at best 1ms AFAIK, because
it's related to HZ. All timekeeping functions interpolate the time
between jiffies using a higher frequency, but less accurate CPU clock.
Turning jiffies into a wall clock time (or real time, or absolute time,
as you call it) should be relatively simple as the time of bootup is
known, so it's just an extra addition. But jiffies have low resolution.
Just look at kernel/time/timekeeping.c for what the functions do and
which variants there are. Which one you want largely depends on
what data type you're using: ktime_t, struct timespec or timeval.