Measure time Difference
Clock interrupts are generated by timed hardware at periodic intervals, which are set by the kernel according to the Hz value, which is an architecture-related constant defined in the file associated with <linux/param.h> or one of the sub-platforms contained in the file.
You can change the frequency at which the system clock interrupts occur by modifying the Hz value, but you must recompile the kernel and all modules.
Each time the clock interrupt occurs, the value of the internal counter of the kernel increases by 1, and the value of this counter is initialized to 0 at system boot time. This counter is 64-bit, called jiffies_64, but driver developers usually access the jiffies variable, which is the Unsignedlong variable, either the same as jiffies_64, and is its low 32 bits. Usually preferred jiffies, faster.
Using the jiffies counter
The counter and the tool function that reads the counter are included in <linux/jiffies.h>, but usually only include <linux/sched.h> the latter is automatically put into jiffies.h. Jiffies and jiffies_64 are read-only variables.
Jiffies Snapshot comparison:
#include <linux/jiffies.h>
Inttime_after (unsigned long A, unsigned long b);//a returns true after B
Inttime_before (unsigned long A, unsigned long B);
Inttime_after_eq (unsigned long A, unsgined long B);
Inttime_before_eq (unsigned long A, unsigned long B);
These macros convert the counter values to Signedlong, subtract, and then compare the results. If you need to calculate the difference of two jiffies instances in a safe way, you can use the same technique:
diff= (Long) t2-(long) T1;
Convert to Millisecond value:
Msec=diff*1000/hz;
Conversions to user space timeval and Timespec:
#include <linux/time.h>
Unsignedlong timespec_to_jiffies (struct timespec *value);
Voidjiffies_to timespec (unsigned long jiffies, struct timespec *value);
Unsignedlong timeval_to jiffies (struct timeval *value);
Voidjiffies_to_timeval (unsigned long jiffies, struct timeval *value);
Access to 64 bits on 32 processors is not atomic, and direct reading of jiffies_64 is unreliable. Use:
#include <linux/jiffies.h>
u64get_jiffies_64 (void);
The actual clock frequency is almost completely invisible to the user space. When user-space program Access contains PARAM.H,HZ macros are always extended to 100. To get the exact value, you can get the count value by/proc/interrupts, divided by the system run time reported by/proc/uptime.
This article is from the "No Front" blog, please be sure to keep this source http://qianyang.blog.51cto.com/7130735/1620578
Measure time Difference