Windows On/Off Transitions Solutions Guide - Shutdown Transition

Fast shutdown is important for asuccessful overall user experience on a system. A lengthy shutdown time cancause frustration among users, increase service times for systemadministrators, and adversely affect the reliability of systems. For example,if shutdown takes too long, the user might press the power button instead andcould potentially lose data.

Services and applications can have asignificant effect on shutdown performance. CPU and disk activity during theshutdown process can also delay the shutdown transition.

Figure 22 shows the three mainphases of the shutdown transition.

Figure 22. Phases ofthe shutdown transition

Shutdown Transition: Capturing Traces

You can use Xbootmgr to capture a traceof the shutdown transition. In the following example, Xbootmgr restarts thesystem three times and saves the trace in the Traces directory on the rootdrive:

xbootmgr -trace shutdown -numRuns 3 -resultPath%systemdrive%\traces
-postBootDelay 180 -traceFlags base

 

The following are some key points toremember:

·        The –postBootDelay value specifies the time-out in seconds that thesystem waits after booting before it starts the next shutdown trace. Thedefault is 120, but we recommend 180 seconds for shutdown traces.

·        The -traceFlags base optionprovides a lightweight and consistent trace.

·        Xbootmgr reboots the system onetime before it collects the first trace.

·        Xbootmgr reboots the system foreach iteration, and a status dialog box appears throughout each iteration.

·        After testing is complete, Xbootmgrreboots the system another time, but no status dialog box appears.

Shutdown Transition: Analyzing Traces

You can use the Xperf.exe and Xperfview.exetools in the WPT to analyze the shutdown transition performance data.

To generate an XML summary of shutdown,use the -a shutdown action with Xperf,as shown in the following example:

xperf -i trace.etl -o summary.xml -a shutdown

 

 

Open the Summary.xml file in an XMLreader such as Internet Explorer. The report contains detailed per-phase timinginformation as Figure 23 shows.

Figure 23. Sample Summary.xmlfile generated in Xperf with the shutdown action

The following are some keypoints to remember:

·        The time unit that is used (suchas milliseconds) appears at the top of the report.

·        The timing node specifies the total time for the system to shut downand for all services to shut down.

·        The perSessionInfo node contains the start time, end time, and durationfor all applications that have session ID 0 or 1.

·        The intervals node lists all activities that occur between the shutdownphases and the start time, end time, and duration for each activity.

·        The services node lists all services and the start time,end time, and time to shut down each service.

Shutdown Transition: UserSession Phase

What Happens in UserSession

During this phase, the Client/Server RuntimeServer Subsystem (Csrss.exe) shuts down all applicationsthat are running in the user session—that is, allapplications that have session ID 1.

When the user chooses Shut Down from the Start menu, Csrss sends two shutdown notifications(WM_QUERYENDSESSION and WM_ENDSESSION messages) to each user interface (UI)thread in each graphical user interface (GUI) application. If after 5 secondsany application blocks shut down, Windows displays the dialog box in Figure 24so that users can choose to force or cancel shutdown.

Figure 24. Shutdown UI in Windows 7 andWindows Vista waiting for user input

To shut down a console application, Windowsinvokes the console control handler by sending CTRL_LOGOFF_EVENT and waits upto 5 seconds for a response.

If the user selects Force shut down from the dialog box, the system gives allapplications 0.25 second to respond to WM_QUERYENDSESSION messages and 0.5 secondto respond to WM_ENDSESSION messages, and then closes any remainingapplications.

For system-forced shutdowns—which a user startsby running Shutdown.exe or an application starts by calling the correspondingsystem function—Windows does not display a dialog box. Instead, it immediately closesany hung applications and closes all GUI and console applications after the 5‑secondtime-out. For example, shutdowns that are triggered by Windows Update areconsidered system-forced shutdowns.

UserSessionPerformance Vulnerabilities

Applications must perform a handshakewith Csrss, and any applications that do not do this correctly are timed outand closed.

Because Windows serially shuts downapplications, any delay in a process’s shutdown path contributes to the totalshutdown duration. To ensure a speedy shutdown, every application must respondquickly to shutdown notification messages (WM_QUERYENDSESSION andWM_ENDSESSION).

Windows uses long time-outs so thatapplications have sufficient time to shut down and save user data. Therefore, applicationscan have a significant effect on shutdown performance.

UserSession Performance Analysis

To analyze the performance of this phase,you can use the Summary.xml output file that you created previously in “ShutdownTransition: Analyzing Traces.”

The time that is required to shut down anapplication varies among applications. Application shutdown timings appear inthe sessionShutdown subnode of the perSessionInfo node as Figure 25shows.

The sessionID attribute in this phase is1, which indicates the user session.

Figure 25. Snippet of the Summary.xmlfile that shows application shutdown times

To identify the cause of any process thatdelays shutdown, note the shutdownStartTimeand shutdownEndTime for that processin the shutdownProcess subnode andopen the trace in Xperfview. The CPU Sampling by Process graph in Xperfview canhelp you identify CPU consumption issues. The CPU Scheduling graph shows waitand ready stacks for each thread in the process and can help you identify delayissues.

For specific instructions on how toidentify applications that delay system shutdown, see Appendix A.

Some applications can take a long timeto process the shutdown notification and thereby delay other applications fromreceiving notifications. Application developers should follow these guidelinesto reduce delays in the shutdown path:

·        Avoid unnecessary disk flushesat the time of shutdown by minimizing the amount of unsaved data at shutdown. Todo so, perform regular disk flushes during processing (that is, regularly and automaticallysave data), keep track of data that is saved to disk, and save only unsaveddata at shutdown. In addition, avoid explicit flushes such as the following twoexamples:

·        Disk flushes that theapplication initiates, such as calling FlushFileBuffers.

·        Registry flush on shutdown.Avoid calling explicit flushes during shutdown because this increases overallshutdown time.

·        Avoid unnecessary networkdependencies during shutdown. Network problems can delay the shutdown ofapplications.

Shutdown Transition: SystemSessionPhase

What Happens in SystemSession

This phase includes two subphases:

·        Preshutdown notification. Windowsserially shuts down all services that registered to receive preshutdownnotifications. Ordered services—services that have set up the shutdown order ofdependent services—are shut down before non-ordered services.

·        Shutdown notification. All services thatregistered to receive shutdown notifications are shut down in parallel.

 

If all services have not exited after 20 seconds(in Windows Vista) or 12 seconds (in Windows 7), the system continues theshutdown. Processes and services that do not shut down in a timely manner areleft running as the system shuts down.

SystemSessionPerformance Vulnerabilities

Only services that must handle preshutdownand shutdown notifications should opt to receive them. In the preshutdownnotification subphase, the SCM serializes the waits. Therefore, these servicesblock system shutdown until they exit or until the wait hint time-out expires.

Services are not guaranteed to have enoughtime to finish all their work in the shutdown notification subphase before thesystem shuts down.

If a service registers to receiveshutdown notifications through its service control handler, it must respond quicklyto notifications from the SCM. To correctly shut down without adversely affectingshutdown transition time, a service should:

·        Mark itself as STOP_PENDING immediatelyafter it receives the shutdown notification. This returns control to the SCM.

·        Enter the stopped state(SERVICE_STOP) as soon as possible.

 

For specific guidelines on how to write servicecontrol handler functions, see “Service Control Handler Function” on MSDN.

SystemSessionPerformance Analysis

By looking at the Summary.xml outputfile that you generated in “Shutdown Transition: Analyzing Traces,” you can seethe total time for this phase in the servicesShutdownDurationattribute of the timing node. Thisvalue appears in the third line in Figure 26.

Figure 26. Snippet of the Summary.xmlfile that shows service shutdown times

Services that do not perform the SCMhandshake correctly and therefore delay shutdown are identified in the unresponsiveServices node. Servicedevelopers must ensure that their services respond quickly to notificationsfrom SCM because unresponsive services can delay shutdown by 12 seconds (in Windows 7)or 20 seconds (in Windows Vista). The sample output in Figure 26shows no unresponsive services.

Following the unresponsiveServices node, the servicesnode lists individual service names, start times, end times, and total durations.The start time indicates the time at which the service began to process theshutdown notification.

You can also use the Xperfview tool tosee services that delay shutdown. Open the trace file by using Xperfview.exe,and then check the Services box inthe Frame List on the left, as shownin Figure 27.

Figure 27. A sample trace file in Xperfviewthat shows service shutdown times

Shutdown Transition: KernelShutdown Phase

What Happens in KernelShutdown

Inthe KernelShutdown phase, the rest of the system, including all devices anddrivers, is shut down.

Tocalculate the time spent in KernelShutdown

1.    In the XML trace file, locate the overallshutdown time. For example:

<results timeFormat="msec">

  <shutdown>

    <timing shutdownTime="13066" servicesShutdownDuration="2675">

      <perSessionInfo>

 

        <sessionShutdown sessionID=”1”startTime=”448” endTime=”7091”

           duration=”6642”>

        <sessionShutdown sessionID=”0”startTime=”8335” endTime=”11010”

           duration=”2675”>

 

2.    Subtract the time that is required to shutdown the system and user sessions from shutdownTime.In this example, the KernelShutdown phase is calculated as 13066 – 6642 – 2675= 3,749 milliseconds.

ShutdownTransition: Summary

The following is a list of important considerationsto help you improve shutdown performance. For details about theserecommendations, see “Windows On/Off Transitions Solutions Guide” on the WHDCWeb site.

ü  Use the WPT to measure the performance of drivers, applications, andservices during all on/off transitions.

ü  Ensure that services do not opt to receive preshutdown and shutdownnotifications (SERVICE_CONTROL_PRESHUTDOWN and SERVICE_CONTROL_SHUTDOWN controlcodes) unless they are absolutely required.

ü  Ensure that all services that opt to receive shutdown notificationsrespond quickly to the SCM.

ü  Ensure that all applications respond quickly to shutdown notifications(WM_QUERYENDSESSION and WM_ENDSESSION messages).

ü  Reduce delays in the shutdown path of services and applications byminimizing CPU, disk, and network activity in response to shutdownnotifications.

ü  Use the perSessionInfoand sessionShutdown nodes to see timingfor each application.

ü  Perform testing in a controlled way, and make comparisons against avalid baseline. Obtain a baseline measurement on a system that has as fewsystem extensions as possible. Add devices, applications, and services one at atime, and test for unacceptable regressions in on/off transition times.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值