硬件时钟

硬件时钟,第1张

有几个概率需要了解

1.时间周期(Clock Cycle)的频率:晶体振荡器在1秒以内时钟周期的个数=1秒以内时钟脉冲的个数,Linux里面用

#define CLOCK_TICK_RATE 1193180 /* Underlying HZ */

单位为HZ

2.时钟滴答(Clock Tick):

当PIT通道0的计数器减到0值时,它就在IRQ0上产生一次时钟中断,也即一次时钟滴答。PIT通道0的计数器的初始值决定了要过多少时钟周期才产生一次时钟中断。

3.时钟滴答的频率(HZ):1秒以内产生时间滴答个数

HZ在arm和i386上定义为100,ALPHA和IA62为1024 ,IBM Power PC为1000

i386 1个时钟滴答时间为1000ms/100=10ms。

4.时钟滴答的时间间隔(tick)

long tick = (1000000 + HZ/2) / HZ/* timer interrupt period */   单位为us ,tick=10ms,也可以通过HZ来计算

linux中#define TICK_SIZE= tick

1s=1,000,000us

(5)宏LATCH:Linux用宏LATCH来定义要写到PIT通道0的计数器中的值,它表示PIT将没隔多少个时钟周期产生一次时钟中断。

    LATCH=(1秒之内的时钟周期个数)÷(1秒之内的时钟中断次数)=(CLOCK_TICK_RATE)÷(HZ)

Linux为取整 #define LATCH=(CLOCK_TICK_RATE + HZ/2) / HZ。

时钟周期理解:时钟脉冲,电平从0到1(或者相反)。

CLOCK_TICK_RATE和HZ都是 频率 :1秒以内时钟周期 频率 ,1秒以内时钟滴答 频率 。

TSC使用CPU频率,在现代计算机中CPU可能降频也可能超率,TSC时钟不准确。

顺序为HPET >APCI PMT>PIC>TSC

local cpu APIC只在单CPU场景上使用,如进程调度时间.

参考:

http://www.dedecms.com/knowledge/servers/linux-bsd/2012/0913/14187.html

Can use ntpdate or rdate command to sync the datetime with remote server.

i.e.:

# ntpdate remote_server

or

# rdate -s remote_server

Also, sync the remote server periodically is a good idea!

# crontab -e

# auto sync with time server at 0:00 everyday

0 0 * * * rdate -t 60 -s stdtime.gov.hk记住,更新完后要用 clock -w 或 hwclock -w 实时间写入到BIOS中,这样下次启动机子时,时间就会自动更新了。 1.在虚拟终端中使用date命令来查看和设置系统时间查看系统时钟的 *** 作:# date设置系统时钟的 *** 作:# date 091713272003.30通用的设置格式:# date 月日时分年.秒2.使用hwclock或clock命令查看和设置硬件时钟查看硬件时钟的 *** 作:# hwclock --show 或# clock --show2003年09月17日 星期三 13时24分11秒 -0.482735 seconds设置硬件时钟的 *** 作:# hwclock --set --date="09/17/2003 13:26:00"或者# clock --set --date="09/17/2003 13:26:00"通用的设置格式:hwclock/clock --set --date=“月/日/年 时:分:秒”。3.同步系统时钟和硬件时钟Linux系统(笔者使用的是Red Hat 8.0,其它系统没有做过实验)默认重启后,硬件时钟和系统时钟同步。如果不大方便重新启动的话(服务器通常很少重启),使用clock或hwclock命令来同步系统时钟和硬件时钟。硬件时钟与系统时钟同步:# hwclock --hctosys或者# clock --hctosys上面命令中,--hctosys表示Hardware Clock to SYStem clock。系统时钟和硬件时钟同步:# hwclock --systohc或者# clock --systohc使用图形化系统设置工具设置时间对于初学者来,笔者推荐使用图形化的时钟设置工具,如Red Hat 8.0中的日期与时间设置工具,可以在虚拟终端中键“redhat-config-time”命令,或者选择“K选单/系统设置/日期与时间”来启动日期时间设置工具。使用该工具不必考虑系统时间和硬件时间,只需从该对话框中设置日期时间,可同时设置、修改系统时钟和硬件时钟。

如果是获取 cpu 时钟 的 tick:

clock_t tick1,tick2

tick1=clock()// 开机到执行这句时的毫秒数 ms

等待一会

tick2=clock()// 开机到执行这句时的毫秒数 ms

dt = (double) (tick2 - tick1) // 或得时间差。

===============

如果是 获取 CPU cycle count

#include <stdint.h>

// Windows

#ifdef _WIN32

#include <intrin.h>

uint64_t rdtsc(){

return __rdtsc()

}

// Linux/GCC

#else

uint64_t rdtsc(){

unsigned int lo,hi

__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi))

return ((uint64_t)hi <<32) | lo

}

#endif

===================

获取高精度时间(MS VC++ 6.0编译器):

// Pentium instruction "Read Time Stamp Counter".

__forceinline unsigned _int64 My_clock(void)

{

_asm_emit 0x0F

_asm_emit 0x31

}

unsigned _int64 Start(void) { return My_clock()}

unsigned _int64 Stop(unsigned _int64 m_start, unsigned _int64 m_overhead)

{return My_clock()-m_start - m_overhead}

==========

获取cpu 速度(MS VC++ 6.0编译器):

void get_CPU_speed()

{

unsigned _int64 m_start=0, m_overhead=0

unsigned int CPUSpeedMHz

m_start = My_clock()

m_overhead = My_clock() - m_start - m_overhead

printf("overhead for calling My_clock=%I64d\n", m_overhead)

m_start = My_clock()

wait_ms(2000)

CPUSpeedMHz=(unsigned int) ( (My_clock()- m_start - m_overhead) / )

printf("CPU_Speed_MHz: %u\n",CPUSpeedMHz)

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7385895.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-05
下一篇2023-04-05

发表评论

登录后才能评论

评论列表(0条)

    保存