How to: Profile Services Using Command Line Tools

A service is a program that runs in the background of the operating system and provides support for operating system features. Examples include the Telnet service and the ASP.NET service. A service cannot be run from within Visual Studio; it must run within the context of the Services Control Manager. Therefore, profiling a service is not as straightforward as profiling other Visual Studio application types.

ms242724.alert_caution(en-us,VS.90).gifImportant Note:

You must install the service before you can profile it. For more information, see How to: Install and Uninstall Services.

You can profile a native service by using VSPerfCmd, VSInstr, and VSPerfMon command line tools. To profile managed services, you must follow additional steps that are required to set up the common language runtime environment. The following procedures list the process you should follow.

To profile a managed service by using sampling

  1. Install your service. For more information, see How to: Install and Uninstall Services.

  2. Open a command line window. At the command prompt, run the following command:

    c:\<path>VSPerfClrEnv /globalsampleon 
    

    This will set up the environment variables that you must have to profile managed services.

  3. Restart the computer.

  4. Use Service Control Manager to make sure that the service is running after installation. For more information, see How to: Start Services.

  5. Next, run the following command:

    c:\<path>VSPerfCmd /start:sample /output:filename
    

    Profiler starts in sampling mode. The /output option creates a .VSP file to store performance data.

    ms242724.alert_caution(en-us,VS.90).gifImportant Note:

    You might have to include the /USER option to allow client access to the monitor from the specified account.

  6. Next, run the following command to attach the process:

    c:\<path>VSPerfCmd /attach:pid | name 
    

    The /attach option attaches the service by using process identifier (pid) or process name (name).

  7. To complete the session, detach the process by running the following command:

    c:\<path>VSPerfCmd /detach:pid | name
    

    You can also stop the service or the process hosting the service in a multi-service process.

  8. Next, run the following command to shut down the profiler:

    c:\<path>VSPerfCmd /shutdown
    
  9. The /shutdown option forces the VSPerfCmd tool to wait for monitored applications to shutdown and then shuts down VSPerfMon.exe and wait for the shutdown completion.

  10. Next, use the following command to remove the environment variables that are required to profile managed services.

    C:\<path>VSPerfClrEnv /globaloff 
    
  11. Restart the computer.

To profile a managed service by using instrumentation

  1. Instrument your service by using VSInstr.exe tool. For information about see, VSInstr.

  2. Install your service. For more information, see How to: Install and Uninstall Services.

    Note

    Service should be configured as "manual." It should not start automatically. You will start the service in step 6.

  3. Open a command line window. At the command prompt, run the following command:

    c:\<path>VSPerfClrEnv /globaltraceon 
    

    This will set up the environment variables that are required to profile managed services.

  4. Restart the computer.

  5. Next, run the command following command:

    c:\<path>VSPerfCmd /start:trace /output:filename
    

    Profiler starts the instrumentation mode. The /OUTPUT option creates a .VSP file to store performance data.

    ms242724.alert_caution(en-us,VS.90).gifImportant Note:

    You might have to include the /USER option to allow client access to the monitor from the specified account.

  6. Use Service Control Manager to make sure that the service is running after installation. For more information, see How to: Start Services.

    ms242724.alert_caution(en-us,VS.90).gifImportant Note:

    When the service starts and the performance monitor is running, the service will attach to the performance monitor. If the service is already running when the performance monitor is started, the opportunity to attach is missed.

  7. To end instrumentation, stop the service.

  8. Next, run the following command:

    c:\<path>VSPerfCmd /shutdown
    

    The /shutdown option forces the VSPerfCmd tool to wait for monitored applications to shut down and then shuts down VSPerfMon.exe and waits for the shutdown completion.

  9. Next, use the following command to remove the environment variables that are required to profile managed services:

    c:\<path>VSPerfClrEnv /globaloff
    
  10. Restart the computer.