Measure time Difference

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.