WinCE註冊表永久儲存導致系統效能下降解決
當我們終於實現註冊表永久儲存的時候我們發現我們不得不面對新的問題: 由於註冊表要儲存到永久儲存空間(如NAND Flash),每次修改註冊表都會去讀寫NAND Flash, 會嚴重影響某些應用程式的運行. 比如播放音視頻時,會寫入很多新的註冊表索引值,每次讀寫NAND Flash來儲存這些索引值會造成大量系統資源的浪費,導致開始播放的時候有停頓遲滯的現象。
這些都是因為我們使用了Aggressive Flush(又稱為 Flush-On-Close)的方式來儲存註冊表,它在每次調用RegCloseKey之後都會調用一次RegFlushKey,這種方式對於使用永久儲存空間的裝置來說是不可取的。那麼是否有更好的辦法呢?答案是肯定的。
(1) 首先,我們去掉Aggressive Flush,它的實現是在註冊表裡面以下位置設定RegistryFlags:
[HKEY_LOCAL_MACHINE/init/BootVars]
"SystemHive"="system.hv"
"DefaultUser"="default"
"Flags"=dword:3
"RegistryFlags"=dword:1
將RegistryFlags的值修改為0或者去掉RegistryFlags這個索引值均可,詳細含義可以到MS的協助文檔裡面尋找。
(2) 然後,使能Lazy Flush (Registry Flush Thread)
在編譯指令碼或者命令列中或(你的BSP名字).bat裡 設定環境變數
@REM For Hive lazy flush.
if /i "%IMGHIVEREG%"=="1" set PRJ_ENABLE_FSREGHIVE=1
if /i "%IMGHIVEREG%"=="1" set PRJ_ENABLE_REGFLUSH_THREAD=1
如果要更改重新整理時間,則在platform.reg 中添加
;----------------------------------------------------------------------------
; @CESYSGEN IF CE_MODULES_FSDMGR
; HIVE BOOT SECTION
[HKEY_LOCAL_MACHINE/System/ObjectStore/RegFlush]
; To monitor the flushing from an external process add "ActivityName" registry value.
; The activity name is a global named event that filesystem will signal on Registry Activity.
; "ActivityName"=""
; Create an thread in filesys to perform flushing
"SpawnThread"=dword:1
; Make the thread IDLE priority
"FlushPriority256"=dword:FF
; ActivityThreshold specifies the # of reg activity before we force a flush
"ActivityThreshold"=dword:100
; Timeout period in ms for a flush (flush occurs if there have been some changes during this period).
"FlushPeriod"=dword:7530
; END HIVE BOOT SECTION
; @CESYSGEN ENDIF CE_MODULES_FSDMGR
;----------------------------------------------------------------------------
系統會按照SpawnThread建立一個優先順序為FlushPriority256的線程,按照FlushPeriod(預設值為0x7530,10s)周期性地去檢查註冊表的變化並
加以儲存。