Linux 内核中获取时间分析基于do_gettimeofday()

Linux 内核中获取时间分析基于do_gettimeofday()


  • 内核代码能一直获取一个当前时间的表示,通过查看jifies的值。通常这个值只代表从最后一次启动以来的时间,这个事实对驱动来说无关,因为它的生命周期受限于系统的uptime。
  • 驱动可以使用jifies的当前值来计算事件之间的时间间隔(例如,在输入驱动中从单击中区分双击或者计算超时)。
  • 驱动不需要墙上时钟以月、天和小时来表达;这个信息常常只对用户程序需要,例如cron 和 syslogd。处理真实世界的时间常常留给用户空间。

内核从墙上时钟转到jiffies值,

#include <linux/time.h> 

unsigned long mktime (unsigned int year, unsigned int mon, unsigned int day, unsigned int hour, unsigned int min, unsigned int sec);

获取绝对时间

  • 有时我们**有时我们需要在内核空间处理绝对时间。**为此<linux/time.h>输出了do_gettimeofday函数。当被调用时,被填充一个struct timeval指针–和在gettimeofday系统调用中使用的相同。数据结构中包含秒和毫秒值。原型如下:
 #include <linux/time.h> 

    void do_gettimeofday(struct timeval *tv);

转化为可读的时间

void do_gettimeofday(struct timeval *tv)
能够得到struct timeval

struct timeval { 
time_t tv_sec; /* seconds */ 
suseconds_t tv_usec; /* microseconds */ 
};
  • 那么就需要将这个tv_sec,即1970年开始至今的秒数转换为年月日时分秒
    其实内核已经有这样的函数
    /*
    *Convert seconds since 01-01-1970 00:00:00 to Gregorian date.
    */
    void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)

  • 唯一的不足是转换得到的是UTC时间,同北京时间差8小时。要想达到用户态localtime()的效果,必须获得/etc/localtime 中的时区信息。
    示例代码:

#include <linux/timer.h> 
#include <linux/timex.h> 
#include <linux/rtc.h>
/*添加到合适位置*/
struct timex  txc; 
struct rtc_time tm; 
do_gettimeofday(&(txc.time)); 
rtc_time_to_tm(txc.time.tv_sec,&tm); 
printk(“UTC time :%d-%d-%d %d:%d:%d /n”,tm.tm_year+1900,tm.tm_mon, tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
  • struct timex
struct timex {
	unsigned int modes;	/* mode selector */
	__kernel_long_t offset;	/* time offset (usec) */
	__kernel_long_t freq;	/* frequency offset (scaled ppm) */
	__kernel_long_t maxerror;/* maximum error (usec) */
	__kernel_long_t esterror;/* estimated error (usec) */
	int status;		/* clock command/status */
	__kernel_long_t constant;/* pll time constant */
	__kernel_long_t precision;/* clock precision (usec) (read only) */
	__kernel_long_t tolerance;/* clock frequency tolerance (ppm)
				   * (read only)
				   */
	struct timeval time;	/* (read only, except for ADJ_SETOFFSET) */
	__kernel_long_t tick;	/* (modified) usecs between clock ticks */

	__kernel_long_t ppsfreq;/* pps frequency (scaled ppm) (ro) */
	__kernel_long_t jitter; /* pps jitter (us) (ro) */
	int shift;              /* interval duration (s) (shift) (ro) */
	__kernel_long_t stabil;            /* pps stability (scaled ppm) (ro) */
	__kernel_long_t jitcnt; /* jitter limit exceeded (ro) */
	__kernel_long_t calcnt; /* calibration intervals (ro) */
	__kernel_long_t errcnt; /* calibration errors (ro) */
	__kernel_long_t stbcnt; /* stability limit exceeded (ro) */

	int tai;		/* TAI offset (ro) */

	int  :32; int  :32; int  :32; int  :32;
	int  :32; int  :32; int  :32; int  :32;
	int  :32; int  :32; int  :32;
};
  • struct rtc_time
struct rtc_time {
	int tm_sec;
	int tm_min;
	int tm_hour;
	int tm_mday;
	int tm_mon;
	int tm_year;
	int tm_wday;
	int tm_yday;
	int tm_isdst;
};

猜你喜欢

转载自blog.csdn.net/qq_22613757/article/details/83862388
今日推荐