Home
%3CLINGO-SUB%20id%3D%22lingo-sub-333990%22%20slang%3D%22en-US%22%3ETasks%2C%20Workers%2C%20Threads%2C%20Scheduler%2C%20Sessions%2C%20Connections%2C%20Requests%20%3B%20what%20does%20it%20all%20mean%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-333990%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Dec%2013%2C%202012%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3EWith%20this%20meditation%20I%20attempt%20to%20explain%20what%20some%20of%20the%20more%20common%20concepts%20that%20get%20used%20with%20SQL%20Server%20thread%20management%20and%20scheduling%20are.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CSTRONG%3EParable%3A%20%3C%2FSTRONG%3E%20There%20was%20an%20all-powerful%2C%20but%20humble%20and%20benign%20Master%2C%20whom%20the%20workers%20revered%20and%20humbly%20served.%20The%20master%20accepted%20requests%20from%20other%20kingdoms%20and%20graciously%20agreed%20to%20grant%20all%20of%20them.%20To%20do%20so%20the%20Master%20assigned%20tasks%20to%20his%20workers%20(servants)%20who%20completed%20them%20cooperating%20with%20each%20%E2%80%93%20allowing%20each%20other%20graciously%20to%20approach%20the%20Master%20one%20at%20a%20time.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1479565660%22%20id%3D%22toc-hId-1597830075%22%20id%3D%22toc-hId-1597830075%22%3E%3CSTRONG%3E%20Components%3A%20%3C%2FSTRONG%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%3CSTRONG%3EScheduler%20%3C%2FSTRONG%3E%20(SOS%20Scheduler)%E2%80%93%20the%20object%20that%20manages%20thread%20scheduling%20in%20SQL%20Server%20and%20allows%20threads%20to%20be%20exposed%20to%20the%20CPU%20(described%20in%20%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms177526.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20sys.dm_os_schedulers%20%3C%2FA%3E%20).%20This%20is%20the%20all-powerful%20but%20benign%20and%20graceful%20master%20whom%20everyone%20abides.%26nbsp%3B%20He%20does%20not%20control%20things%20but%20lets%20the%20workers%20work%20with%20each%20other%20and%20relies%20on%20their%20cooperation%20(co-operative%20scheduling%20mode).%20Each%20scheduler%20%2Fmaster%20(one%20per%20logical%20CPU)%20accepts%20new%20tasks%20and%20hands%20them%20off%20to%20workers.%20SOS%20Scheduler%20allows%20one%20worker%20at%20a%20time%20to%20be%20exposed%20to%20the%20CPU.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CSTRONG%3ETask%20%3C%2FSTRONG%3E%20%E2%80%93a%20task%20represents%20the%20work%20that%20needs%20to%20be%20performed%20(%20%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms174963.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20sys.dm_os_tasks%20%3C%2FA%3E%20).%20A%20task%26nbsp%3Bcontains%20one%20of%20the%20following%20events%3A%20%3CEM%3E%20query%20%3C%2FEM%3E(RPC%20event%20or%20Language%20event)%2C%20%3CEM%3E%20a%20prelogin%20%3C%2FEM%3E(prelogin%20event)%2C%26nbsp%3B%20a%20%3CEM%3E%20login%20%3C%2FEM%3E(connect%20event)%2C%20a%20%3CEM%3E%20logout%26nbsp%3B%20%3C%2FEM%3E(disconnect%20event)%2C%20a%20%3CEM%3E%20query%20cancellation%20%3C%2FEM%3E(an%20Attention%20event)%2C%20a%20%3CEM%3E%20bulk%20load%20%3C%2FEM%3E(bulk%20load%20event)%2C%20a%20%3CEM%3E%20distributed%20transaction%20%3C%2FEM%3E(transaction%20manager%20event).%20A%20task%20is%20what%20the%20Master%20is%20about%20%E2%80%93%20it%20is%20what%20defines%20its%20existence.%20Note%20these%20are%20tracked%20at%20the%20SOS%20scheduler%20layer%20(thus%20dm_OS_tasks)%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSTRONG%3E%20Worker%20%3C%2FSTRONG%3E%20(worker%20thread)%20%E2%80%93%20This%20is%20the%20logical%20SQL%20Server%20representation%20of%20a%20thread%20(think%20of%20it%20as%20a%20wrapper%20on%20top%20of%20the%20OS%20thread).%20It%20is%20a%20structure%20within%20the%20Scheduler%20which%20maintains%20SQL%20Server-specific%20information%20about%20what%20a%20worker%20thread%20does.%20%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms178626.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Esys.dm_os_workers%20%3C%2FA%3E%20.%20Workers%20are%20the%20humble%20servants%20who%20carry%20out%20the%20task%20assigned%20to%20them%20by%20the%20Master%20(scheduler).%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CSTRONG%3EThread%20%3C%2FSTRONG%3E%20%E2%80%93%20this%20is%20the%20OS%20thread%20%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms187818.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20sys.dm_os_threads%20%3C%2FA%3E%20that%20is%20created%20via%20calls%20like%20%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms682453(VS.85).aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20CreateThread()%20%3C%2FA%3E%20%2F%20%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fkdzttdcb(v%3DVS.80).aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20_beginthreadex()%20%3C%2FA%3E%20.%20A%20Worker%20is%20mapped%201-to-1%20to%20a%20Thread.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CSTRONG%3ERequest%20%3C%2FSTRONG%3E%20is%20the%20logical%20representation%20of%20a%20%3CEM%3E%20query%20request%20%3C%2FEM%3E%20made%20from%20the%20client%20application%20to%20SQL%20Server%20(%20%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms177648(v%3Dsql.105).aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20sys.dm_exec_requests%20%3C%2FA%3E%20).%20This%20query%20request%20has%20been%20assigned%20to%20a%20task%20that%20the%20scheduler%20hands%20off%20to%20a%20worker%20to%20process.%20This%20represents%20query%20requests%20as%20well%20as%20system%20thread%20operations%20(like%20checkpoint%2C%20log%20writer%2C%20etc)%3B%26nbsp%3Byou%20will%20not%20find%20login%2C%20logouts%2C%20attentions%20and%20the%20like%26nbsp%3Bhere.%26nbsp%3BAlso%2C%20note%20that%20this%20is%20a%20representation%20at%20the%20SQL%20execution%20engine%20level%20(thus%20dm_EXEC_requests)%20not%20at%20the%20SOS%20Scheduler%20layer.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CSTRONG%3ESessions%20%3C%2FSTRONG%3E%20%E2%80%93%20when%20the%20client%20application%20connects%20to%20SQL%20Server%20the%20two%20sides%20establish%20a%20%22session%22%20on%20which%20to%20exchange%20information.%20Strictly%20speaking%20a%20session%20is%20not%20the%20same%20as%20the%20underlying%20physical%20connection%2C%20it%20is%20a%20SQL%20Server%20logical%20representation%20of%20a%20connection.%20But%20for%20practical%20purposes%2C%20you%20can%20think%20of%20this%20as%20being%20a%20connection%20(session%20%3D~%20connection).%20See%20%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms176013(v%3Dsql.105).aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20sys.dm_exec_sessions%20%3C%2FA%3E%20.%20This%20is%20the%20old%20SPID%20that%20existed%20in%20SQL%20Server%202000%20and%20earlier.%20You%20may%20sometimes%20notice%20a%20single%20session%20repeating%20multiple%20times%20in%20a%20DMV%20output.%20This%20happens%20because%20of%20parallel%20queries.%20A%20parallel%20query%20uses%20the%20same%20session%20to%20communicate%20with%20the%20client%2C%20but%20on%20the%20SQL%20Server%20side%20multiple%20worker%20(threads)%20are%20assigned%20to%20service%20this%20request.%20So%20if%20you%20see%20multiple%20rows%20with%20the%20same%20session%20ID%2C%20know%20that%20the%20query%20request%20is%20being%20serviced%20by%20multiple%20threads.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CSTRONG%3EConnections%20%3C%2FSTRONG%3E%20%E2%80%93%20this%20is%20the%20actual%20physical%20connection%20established%20at%20the%20lower%20protocol%20level%20with%20all%20of%20its%20characteristics%20%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms181509.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%20sys.dm_exec_connections%20%3C%2FA%3E%20.%20There%20is%20a%201%3A1%20mapping%20between%20a%20Session%20and%20a%20Connection.%3C%2FBLOCKQUOTE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1072591301%22%20id%3D%22toc-hId--954326886%22%20id%3D%22toc-hId--954326886%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-670219034%22%20id%3D%22toc-hId-788483449%22%20id%3D%22toc-hId-788483449%22%3E%3CSTRONG%3E%20Interconnection%20between%20the%20Components%3A%20%3C%2FSTRONG%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20client%20application%20creates%20a%20physical%20connection%20to%20SQL%20Server.%20Then%20the%20application%20sends%20a%20pre-login%20request%20and%20a%20task%20is%20created%20and%20assigned%20to%20a%20worker%20to%20fulfill.%20Once%20the%20server%20and%20client%20finish%20the%20pre-login%20process%2C%20a%20login%20request%20is%20sent%20and%20another%20task%20is%20formed%20and%20handed%20off%20to%20a%20worker%20thread.%20Once%20the%20login%20is%20completed%2C%20SQL%20Server%20creates%20a%20session%20that%20represents%20this%20logical%20connection%20where%20it%20will%20exchange%20information%20with%20the%20client.%20When%20the%20client%20application%20sends%20a%20query%20request%20(or%20DTC%20or%20bulk%20load)%2C%20the%20server%20again%20creates%20a%20task%20and%20assigns%20it%20to%20a%20worker%20thread%20for%20completion.%20If%20the%20query%20is%20cancelled%20in%20the%20middle%20of%20execution%2C%20for%20some%20reason%2C%20the%20server%20will%20receive%20an%20Attention%20request%20upon%20which%20the%20IOCP%20listener%20will%20mark%20a%20bit%20that%20the%20query%20is%20cancelled%20and%20the%20worker%20that%20was%20running%20the%20query%20would%20stop%20executing%20when%20it%20sees%20the%20bit.%20If%20the%20query%20is%20allowed%20to%20complete%2C%20on%20the%20other%20hand%2C%20and%20the%20client%20application%20is%20done%2C%20it%20can%20send%20a%20disconnect%20or%20logout%20request%20which%20again%20is%20packaged%20as%20a%20task%20and%20serviced%20by%20a%20worker.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENamaste!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EJoseph%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-333990%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20MSDN%20on%20Dec%2013%2C%202012%20With%20this%20meditation%20I%20attempt%20to%20explain%20what%20some%20of%20the%20more%20common%20concepts%20that%20get%20used%20with%20SQL%20Server%20thread%20management%20and%20scheduling%20are.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-333990%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPerformance%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESQL%20OS%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-776142%22%20slang%3D%22en-US%22%3ERe%3A%20Tasks%2C%20Workers%2C%20Threads%2C%20Scheduler%2C%20Sessions%2C%20Connections%2C%20Requests%20%3B%20what%20does%20it%20all%20mean%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-776142%22%20slang%3D%22en-US%22%3EFew%20questions%20-%201.%20DMV%20data%20for%20Connection%20%26amp%3B%20Session%20appear%20same%20since%20those%20are%201%3A1%20mapped%2C%20unless%20uniquely%20used%20in%20Troubleshooting%2FOptimisation%20Process.%20Can%20you%20share%20any%20instance%20wherein%20we%20could%20use%20these%202%20in%20unison%20or%20separately%3F%202.%20In%20which%20part%20of%20Troubleshooting%2FOptimisation%20Process%20are%20these%20DMVs%20used.%20Any%20lead%20is%20appreciated%20and%20would%20set%20a%20starting%20point%20for%20further%20understanding.%20Thank%20You!%20--In%20'thoughts'...%3C%2FLINGO-BODY%3E
Microsoft

First published on MSDN on Dec 13, 2012
With this meditation I attempt to explain what some of the more common concepts that get used with SQL Server thread management and scheduling are.

Parable: There was an all-powerful, but humble and benign Master, whom the workers revered and humbly served. The master accepted requests from other kingdoms and graciously agreed to grant all of them. To do so the Master assigned tasks to his workers (servants) who completed them cooperating with each – allowing each other graciously to approach the Master one at a time.


Components:

 

Scheduler (SOS Scheduler)– the object that manages thread scheduling in SQL Server and allows threads to be exposed to the CPU (described in sys.dm_os_schedulers ). This is the all-powerful but benign and graceful master whom everyone abides.  He does not control things but lets the workers work with each other and relies on their cooperation (co-operative scheduling mode). Each scheduler /master (one per logical CPU) accepts new tasks and hands them off to workers. SOS Scheduler allows one worker at a time to be exposed to the CPU.

Task –a task represents the work that needs to be performed ( sys.dm_os_tasks ). A task contains one of the following events: query (RPC event or Language event), a prelogin (prelogin event),  a login (connect event), a logout  (disconnect event), a query cancellation (an Attention event), a bulk load (bulk load event), a distributed transaction (transaction manager event). A task is what the Master is about – it is what defines its existence. Note these are tracked at the SOS scheduler layer (thus dm_OS_tasks)

Worker (worker thread) – This is the logical SQL Server representation of a thread (think of it as a wrapper on top of the OS thread). It is a structure within the Scheduler which maintains SQL Server-specific information about what a worker thread does. sys.dm_os_workers . Workers are the humble servants who carry out the task assigned to them by the Master (scheduler).

Thread – this is the OS thread sys.dm_os_threads that is created via calls like CreateThread() / _beginthreadex() . A Worker is mapped 1-to-1 to a Thread.

Request is the logical representation of a query request made from the client application to SQL Server ( sys.dm_exec_requests ). This query request has been assigned to a task that the scheduler hands off to a worker to process. This represents query requests as well as system thread operations (like checkpoint, log writer, etc); you will not find login, logouts, attentions and the like here. Also, note that this is a representation at the SQL execution engine level (thus dm_EXEC_requests) not at the SOS Scheduler layer.

Sessions – when the client application connects to SQL Server the two sides establish a "session" on which to exchange information. Strictly speaking a session is not the same as the underlying physical connection, it is a SQL Server logical representation of a connection. But for practical purposes, you can think of this as being a connection (session =~ connection). See sys.dm_exec_sessions . This is the old SPID that existed in SQL Server 2000 and earlier. You may sometimes notice a single session repeating multiple times in a DMV output. This happens because of parallel queries. A parallel query uses the same session to communicate with the client, but on the SQL Server side multiple worker (threads) are assigned to service this request. So if you see multiple rows with the same session ID, know that the query request is being serviced by multiple threads.

Connections – this is the actual physical connection established at the lower protocol level with all of its characteristics sys.dm_exec_connections . There is a 1:1 mapping between a Session and a Connection.

 

 

 

Interconnection between the Components:

 

A client application creates a physical connection to SQL Server. Then the application sends a pre-login request and a task is created and assigned to a worker to fulfill. Once the server and client finish the pre-login process, a login request is sent and another task is formed and handed off to a worker thread. Once the login is completed, SQL Server creates a session that represents this logical connection where it will exchange information with the client. When the client application sends a query request (or DTC or bulk load), the server again creates a task and assigns it to a worker thread for completion. If the query is cancelled in the middle of execution, for some reason, the server will receive an Attention request upon which the IOCP listener will mark a bit that the query is cancelled and the worker that was running the query would stop executing when it sees the bit. If the query is allowed to complete, on the other hand, and the client application is done, it can send a disconnect or logout request which again is packaged as a task and serviced by a worker.

 

Namaste!

 

Joseph

1 Comment
Occasional Visitor
Few questions - 1. DMV data for Connection & Session appear same since those are 1:1 mapped, unless uniquely used in Troubleshooting/Optimisation Process. Can you share any instance wherein we could use these 2 in unison or separately? 2. In which part of Troubleshooting/Optimisation Process are these DMVs used. Any lead is appreciated and would set a starting point for further understanding. Thank You! --In 'thoughts'...