Sample Profiling And Stdin
Some command-line applications can read from stdin for use as program input. The canonical example of this is sort.exe which sorts the list of strings given to it and prints it back out to the console:
C:\Temp> type letters.txt
C:\Temp> sort < letters.txt
Sample profiling an application that reads from stdin is a bit trickier than typical sample profiling. It is not easily possible from the VS IDE, so you have to drop down to the profiler’s command-line tools. The command-line tools support passing arguments to the target program via vsperfcmd /args, but a stdin file parameter is not considered to be a program argument. In other words, the following does not work as you might expect:
vsperfcmd /start:sample /output:profile.vsp /launch:sort.exe /args:”< letters.txt”
This will behave as if you launched sort.exe “< letters.txt”. Unfortunately, this is not the intent here. In order to get around this, we can use cmd.exe’s built-in start utility and its background feature. The start utility is the Swiss Army knife for launching programs. Its background feature lets you launch a command in the background without creating a new cmd window while still passing through any stdin parameters. Knowing this, and assuming we’re profiling a native application, we can create a simple batch file to get the job done:
start /b sort.exe < letters.txt
vsperfcmd /start:sample /attach:sort.exe /output:profile.vsp
Because we are attaching after launching the application, we will probably miss a few samples at the beginning. Having these commands in a batch file, however, ensures that we miss as few samples as possible. For any real workload, losing a few samples should not be an issue.
Interestingly, instrumentation (trace) profiling does not suffer from this issue at all because of the way in which instrumented binaries are launched. To achieve the above with instrumentation, you can simply do the following:
vsperfcmd /start:trace /output:profile.vsp
sort.exe < letters.txt
If all else fails, or if your scenario is more complicated than above, you can always just modify your application to use a hardcoded input path.