Linu系統(tǒng)時(shí)間不準(zhǔn)問(wèn)題分析_第1頁(yè)
Linu系統(tǒng)時(shí)間不準(zhǔn)問(wèn)題分析_第2頁(yè)
Linu系統(tǒng)時(shí)間不準(zhǔn)問(wèn)題分析_第3頁(yè)
Linu系統(tǒng)時(shí)間不準(zhǔn)問(wèn)題分析_第4頁(yè)
Linu系統(tǒng)時(shí)間不準(zhǔn)問(wèn)題分析_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Linux系統(tǒng)時(shí)間不準(zhǔn)問(wèn)題分析CPU: PowerPC P1020RDB-PCOS: Linux-2.6.35問(wèn)題:系統(tǒng)跑一段時(shí)間后,執(zhí)行date和hwclock分別獲取系統(tǒng)時(shí)鐘和rtc時(shí)鐘,出現(xiàn)差異, 差異為sysclk時(shí)間比rtc每分鐘慢0.6秒。誤差是比較人了。問(wèn)題分析轉(zhuǎn)換誤差時(shí)鐘不穩(wěn)定時(shí)鐘頻率不對(duì)結(jié)構(gòu)分析卜面是CPU內(nèi)部的核心時(shí)鐘組成Note: The logic circuits shown depict functional relationships only; they do not represent physical implementation details.Figur

2、e 4-7. RTC and Core Timer Facilities Clocking Options如上,Core Time Base (TBU+TBL)是一個(gè)時(shí)鐘計(jì)數(shù)器,里面存放的cycles隨著CCB Clock分 頻出來(lái)的時(shí)鐘一直遞增,cycles =系統(tǒng)時(shí)間怕乙DEC作為時(shí)鐘事件的中斷觸發(fā),按照時(shí)鐘頻率遞減,減到0后產(chǎn)生Decrementer Event爭(zhēng)件, 可以在中斷觸發(fā)時(shí)由程序?qū)懭氤跏贾?,也可以設(shè)置為Auto-Reload DECAR的值。這一塊還沒(méi) 具體分析有什么用。上圖中的RTC并沒(méi)有使用。當(dāng)前系統(tǒng)參數(shù)外部時(shí)鐘 =66.0MCCB Clock= 400MSYSCLK=

3、800M從CCB 8分頻出來(lái)給Core Time Base和DEC的時(shí)鐘為50M接下來(lái)來(lái)看一下Linux系統(tǒng)內(nèi)時(shí)間管理的內(nèi)容clock source用于為linux內(nèi)核提供一個(gè)時(shí)間基線(xiàn),實(shí)際上就是一,個(gè)時(shí)間相關(guān)的結(jié)構(gòu)體,如 果你用linux的date命令獲取當(dāng)前時(shí)間,內(nèi)核會(huì)讀取當(dāng)前的clock source,轉(zhuǎn)換并返回合適的 時(shí)間單位給用戶(hù)空間。在硬件層,它通常實(shí)現(xiàn)為一個(gè)由固定時(shí)鐘頻率驅(qū)動(dòng)的計(jì)數(shù)器(上面的 TimeBase),計(jì)數(shù)器只能單調(diào)地增加,直到溢出為止。系統(tǒng)啟動(dòng)時(shí),內(nèi)核通過(guò)硬件RTC獲得 當(dāng)前時(shí)間,并設(shè)置到計(jì)數(shù)器內(nèi),在這以后,內(nèi)核通過(guò)選定的時(shí)鐘源更新實(shí)時(shí)時(shí)間信息(墻上 時(shí)間),而不再讀

4、取RTC的時(shí)間。clock source可以有多個(gè),系統(tǒng)啟動(dòng)時(shí)會(huì)檢查所有clock source,然后將精度最好的時(shí)鐘源設(shè) 為當(dāng)前時(shí)鐘源。每個(gè)時(shí)鐘源的精度由驅(qū)動(dòng)它的時(shí)鐘頻率決定,可以用如下命令查看clocksource:roott:home#cat/sys/devices/system/clocksource/clocksourceO/available_c locksource timebase roott:home#cat /sys/devices/system/clocksource/clocksourceO/curre nt_clocksource timebase看一下我們系統(tǒng)中ti

5、mebase時(shí)鐘源的定義time.c (arch/powerpc/kernel) static struct clocksource clocksource_timebase = .n ame .rating .flags.mask .shift .mult .read=“timebase 蔦= 400,=CLOCK_SOURCES_CONTINUOUS,=CLOCKSOURCE_MASK(64),= 22,=0,/* To be filled in */=timebase.read,.read = timebase_read,該函數(shù)就是讀取TB的計(jì)數(shù)值 timebase_read是通過(guò)匯編來(lái)

6、實(shí)現(xiàn)的。static cycle_t timebase_read(struct clocksource *cs)return (cycle_t)get_tb();static inline u64 get_tb(void)return mftb();#define mftbu()(unsigned long rval; asm volatile(mftbu %0 : =r (rval); rval;)具體分析1 轉(zhuǎn)換誤差 現(xiàn)在我們可以獲取到cycles的計(jì)數(shù)值,也知道了 HZ=50M,那么根據(jù)公式很容易就得到系統(tǒng) 時(shí)間了。times_elapse = cycles_interval / fre

7、quency但是,因?yàn)閮?nèi)核中使用除法不太方便,所以將這個(gè)公式轉(zhuǎn)換成了乘法與移位操作 times_elapse = cycles_interval * mult shift關(guān)于這個(gè)轉(zhuǎn)換有個(gè)專(zhuān)門(mén)的內(nèi)核函數(shù),可以由frequency和精度值計(jì)算出mult和shift 后面再貼。從上面clocksource_tiniebase的定義己經(jīng)看到shift=22 mult=O(后續(xù)計(jì)算)了,看一下mult 的計(jì)算。在clocksourcejnit函數(shù)中找到mult的初始化clock-mult = clocksouiXQ-hz2mult(tb_tickS-pQr_SQC” clock-shift);打印出來(lái)這

8、個(gè)值為clock-mult =83886080現(xiàn)在shift和mult的值都有了,那我們來(lái)驗(yàn)證一下轉(zhuǎn)換的誤差就以 times_elapse = Is 為例,則 cyclesjnterval = frequency = 50000000按照公式:times_elapse = cyclesjnterval * mult shift (50000000*83886080)221000000000L= Is由此可見(jiàn),將除法轉(zhuǎn)換成乘法并未帶來(lái)誤差。2耐鐘頻率不對(duì)前面的計(jì)算都是按照CCB Clock 8分頻50M來(lái)計(jì)算,但是這個(gè)50M是否準(zhǔn)確?那就看看這個(gè)50M到底從哪來(lái)的timenit (/arch/p

9、owerpc/kernel/time.c)-ppc_md.calibrate_decr(); = generic_calibrate_decr(void)-get_freq(Htimebase-frequency: 1, &ppc_tb_freq)此處獲取到的ppc_tb_freq = 50Mget_freq是從設(shè)備樹(shù)中讀取的,但實(shí)際的設(shè)備樹(shù)中并沒(méi)有timebase-frequency這個(gè)選項(xiàng)最終找到 uboot 中 fdt.c (arch/powerpc/cpu/mpc85xx)void ft_cpu_setup(void *blob, bd_t *bd)do_fixup_by_prop_u

10、32(blob,device_type,/ cpu, 4,timebase-frequency, get_tbclk(), 1);由 do_fixup_by_prop_u32 將 get_tbclk()的值填入timebase-frequency,原來(lái)是 uboot 創(chuàng)建了 這個(gè)選項(xiàng),繼續(xù)查找50M的來(lái)歷,看看get_tbclk函數(shù)今#ifndef CONFIG_SYS_FSL_TBCLK_DIV#define CONFIG_SYS_FSL_TBCLK_DIV 8# endifunsigned long get_tbclk (void)unsigned long tbclk.div = CON

11、FIG_SYS.FSL.TBCLK.DIV;return (gd-bus_clk + (tbclk.div 1) / tbclk_div;-get_clocksgd-bus_clk = sy s J nf o .f r e qSy st e m B u s;Tget_sysnfounsigned long sysclk = CONFIG.SYS_CLK_FREQ sysl nfo-freqSystemBus = sysclk;syslnfo-freqSystemBus *= (in_be32(&gur-rcwsr0j) 25) & Oxlf;上面代碼可以看出get_tbclk()的原始值是從C

12、ONFIG_SYS_CLK_FREQ得來(lái)的 cp u_pl020.h(include/configs)中的定義#define CONFIG_SYS_CLK_FREQ 66666666而實(shí)際上外部時(shí)鐘是66.0M,原來(lái)是配置文件指定錯(cuò)了。系統(tǒng)實(shí)際參數(shù)外部時(shí)鐘 =66.0M TOC o 1-5 h z CCB Clock=396MSYSCLK=792MDDR=396Mppc_tb_freq=49500000clock-mult=84733414clock-shift=22重新計(jì)算一下轉(zhuǎn)換誤差:times_elapse = cyclesjnterval * mult shift (49500000*

13、84733414)22999999998L誤差為每秒2ns,已經(jīng)很小了附:內(nèi)核中由除法轉(zhuǎn)換成乘法的函數(shù)clocksource.c(kernel/time)/*clocks_calc_mult_shift - calculate mult/shift factors for scaled math of clocksmult:pointer to mult variableshift:pointer to shift variablefrom: frequency to convert fromto:frequency to convert tomin sec: guaranteed run t

14、ime conv ersion range in seconds*The function evaluates the shift/mult pair for the scaled mathoperations of clocksources and clockeventsto and from are frequency values in HZ. For clock sources to isNSEC_PER_SEC = 1GHz and from is the counter frequency. For clockevent to is the counter frequency an

15、d from is NSEC_PER_SEC*The minsec conversion range argument controls the time frame insec onds which must be covered by the run time conv ersio n with thecalculated mult and shift factors This guarantees that no 64bitoverflow happens when the input value of the conversion ismultiplied with the calcu

16、lated mult factor. Larger ranges mayreduce the conversion accuracy by chosing smaller mult and shiftfactors.*/voidclocks_calc_mult_shift(u32 *mult, u32 * shift, u32 from, u32 to, u32 minsec) u64 tmp;u32 sft, sftacc= 32;Calculate the shift factor which is limiting the conversionrange:*/tmp = (u64)min

17、sec * from) 32;while (tmp) tmp =1;sftacc-;Find the con version shift/mult pair which has the bestaccuracy and fits the maxsec conversion range:*/for (sft = 32; sft 0; sft-) tmp = (u64) to sft; do_div(tmp, from);if (tmp sftacc) = 0) break;*mult = tmp;*shift = sft;使用舉例:#in clude# include typedef unsig

18、ned int u32;typedef unsigned long long u64;#define NSEC_PER_SEC 1000000000Lvoidclocks_calc_mult_shift(u32 *mult/ u32 * shift, u32 from, u32 to, u32 maxsec) u64 tmp;u32 sft, sftacc= 32;* Calculate the shift factor which is limiting the conversion* range:tmp = (u64)maxsec * from) 32;while (tmp) tmp =1;sftacc-;* Find the conversio

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論