270 likes | 554 Vues
PC 19. Windows 7: Designing Efficient Background Processes. Vikram Singh Program Manager Microsoft Corporation. Agenda. Introduction Choosing the Right Model Windows 7 Trigger-Start Services Task Scheduler Conditions and Triggers Security and Performance Recommendations.
E N D
PC19 Windows 7: Designing Efficient Background Processes Vikram Singh Program Manager Microsoft Corporation
Agenda • Introduction • Choosing the Right Model • Windows 7 Trigger-Start Services • Task Scheduler Conditions and Triggers • Security and Performance Recommendations
What Is Background Activity? • 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
Performance • Responsiveness to the user • Consumes resources from foreground applications • Boot, Shutdown, Logoff, etc. • Reliability • Memory leaks • System crashes, hangs • Dependent application crashes Impact Of Background Activity • 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
Impact Of Background Activity • Internal evaluation of 49 Windows services • Not critical for boot and login • Critical and required for their individual scenarios
Background Activity Performance Impact • Compared IT image to clean Windows Vista installation: • 10 additional 3rd-party services
Designing For Efficiency • 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)
Services And Scheduled Tasks • Continuous activity from boot to shutdown • Service Control Manager (SCM) programming model • Can specify dependency • Short duration action • Idle activity • Take action on user login • Standalone executable or out-of-process COM server • Generally execute in user session • Windows Service • Scheduled Task
Windows Services: Auto_Start • Auto_Startstart 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 7: Trigger-Start Services • 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
demo Converting a Service to Trigger-Start
SomeTrigger-Start Services In Windows 7 • Windows 7 (Build 6801)
Service Shutdown • Self-stop the service • Immediately or after pre-determined idle time when task is complete if Trigger-StartorDemand_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 Control Handler • 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 Security • [DllImport(“advapi32.dll”, SetLastError=true)] • Public static extern bool ChangeServiceConfig2( • IntPtrhService, • uint32dwInfoLevel, • IntPtrlpInfo • ); • Const uint32 SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO = 6 • // Example UPS Service • protected override void OnStart (string[] args) • { • IntPtrServiceHandle; • IntPtrRequiredPrivilegesString; • // set required privileges • string RequiredPrivileges = “SeShutdownPrivilege\0\0”; • RequiredPrivilegesString = Marshal.StringToHGlobalAuto(RequiredPrivileges); • ChangeServiceConfig2( • this.ServiceHandle, • SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO, • RequiredPrivilegesString • ); • } • Run in the least privilege required • LocalService/NetworkService • Avoid LocalSystem • Remove unneeded privileges • E.g., SeImpersonatePrivilege • Express required privileges • SCM automatically removes all others
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 Kernel Detects Idle Condition Task Scheduler (schedule) Launches Task Power Efficiency Diagnostics (powercfg.exe)
Task Triggers • Calendar • Boot • Logon • Idle • Event log based entry • Workstation lock • Workstation unlock
Task Conditions • Idle condition • Stop when not idle • AC power only • Stop on battery • Wake computer from sleep • Specific or any network connection
Task Recommendations • 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
Performance And Power • 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)
Call To Action • 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
Additional Resources • 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-8017-4bae-9fde-d599bac8184a/Vista_Services.doc • Service Control Manager [MSDN] • http://msdn.microsoft.com/en-us/library/ms685150(VS.85).aspx • Task Scheduler • http://msdn.microsoft.com/en-us/library/aa383614.aspx • Windows Performance Tools Kit • http://www.microsoft.com/whdc/system/sysperf/perftools.mspx
Evals & Recordings Please fill out your evaluation for this session at: This session will be available as a recording at: www.microsoftpdc.com
Q&A Please use the microphones provided
© 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.