Skip to content
Tcl Source Code Artifact [9296c234e1]
Login

Artifact [9296c234e1]

Artifact 9296c234e14ee6364ad60948032783640ce63b9d:

Ticket change [9296c234e1] - New ticket [965722ffffffffff|965722ffff] <i>Poor time granularity on hyperthreaded CPU</i>. by kennykb on 2004-06-03 13:17:03.
D 2004-06-03T13:17:03
J assignee kennykb
J cmimetype text/plain
J comment Jeff\sGodfrey\swrote:\n>\sI'm\strying\sto\sdo\ssome\sanimation\son\sa\scanvas,\sand\sI\nneed\sa\shigh-resolution\n>\stimer\sin\sorder\sto\skeep\sthe\sframe-rates\sconsistent\nacross\svarying\smachines.\n>\sI\swent\sthrough\ssome\stroubles\swith\sthe\sresolution\sof\n[clock\n>\sclicks\s-milliseconds]\sa\snumber\sof\syears\sago\son\sWin98,\nbut\sKevin\sKenny\swas\n>\sable\sto\ssupply\sa\score\spatch\sto\sfix\sthat\sproblem.\n\nTcl\sis\sclose\sto\sthe\sragged\sedge\sof\swhat's\spossible\son\nWindows,\nI'm\safraid.\s\sLet\sme\sgive\sa\slittle\sbit\sof\sbackground\nhere,\sand\nmaybe\sthe\sWindows\sexperts\shere\scan\sfigure\sout\swhat's\nThe\sRight\nThing.\n\nYou\swill\snotice\sthat\sTcl\sactually\sgives\syou\stiming\sto\nsubmillisecond\saccuracy\son\smany\sWindows\smachines.\s\sAs\sfar\nas\sI\sknow,\s*no\sother\sproduct\sattempts\sthis\sfeat\swithout\nadditional\shardware\ssupport.*\s\sHow\sdo\swe\sdo\sit?\s\sWe\suse\sa\nphase-locked\sloop\sto\sdiscipline\sthe\ssystem\sperformance\ncounter\s(which\sis\suncalibrated)\sto\sthe\ssystem\sclock,\nallowing\sus\sto\sinterpolate\sbetween\sticks\sof\sthe\sclock.\n\nAlas,\sthe\ssystem\sperformance\scounter\sis\sat\sbest\simperfect.\nReading\sthe\sdocumentation\sfor\sQueryPerformanceCounter,\n\s\s\shttp://tinyurl.com/3xp96\nwe\sfind\sthe\sbizarre\sremark\s(which\shas\sbeen\sthere\ssince\nat\sleast\nNT\s4.0):\n\n\s\s\s\sOn\sa\smultiprocessor\smachine,\sit\sshould\snot\smatter\n\s\s\s\swhich\sprocessor\sis\scalled.\sHowever,\syou\scan\sget\n\s\s\s\sdifferent\sresults\son\sdifferent\sprocessors\sdue\sto\n\s\s\s\sbugs\sin\sthe\sbasic\sinput/output\ssystem\s(BIOS)\sor\sthe\n\s\s\s\shardware\sabstraction\slayer\s(HAL).\n\nIn\sfact,\sI\sam\swell\saware\sof\sthe\sproblem.\s\sAt\sleast\sone\sHAL\nfor\smultiprocessor\smachines\simplemented\sthe\sperformance\ncounter\nwith\sa\ssimple\sRDTSC\sinstruction,\sand\sderived\sthe\sclock\nreferences\sfor\sthe\stwo\sCPU's\sfrom\sseparate\scrystals.\s\sThis\nlimitation\smakes\sthe\sperformance\scounter\sessentially\nuseless\nunless\syou\sset\sthe\saffinity\smask\sof\sthe\sthread\sthat's\susing\nit\sto\sconstrain\sit\sto\sone\sor\sthe\sother\sCPU.\n\nTcl\sdetects\ssystems\sthat\shave\sthe\sproblem\sby\sa\ssimple\ncheck.\nThe\sinitialization\scode\sin\sTcl_GetTime\s(tclWinTime.c,\nstarting\naround\sline\s282)\slooks\sat\sthe\sperformance\scounter\nfrequency.\nIf\sit's\sat\smost\s15\sMHz,\sit's\spresumed\sto\sbe\sderived\nfrom\sthe\n8254-compatible\sreal-time\sclock,\sand\sTcl\suses\sthe\nperformance\ncounter\sto\sinterpolate\sbetween\sticks\sof\sthe\ssystem\sclock.\nOtherwise,\sit's\spresumed\sto\sbe\sthe\sresult\sof\sRDTSC,\sor\nof\sthe\nACPI\spower\stimer,\sand\sis\sconsidered\suntrustworthy.\s\sIf\sthe\nperformance\scounter\sis\suntrustworthy,\sthe\scode\sfalls\nback\son\nusing\sthe\ssystem\sclock,\swhich\shas\sa\smuch\scoarser\ngranularity\n(up\sto\s20\sms\son\smodern\sWindows;\ssome\sversions\sof\sWin95\sused\n55\sms).\n\nThis\sheuristic\shas\sbeen\s"good\senough"\sin\s8.4;\sit\ncaptured\sby\nfar\sthe\sgreater\snumber\sof\sdesktops\sand\slaptops,\sfailing\nonly\non\sa\ssmall\snumber\sof\sserver-class\smachines.\s\sThe\sadvent\sof\nhyperthreading\shas\schanged\sall\sthat.\s\sOn\sa\nhyperthreaded\smachine,\nthe\sperformance\scounter\sis\susually\sderived\susing\sRDTSC.\n\sNow,\nhowever,\sboth\sCPU's\sare\saboard\sthe\ssame\schip,\sand\sshare\na\scommon\ntimestamp\scounter\s(or\sperhaps\shave\sseparate\sTSC\nregisters\sthat\nincrement\sin\slockstep\s-\sessentially\sthe\ssame\sthing\sfor\sthe\npurposes\sof\sthis\sdiscussion).\s\sNow\swe\shave\sa\slarge\sclass\sof\nmachines\son\swhich\sQueryPerformanceFrequency\swill\sbe\sa\ncouple\nof\sGHz,\sand\sthe\sperformance\scounter\s*is*\sreliable.\n\nConservatism\sis\sbest\shere.\s\sIf\swe\suse\san\sunreliable\nperformance\scounter,\swe\sreturn\swildly\sincorrect\stimes\nto\sthe\nscript.\s\sIf\swe\sfail\sto\suse\sa\sreliable\sone,\swe\ssuffer\sat\nworst\nfrom\simprecision.\s\sSo\swhat\sI\sneed\sis\sa\sreliable\sway\sto\stest\nwhether\sthe\sperformance\scounter\smay,\sin\sfact,\sbe\srelied\son.\n\nI\scan\smanage\sto\sgrind\smy\sway\sthrough\sdecoding\sthe\ninformation\nfrom\sCPUID\sif\sI\smust.\s\sHowever,\sthat\swill\srequire\sa\sshort\ncodeburst\sin\sassembly\slanguage.\s\sI'm\sa\strifle\sreluctant\sto\nintroduce\sthat\scode\sowing\sto\sthe\sobvious\smaintainability\nissues\s-\sat\sthe\svery\sleast,\sit\sneeds\sto\ssupport\sboth\ngcc\sand\nVC++,\swhose\sassemblers\sare\svery\sdifferent.\s\sIf\sthere's\nsomething\noff\sthe\sshelf\sin\sWindows\sthat\sI\scan\suse\sto\sget\sthe\ninformation,\nI've\snot\sbeen\sable\sto\sfind\sit.
J foundin obsolete:\s8.4.6
J priority 7\sHigh
J resolution None
J status Open
J submitter kennykb
J subsystem 06.\sTime\sMeasurement
J title Poor\stime\sgranularity\son\shyperthreaded\sCPU
J type Bug
K 965722ffffffffffffffffffffffffffffffffff
U kennykb
Z 7dc953ff7fd7a7eda2366bb6f9a718a5