Thread 組員:王傳陞、林威丞 黃品舜、李盈賢. Outline Examining Thread Activity...

Preview:

Citation preview

Thread

組員:王傳陞、林威丞 黃品舜、李盈賢

Outline Examining Thread Activity

Thread-related tools : Process Explorer Why a process is hungViewing Ready Threads

Thread SchedulingProcessor affinity Quantum thread dispatching Priority LevelsWindows Scheduling APIs

Outline Examining Thread Activity

Thread-related tools : Process Explorer Why a process is hungViewing Ready Threads

Thread SchedulingProcessor affinity Quantum thread dispatching Priority LevelsWindows Scheduling APIs

Thread-related tools : Process Explorer

Thread-related tools : Process Explorer(con

t.)

Thread-related tools : Process Explorer(con

t.) Download Process Explorer

http://www.sysinternals.com Context switch delta : The number of times that thread began ru

nning between the refreshes Process Explorer

Thread activityContext switch deltaCPU percentage

Thread-related tools : Process Explorer(con

t.) Thread start address : Displayed in the fo

rm “module!function” address ,where module is the name of the .exe or .dll.

Note : Windows threads start at a common process or thread startup wrapper function

Problem : most threads would appear to have started at same address

Thread-related tools : Process Explorer(con

t.)

Thread-related tools : Process Explorer(con

t.)

Outline Examining Thread Activity

Thread-related tools : Process Explorer Why a process is hungViewing Ready Threads

Thread SchedulingProcessor affinity Quantum thread dispatching Priority LevelsWindows Scheduling APIs

Why a process is hung Use Process Explorer viewing the thread s

tack help us determine why a process is hung.

Example :Show that PowerPoint of thread stack.

Why a process is hung (cont.)

Why a process is hung (cont.)

Why a process is hung (cont.)

Outline Examining Thread Activity

Thread-related tools : Process Explorer Why a process is hungViewing Ready Threads

Thread SchedulingProcessor affinity Quantum thread dispatching Priority LevelsWindows Scheduling APIs

Viewing Ready Threads In the WinDbg

kd> !ready 1

Outline Examining Thread Activity

Thread-related tools : Process Explorer Why a process is hungViewing Ready Threads

Thread SchedulingProcessor affinity Quantum thread dispatching Priority LevelsWindows Scheduling APIs

Processor affinity

the highest-priority runnable (ready) thread always runs, with the caveat that the thread chosen to run might be limited by the processors, a phenomenon called processor affinity.

Processor affinity [wiki] (cont.)

Example :Involves two processor-intensive tasks (A & B) having affinity to one processor while another processor lies unused.

Outline Examining Thread Activity

Thread-related tools : Process Explorer Why a process is hungViewing Ready Threads

Thread SchedulingWindows Scheduling APIsProcessor affinity Quantum thread dispatching Priority Levels

Quantum It run for an amount of time called a

quantum Quantum values can vary

System configuration settingsForeground/background status of the processUse of the job object to alter the quantum

A thread might not complete its quantumPreemptive scheduler

Outline Examining Thread Activity

Thread-related tools : Process Explorer Why a process is hungViewing Ready Threads

Thread SchedulingWindows Scheduling APIsProcessor affinity QuantumThread dispatching Priority Levels

Thread dispatching Events might require thread dispatching

A thread become ready to execute A thread leaves the running state A thread’s priority changes A thread’s processor affinity changes

Note: Windows schedule at the thread granularity Example : If process A has 10 runnable threads, proce

ss B has 2 runnable threads, and all 12 threads are at the same priority.

Threads would receive one-twelfth of CPU time Windows wouldn’t give 50% CPU to process A and 50% CPU t

o process B.

Thread dispatching (cont.)

Outline Examining Thread Activity

Thread-related tools : Process Explorer Why a process is hungViewing Ready Threads

Thread SchedulingProcessor affinity Quantum thread dispatching Priority LevelsWindows Scheduling APIs

Priority Levels

Priority Levels Thread priority levels are assigned form two diffe

rent perspectives: Windows API :

First organizes processes Windows kernel :

Set relative priority of the individual threads within those processes

Priority class Real-time High Above Normal Normal Below Normal Idle

Priority Levels (cont.)

Priority Levels (cont.)

Process has only one priority valuesCurrent priority

Thread has two priority valuesCurrent priorityBase priority

Priority Levels (cont.)

priority boosting in the dynamic range (1 through 15) for brief p

eriods. Windows never adjusts the priority of threads

in the real-time range (16 through 31), so they always have the same base and current pri

ority.

Priority Levels (cont.)

A thread’s initial bas priority is inherited from the process base priority

A process inherits its base priority from the process that created it. CreateProcess function Command-line START command

Changed process priority SetPriorityClass Various tools that expose that function

Task Manager Process Explorer

Priority Levels (cont.)

Process base priorityDefault to the value at middle of each process

priority range(24,13,10,8,6,4)Some Windows system processes have a bas

process priority slightly higer than the default for the Normal class(8)

Session Manager, service controller, local security authentication server

Use an internal system call (NtSetInformationProcess) to set process base priority

Outline Examining Thread Activity

Thread-related tools : Process Explorer Why a process is hungViewing Ready Threads

Thread SchedulingProcessor affinity Quantum thread dispatching Priority LevelsWindows Scheduling APIs

Windows Scheduling APIs Scheduling-Related APIs and Their FunctionsAPI Function

Suspend/ResumeThread Suspends or resumes a paused thread from execution.

Get/SetPriorityClass Returns or sets a process's priority class (base priority).

Get/SetThreadPriority Returns or sets a thread's priority (relative to its process base priority).

Get/SetProcessAffinityMask

Returns or sets a process's affinity mask.

SetThreadAffinityMask Sets a thread's affinity mask (must be a subset of the process's affinity mask) for a particular set of processors, restricting it to running on those processors.

API Function

SetInformationJobObject Sets attributes for a job; some of the attributes affect scheduling, such as affinity and priority.

GetLogicalProcessorInformation

Returns details about processor hardware configuration (for hyper-threaded [wiki] and NUMA [wiki] systems).

Get/SetThreadPriorityBoost Returns or sets the ability for Windows to boost the priority of a thread temporarily. (This ability applies only to threads in the dynamic range.)

SetThreadIdealProcessor Establishes a preferred processor for a particular thread, but doesn't restrict the thread to that processor.

Windows Scheduling APIs (cont.)

Scheduling-Related APIs and Their Functions

API Function

Get/SetProcessPriorityBoost

Returns or sets the default priority boost control state of the current process. (This function is used to set the thread priority boost control state when a thread is created.)

SwitchToThread

Yields execution to another thread (at priority 1 or higher) that is ready to run on the current processor.

Sleep Puts the current thread into a wait state for a specified time interval (figured in milliseconds [msec]). A zero value relinquishes the rest of the thread's quantum.

SleepEx Causes the current thread to go into a wait state until either an I/O completion callback is completed, an APC is queued to the thread, or the specified time interval ends.

Scheduling-Related APIs and Their Functions

Windows Scheduling APIs (cont.)

Recommended