Upload
insight-technology-inc
View
962
Download
7
Embed Size (px)
Citation preview
SQL Serverのスレッド管理
1
©2012 Microsoft Corporation. All Rights Reserved.
Agenda
SQL Server スレッド スケジューリングの歴史 (5分) SQL Serverのスレッド スケジューリング(30分) SQL Serverのスレッド モード (5分) 質疑応答 (5分)
2
SQL Server スケジューリングの歴史
3
©2012 Microsoft Corporation. All Rights Reserved.
SQL Server Schedulingの歴史
SQL Server 6.x Preemptive Thread SQL Server 7/2000 Non-Preemptive Thread (UMS) SQL Server 2005/2008/2008R2/2012 Non-Preemptive Thread (SQLOS)
4
©2012 Microsoft Corporation. All Rights Reserved.
Why non-preemptive scheduling
“RDBMSエンジンに関する調査研究によると、パフォーマンスとスケーラビリティの要件をみたすためには、non-preemptiveスケジューリングの効用が必要だということがわかった。” “As adequate research shows for RDBMS engine to meet performance and scalability requirements it needs to leverage non-preemptive scheduling.” From http://blogs.msdn.com/b/slavao/archive/2005/02/05/367816.aspx
5
SQL Serverのスレッド スケジューリング
6
©2012 Microsoft Corporation. All Rights Reserved.
PreemptiveとNon-Preemptive
• Preemptive (Windows) システム (OS) が、CPUを使用するスレッドを決定する。CPU使用権の横取り。 • Non-Preemptive (SQL Server) ユーザープログラム(スレッド自身)が、 CPUを使用するスレッドを決定する。
7
©2012 Microsoft Corporation. All Rights Reserved.
Non-Preemptive実現性
システムの観点から見て、完全なNon-Preemptiveスケジューリングを実装することはできない ただし、プロセスという閉じた観点であれば、Non-Preemptiveスケジューリングは実装可能
8
©2012 Microsoft Corporation. All Rights Reserved.
Windowsのスケジューリング
CPUを使用するスレッドが切り替わるタイミング(コンテキストスイッチ) • クォンタムの終了
実行中のスレッドが割り当てられたクォンタムを使い切った(CPU時間を使い切った)
• 自発的な待機 イベント、セマフォなどの同期オブジェクト上で待機が発生
• プリエンプション より高い優先度のスレッドによる横取り
9
©2012 Microsoft Corporation. All Rights Reserved.
SQL Serverのスケジューリング
CPUを使用するスレッドが切り替わるタイミング(コンテキストスイッチ)
• SQLOS Yield APIの呼び出し スレッドが任意のタイミングで自らCPUの使用権を放棄する
• 自発的な待機 同期オブジェクト上での待機が発生 (ロック、ラッチ待ちなど)
10
©2012 Microsoft Corporation. All Rights Reserved.
スレッドを管理しているコンポーネント
• SOS Scheduler – Yield APIや、同期オブジェクトで待機する場合は最終的に、SOS
SchedulerクラスのAPIを呼び出して、スレッドを操作している • スケジューラーごとにワーカープールを持っている • DMV sys.dm_os_shceduler
©2012 Microsoft Corporation. All Rights Reserved.
スレッドの状態
• Running状態 SQLOS上でCPUを使用しているスレッド
• Runnable状態 SQLOS上でCPUの使用権を待機しているスレッド
• Suspended (Waiting) 状態 同期オブジェクト上で待機しているスレッド
12
©2012 Microsoft Corporation. All Rights Reserved.
スレッドの状態遷移
Runnable Suspended (Waiting)
Running CPU使用権放棄
CPU使用権譲受
同期オブジェクト取得
同期オブジェクト待機
同期オブジェクト取得
©2012 Microsoft Corporation. All Rights Reserved.
スレッド状態確認
• DMV sys.dm_os_workers – state カラム
©2012 Microsoft Corporation. All Rights Reserved.
スレッド状態確認
• DMV sys.dm_os_workers – state カラム
©2012 Microsoft Corporation. All Rights Reserved.
SQLスレッドのスケジューリング
• SQLのスレッドは、他のスレッドからCPU使用権を譲りうけることで初めて動作できる – Cooperative Scheduling
T1
T2
T 2がT1にCPU使用権を譲渡
T1 がT2にCPU使用権を譲渡
Running State Runnable State
コンテキストスイッチ
コンテキストスイッチ
コンテキストスイッチ
©2012 Microsoft Corporation. All Rights Reserved.
CPUの使用権譲渡とは
イベントオブジェクト(信号)をシグナル状態(青)に変えてあげること
T1
T2
T1 は、T2が待機しているイベントオブジェクトをシグナル化する
T1 は誰かが自分のイベントオブジェクトをシグナル化してくれるのを待機する
Running State Runnable State
Signaled State Event
Not Signaled State Event
©2012 Microsoft Corporation. All Rights Reserved.
コンテキストスイッチ時の重要な処理
• I/Oキューのチェック – 完了しているI/Oのチェックと、I/O完了ルーチンの呼び出し
T1
… Yield API コール
… I/O キューのチェック
… I/O 要求ごとの完了ルーチンを呼び出す
… Tnのイベントオブジェクトシグナル化
Call Stack ntdll!ZwSignalAndWaitForSingleObject+0xa kernel32!SignalObjectAndWait+0x110 sqldk!SystemThread::SignalAndWait+0x2a sqldk!SOS_Scheduler::Switch+0xb5 sqldk!SOS_Scheduler::SuspendNonPreemptive+0xd2 …. コンテ
キストスイッチ処理
©2012 Microsoft Corporation. All Rights Reserved.
I/O完了ルーチン:具体例 I/Oラッチ I/Oラッチ:物理I/O中のメモリバッファにかける排他のしくみ
T1
③ ReadFile/WriteFile API 非同期コール
⑤ Yield API コール
⑧ Tnのイベントオブジェクトシグナル化
① I/Oラッチをバッファにかける
⑥ I/O キューのチェック
I/O要求発行
I/O完了
⑦ I/O完了ルーチンの呼び出し (I/Oラッチの解放)
② I/O完了ルーチンを設定 (ラッチ解放処理)
コンテキストスイッチ処理
④ I/O キューに発行したI/O要求を追加
©2012 Microsoft Corporation. All Rights Reserved.
I/O完了ルーチン:コールスタック例
0:073> k Child-SP RetAddr Call Site 00000000`0a8fcc00 000007fe`d7f21e10 sqlmin!FCB::PostRead+0x14 00000000`0a8fcc50 000007fe`d7f2163c sqlmin!BPool::ReadPageCompletion+0xe7 00000000`0a8fccc0 000007fe`db8709a8 sqlmin!FCB::IoCompletion+0x97 (Inline Function) --------`-------- sqldk!SOS_IOCompRequest::ExecuteCompRoutine+0x6 00000000`0a8fcd10 000007fe`db83222f sqldk!IOQueue::CheckForIOCompletion+0x18e 00000000`0a8fce10 000007fe`db83c169 sqldk!SOS_Scheduler::SwitchContext+0x67 00000000`0a8fd0b0 000007fe`db845ebc sqldk!SOS_Scheduler::SuspendNonPreemptive+0xd2 (Inline Function) --------`-------- sqldk!SOS_Scheduler::Suspend+0x20 00000000`0a8fd0f0 000007fe`db850ddf sqldk!SOS_Task::Sleep+0x1a8 00000000`0a8fd150 000007fe`d7f2258b sqldk!Worker::OSYieldNoAbort+0x2f (Inline Function) --------`-------- sqlmin!SOS_Task::OSYieldNoAbort+0x263 00000000`0a8fd180 000007fe`d7f07925 sqlmin!BPool::GetFromDisk+0x81b ...
©2012 Microsoft Corporation. All Rights Reserved.
その他のIO完了ルーチン
[代表例] • ページ整合性検証
– チェックサム (既定) • Set the PAGE_VERIFY Database Option to CHECKSUM
http://msdn.microsoft.com/en-us/library/bb402873.aspx
– Stale Read Detection (Trace Flag 818) • Additional SQL Server diagnostics added to detect unreported I/O problems
http://support.microsoft.com/kb/826433
©2012 Microsoft Corporation. All Rights Reserved.
I/Oキュー
• I/Oキューの数 – sys.dm_os_schedulers の pending_disk_io_count
• I/O要求の詳細 – sys.dm_io_pending_io_requests
©2012 Microsoft Corporation. All Rights Reserved.
IO完了ルーチン処理中の確認
• DMV sys.dm_os_workers – is_in_polling_io_completion_routine カラム
SQL Serverのスレッド モード
24
©2012 Microsoft Corporation. All Rights Reserved.
ノンプリエンプティブスレッドの問題点
問題例 • 処理時間の長い Windows API 呼び出し • ユーザーが作成した拡張ストアドプロシージャの呼び出し
T1
… CreateFile API 呼び出し/ 完了待ち
I/O要求発行
CreateFile では内部でイ
ベントオブジェクトを使用し、自発的に待機状態となることがある
©2012 Microsoft Corporation. All Rights Reserved.
解決策:プリエンプティブスレッド
スレッドを管理できなくなるタイミングで、プリエンプティブモードにスイッチする
T1
… CreateFile API 呼び出し/ 完了待ち
I/O要求発行
T2
CPU使用権をT2に譲渡した後、SOSで管理されないスレッドモードにスイッチ
©2012 Microsoft Corporation. All Rights Reserved.
スレッドモードの確認
• DMV sys.dm_os_workers – Is_preemptive カラム
©2012 Microsoft Corporation. All Rights Reserved.
プリエンプティブモードで動くスレッド
• ヘルスチェック スレッド – sp_server_diagnosticsスレッド – Lease HandShake スレッド (Availability Group) – Scheduler Monitorスレッド
など
©2012 Microsoft Corporation. All Rights Reserved.
まとめ
• SQL Server はNon-Preemptiveなスレッドスケジューリングを実装している
• Non-Preemptiveなスレッドは、自身でCPU時間を管理する
• SOSのコンテキストスイッチ時にはI/Oの完了がチェックさ
れる • I/O完了時には、I/O完了ルーチンが実行される
• SQL Serverのスレッドは必要に応じてプリエンプティブ
モードにスイッチする
©2012 Microsoft Corporation. All Rights Reserved.
参考
SQL Serverのスレッドスケジューリング (DB Online) http://enterprisezine.jp/dbonline/detail/4157 http://enterprisezine.jp/dbonline/detail/4168 SQLOS - unleashed http://blogs.msdn.com/b/slavao/archive/2005/02/05/367816.aspx SQL Server 2008 SQLOS http://blogs.technet.com/b/josebda/archive/2009/03/30/sql-server-2008-sqlos.aspx Q&A on Latches in the SQL Server Engine…. http://blogs.msdn.com/b/psssql/archive/2009/07/08/q-a-on-latches-in-the-sql-server-engine.aspx
©2012 Microsoft Corporation. All Rights Reserved. 31
質疑応答
© 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market
conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
© 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market
conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. ©2012 Microsoft Corporation. All Rights
Reserved. 32