Xperf-Windows performance Toolkit
Event tracing for Windows has been with us since Windows 2000. It is an infrastructure for raising events from varous system components, and has only been used by a small number of kernel-mode
Entities. in Windows XP, MOF files (familiar from WMI provider metadata) were used to describe events. finally, in Windows Vista and Windows Server 2008 events were described by XML manifests, an investment was made in popularizing ETW, and hundreds of new
Event providers were added.
What kind of information is generated by all these providers? Well, first of all, there's
Windows event log which consumes
Some of the information generated by ETW providers (but not all ). so we get all kind of diagnostic messages on things happening in the system. another provider is the Performance Monitor, which features the ability to query a collection set of ETW events.
Integrating these varous sources of information is not an easy task, especially if you are alternating between analyzing a system as a whole and analyzing a set of specific applications within the same trace.
It is this integration that has led to the birth of
Windows performance toolkit. it features the data collection and integration tools necessary to interpret and utilize ETW output correctly. it is specifically constructed so that lots of information can be viewed in a coherent fashion, and so that a system-wide
Image of what's going on can be obtained. Additionally, through the use of the kernel sampling interrupt, a global sampling profiler is available (including call stack analysis). And best of all? It's completely free!
So let's take a look at some of the abilities of this new toolkit. First of all, you need to install it from
WHDC. Note that the installation is only
Supported on Windows Vista SP1 and Windows Server 2008 (I got it to be almost fully functional without Vista SP1, but caveat emptor). Data collection can be performed med on a Windows XP SP2
Or Windows Server 2003 System (you only need xperf.exe and perfctrl. DLL for that), but the trace decoding can only be written med on NT 6.0 and higher.
Let's start with a sample global data collection. (By the way, if you're not into my own throughs, the toolkit comes with an extensive set of documentation-
Online and offline. The starter document is 65 pages, which gives you an idea of how big this thing really is .)
I went to an administrative command line prompt on my Vista, navigated to the toolkit's installation directory (c: \ Program Files \ Microsoft Windows performance Toolkit by default) and typed:
xperf -on Base
This enables a set of ETW providers to publish their events (the exact set of providers can be seen by typing
Xperf-providers). These events are then collected and written to temporary buffers. note that these temporary buffers might grow very large, so if you plan to perform data collection should ss a long period of time, you better have some free disk space
And lots of memory available on the box.
I then proceeded to write a few lines in this post, open a couple of programs, and then went back to the command line and typed:
xperf -d result.etl
This disables the selected ETW data collection and writes the raw results to the result. ETL file (note that this command might take quite some time to complete ). these results are, well, raw, so they need to be analyzed soon. we can accomplish this
The command line by launching a set of actions on the output file, or using the GUI Performance Analyzer tool (xperfview) which is part of the toolkit.
An example of what you can accomplish on the command line can be demonstrated by typing the following action sequence:
xperf -i result.etl -o proclist.txt -a process
This generates a list of processes active during the trace into the proclist.txt file. Here's some of the sample content from my box:
Start Time, End Time, Process, DataPtr, Process Name ( PID), ParentPID, SessionID, UniqueKey MIN, MAX, Process, 0X0000000000168EE0, Idle ( 0), 0, 0, 0x0000f80002150400 MIN, MAX, Process, 0X00000000001693C0, System ( 4), 0, 0, 0x0000fa8001897040 MIN, MAX, Process, 0X00000000001A17A0, svchost.exe ( 356), 708, 0, 0x0000fa8004ce4040 MIN, MAX, Process, 0X0000000000171080, smss.exe ( 520), 4, 0, 0x0000fa800455f610 MIN, MAX, Process, 0X00000000001712B0, csrss.exe ( 616), 604, 0, 0x0000fa8004804c10 MIN, MAX, Process, 0X00000000001904A0, svchost.exe ( 620), 708, 0, 0x0000fa8004d2ac10
Another example of Command Line Info generation wocould be:
xperf -i result.etl -o diskio.txt -a diskio
This gives you some disk I/O statistics for the duration of the trace. Here's the sample from my box:
Start Time, End Time, Read, Write, Usage % 0, 1000000, 18, 30, 51.37 1000000, 2000000, 0, 11, 2.44 2000000, 3000000, 0, 2, 1.05 3000000, 4000000, 1, 4, 4.26 4000000, 5000000, 1, 15, 20.17 5000000, 6000000, 0, 4, 1.26 6000000, 7000000, 0, 7, 2.19 7000000, 8000000, 1, 30, 2.02 8000000, 9000000, 9, 3, 13.47
Finally, if we're interested in a graphical interpretation of everything, just go ahead and launch the Performance Analyzer (xperfview) and then navigate to the trace file, or just type:
xperf result.etl
... From the command line. Here's what the initial output looks like on my system:
That sure looks like a lot of useful data! The first graph is CPU utilization by process, the second one is disk utilization by process. You can filter some processes out, of course:
... And you can zoom in, select, make the graphs overlay so you can see the information side by side:
And then there's the summary table feature, if you just right click on the graph you're interested in and choose "summary table ":
... And if you look at the disk I/O summary and right click for a detail graph, you get the breakdown of specific I/O requests and can even visually see fragmented file access if present (!) :
Note how you can see the volume on the right (C and D in my case ), and if you hover on each individual dot you see the information on the process making the request and the actual file path. on the left you see the disk location information (offset in bytes
From the disk start), so that if there are jumps starting ss the disk all the time, we have some fragmented non-sequential access going on.
And then there's the page fault details, to the level of the file being requested, the size of the I/O Request, the average time spent servicing the request, the total I/O time, etc.:
Bear in mind: we have seen nothing yet. There are some preconfigured profiles for analyzing
Standby/resumption, hibernation, Boot, application startup, network activity, and other scenarios. You can get events generated on virtual memory allocation, power management events,
Registry Access, Driver events, system CILS, interrupts, dpcs/APCs, context switches, what not. (Friendly reminder once more: There's documentation available both
Online and offline, go and read it right away! :-))
However, there's another piece of coolness to see if you're still with me and interested in what else the tool is capable of. Using
Kernel sample profile interrupt, the ETW system can capture the instruction pointer and the stack during trace execution. this data is logged to an ETL file, and can then be analyzed to see what kind of call stacks
Your application encountered. (This also gives away the information on performance bottlenecks-I. e. Where your application has Ds the most of its time .)
So what I did to demonstrate this was write a very simple application which displays prime numbers in a given range. it's written in a very inefficient way so that it takes quite some time to execute. to profile what's going on inside, I went to the Administrative
Command Prompt again and typed:
xperf -on SysProf
Note for advanced users: "base" wocould also have worked, because it also enables profiling. if I'm not interested in collecting disk I/O and memory statistics, however, then I'm better off with "sysprof" (which resolves to proc_thread + loader + profile ). without
Proc_thread and loader you won't get reliable results.
Then all I had to do is launch my application, let it run to termination, generate a results file as before (Xperf-d...) And launch the Performance Analyzer to see what we got.
The first and foremost thing that you have to do at this point is have
Debugging symbols configured properly. you cocould spend hours on trying to understand what's wrong with what you have done, only to discover it's due to symbols improperly configured. to get symbols for Windows code lined up properly all you need to do
Is set up the _ nt_symbol_path system environment variable (my computer-> properties-> advanced system settings-> environment variables) to the following string:
SRV*C:\Symbols*http://msdl.microsoft.com/download/symbols
You can replace c: \ symbols with any other downstream store on your disk that you prefer (or you cocould omit this section entirely ). what this tells the symbols engine is that whenever symbols for windows are needed, they are automagically downloaded from
Microsoft and cached at the downstream store (C: \ symbols ). yes, this requires a connection to the Internet. yes, the symbols will be re-downloaded if windows updates are installed or a service pack is deployed. yes, if you're offline, you coshould also
Download the entire cache and install it, but whenever something is updated you risk getting out of sync.
For your own code, you can prepend the path to the PDB files to the above string, for example:
set _NT_SYMBOL_PATH = C:\Code\MyApp\release;%NT_SYMBOL_PATH%''' OR '''set _NT_SYMBOL_PATH = C:\Code\MyApp\release;SRV*C:\Symbols*http://msdl.microsoft.com/download/symbols
When you finally launch the tool, before you go ahead and open any graphs, go to the trace menu and click load symbols. this might takes some time now or later, depending on what you're re trying to do. essential, it might be downloading symbols for
Entire system for the first time, which cocould take several minutes or even more on a slow connection. on my system, there were 142 modules for which symbols were downloaded, occupying a total of 93 MB disk space.
After you 've got symbols loaded, choose a region on the graph or the entire graph, right click and select "summary table ". use the column chooser on the left to group by process and stack, and show you the weight and the weight %. note that you can do this
For any process, not just for your code-all you need is symbols (which you can have on debug and release builds nowadays with no problem at all ). for my prime numbers application on my system I get the following output:
So it seems that myapp.exe! Isprime took 38.26% of the weight in this profiling session. printf, on the other hand, took 0.71% of the weight.
Note we can't deduce the executionTimeFrom this output; we can only see
Relative WeightA function had (I. e. the number of samples where this function was on the stack compared to the total number of samples taken by the profiler ).
After optimizing the code a little bit (so that prime numbers are calculated more efficiently) and running it again under the profiler, here are the results:
The Code was so optimized that there are no samples for the isprime function anymore (it was not inlined-we're talking about a debug build ), and printf is responsible for 2.92% of the weight.
Yes, there is some room for improvement here because extends cial tools like Microsoft's own
Visual Studio profiler give you so much more, including the ability to compare performance reports, analyze managed code, use a convenient API to determine when profiling shoshould
Start and when it shocould end, profile memory allocations etc. -But this one is for free and it's part of an integrated suite of lots and lots of additional functionality. additionally, if you look closely into the output, you can see that the grouping was med
By this profiler can be done byStack traceAnd not the function-so you can see how many times your function or sequence of funhing was called in a particle order and manner. This is something even implements cial profilers make a difficult
Objective To achieve.
Just one final note before you go ahead and try it: Stack profiling works only on the 32-bit editions of Windows Vista or Windows Server 2008. to be more accurate, I couldn't figure out how to make it work on a 64-bit system and gave up (and the following
Blog Post gives me some hope that one day we'll figure it out ). all I cocould get on a 64-bit system is just a list of funich getting called with their sample times and counts (which is great ), but not the detailed elegant call stacks you can get on
32-bit machine.
So get your hands on
Windows performance toolkit and try it out! Let me know what you discovered.