一直以来对Linux下的时间管理知之不详,GFree_wind在微博发起过几次Linux下时钟的讨论,和Godbach这些大牛比,我完全插不上话,因为不懂。近来闲暇时间研究了下Linux下的时间管理,分享出来,请大家指正。
从我们大白话的角度想,时间管理其实分成两部分,就像我们小时候学习物理的时候物理老师不断强调时间和时刻的区别。一个是时刻,比如现在是20:44:37秒,指的是时刻,我们手机上看时间,指的也是时刻。另一块是时间,比如说,我每天工作八小时,再比如说,半小时之后,我要出门了,结束时间指向的是未来,但是仍然是一段时间。OK。无论是时刻还是时望间,都是需要硬件支持的,你手里只有一块最小刻度只有1秒的手表,就不要指用这块手表给百米大赛度量成绩了,何哉,硬件太挫。Linux也是如此,之所以Linux启动之后,可以精确的计时,那是因为Linux的下面有相应的硬件为依托。
RTC
RTC,real time clock,实时时钟和其他的硬件是不同的,RTC吐出来的是时刻,而其他硬件时钟吐出来的是时间。也就是说,RTC能告诉我们,当前是2013年9月12日,21:49:38,但是其他的硬件如TSC,PIT,HPET只能告诉我们,我应该走过了XX个cycle,按照我的频率,已经过去了10分钟了。
为啥RTC这么牛X,可以告诉我们当前时刻,哪怕用户关了机?以X86为例,RTC是主板上的一块CMOS芯片,哪怕你的Linux关了机,她也可以依赖主板上的电池维持时钟的准确。当然了,在Linux下,RTC存储的是UTC时间,而不会考虑timezone。
所以,Linux启动的时候,一定会拜访RTC来获得当前的时刻值,尽管精度不高(精确到秒)。When and How?
首先回答When。Linux启动的时候,start_kernel有四大time相关的函数调用:
init_timers();
hrtimers_init();
timekeeping_init()
time_init();
从RTC中读取当前的UTC时间是timekeeping_init中做的事情,调用路径如下:
timekeeping_init
|___________read_persistent_clock (arch/x86/kernel/rtc.c)
|_____x86_platform.get_wallclock()
|_____mach_get_cmos_time (arch/x86/kernel/x86_init.c)
/************arch/x86/kernel/rtc.c*****************/ void read_persistent_clock(struct timespec *ts) { unsigned long retval; retval = x86_platform.get_wallclock(); ts->tv_sec = retval; ts->tv_nsec = 0; } /*****************arch/x86/kernel/x86_init.c ****************/ struct x86_platform_ops x86_platform = { .calibrate_tsc = native_calibrate_tsc, .wallclock_init = wallclock_init_noop, .get_wallclock = mach_get_cmos_time, .set_wallclock = mach_set_rtc_mmss, .iommu_shutdown = iommu_shutdown_noop, .is_untracked_pat_range = is_ISA_range, .nmi_init = default_nmi_init, .get_nmi_reason = default_get_nmi_reason, .i8042_detect = default_i8042_detect, .save_sched_clock_state = tsc_save_sched_clock_state, .restore_sched_clock_state = tsc_restore_sched_clock_state, }
对于我们而言,我们要读的function是mach_get_cmos_time
unsigned long mach_get_cmos_time(void) { unsigned int status, year, mon, day, hour, min, sec, century = 0; unsigned long flags; spin_lock_irqsave(&rtc_lock, flags); /* * If UIP is clear, then we have >= 244 microseconds before * RTC registers will be updated. Spec sheet says that this * is the reliable way to read RTC - registers. If UIP is set * then the register access might be invalid. */ while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)) cpu_relax(); sec = CMOS_READ(RTC_SECONDS); min = CMOS_READ(RTC_MINUTES); hour = CMOS_READ(RTC_HOURS); day = CMOS_READ(RTC_DAY_OF_MONTH); mon = CMOS_READ(RTC_MONTH); year = CMOS_READ(RTC_YEAR); #ifdef CONFIG_ACPI if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID && acpi_gbl_FADT.century) century = CMOS_READ(acpi_gbl_FADT.century); #endif status = CMOS_READ(RTC_CONTROL); WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY)); spin_unlock_irqrestore(&rtc_lock, flags); if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) { sec = bcd2bin(sec); min = bcd2bin(min); hour = bcd2bin(hour); day = bcd2bin(day); mon = bcd2bin(mon); year = bcd2bin(year); } if (century) { century = bcd2bin(century); year += century * 100; printk(KERN_INFO "Extended CMOS year: %d\n", century * 100); } else year += CMOS_YEARS_OFFS; return mktime(year, mon, day, hour, min, sec); }
这一段代码已经牵扯到了硬件相关的编程,我们其实并不关心驱动,对于硬件比较感兴趣的筒子,可以访问http://xenyinzen.wikidot.com/reship:080225-2
获得更多更详细的信息。mktime是将年月日时分秒组装成1970年1月1日00:00:00这个UNIX基准时间以来的秒数。我们在Linux下可以通过一下方式获得这个值:
root@manu:/sys/class/rtc/rtc0# date +%s ;cat /sys/class/rtc/rtc0/since_epoch 1379081060 1379081060
既然Linux上电的时候,可以从RTC中读出当前的时间,我们也可以设置时间,并且写入到RTC。用户层也可以操作RTC硬件时钟,通过ioctl。下面给出一个样例:
#include #include #include #include #include int main(int argc,char *argv[]) { int retval,fd; struct rtc_time rtc_tm; fd=open("/dev/rtc",O_RDONLY); if(fd==-1) { perror("error open /dev/rtc"); return -1; } retval=ioctl(fd,RTC_RD_TIME,&rtc_tm); if(retval==-1) { perror("error exec RTC_RD_TIME ioctl"); return -2; } printf("RTC time is %d-%d-%d %d:%d:%d \n", rtc_tm.tm_year+1900,rtc_tm.tm_mon,rtc_tm.tm_mday, rtc_tm.tm_hour,rtc_tm.tm_min,rtc_tm.tm_sec); close(fd); return 0; }
输出如下:
root@manu:~/code/c/self/rtc# ./rtc_test RTC time is 2013-8-14 15:46:2
对于set RTC 也可以通过ioctl RTC_SET_TIME参数来实现,我就不多说了,对这部分感兴趣的,可以自行man rtc,或者Kernel的Documentation/rtc.txt有一个示例代码,比较详细。
已经说过了,RTC在时间相关的硬件中是个独树一帜的奇葩,作用和其他的硬件不同。而其他的硬件只是以一定的频率产生时钟中断,帮助OS完成计时。前面我也提到过,你手里拿着个手表,就不要指望给百米大赛计时,原因就是精度太低。硬件也是如此,有精度高的有精度低的。Linux操作系统抽象出了clocksource(时钟源)来管理这些硬件。Linux会在所有的硬件时钟中选择出精度最高作为当前在用的时钟源。
如何查看当前所有的可用的时钟源已经当前在用的时钟源呢?
manu@manu:/$ cat /sys/devices/system/clocksource/clocksource0/available_clocksource tsc hpet acpi_pm manu@manu:/$ cat /sys/devices/system/clocksource/clocksource0/current_clocksource tsc
相关推荐
嵌入式Linux——硬件、软件与接口(Embedded Linux_ Hardware Software and Interfacing)
本书介绍了Linux下进行嵌入式开发的细节,包括中断,计时器,I/O,调试方法等
If you are a hardware developer, software developer, system integrator, or product manager who's begun exploring embedded Linux for interfacing applications, this book is for you.
Windows 下 批量管理,批量监控,批量操作,批量安全扫描 linux Solaris Aix HP-Unix Windows 等操作系统界面最简洁的图形工具。 批量执行多台服务器命令,批量修改密码,批量执行服务器脚本,批量上传文件,解放你...
介绍linux下的硬体除错工具汗技巧,对embedded linux BSP开发很有帮助
Embedded.Linux.Hardware.Software.and.Interfacing.2002
Hardware
Windows 下 批量管理,批量监控,批量操作,批量安全扫描 linux Solaris Aix HP-Unix Windows 等操作系统界面最简洁的图形工具。 批量执行多台服务器命令,批量修改密码,批量执行服务器脚本,批量上传文件,解放...
This book is focused mainly on the Raspberry Pi’s hardware. The content is extracted from the complete work, Mastering the Raspberry Pi. As such, it will serve you as an owner’s manual of sorts, ...
UnixShellManager 实验室批量管理监控系统,能够实现对 windows 系统远程桌面登录管理操作,VNC 远程管理操作 可以实现对WINDOWS,LINUX, UNIX, 图形界面登陆管理操作,以上均可实现自动登录系统,,登录用户,根据...
Linux内核硬件检测工具
The Linux GPIB Package is a support package for GPIB (IEEE 488) hardware. The package contains kernel driver modules, and a C user-space library with Guile, Perl, PHP, Python and TCL bindings. The API...
Windows 下 批量管理,批量监控,批量操作,批量安全扫描 linux Solaris Aix HP-Unix Windows 等操作系统界面最简洁的图形工具。 批量执行多台服务器命令,批量修改密码,批量执行服务器脚本,批量上传文件,解放...
史上图形最简单Linux-Unix-Windows批量管理服务器软件工具 Windows 下 批量管理,批量监控,批量操作,批量安全扫描 linux Solaris Aix HP-Unix Windows 等操作系统界面最简洁的图形工具。 批量执行多台服务器...
Title: Hardware Malware Author(s): Christian Krieg, Adrian Dabrowski, Heidelinde Hobel, Katharina Krombholz, Edgar Weippl Series: Synthesis Lectures on Information Security, Privacy, and Tru ...
Generate HardwareID on Delphi
Hardware and Computer Organization
Over the years the book has helped countless programmers learn: * how to support computer peripherals under the Linux operating system * how to develop and write software for new hardware under Linux ...