title: How to get high precision nano second delay in linux
body: <p>I have written  kernel module to measure the correctness of ndelay() kernel function.</p>
  
  <pre><code>#include &lt;linux/module.h&gt;
  #include &lt;linux/init.h&gt;
  #include &lt;linux/kernel.h&gt;
  #include &lt;linux/time.h&gt;
  #include &lt;linux/delay.h&gt;
  static int __init initialize(void)
  {
      ktime_t start, end;
      s64 actual_time;
      int i;
      for(i=0;i&lt;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");
  </code></pre>
  
  <p>the dmesg output is like this:</p>
  
  <pre><code>[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
  </code></pre>
  
  <p>I have gone through one of the posts in stackoverflow:
  <a href="https://stackoverflow.com/questions/30396869/why-udelay-and-ndelay-is-not-accurate-in-linux-kernel">Why udelay and ndelay is not accurate in linux kernel?</a></p>
  
  <p>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?</p>