Vikram Singh Program Manager Microsoft Corporation PC19.

27
Windows 7: Designing Efficient Background Processes Vikram Singh Program Manager Microsoft Corporation PC19

Transcript of Vikram Singh Program Manager Microsoft Corporation PC19.

Page 1: Vikram Singh Program Manager Microsoft Corporation PC19.

Windows 7: Designing Efficient Background Processes

Vikram SinghProgram ManagerMicrosoft Corporation

PC19

Page 2: Vikram Singh Program Manager Microsoft Corporation PC19.

Introduction Choosing the Right Model Windows 7 Trigger-Start Services Task Scheduler Conditions and Triggers Security and Performance

Recommendations

Agenda

Page 3: Vikram Singh Program Manager Microsoft Corporation PC19.

System activity that is not directly initiated by user Services, Scheduled tasks,

some COM processes such as a COM service, WMI providers, etc.

Part of nearly every usage scenario: Search indexing System security and maintenance Network management Device management System configuration

What Is Background Activity?

Page 4: Vikram Singh Program Manager Microsoft Corporation PC19.

Impact Of Background Activity

Performance• Responsiveness to the user• Consumes resources from

foreground applications• Boot, Shutdown, Logoff, etc.

Reliability• Memory leaks• System crashes, hangs• Dependent application

crashes

Security• Activity may require system

privileges• Successful attack may

compromise entire system

Power Consumption• Extra disk, CPU utilization• Decrease in battery life• Prevents idle efficiencies

Page 5: Vikram Singh Program Manager Microsoft Corporation PC19.

Internal evaluation of 49 Windows services Not critical for boot and login Critical and required for

their individual scenarios

Impact Of Background Activity

Resource Quantity

File I/O 47,286

Copy-on-Write (COW) Pages 4,656 (~18MB)

Memory Pages (Total) 15,967 (~60MB)

Registry Operations 38,508

Threads 367

Page 6: Vikram Singh Program Manager Microsoft Corporation PC19.

Background Activity Performance Impact

Compared IT image to clean Windows Vista installation: 10 additional 3rd-party services

Clean IT0

20

40

24.746.1

Boot Timeseconds

Clean IT0

153045

25.6 30

Shutdown Timeseconds

Clean IT0%

4%

8%

1.01%6.04%

Idle CPU Utilization15 second trace

Clean IT0

20,000

40,000

10,19231,401

Disk Read Count15 second trace

Page 7: Vikram Singh Program Manager Microsoft Corporation PC19.

Choose the right model Windows Service or Scheduled Task?

Leverage the latest Windows infrastructure Trigger-Start Services for Windows 7

Make performance optimizations Eliminate unnecessary privileges Evaluate and measure Iterate (step 1)

Designing For Efficiency

Page 8: Vikram Singh Program Manager Microsoft Corporation PC19.

Services And Scheduled TasksW

indo

ws

Serv

ice • Continuous activity

from boot to shutdown

• Service Control Manager (SCM) programming model

• Can specify dependency Sc

hedu

led

Task

• Short duration action

• Idle activity• Take action

on user login• Standalone

executable or out-of-process COM server

• Generally execute in user session

Page 9: Vikram Singh Program Manager Microsoft Corporation PC19.

Auto_Start start type enables the service to launch at boot and never terminate

Most common service start type Easy for developers—service is always running!

Problems Startup time adds to boot time Adds to system base “footprint” Many Auto_Start services wait for rare events

Windows Services: Auto_Start

Page 10: Vikram Singh Program Manager Microsoft Corporation PC19.

Trigger-Start centralizes environmental detection logic SCM registers for system

events via interesting providers: Device arrival IP address Domain join and leave Group policy updates Custom ETW (Event Tracing for Windows) events

SCM starts or stops registered services: Examples:

TabletInputService started only if digitizer is present StorSvc starts when group policy

updates are applied, automatically stops

Windows 7: Trigger-Start Services

Page 11: Vikram Singh Program Manager Microsoft Corporation PC19.

Converting a Service to Trigger-Start

demo

Page 12: Vikram Singh Program Manager Microsoft Corporation PC19.

Windows 7 (Build 6801)

Some Trigger-Start Services In Windows 7

Service Name Description Trigger Type

AELookupSvc Processes application compatibility cache requests for applications as they are launched

Custom ETW

BDESVC Provides BitLocker client services for user interface and auto-unlocking of data volumes

Custom ETW

BTHSERV The Bluetooth service supports discovery and association of remote Bluetooth devices.

Device

SensorsMTPMonitor Monitors MTP (Media Transfer Protocol) sensors (such as a cell phone with a GPS receiver) to communicate sensor data to programs

Device

TabletInputService Enables Tablet PC pen and ink functionality Device

WinDefend Protection against spyware and potentially unwanted software

Group Policy

Page 13: Vikram Singh Program Manager Microsoft Corporation PC19.

Self-stop the service Immediately or after pre-determined

idle time when task is complete if Trigger-Start or Demand_Start

System shutdown Do not set SERVICE_ACCEPT_SHUTDOWN

unless required No need to free memory if the

service is in a standalone process Target less than 200ms for

completing shutdown notification

Service Shutdown

Page 14: Vikram Singh Program Manager Microsoft Corporation PC19.

No blocking calls! WaitForSingleObject, CreateFile, RPCs, etc. Strive for lock-free code

Execute work items in a thread pool Follow the MSDN guidelines for specific

return codes SERVICE_CONTROL_STOP

Call SetServiceStatus(SERVICE_STOP_PENDING)

Post stop work to a thread pool Order is key!

Service Control Handler

Page 15: Vikram Singh Program Manager Microsoft Corporation PC19.

Run in the least privilege required LocalService/

NetworkService Avoid LocalSystem

Remove unneeded privileges E.g., SeImpersonatePrivilege

Express required privileges SCM automatically

removes all others

Service Security

[DllImport(“advapi32.dll”, SetLastError=true)]Public static extern bool ChangeServiceConfig2(

IntPtr hService,uint32 dwInfoLevel,IntPtr lpInfo

);

Const uint32 SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO = 6

// Example UPS Service

protected override void OnStart (string[] args){

IntPtr ServiceHandle;IntPtr RequiredPrivilegesString;

// set required privilegesstring RequiredPrivileges = “SeShutdownPrivilege\0\0”;

RequiredPrivilegesString = Marshal.StringToHGlobalAuto(RequiredPrivileges);

ChangeServiceConfig2(this.ServiceHandle,

SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO,RequiredPrivilegesString);

}

Page 16: Vikram Singh Program Manager Microsoft Corporation PC19.

Scheduled Tasks

Page 17: Vikram Singh Program Manager Microsoft Corporation PC19.

Scheduled Task Example

Power Efficiency Diagnostics Windows 7 power

problem analysis Executes every 2 weeks

when the system is idle Requires SYSTEM

privilege to access NT Kernel Logger

Saves report data for user

Uploads CEIP data to Microsoft

Task Scheduler(schedule)

Power Efficiency Diagnostics

(powercfg.exe)

Kernel

Detects Idle Condition

Launches Task

Page 18: Vikram Singh Program Manager Microsoft Corporation PC19.

Calendar Boot Logon Idle Event log based

entry Workstation lock Workstation

unlock

Task Triggers

Page 19: Vikram Singh Program Manager Microsoft Corporation PC19.

Idle condition Stop when

not idle AC power only Stop on battery Wake computer

from sleep Specific or

any network connection

Task Conditions

Page 20: Vikram Singh Program Manager Microsoft Corporation PC19.

Use the idle condition to prevent background activity from interrupting the user

Enable the Power condition—do not run when the system is on battery

Enable the Network condition if the task requires network connectivity E.g., download software update

Task Recommendations

Page 21: Vikram Singh Program Manager Microsoft Corporation PC19.

Performance is critical for background activity Adds to the base footprint of the system Interferes with foreground user activity Resource utilization directly

tied to Power Consumption Target:

Less than 2% CPU activity when the system is idle

No disk activity when the system is idle Evaluate and Measure:

Use XPerf (Windows Performance Tools Kit)

Performance And Power

Page 22: Vikram Singh Program Manager Microsoft Corporation PC19.

Understand that background activity has tangible impact on system quality

Select the right model: service or task Use Windows 7 Trigger-Start

instead of Auto_Start Configure your service to use the

smallest set of security privileges Leverage scheduled task idle,

power and network conditions Use Windows Performance

Tools Kit (XPerf) to measure

Call To Action

Page 23: Vikram Singh Program Manager Microsoft Corporation PC19.

Developing Efficient Background Processes for Windows http://go.microsoft.com/fwlink/?LinkId=128622

Services in Windows Vista http://download.microsoft.com/download/9/c/5/9c5b2167-80

17-4bae-9fde-d599bac8184a/Vista_Services.doc

Service Control Manager [MSDN] http://msdn.microsoft.com/en-us/library/ms685150(VS.85).asp

x

Task Scheduler http://msdn.microsoft.com/en-us/library/aa383614.aspx

Windows Performance Tools Kit http://www.microsoft.com/whdc/system/sysperf/perftools.ms

px

Additional Resources

Page 24: Vikram Singh Program Manager Microsoft Corporation PC19.

Evals & Recordings

Please fill

out your

evaluation for

this session at:

This session will be available as a recording at:

www.microsoftpdc.com

Page 25: Vikram Singh Program Manager Microsoft Corporation PC19.

Please use the microphones provided

Q&A

Page 26: Vikram Singh Program Manager Microsoft Corporation PC19.

© 2008 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.

Page 27: Vikram Singh Program Manager Microsoft Corporation PC19.