Home
%3CLINGO-SUB%20id%3D%22lingo-sub-530142%22%20slang%3D%22en-US%22%3EWhen%20is%20Spinlock%20a%20Significant%20Driver%20of%20CPU%20utilization%20in%20SQL%20Server%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-530142%22%20slang%3D%22en-US%22%3E%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3EWhen%20is%20Spinlock%20a%20Significant%20Driver%20of%20CPU%20utilization%3F%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3E%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3ESQL%20Server%20uses%20multiple%20mechanisms%20for%20synchronizing%20access%20to%20shared%20structures.%20One%20such%20mechanism%20is%20a%20spinlock.%20Briefly%2C%20a%20spinlock%20is%20a%20type%20of%20lock%20acquisition%20mechanism%20which%20is%20designed%20to%20avoid%20%3CA%20style%3D%22background-color%3A%20transparent%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23146cac%3B%20text-decoration%3A%20underline%3B%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fdesktop%2FProcThread%2Fcontext-switches%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Econtext%20switching%3C%2FA%3E%20.%20In%20essence%2C%20instead%20of%20going%20to%20sleep%20(Sleep())%20or%20waiting%20to%20be%20signaled%20(WaitForSingleObject())%2C%20a%20thread%20performs%20%22idle%22%20loops%20on%20the%20CPU%20and%20periodically%20checks%20if%20the%20lock%20becomes%20available%20so%20it%20can%20acquire%20it.%20Think%20of%20it%20as%20%22Spin%20until%20I%20can%20get%20the%20lock%22.%20The%20goal%20is%20to%20avoid%20thread%20Context%20switching%20which%20is%20an%20expensiveness%20operation%20from%20an%20OS%20perspective.%20Spinlocks%20use%20an%20optimistic%20approach%20assuming%20that%20it%20won't%20take%20%22too%20many%22%20loops%20or%20spins%20until%20the%20lock%20is%20available.%20Spinlocks%20can%20become%20a%20problem%20if%20this%20assumption%20is%20not%20true%2C%20that%20is%2C%20an%20excessive%20number%20of%20spins%20are%20performed%20leading%20to%20high%20CPU%20usage.%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3EFor%20more%20details%20about%20Spinlocks%2C%20their%20implementation%20and%20t-shooting%2C%20please%20review%20the%20following%20white%20paper%3A%20%3CA%20style%3D%22background-color%3A%20transparent%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23146cac%3B%20text-decoration%3A%20underline%3B%22%20href%3D%22https%3A%2F%2Fwww.microsoft.com%2Fen-us%2Fdownload%2Fdetails.aspx%3Fid%3D26666%2520%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EDiagnosing%20and%20Resolving%20Spinlock%20Contention%20on%20SQL%20Server%3C%2FA%3E%26nbsp%3B%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3E%22Spinlocks%20are%20lightweight%20synchronization%20primitives%20which%20are%20used%20to%20protect%20access%20to%20data%20structures.%20Spinlocks%20are%20not%20unique%20to%20SQL%20Server.%20They%20are%20generally%20used%20when%20it%20is%20expected%20that%20access%20to%20a%20given%20data%20structure%20will%20need%20to%20be%20held%20for%20a%20very%20short%20period%20of%20time.%20When%20a%20thread%20attempting%20to%20acquire%20a%20spinlock%20is%20unable%20to%20obtain%20access%20it%20executes%20in%20a%20loop%20periodically%20checking%20to%20determine%20if%20the%20resource%20is%20available%20instead%20of%20immediately%20yielding.%20After%20some%20period%20of%20time%20a%20thread%20waiting%20on%20a%20spinlock%20will%20yield%20before%20it%20is%20able%20to%20acquire%20the%20resource%20in%20order%20to%20allow%20other%20threads%20running%20on%20the%20same%20CPU%20to%20execute.%20This%20is%20known%20as%20a%20backoff%20and%20will%20be%20discussed%20in%20more%20depth%20later%20in%20this%20paper.%22%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3EThe%20question%20this%20post%20will%20attempt%20to%20address%20is%20%22How%20many%20spins%20is%20too%20many%3F%22%20or%20%22When%20do%20we%20consider%20spinlocks%20to%20be%20a%20significant%20driver%20of%20CPU%20usage%22%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3EThe%20key%20to%20answering%20this%20is%20examining%20%22Spins%20per%20second%20per%20CPU%22.%20If%20this%20number%20exceeds%20approximately%20%3CSTRONG%20style%3D%22box-sizing%3A%20border-box%3B%20font-weight%3A%20bold%3B%22%3E20000%3C%2FSTRONG%3E%20spins%20per%20millisecond%20on%20a%20single%20CPU%2C%20then%20the%20spinlock%20has%20reached%20a%20level%20where%20it%20impacts%20CPU%20usage%20significantly.%20How%20do%20I%20arrive%20at%20this%20number%3F%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3ESpinlocks%20are%20a%20little%20more%20than%20regular%20for%2Fwhile%20loops%2C%20so%20if%20you%20measure%20approximately%20how%20many%20tight%20loops%20a%20CPU%20can%20perform%20in%20a%20unit%20of%20time%20-%20say%20millisecond%20-%20then%20you%20would%20know%20what%20is%20a%20reasonable%20threshold%20to%20compare%20against.%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3EThe%20following%20sample%20C%2B%2B%20application%20accomplishes%20just%20that%20on%20a%20single%20CPU%20(it%20introduces%20some%20pause%20similar%20to%20spinlock%20code)%20(get%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FPiJoCoder%2FSpinlockSimulator%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Eentire%20solution%20at%20GitHub%3C%2FA%3E%20)%3A%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%0A%3CPRE%20style%3D%22background-color%3A%20%23f5f5f5%3B%20border-bottom-left-radius%3A%204px%3B%20border-bottom-right-radius%3A%204px%3B%20border-image-outset%3A%200%3B%20border-image-repeat%3A%20stretch%3B%20border-image-slice%3A%20100%25%3B%20border-image-source%3A%20none%3B%20border-image-width%3A%201%3B%20border-top-left-radius%3A%204px%3B%20border-top-right-radius%3A%204px%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%233e3e3e%3B%20display%3A%20block%3B%20font-family%3A%20Menlo%2CMonaco%2CConsolas%2C%26amp%3Bquot%3B%20courier%20new%26amp%3Bquot%3B%2Cmonospace%3B%20font-size%3A%2013px%3B%20line-height%3A%201.7142%3B%20overflow%3A%20auto%3B%20overflow-wrap%3A%20break-word%3B%20word-break%3A%20break-all%3B%20padding%3A%2011.5px%3B%20margin%3A%200px%200px%2012px%200px%3B%20border%3A%201px%20solid%20%23cccccc%3B%22%3E%23include%20%26lt%3Bstdio.h%26gt%3B%0A%23include%20%26lt%3Btchar.h%26gt%3B%0A%23include%20%26lt%3BWindows.h%26gt%3B%0A%23include%20%26lt%3Bassert.h%26gt%3B%20%20%20%0A%23include%20%26lt%3Bprocess.h%26gt%3B%0A%0A%2F%2Fmake%20this%20global%20for%20easy%20access%0ALONG%20volatile%20lockValue%20%3D%201%3B%0Aconst%20UINT64%20MIN_SPIN%20%3D%20250%3B%0Aconst%20UINT64%20MAX_SPIN%20%3D%201000000%3B%0AUINT64%20globalSpins%20%3D%200%3B%0A%0A%0ABOOL%20GetLock(%0A%20__in%20LONG%20Id)%20%20%20%2F%2F%20I%20Thread%20id%20and%20additional%20information%0A%7B%0A%20assert(Id%20!%3D%200)%3B%0A%0A%20UINT64%20oldId%20%3D%20InterlockedCompareExchangeAcquire(%26amp%3BlockValue%2C%20Id%2C%200)%3B%0A%0A%20return%20oldId%20%3D%3D%200%3B%0A%7D%0A%0Avoid%20ReleaseLock(%20void%20*)%0A%7B%0A%20%2F%2Fsleep%20an%20arbitrarily%20hard-coded%20time%0A%20Sleep(5000)%3B%0A%0A%20%2F%2Fset%20the%20value%20to%200%20so%20the%20lock%20is%20released%20and%20spinlock%20acquires%20the%20lock%0A%20lockValue%20%3D%200%3B%20%0A%0A%7D%0A%0Avoid%0ASpinToAcquireLockWithExponentialBackoff(__in%20DWORD%20Id%2C%20__out%20UINT32%20*%20BackoffCount)%20%20%20%0A%7B%0A%0A%20UINT32%20Backoffs%20%3D%200%3B%0A%0A%20UINT64%20spinLimit%20%3D%20MIN_SPIN%3B%0A%0A%20UINT64%20iSpin%20%3D%200%3B%0A%0A%20while%20(true)%0A%20%7B%0A%20%20assert(spinLimit%20%26lt%3B%3D%20MAX_SPIN%20%26amp%3B%26amp%3B%20spinLimit%20%26gt%3B%3D%20MIN_SPIN)%3B%0A%0A%20%20%2F%2F%20Spin%20for%20a%20while%20without%20reading%20the%20lock.%0A%20%20%2F%2F%0A%20%20for%20(iSpin%20%3D%200%3B%20iSpin%20%26lt%3B%20spinLimit%3B%20iSpin%2B%2B)%0A%20%20%7B%0A%20%0A%20%20%20_mm_pause()%3B%20%0A%20%20%7D%0A%0A%20%20%2F%2Fprintf_s(%22iSpin%20%3D%20%25ld%5Cn%22%2C%20iSpin)%3B%0A%0A%20%20%2F%2Fincrement%20the%20global%20spins%0A%20%20globalSpins%20%3D%20globalSpins%20%2B%20iSpin%3B%0A%0A%20%20%2F%2Fprintf_s(%22globalSpins%20%3D%20%25ld%5Cn%22%2C%20globalSpins)%3B%0A%0A%0A%20%20%2F%2F%20Try%20to%20get%20the%20lock.%0A%20%20if%20(GetLock(Id))%0A%20%20%7B%0A%20%20%20break%3B%0A%20%20%7D%0A%0A%20%20%2F%2F%0A%20%20spinLimit%20%3D%20min(MAX_SPIN%2C%20spinLimit%20*%202)%3B%0A%0A%20%20%2F%2F%20See%20if%20we%20need%20to%20backoff%0A%20%20%2F%2F%0A%20%20Backoffs%2B%2B%3B%0A%0A%20%7D%0A%0A%20%2F%2Foutput%20the%20final%20backoffs%0A%20*BackoffCount%20%3D%20Backoffs%3B%20%0A%0A%7D%20%2F%2F%20SpinToAcquireLockWithExponentialBackoff%0A%0A%0Avoid%20ExerciseSpinLockCode()%0A%7B%0A%20UINT32%20backoffCount%20%3D%200%3B%0A%20LARGE_INTEGER%20beforeHighRes%2C%20afterHighRes%2C%20elapsedMicroseconds%2C%20frequency%3B%0A%0A%20%2F%2Fspawn%20a%20thread%20to%20simulate%20lock%20release%0A%20HANDLE%20hThread%20%3D%20(HANDLE)_beginthread(ReleaseLock%2C%200%2C%20NULL)%3B%0A%0A%20%2F%2Fget%20start%20times%0A%20QueryPerformanceCounter(%26amp%3BbeforeHighRes)%3B%0A%20long%20int%20before%20%3D%20GetTickCount()%3B%0A%20%0A%20%2F%2Finvoke%20Spinlock%20code%0A%20SpinToAcquireLockWithExponentialBackoff(GetCurrentThreadId()%2C%20%26amp%3BbackoffCount)%3B%0A%20%0A%20%2F%2Fget%20the%20end%20times%0A%20long%20int%20after%20%3D%20GetTickCount()%3B%0A%20QueryPerformanceCounter(%26amp%3BafterHighRes)%3B%0A%20QueryPerformanceFrequency(%26amp%3Bfrequency)%3B%0A%0A%20%2F%2Fcalculate%20microseconds%0A%20%2F%2Fticks%20divided%20by%20ticks-per-second%20(frequency)%20%2C%20gives%20us%20seconds%0A%20%2F%2Fconverted%20to%20microseconds%20by%20multiplying%20to%201%20mil%0A%20%0A%20elapsedMicroseconds.QuadPart%20%3D%20(afterHighRes.QuadPart%20-%20beforeHighRes.QuadPart)%3B%0A%20elapsedMicroseconds.QuadPart%20*%3D%201000000%3B%20%20%0A%20elapsedMicroseconds.QuadPart%20%2F%3D%20frequency.QuadPart%3B%0A%0A%20UINT64%20SpinsPerMillisecond%20%3D%20(globalSpins)%20%2F%20(after%20-%20before)%3B%0A%0A%20printf_s(%22SpinToAcquireLockWithExponentialBackoff%3A%20Milliseconds%20elapsed%20%3D%20%25ld%2C%20Spins%3D%25I64d%2C%20Backoffs%3D%25ld%5Cn%22%2C%20after%20-%20before%2C%20globalSpins%2C%20backoffCount)%3B%0A%20printf_s(%22SpinToAcquireLockWithExponentialBackoff%3A%20Spins%2FMillisecond%3D%25I64d%5Cn%22%2C%20SpinsPerMillisecond)%3B%0A%20printf_s(%22SpinToAcquireLockWithExponentialBackoff%3A%20Spins%2FMillisecond(QPC)%3D%25I64d%5Cn%22%2C%20globalSpins%2F(elapsedMicroseconds.QuadPart%2F1000))%3B%0A%0A%20%2F%2Fwait%20on%20the%20thread%20completion%0A%20WaitForSingleObject(hThread%2C%20INFINITE)%3B%0A%7D%0A%0Avoid%20ExerciseSimpleLoopCode()%0A%7B%0A%20long%20i%3D0%3B%0A%20LARGE_INTEGER%20beforeHighRes%2C%20afterHighRes%2C%20elapsedMicroseconds%2C%20frequency%3B%0A%20%0A%20QueryPerformanceCounter(%26amp%3BbeforeHighRes)%3B%0A%0A%20%2F%2Floop%2C%20no%20pause%0A%20for%20(i%3B%20i%20%26lt%3B%20MAX_SPIN*10%3B%20i%2B%2B)%0A%20%7B%0A%20%20%3B%0A%20%7D%0A%0A%0A%20QueryPerformanceCounter(%26amp%3BafterHighRes)%3B%0A%20QueryPerformanceFrequency(%26amp%3Bfrequency)%3B%0A%0A%20%0A%20%2F%2Fhttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fdesktop%2FSysInfo%2Facquiring-high-resolution-time-stamps%0A%20elapsedMicroseconds.QuadPart%20%3D%20(afterHighRes.QuadPart%20-%20beforeHighRes.QuadPart)%3B%0A%20elapsedMicroseconds.QuadPart%20*%3D%201000000%3B%0A%20elapsedMicroseconds.QuadPart%20%2F%3D%20frequency.QuadPart%3B%0A%0A%0A%20long%20elapsedTimeQPC%20%3D%20elapsedMicroseconds.QuadPart%20%2F%201000%20%3D%3D%200%20%3F%201%20%3A%20(elapsedMicroseconds.QuadPart%20%2F%201000)%3B%0A%0A%20printf_s(%22Simple%20Loop%3A%20Millisecond%20elapsed%20(QPC)%3D%25lld%2C%20Loops%3D%25d%5Cn%22%2C%20(elapsedMicroseconds.QuadPart%20%2F%201000)%2C%20i)%3B%0A%20printf_s(%22Simple%20Loop%3A%20Spins%2FMillisecond%20(QPC)%3D%25ld%5Cn%22%2C%20i%20%2F%20elapsedTimeQPC)%3B%0A%0A%7D%0A%0Aint%20main()%0A%7B%0A%20ExerciseSpinLockCode()%3B%0A%0A%20ExerciseSimpleLoopCode()%3B%0A%0A%20printf_s(%22Press%20ENTER%20to%20end...%22)%3B%0A%20_gettchar()%3B%0A%0A%20return%200%3B%0A%7D%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3E%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3ESample%20Results%201%20(%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3EIntel%20i7%26nbsp%3B2.7%20Ghz%3C%2FFONT%3E)%3A%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3E%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3ESpinToAcquireLockWithExponentialBackoff%3A%20Milliseconds%20elapsed%20%3D%205031%2C%20Spins%3D102023750%2C%20Backoffs%3D112%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3ESpinToAcquireLockWithExponentialBackoff%3A%20Spins%2FMillisecond%3D%3CSTRONG%20style%3D%22box-sizing%3A%20border-box%3B%20font-weight%3A%20bold%3B%22%3E20279%3C%2FSTRONG%3E%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3ESpinToAcquireLockWithExponentialBackoff%3A%20Spins%2FMillisecond(QPC)%3D%3CSTRONG%20style%3D%22box-sizing%3A%20border-box%3B%20font-weight%3A%20bold%3B%22%3E20315%3C%2FSTRONG%3E%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3ESimple%20Loop%3A%20Millisecond%20elapsed%20(QPC)%3D4%2C%20Loops%3D10000000%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3ESimple%20Loop%3A%20Spins%2FMillisecond%20(QPC)%3D2500000%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3EPress%20ENTER%20to%20end...%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3ESample%20Results%202%20(%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3EAMD%20A6-6310%20APU%201.8%20Ghz%3C%2FFONT%3E)%3A%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3E%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3ESpinToAcquireLockWithExponentialBackoff%3A%20Milliseconds%20elapsed%20%3D%205016%2C%20Spins%3D187023750%2C%20Backoffs%3D197%3CBR%20%2F%3ESpinToAcquireLockWithExponentialBackoff%3A%20Spins%2FMillisecond%3D%3CSTRONG%3E37285%3C%2FSTRONG%3E%3CBR%20%2F%3ESpinToAcquireLockWithExponentialBackoff%3A%20Spins%2FMillisecond(QPC)%3D%3CSTRONG%3E37292%3C%2FSTRONG%3E%3CBR%20%2F%3ESimple%20Loop%3A%20Millisecond%20elapsed%20(QPC)%3D11%2C%20Loops%3D10000000%3CBR%20%2F%3ESimple%20Loop%3A%20Spins%2FMillisecond%20(QPC)%3D909090%3CBR%20%2F%3EPress%20ENTER%20to%20end...%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3ESample%20Results%203%20(%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3EIntel%20Q6600%26nbsp%3B2.4%20Ghz)%3C%2FFONT%3E%3A%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3E%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3ESpinToAcquireLockWithExponentialBackoff%3A%20Milliseconds%20elapsed%20%3D%205015%2C%20Spins%3D916023750%2C%20Backoffs%3D926%3CBR%20%2F%3ESpinToAcquireLockWithExponentialBackoff%3A%20Spins%2FMillisecond%3D%3CSTRONG%3E182656%3C%2FSTRONG%3E%3CBR%20%2F%3ESpinToAcquireLockWithExponentialBackoff%3A%20Spins%2FMillisecond(QPC)%3D%3CSTRONG%3E183058%3C%2FSTRONG%3E%3CBR%20%2F%3ESimple%20Loop%3A%20Millisecond%20elapsed%20(QPC)%3D4%2C%20Loops%3D10000000%3CBR%20%2F%3ESimple%20Loop%3A%20Spins%2FMillisecond%20(QPC)%3D2500000%3CBR%20%2F%3EPress%20ENTER%20to%20end...%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3ESample%20Results%204%20(%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3EIntel%20Xeon%20E5-2620%20v4%202.10%20Ghz%3C%2FFONT%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3E)%3C%2FFONT%3E%3A%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3E%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3ESpinToAcquireLockWithExponentialBackoff%3A%20Milliseconds%20elapsed%20%3D%205016%2C%20Spins%3D81123750%2C%20Backoffs%3D821%3CBR%20%2F%3ESpinToAcquireLockWithExponentialBackoff%3A%20Spins%2FMillisecond%3D%3CSTRONG%3E161687%3C%2FSTRONG%3E%3CBR%20%2F%3ESpinToAcquireLockWithExponentialBackoff%3A%20Spins%2FMillisecond(QPC)%3D%3CSTRONG%3E161977%3C%2FSTRONG%3E%3CBR%20%2F%3ESimple%20Loop%3A%20Millisecond%20elapsed%20(QPC)%3D4%2C%20Loops%3D10000000%3CBR%20%2F%3ESimple%20Loop%3A%20Spins%2FMillisecond%20(QPC)%3D2500000%3CBR%20%2F%3EPress%20ENTER%20to%20end...%3C%2FFONT%3E%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3E%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3ESince%20the%20above%20example%20is%20designed%20to%20perform%20a%20little%20more%20than%20a%20loop%20with%20periodic%20pauses%20until%20the%20lock%20is%20acquired%2C%20it%20would%20drive%20CPU%20utilization%20to%20100%25%20on%20a%20single%20CPU.%20%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3EBased%20on%20these%20values%20one%20can%20conclude%20that%20if%20spinlock%20statistics%20show%20values%20greater%20than%20about%20%3CSTRONG%20style%3D%22box-sizing%3A%20border-box%3B%20font-weight%3A%20bold%3B%22%3E20000%26nbsp%3B%3C%2FSTRONG%3E%20spins%20per%20millisecond%20per%20CPU%2C%20then%20the%20CPU%20usage%20is%20likely%20fairly%20high%20and%20close%20to%20100%25%20capacity.%20%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3ENaturally%2C%20CPU%20clock%20speeds%20would%20impact%20the%20results%20and%20faster%20CPUs%20would%20be%20able%20to%20perform%20more%20spins%20per%20millisecond.%20Other%20CPU-related%20specifications%20like%20L1%2C%20L2%2C%20L3%20caches%20may%20also%20play%20somewhat%20of%20a%20role%20here.%20However%2C%20the%20goal%20of%20this%20post%20is%20to%20provide%20guidance%2C%20a%20%22ballpark%22%20figure%20so%20to%20speak.%20Please%20feel%20free%20to%20report%20your%20test%20results%20in%20the%20Comments%20section.%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3E%3CBR%20style%3D%22box-sizing%3A%20border-box%3B%22%20%2F%3EFor%20more%20information%20on%20how%20to%20collect%20Spinlock%20t-shooting%20information%2C%20please%20see%20above%20white%20paper%20-%20Appending%20B.%20%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%22%3EAlternatively%2C%20below%20is%20a%20spinlock%20data-collection%20and%20analysis%20script%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%0A%3CPRE%20style%3D%22background-color%3A%20%23f5f5f5%3B%20border-bottom-left-radius%3A%204px%3B%20border-bottom-right-radius%3A%204px%3B%20border-image-outset%3A%200%3B%20border-image-repeat%3A%20stretch%3B%20border-image-slice%3A%20100%25%3B%20border-image-source%3A%20none%3B%20border-image-width%3A%201%3B%20border-top-left-radius%3A%204px%3B%20border-top-right-radius%3A%204px%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%233e3e3e%3B%20display%3A%20block%3B%20font-family%3A%20Menlo%2CMonaco%2CConsolas%2C%26amp%3Bquot%3B%20courier%20new%26amp%3Bquot%3B%2Cmonospace%3B%20font-size%3A%2013px%3B%20line-height%3A%201.7142%3B%20overflow%3A%20auto%3B%20overflow-wrap%3A%20break-word%3B%20word-break%3A%20break-all%3B%20padding%3A%2011.5px%3B%20margin%3A%200px%200px%2012px%200px%3B%20border%3A%201px%20solid%20%23cccccc%3B%22%3Euse%20tempdb%0Ago%0Adeclare%20%40cntr%20int%20%3D%200%0Aif%20exists(select%20name%20from%20tempdb.sys.sysobjects%20where%20name%20like%20'%23_spin_waits%25')%0Adrop%20table%20%23_spin_waits%0A%0A%0Acreate%20table%20%23_spin_waits%0A(%0Alock_name%20varchar(128)%0A%2Ccollisions%20bigint%0A%2Cspins%20bigint%0A%2Csleep_time%20bigint%0A%2Cbackoffs%20bigint%0A%2Csnap_time%20datetime%0A)%0A%0Awhile%20(%40cntr%20%26lt%3B%204)%0Abegin%0A%20--capture%20the%20current%20stats%0A%20insert%20into%20%23_spin_waits%0A%20(%0A%20%20lock_name%0A%20%20%2Ccollisions%0A%20%20%2Cspins%0A%20%20%2Csleep_time%0A%20%20%2Cbackoffs%0A%20%20%2Csnap_time%0A%20)%0A%20select%20name%0A%20%20%2Ccollisions%0A%20%20%2Cspins%0A%20%20%2Csleep_time%0A%20%20%2Cbackoffs%0A%20%20%2Cgetdate()%0A%20from%20sys.dm_os_spinlock_stats%0A%0A%20waitfor%20delay%20'00%3A00%3A10'%0A%20set%20%40cntr%20%3D%20%40cntr%20%2B%201%0A%0Aend%0A%0A--Analysis%0A%0Adeclare%20%40cpu%20int%0Aselect%20%40cpu%20%3D%20cpu_count%20from%20sys.dm_os_sys_info%0A%0A--SPINLOCKS%20compute%20delta%20%0Aselect%20%20t2.%5Block_name%5D%20as%20spinlock_name%2C%20%20%0Acast(cast(t2.spins%20as%20float)%20-%20cast(t1.spins%20as%20float)%20as%20bigint)%20delta_spins%2C%20%20%0Acast%20(cast(t2.Backoffs%20as%20float)%20-%20cast%20(t1.Backoffs%20as%20float)%20as%20bigint)%20delta_backoff%2C%20%0ADATEDIFF(MILLISECOND%2Ct1.snap_time%2Ct2.snap_time)%20delta_minuntes%2C%0Acast(cast(t2.spins%20as%20float)%20-%20cast(t1.spins%20as%20float)%20as%20bigint)%2FDATEDIFF(MILLISECOND%2Ct1.snap_time%2Ct2.snap_time)%2F%40cpu%20delta_spins_per_millisecond_per_CPU%0A%0Afrom%20%0A(select%20row_number%20()%20over%20(%20partition%20by%20%5Block_name%5D%20%20order%20by%20%20%20%20snap_time)%20row%2C%20*%20%20from%20%5B%23_spin_waits%5D%20%0A%20where%20snap_time%20in%20%0A%20(select%20min(snap_time)%20from%20%23_spin_waits)%20)%20t1%0Ajoin%20%0A(select%20row_number%20()%20over%20(%20partition%20by%20%5Block_name%5D%20%20order%20by%20%20%20%20snap_time)%20row%2C%20*%20%20from%20%5B%23_spin_waits%5D%20%20%0A%20where%20snap_time%20in%20%0A%20(select%20max(snap_time)%20from%20%23_spin_waits)%20)%20t2%0Aon%20t1.row%20%3D%20t2.row%20and%20t1.%5Block_name%5D%3Dt2.%5Block_name%5D%0Awhere%20cast(cast(t2.spins%20as%20float)%20-%20cast(t1.spins%20as%20float)%20as%20bigint)%20%26gt%3B%200%0Aorder%20by%20delta_spins%20desc%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CDIV%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20inherit%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20line-height%3A%201.7142%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-530142%22%20slang%3D%22en-US%22%3E%3CP%3ESQL%20Server%20Spinlocks%20-%26nbsp%3B%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3E%22How%20many%20spins%20is%20too%20many%3F%22%3C%2FFONT%3E%20%3C%2FP%3E%0A%3CP%3EThe%20answer%3A%20approximately%2018%2C000%20-%2020%2C000%20or%20more%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-530142%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPerformance%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESQL%20OS%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ETSQL%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
When is Spinlock a Significant Driver of CPU utilization?

SQL Server uses multiple mechanisms for synchronizing access to shared structures. One such mechanism is a spinlock. Briefly, a spinlock is a type of lock acquisition mechanism which is designed to avoid context switching . In essence, instead of going to sleep (Sleep()) or waiting to be signaled (WaitForSingleObject()), a thread performs "idle" loops on the CPU and periodically checks if the lock becomes available so it can acquire it. Think of it as "Spin until I can get the lock". The goal is to avoid thread Context switching which is an expensiveness operation from an OS perspective. Spinlocks use an optimistic approach assuming that it won't take "too many" loops or spins until the lock is available. Spinlocks can become a problem if this assumption is not true, that is, an excessive number of spins are performed leading to high CPU usage.
For more details about Spinlocks, their implementation and t-shooting, please review the following white paper: Diagnosing and Resolving Spinlock Contention on SQL Server 
 
"Spinlocks are lightweight synchronization primitives which are used to protect access to data structures. Spinlocks are not unique to SQL Server. They are generally used when it is expected that access to a given data structure will need to be held for a very short period of time. When a thread attempting to acquire a spinlock is unable to obtain access it executes in a loop periodically checking to determine if the resource is available instead of immediately yielding. After some period of time a thread waiting on a spinlock will yield before it is able to acquire the resource in order to allow other threads running on the same CPU to execute. This is known as a backoff and will be discussed in more depth later in this paper."
 
The question this post will attempt to address is "How many spins is too many?" or "When do we consider spinlocks to be a significant driver of CPU usage"
 
The key to answering this is examining "Spins per second per CPU". If this number exceeds approximately 20000 spins per millisecond on a single CPU, then the spinlock has reached a level where it impacts CPU usage significantly. How do I arrive at this number?
 
Spinlocks are a little more than regular for/while loops, so if you measure approximately how many tight loops a CPU can perform in a unit of time - say millisecond - then you would know what is a reasonable threshold to compare against.
The following sample C++ application accomplishes just that on a single CPU (it introduces some pause similar to spinlock code) (get entire solution at GitHub ):
 
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <assert.h>   
#include <process.h>

//make this global for easy access
LONG volatile lockValue = 1;
const UINT64 MIN_SPIN = 250;
const UINT64 MAX_SPIN = 1000000;
UINT64 globalSpins = 0;


BOOL GetLock(
	__in LONG Id)			// I Thread id and additional information
{
	assert(Id != 0);

	UINT64 oldId = InterlockedCompareExchangeAcquire(&lockValue, Id, 0);

	return oldId == 0;
}

void ReleaseLock( void *)
{
	//sleep an arbitrarily hard-coded time
	Sleep(5000);

	//set the value to 0 so the lock is released and spinlock acquires the lock
	lockValue = 0; 

}

void
SpinToAcquireLockWithExponentialBackoff(__in DWORD Id, __out UINT32 * BackoffCount)			
{

	UINT32	Backoffs = 0;

	UINT64 spinLimit = MIN_SPIN;

	UINT64 iSpin = 0;

	while (true)
	{
		assert(spinLimit <= MAX_SPIN && spinLimit >= MIN_SPIN);

		// Spin for a while without reading the lock.
		//
		for (iSpin = 0; iSpin < spinLimit; iSpin++)
		{
	
			_mm_pause(); 
		}

		//printf_s("iSpin = %ld\n", iSpin);

		//increment the global spins
		globalSpins = globalSpins + iSpin;

		//printf_s("globalSpins = %ld\n", globalSpins);


		// Try to get the lock.
		if (GetLock(Id))
		{
			break;
		}

		//
		spinLimit = min(MAX_SPIN, spinLimit * 2);

		// See if we need to backoff
		//
		Backoffs++;

	}

	//output the final backoffs
	*BackoffCount = Backoffs; 

} // SpinToAcquireLockWithExponentialBackoff


void ExerciseSpinLockCode()
{
	UINT32 backoffCount = 0;
	LARGE_INTEGER beforeHighRes, afterHighRes, elapsedMicroseconds, frequency;

	//spawn a thread to simulate lock release
	HANDLE hThread = (HANDLE)_beginthread(ReleaseLock, 0, NULL);

	//get start times
	QueryPerformanceCounter(&beforeHighRes);
	long int before = GetTickCount();
	
	//invoke Spinlock code
	SpinToAcquireLockWithExponentialBackoff(GetCurrentThreadId(), &backoffCount);
	
	//get the end times
	long int after = GetTickCount();
	QueryPerformanceCounter(&afterHighRes);
	QueryPerformanceFrequency(&frequency);

	//calculate microseconds
	//ticks divided by ticks-per-second (frequency) , gives us seconds
	//converted to microseconds by multiplying to 1 mil
	
	elapsedMicroseconds.QuadPart = (afterHighRes.QuadPart - beforeHighRes.QuadPart);
	elapsedMicroseconds.QuadPart *= 1000000;  
	elapsedMicroseconds.QuadPart /= frequency.QuadPart;

	UINT64 SpinsPerMillisecond = (globalSpins) / (after - before);

	printf_s("SpinToAcquireLockWithExponentialBackoff: Milliseconds elapsed = %ld, Spins=%I64d, Backoffs=%ld\n", after - before, globalSpins, backoffCount);
	printf_s("SpinToAcquireLockWithExponentialBackoff: Spins/Millisecond=%I64d\n", SpinsPerMillisecond);
	printf_s("SpinToAcquireLockWithExponentialBackoff: Spins/Millisecond(QPC)=%I64d\n", globalSpins/(elapsedMicroseconds.QuadPart/1000));

	//wait on the thread completion
	WaitForSingleObject(hThread, INFINITE);
}

void ExerciseSimpleLoopCode()
{
	long i=0;
	LARGE_INTEGER beforeHighRes, afterHighRes, elapsedMicroseconds, frequency;
	
	QueryPerformanceCounter(&beforeHighRes);

	//loop, no pause
	for (i; i < MAX_SPIN*10; i++)
	{
		;
	}


	QueryPerformanceCounter(&afterHighRes);
	QueryPerformanceFrequency(&frequency);

	
	//https://docs.microsoft.com/en-us/windows/desktop/SysInfo/acquiring-high-resolution-time-stamps
	elapsedMicroseconds.QuadPart = (afterHighRes.QuadPart - beforeHighRes.QuadPart);
	elapsedMicroseconds.QuadPart *= 1000000;
	elapsedMicroseconds.QuadPart /= frequency.QuadPart;


	long elapsedTimeQPC = elapsedMicroseconds.QuadPart / 1000 == 0 ? 1 : (elapsedMicroseconds.QuadPart / 1000);

	printf_s("Simple Loop: Millisecond elapsed (QPC)=%lld, Loops=%d\n", (elapsedMicroseconds.QuadPart / 1000), i);
	printf_s("Simple Loop: Spins/Millisecond (QPC)=%ld\n", i / elapsedTimeQPC);

}

int main()
{
	ExerciseSpinLockCode();

	ExerciseSimpleLoopCode();

	printf_s("Press ENTER to end...");
	_gettchar();

	return 0;
}
 

Sample Results 1 (Intel i7 2.7 Ghz):
=======================
SpinToAcquireLockWithExponentialBackoff: Milliseconds elapsed = 5031, Spins=102023750, Backoffs=112
SpinToAcquireLockWithExponentialBackoff: Spins/Millisecond=20279
SpinToAcquireLockWithExponentialBackoff: Spins/Millisecond(QPC)=20315
Simple Loop: Millisecond elapsed (QPC)=4, Loops=10000000
Simple Loop: Spins/Millisecond (QPC)=2500000
Press ENTER to end...
 
Sample Results 2 (AMD A6-6310 APU 1.8 Ghz):
===============================
SpinToAcquireLockWithExponentialBackoff: Milliseconds elapsed = 5016, Spins=187023750, Backoffs=197
SpinToAcquireLockWithExponentialBackoff: Spins/Millisecond=37285
SpinToAcquireLockWithExponentialBackoff: Spins/Millisecond(QPC)=37292
Simple Loop: Millisecond elapsed (QPC)=11, Loops=10000000
Simple Loop: Spins/Millisecond (QPC)=909090
Press ENTER to end...
 
Sample Results 3 (Intel Q6600 2.4 Ghz):
===========================
SpinToAcquireLockWithExponentialBackoff: Milliseconds elapsed = 5015, Spins=916023750, Backoffs=926
SpinToAcquireLockWithExponentialBackoff: Spins/Millisecond=182656
SpinToAcquireLockWithExponentialBackoff: Spins/Millisecond(QPC)=183058
Simple Loop: Millisecond elapsed (QPC)=4, Loops=10000000
Simple Loop: Spins/Millisecond (QPC)=2500000
Press ENTER to end...
 
Sample Results 4 (Intel Xeon E5-2620 v4 2.10 Ghz):
==================================
SpinToAcquireLockWithExponentialBackoff: Milliseconds elapsed = 5016, Spins=81123750, Backoffs=821
SpinToAcquireLockWithExponentialBackoff: Spins/Millisecond=161687
SpinToAcquireLockWithExponentialBackoff: Spins/Millisecond(QPC)=161977
Simple Loop: Millisecond elapsed (QPC)=4, Loops=10000000
Simple Loop: Spins/Millisecond (QPC)=2500000
Press ENTER to end...

Since the above example is designed to perform a little more than a loop with periodic pauses until the lock is acquired, it would drive CPU utilization to 100% on a single CPU.
Based on these values one can conclude that if spinlock statistics show values greater than about 20000  spins per millisecond per CPU, then the CPU usage is likely fairly high and close to 100% capacity.
 
Naturally, CPU clock speeds would impact the results and faster CPUs would be able to perform more spins per millisecond. Other CPU-related specifications like L1, L2, L3 caches may also play somewhat of a role here. However, the goal of this post is to provide guidance, a "ballpark" figure so to speak. Please feel free to report your test results in the Comments section.

For more information on how to collect Spinlock t-shooting information, please see above white paper - Appending B.
Alternatively, below is a spinlock data-collection and analysis script
 
use tempdb
go
declare @cntr int = 0
if exists(select name from tempdb.sys.sysobjects where name like '#_spin_waits%')
drop table #_spin_waits


create table #_spin_waits
(
lock_name varchar(128)
,collisions bigint
,spins bigint
,sleep_time bigint
,backoffs bigint
,snap_time datetime
)

while (@cntr < 4)
begin
	--capture the current stats
	insert into #_spin_waits
	(
		lock_name
		,collisions
		,spins
		,sleep_time
		,backoffs
		,snap_time
	)
	select name
		,collisions
		,spins
		,sleep_time
		,backoffs
		,getdate()
	from sys.dm_os_spinlock_stats

	waitfor delay '00:00:10'
	set @cntr = @cntr + 1

end

--Analysis

declare @cpu int
select @cpu = cpu_count from sys.dm_os_sys_info

--SPINLOCKS compute delta 
select  t2.[lock_name] as spinlock_name,  
cast(cast(t2.spins as float) - cast(t1.spins as float) as bigint) delta_spins,  
cast (cast(t2.Backoffs as float) - cast (t1.Backoffs as float) as bigint) delta_backoff, 
DATEDIFF(MILLISECOND,t1.snap_time,t2.snap_time) delta_minuntes,
cast(cast(t2.spins as float) - cast(t1.spins as float) as bigint)/DATEDIFF(MILLISECOND,t1.snap_time,t2.snap_time)/@cpu delta_spins_per_millisecond_per_CPU

from 
(select row_number () over ( partition by [lock_name]  order by    snap_time) row, *  from [#_spin_waits] 
	where snap_time in 
	(select min(snap_time) from #_spin_waits) ) t1
join 
(select row_number () over ( partition by [lock_name]  order by    snap_time) row, *  from [#_spin_waits]  
	where snap_time in 
	(select max(snap_time) from #_spin_waits) ) t2
on t1.row = t2.row and t1.[lock_name]=t2.[lock_name]
where cast(cast(t2.spins as float) - cast(t1.spins as float) as bigint) > 0
order by delta_spins desc