Sunday, April 24, 2011

Sleep command

In QMSL QPHONEMS/user defined transport, we use sleep(1) in worker thread to pull data from virtual COM ports. However, this “Sleep(1)” doesn’t really yield 1ms to other thread to execute. In fact, it yields close to 15ms. This symptom was observed in different incidents




1. Adnan’s station will take 10 mins to program LCU when “Sleep()” is used to control to data flow into the device. Other machines will take 3 mins

2. Foxconn’s machine takes 133 seconds to backup NVs with QPHONEMS. The same QMSL APIs running with QPST takes 20. This issue was later reproduced with QRD2 and QDART1.



It turns out that the default clock res, which is 15.625ms, affects the sleep behavior in OS.



You can download the tool from

http://technet.microsoft.com/en-us/sysinternals/bb897568.aspx

to check the clock res.



The slow machine has a clock resolution of 15.625ms. The normal machine has a clock resolution of 0.977ms (1/16 clock of 15.625). These slow machines usually don’t have much software installed. I guess some software, such as GPIB driver, running in background changes the default resolution to 0.977ms. In which, Sleep(1) will yield close to 1ms.



It turns out that there is undocumented API to change the clock resolution. The API is called “NtSetTimerReolution” in NTDLL.dll



http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/Time/NtSetTimerResolution.html



Once I call the API outside QMSL, all the problems were solved.



I plan to make some changes to QMSL worker thread.



1. Incorporate “NtSetTimerReoution” before worker thread is started

2. Expose new QMSL API to configure how often the worker thread goes to sleep. Currently, it sleep at each iteration. It takes 22 sec to backup NV in QPST mode. It takes 27s in QPHONEMS. Reducing the sleep frequency will speed up QHONEMS performance at the cost CPU usage. The default will still be Sleep(1) per iteration. The performance delta will be realized in 1) NON-Signaling test, 2) GPS I/Q capture, 3) NV backup/restore.

No comments:

Post a Comment