So far, this article has abstracted the
concept of the SQLOS to make the flow of components through the
architecture easier to understand without going off on too many
tangents. However, the SQLOS is core to SQL Server’s architecture so you
need to understand why it exists and what it does to complete your view
of how SQL Server works.
In short, the SQLOS is a thin user-mode layer that
sits between SQL Server and Windows. It is used for low-level
operations such as scheduling, I/O completion, memory management, and
resource management. To explore exactly what this means and why it’s
needed, you first need to understand SQL Server’s execution model.
Execution Model
When an application authenticates to SQL Server it establishes a connection in the context of a session, which is identified by a session_id (in older versions of SQL Server this was called a SPID). You can view a list of all authenticated sessions by querying the sys.dm_exec_sessions DMV.
When an execution request is made within a session, SQL Server divides the work into one or more tasks and then associates a worker thread to each task for its duration. Each thread can be in one of three states (that you need to care about):
- Running — A processor can only execute one thing at a time and the thread currently executing on a processor will have a state of running.
- Suspended — SQL Server has a co-operative scheduler (see below) so running threads will yield the processor and become suspended while they wait for a resource. This is what we call a wait in SQL Server.
- Runnable — When a thread has finished waiting, it becomes runnable which means that it’s ready to execute again. This is known as a signal wait.
If no worker threads are available and max worker threads has not been reached, then SQL Server will allocate a new worker thread. If the max worker threads count has been reached, then the task will wait with a wait type of THREADPOOL until a thread becomes available. Waits and wait types are covered later in this section.
The default max workers count is based on the CPU
architecture and the number of logical processors. The formulas for this
are as follows:
For a 32-bit operating system:
- Total available logical CPUs <= 4
- Total available logical CPUs > 4
- Max Worker Threads = 256 + ((logical CPUs - 4)*8)
For a 64-bit operating system:
- Total available logical CPUs <= 4
- Total available logical CPUs > 4
- Max Worker Threads = 512 + ((logical CPUs - 4)*16)
As an example, a 64-bit SQL Server with 16 processors would have a Max Worker Threads setting of 512 + ((16–4)*16) = 704.
You can also see the max workers count on a running system by executing the following:
SELECT max_workers_count
FROM sys.dm_os_sys_info
INCREASING THE MAX WORKER THREADS SETTING
Running out of worker threads (THREADPOOL
wait type) is often a symptom of large numbers of concurrent parallel
execution plans (since one thread is used per processor), or it can even
indicate that you’ve reached the performance capacity of the server and
need to buy one with more processors. Either way, you’re usually better
off trying to solve the underlying problem rather than overriding the
default Max Worker Threads setting.
Each worker thread requires 2MB of RAM on
a 64-bit server and 0.5MB on a 32-bit server, so SQL Server creates
threads only as it needs them, rather than all at once.
The sys.dm_os_workers
DMV contains one row for every worker thread, so you can see how many
threads SQL Server currently has by executing the following:
SELECT count(*) FROM sys.dm_os_workers
Schedulers
Each thread has an associated scheduler,
which has the function of scheduling time for each of its threads on a
processor. The number of schedulers available to SQL Server equals the
number of logical processors that SQL Server can use plus an extra one
for the dedicated administrator connection (DAC).
You can view information about SQL Server’s schedulers by querying the sys.dm_os_schedulers DMV.
Figure 1 illustrates the relationship between sessions, tasks, threads, and schedulers.
Windows is a general-purpose OS and is not
optimized for server-based applications, SQL Server in particular.
Instead, the goal of the Windows development team is to ensure that all
applications, written by a wide variety of developers inside and outside
Microsoft, will work correctly and have good performance. Because
Windows needs to work well in a broad range of scenarios, the
development team is not going to do anything special that would only be
used in less than 1% of applications.
For example, the scheduling in Windows is very
basic to ensure that it’s suitable for a common cause. Optimizing the
way that threads are chosen for execution is always going to be limited
because of this broad performance goal; but if an application does its
own scheduling then there is more intelligence about what to choose
next, such as assigning some threads a higher priority or deciding that
choosing one thread for execution will avoid other threads being blocked
later.
The basic scheduler in Windows is known as a pre-emptive scheduler and it assigns slices of time known as quantums
to each task to be executed. The advantage of this is that application
developers don’t have to worry about scheduling when creating
applications; the downside is that execution can be interrupted at any
point as Windows balances execution requests from multiple processes.
All versions of SQL Server up to and including
version 6.5 used the Windows scheduler to take advantage of the work
that the Windows team had done through a long history of optimizing
processor usage. There came a point, however, when SQL Server 6.5 could
not scale any further and it was limited by the general-purpose
optimizations of the pre-emptive scheduler in Windows.
For SQL Server 7.0, Microsoft decided that SQL
Server should handle its own scheduling, and created the User Mode
Scheduler (UMS) to do just that. The UMS was designed as a co-operative
scheduling model whereby threads aren’t forcibly interrupted during
execution but instead voluntarily yield the processor when they need to
wait for another resource. When a thread yields the processor, a wait type is assigned to the task to help describe the wait and aid you in troubleshooting performance issues.
The SQLOS
Prior to SQLOS (which was first
implemented in SQL Server 2005), low-level operations such as
scheduling, I/O completion, memory management, and resource management
were all handled by different teams, which resulted in a lot of
duplication of effort as the product evolved.
The idea for SQLOS was to consolidate all these
efforts of the different internal SQL Server development teams to
provide performance improvements on Windows, putting them in a single
place with a single team that can continue to optimize these low-level
functions. This enables the other teams to concentrate on challenges
more specific to their own domain within SQL Server.
Another benefit to having everything in one place
is that you now get better visibility of what’s happening at that level
than was possible prior to SQLOS. You can access all this information
through dynamic management views (DMVs). Any DMV that starts with sys.dm_os_ provides an insight into the workings of SQLOS, such as the following:
- sys.dm_os_schedulers — Returns one
row per scheduler (remember, there is one user scheduler per logical
processor) and displays information about scheduler load and health.
- sys.dm_os_waiting_tasks — Returns one row for every executing task that is currently waiting for a resource, as well as the wait type
- sys.dm_os_memory_clerks — Memory
clerks are used by SQL Server to allocate memory. Significant components
within SQL Server have their own memory clerk. This DMV shows all the
memory clerks and how much memory each one is using.
Relating SQLOS back to the architecture
diagrams shown earlier, many of the components make calls to the SQLOS
in order to fulfill low-level functions required to support their roles.
Just to be clear, the SQLOS doesn’t replace
Windows. Ultimately, everything ends up using the documented Windows
system services; SQL Server just uses them in a way optimized for its
own specific scenarios.
NOTE
SQLOS is not
a way to port the SQL Server architecture to other platforms like Linux
or Mac OS, so it’s not an OS abstraction layer. It doesn’t wrap all the
OS APIs like other frameworks such as .NET, which is why it’s referred
to as a “thin” user-mode layer. Only the things that SQL Server really
needs have been put into SQLOS.
DEFINING DMVS
Dynamic management views (DMVs)
enable much greater visibility into the workings of SQL Server than any
version prior to SQL Server 2005. They are basically just views on top
of the system tables or in-memory system counters, but the concept
enables Microsoft to provide a massive amount of useful information
through them.
The standard syntax starts with sys.dm_,
which indicates that it’s a DMV (there are also dynamic management
functions, but DMV is still the collective term in popular use),
followed by the area about which the DMV provides information — for
example, sys.dm_os_ for SQLOS, sys.dm_db_ for database, and sys.dm_exec_ for query execution.