23,127
社区成员
发帖
与我相关
我的任务
分享
#define WRAPAROUND_VALUE (0xffffffffUL / HZ + 1) /* HZ = frequency of ticks
per second. */
/* Static function to get current sec and usec. */
static int
system_uptime (struct pal_timeval *tv, struct pal_tzval *tz)
{
struct sysinfo info;
static unsigned long prev = 0;
static unsigned long wraparound_count = 0;
unsigned long uptime;
static long base = 0;
static long offset = 0;
long leap;
long diff;
/* Get sysinfo. */
if (sysinfo (&info) < 0) //得到系统信息包括启动时间
return RESULT_ERROR;
/* Check for wraparound. */
if (prev > info.uptime)
wraparound_count++; //这个有什么用? 这个count是代表什么, 这里表达意思是什么? 怎么样count才++
/* System uptime. */
uptime = wraparound_count * WRAPAROUND_VALUE + info.uptime; //这里算的是什么?
prev = info.uptime;
/* Get tv_sec and tv_usec. */
gettimeofday (tv, tz);
//下面一连串的调整 都不知道是什么意思了. 几个静态变量和临时变量混和在一起...
/* Deffernce between gettimeofday sec and uptime. */
leap = tv->tv_sec - uptime;
/* Remember base diff for adjustment. */
if (! base)
base = leap;
/* Basically we use gettimeofday's return value because it is the
only way to get required granularity. But when diff is very
different we adjust the value using base value. */
diff = (leap - base) + offset;
/* When system time go forward than 2 sec. */
if (diff > 2 || diff < -2)
offset -= diff;
/* Adjust second. */
tv->tv_sec += offset;
return RESULT_OK;
}
/* Check for wraparound. */
if (prev > info.uptime)
wraparound_count++; //溢出一次 wraparound_count++
/* System uptime. */
uptime = wraparound_count * WRAPAROUND_VALUE + info.uptime; 所有溢出时间相加+info.uptime就是总uptime
prev = info.uptime;
// 溢出的例子 比如总量为10 ,此时prev = 10 那么info.uptime为 1而不是11,此时prev > info.uptime
那么此时wraparound_count=1 ,然后再算出现在的uptime