Linux 核心中獲取時間分析基於do_gettimeofday()
阿新 • • 發佈:2018-11-09
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;
};