1

I want to implement non-busy wait mechanism for creating a delay in nanoseconds in linux kernel module.

Right now I was looking at the function

ndelay(ns);

But https://www.kernel.org/doc/Documentation/timers/timers-howto.txt

Here it is mentioned that ndelay function is a busy wait mechanism.

But I want non busy wait delay. So, I'm thinking about nop in linux kernel module like this:

asm volatile ("nop");

I have studied that nop takes one cycle to execute. So, I will be implementing nested nop's to get the required delay. My question is -

By using nop will I be implementing a delay in nanoseconds in a non-busy waiting way? Can anyone please confirm on this? Also some reference to some journals and resources will be helpful.

Bhaskar Jupudi
  • 45
  • 1
  • 11
  • it's a 1 cycle "do nothing" instruction. if your cpu happens have to a nanosecond-scale cycle time, then yes, nop would be a "some number of nanoseconds of delay". but note it'd be a different delay based on cpu clock speed. a 1ghz cpu would have a 1 nanosecond delay, while a 100mhz cpu would be 10 nanoseconds. – Marc B Apr 01 '16 at 17:47
  • Thank you for your comment. But will this create a non-busy wait delay? – Bhaskar Jupudi Apr 01 '16 at 17:48
  • I'm not sure what 'non-busy' means to you. In my experience you are either doing a busy wait (repeatedly checking a condition), or you are calling a kernel function that will prevent the thread scheduler from allocating time to the thread until a condition is met (like a semaphore). Any instruction that you execute is, by definition, keeping the cpu busy. That said, have you looked at the `pause` instruction? – David Wohlferd Apr 01 '16 at 22:24

1 Answers1

1

Like its name implies, a "NOP" is an operation that doesn't modify the state of the machine, however, that doesn't mean the processor is idle or "non-busy" while executing a NOP. Like any other instruction a NOP still claims some resources of the processor while it's being executed.

Obviously modern super pipelined processors are complex and typically execute many operations concurrently, so a it's likely a NOP would use less resources than a more complex operation like a branch, string compare, or floating-point operation, but it's not safe to assume a NOP is non-busy like you say.

Anthony E
  • 11,072
  • 2
  • 24
  • 44
  • Is there any other thing that I can do for implementing a non-busy nanosecond precision delay in a kernel module? I'm out of ideas. – Bhaskar Jupudi Apr 01 '16 at 19:06