-
Notifications
You must be signed in to change notification settings - Fork 45
/
How to get high precision nano second delay in linux
56 lines (44 loc) · 1.25 KB
/
How to get high precision nano second delay in linux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//How to get high precision nano second delay in linux
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/time.h>
#include <linux/delay.h>
static int __init initialize(void)
{
ktime_t start, end;
s64 actual_time;
int i;
for(i=0;i<10;i++)
{
start = ktime_get();
ndelay(100);
end = ktime_get();
actual_time = ktime_to_ns(ktime_sub(end, start));
printk("%lld\n",(long long)actual_time);
}
return 0;
}
static void __exit final(void)
{
printk(KERN_INFO "Unload module\n");
}
module_init(initialize);
module_exit(final);
MODULE_AUTHOR("Bhaskar");
MODULE_DESCRIPTION("delay of 100ns");
MODULE_LICENSE("GPL");
////////////////////////////////////////////////
the dmesg output is like this:
[16603.805783] 514
[16603.805787] 350
[16603.805789] 373
[16603.805791] 323
[16603.805793] 362
[16603.805794] 320
[16603.805796] 331
[16603.805797] 312
[16603.805799] 304
[16603.805801] 350
I have gone through one of the posts in stackoverflow: Why udelay and ndelay is not accurate in linux kernel?
But I want a fine tuned nanosecond delay (probably in the range of 100-250ns) in kernel space. Can anyone please suggest me any alternative for doing this?