{"id":4649,"date":"2016-10-04T04:16:36","date_gmt":"2016-10-04T11:16:36","guid":{"rendered":"http:\/\/3.209.169.194\/blogs\/paul\/?p=4649"},"modified":"2016-10-04T04:16:36","modified_gmt":"2016-10-04T11:16:36","slug":"capturing-spinlock-statistics-for-a-period-of-time","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/","title":{"rendered":"Capturing spinlock statistics for a period of time"},"content":{"rendered":"<p>This is a quick post to blog a script that allows spinlock statistics to be captured for a defined period of time (as I need to reference it in my next post). Enjoy!<\/p>\n<pre class=\"brush: sql; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nIF EXISTS (SELECT * FROM &#x5B;tempdb].&#x5B;sys].&#x5B;objects]\r\n\tWHERE &#x5B;name] = N'##TempSpinlockStats1')\r\n\tDROP TABLE &#x5B;##TempSpinlockStats1];\r\n\r\nIF EXISTS (SELECT * FROM &#x5B;tempdb].&#x5B;sys].&#x5B;objects]\r\n\tWHERE &#x5B;name] = N'##TempSpinlockStats2')\r\n\tDROP TABLE &#x5B;##TempSpinlockStats2];\r\nGO\r\n\r\n-- Baseline\r\nSELECT * INTO &#x5B;##TempSpinlockStats1]\r\nFROM sys.dm_os_spinlock_stats\r\nWHERE &#x5B;collisions] &gt; 0\r\nORDER BY &#x5B;name];\r\nGO\r\n\r\n-- Now wait...\r\nWAITFOR DELAY '00:00:05';\r\nGO\r\n\r\n-- Capture updated stats\r\nSELECT * INTO &#x5B;##TempSpinlockStats2]\r\nFROM sys.dm_os_spinlock_stats\r\nWHERE &#x5B;collisions] &gt; 0\r\nORDER BY &#x5B;name];\r\nGO\r\n\r\n-- Diff them\r\nSELECT\r\n\t\t'***' AS &#x5B;New],\r\n\t\t&#x5B;ts2].&#x5B;name] AS &#x5B;Spinlock],\r\n\t\t&#x5B;ts2].&#x5B;collisions] AS &#x5B;DiffCollisions],\r\n\t\t&#x5B;ts2].&#x5B;spins] AS &#x5B;DiffSpins],\r\n\t\t&#x5B;ts2].&#x5B;spins_per_collision] AS &#x5B;SpinsPerCollision],\r\n\t\t&#x5B;ts2].&#x5B;sleep_time] AS &#x5B;DiffSleepTime],\r\n\t\t&#x5B;ts2].&#x5B;backoffs] AS &#x5B;DiffBackoffs]\r\n\tFROM &#x5B;##TempSpinlockStats2] &#x5B;ts2]\r\n\tLEFT OUTER JOIN &#x5B;##TempSpinlockStats1] &#x5B;ts1]\r\n\t\tON &#x5B;ts2].&#x5B;name] = &#x5B;ts1].&#x5B;name]\r\n\tWHERE &#x5B;ts1].&#x5B;name] IS NULL\r\nUNION\r\nSELECT\r\n\t\t'' AS &#x5B;New],\r\n\t\t&#x5B;ts2].&#x5B;name] AS &#x5B;Spinlock],\r\n\t\t&#x5B;ts2].&#x5B;collisions] - &#x5B;ts1].&#x5B;collisions] AS &#x5B;DiffCollisions],\r\n\t\t&#x5B;ts2].&#x5B;spins] - &#x5B;ts1].&#x5B;spins] AS &#x5B;DiffSpins],\r\n\t\tCASE (&#x5B;ts2].&#x5B;spins] - &#x5B;ts1].&#x5B;spins]) WHEN 0 THEN 0\r\n\t\t\tELSE (&#x5B;ts2].&#x5B;spins] - &#x5B;ts1].&#x5B;spins]) \/\r\n\t\t\t\t(&#x5B;ts2].&#x5B;collisions] - &#x5B;ts1].&#x5B;collisions]) END\r\n\t\t\t\tAS &#x5B;SpinsPerCollision],\r\n\t\t&#x5B;ts2].&#x5B;sleep_time] - &#x5B;ts1].&#x5B;sleep_time] AS &#x5B;DiffSleepTime],\r\n\t\t&#x5B;ts2].&#x5B;backoffs] - &#x5B;ts1].&#x5B;backoffs] AS &#x5B;DiffBackoffs]\r\n\tFROM &#x5B;##TempSpinlockStats2] &#x5B;ts2]\r\n\tLEFT OUTER JOIN &#x5B;##TempSpinlockStats1] &#x5B;ts1]\r\n\t\tON &#x5B;ts2].&#x5B;name] = &#x5B;ts1].&#x5B;name]\r\n\tWHERE &#x5B;ts1].&#x5B;name] IS NOT NULL\r\n\tAND &#x5B;ts2].&#x5B;collisions] - &#x5B;ts1].&#x5B;collisions] &gt; 0\r\nORDER BY &#x5B;New] DESC, &#x5B;Spinlock] ASC;\r\nGO\r\n<\/pre>\n<p>Example output (trimmed to fit here):<\/p>\n<pre class=\"brush: plain; gutter: false; title: ; toolbar: true; wrap-lines: true; notranslate\" title=\"\">\r\nNew  Spinlock                DiffCollisions   DiffSpins   SpinsPerCollision DiffSleepTime   DiffBackoffs\r\n---- ----------------------  ---------------- ----------- ----------------- --------------- ------------\r\n     ALLOC_CACHES_HASH       999              257750      258               0               5\r\n     BLOCKER_ENUM            103              27250       264               0               2\r\n     CMED_HASH_SET           286              71500       250               0               0\r\n     COMPPLAN_SKELETON       148              37000       250               0               0\r\n     DBTABLE                 14               3500        250               0               0\r\n     FGCB_PRP_FILL           4                1000        250               0               0\r\n     FREE_SPACE_CACHE_ENTRY  983              255250      259               0               7\r\n     LOGCACHE_ACCESS         3353             314628      93                0               2241\r\n     LOGFLUSHQ               797              206250      258               0               10\r\n     OPT_IDX_STATS           147              36750       250               0               0\r\n     RESQUEUE                23               5750        250               0               0\r\n     SECURITY_CACHE          106              26500       250               0               0\r\n     SOS_CACHESTORE          235              60750       258               0               3\r\n     SOS_OBJECT_STORE        55               13750       250               0               0\r\n     SOS_SCHEDULER           219              54750       250               0               0\r\n     SOS_SUSPEND_QUEUE       72               18000       250               0               0\r\n     SOS_TASK                69               18750       271               0               2\r\n     SQL_MGR                 394              98500       250               0               0\r\n     XDES                    52               13000       250               0               0\r\n     XDESMGR                 840              341500      406               0               16\r\n     XTS_MGR                 165              42750       259               0               2\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a quick post to blog a script that allows spinlock statistics to be captured for a defined period of time (as I need to reference it in my next post). Enjoy! IF EXISTS (SELECT * FROM &#x5B;tempdb].&#x5B;sys].&#x5B;objects] WHERE &#x5B;name] = N&#8217;##TempSpinlockStats1&#8242;) DROP TABLE &#x5B;##TempSpinlockStats1]; IF EXISTS (SELECT * FROM &#x5B;tempdb].&#x5B;sys].&#x5B;objects] WHERE &#x5B;name] = [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66,84,101],"tags":[],"class_list":["post-4649","post","type-post","status-publish","format-standard","hentry","category-performance-tuning","category-spinlocks","category-wait-stats"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Capturing spinlock statistics for a period of time - Paul S. Randal<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Capturing spinlock statistics for a period of time - Paul S. Randal\" \/>\n<meta property=\"og:description\" content=\"This is a quick post to blog a script that allows spinlock statistics to be captured for a defined period of time (as I need to reference it in my next post). Enjoy! IF EXISTS (SELECT * FROM &#x5B;tempdb].&#x5B;sys].&#x5B;objects] WHERE &#x5B;name] = N&#039;##TempSpinlockStats1&#039;) DROP TABLE &#x5B;##TempSpinlockStats1]; IF EXISTS (SELECT * FROM &#x5B;tempdb].&#x5B;sys].&#x5B;objects] WHERE &#x5B;name] = [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2016-10-04T11:16:36+00:00\" \/>\n<meta name=\"author\" content=\"Paul Randal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Paul Randal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/\",\"name\":\"Capturing spinlock statistics for a period of time - Paul S. Randal\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2016-10-04T11:16:36+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Capturing spinlock statistics for a period of time\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\",\"name\":\"Paul S. Randal\",\"description\":\"In Recovery...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\",\"name\":\"Paul Randal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"caption\":\"Paul Randal\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/paul\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Capturing spinlock statistics for a period of time - Paul S. Randal","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/","og_locale":"en_US","og_type":"article","og_title":"Capturing spinlock statistics for a period of time - Paul S. Randal","og_description":"This is a quick post to blog a script that allows spinlock statistics to be captured for a defined period of time (as I need to reference it in my next post). Enjoy! IF EXISTS (SELECT * FROM &#x5B;tempdb].&#x5B;sys].&#x5B;objects] WHERE &#x5B;name] = N'##TempSpinlockStats1') DROP TABLE &#x5B;##TempSpinlockStats1]; IF EXISTS (SELECT * FROM &#x5B;tempdb].&#x5B;sys].&#x5B;objects] WHERE &#x5B;name] = [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/","og_site_name":"Paul S. Randal","article_published_time":"2016-10-04T11:16:36+00:00","author":"Paul Randal","twitter_misc":{"Written by":"Paul Randal","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/","name":"Capturing spinlock statistics for a period of time - Paul S. Randal","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2016-10-04T11:16:36+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/capturing-spinlock-statistics-for-a-period-of-time\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/paul\/"},{"@type":"ListItem","position":2,"name":"Capturing spinlock statistics for a period of time"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/","name":"Paul S. Randal","description":"In Recovery...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce","name":"Paul Randal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","caption":"Paul Randal"},"sameAs":["http:\/\/3.209.169.194\/blogs\/paul"],"url":"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/4649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/comments?post=4649"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/4649\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=4649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=4649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=4649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}