Profiling Java code

This page describes setting up Stackdriver Profiler for profiling Java code. For Java, the Profiler offers CPU and wall-time profiling; see Profiling Concepts for more information.

You must use version 7, 8, or 9 of either the OpenJDK or the Oracle JDK.

Setting up Stackdriver Profiler typically involves installing the profiling agent and loading it when you start Java, providing configuration values for the agent as arguments. If you are using Java, you can use the profiling agent on Linux in the following environments:

  • Compute Engine
  • Google Kubernetes Engine
  • App Engine flexible environment
  • App Engine standard environment

You can also profile Java code on non-Google Cloud Platform systems. See Profiling Outside Google Cloud Platform for more information.

Enabling the Profiler API

Before you use the profiling agent, ensure that the underlying Profiler API is enabled. You can check the status of the API and enable it if necessary by using either the Cloud SDK gcloud command-line tool or the Cloud Console:

Cloud SDK

  1. If you have not already installed the Cloud SDK on your workstation, see Google Cloud SDK.

  2. Run the following command:

    gcloud services enable cloudprofiler.googleapis.com
    

For more information, see gcloud services.

Cloud Console

  1. Go to the APIs & Services dashboard:

    Go to APIs & services

  2. Select the project you will use to access the API.

  3. Click the Add APIs and Services button.

    Add APIs and Services

  4. Search for Profiler API.

  5. In the search results, select Stackdriver Profiler API.

  6. If API enabled is displayed, then the API is already enabled. If not, click the Enable button.

Installing the Profiler agent

Compute Engine

Create a directory, for example, /opt/cprof, in which to install Stackdriver Profiler:

sudo mkdir -p /opt/cprof

Download the agent archive from the storage.googleapis.com repository and extract it into the installation directory:

wget -q -O- https://storage.googleapis.com/cloud-profiler/java/latest/profiler_java_agent.tar.gz \
| sudo tar xzv -C /opt/cprof

GKE

Modify the service container Dockerfile to create a directory in which to install Stackdriver Profiler, download the agent archive, and extract it into the installation directory:

RUN mkdir -p /opt/cprof && \
    wget -q -O- https://storage.googleapis.com/cloud-profiler/java/latest/profiler_java_agent.tar.gz \
    | tar xzv -C /opt/cprof

App Engine flexible environment

The Stackdriver Profiler agent is included in the base Java images for Java 8 and Java 9, so there is nothing to install.

App Engine standard environment

The Stackdriver Profiler agent is included as part of the Java 8 runtime support, so there is nothing to install.

Loading the Profiler agent

To profile your code, start Java as you normally would to run your program, but specify the agent-configuration options. You specify the path to the agent library, and you can pass options to the library.

For the App Engine standard environment, the agent is automatically loaded and configured. Skip ahead to Starting your program, for details on configuring, and starting, your program.

Agent configuration

Use the -agentpath option to specify the path to the agent library in the installation directory, and pass additional configuration to the library.

The syntax looks like this:

 -agentpath:[INSTALL_DIR]/profiler_java_agent.so=[OPTION1],[OPTION2],[OPTION3]

The most common options include:

  • -cprof_service: A name for the service being profiled
  • -cprof_service_version: (optional) The version of the service being profiled

To profile your code, add the -agentpath configuration to your regular Java invocation. For example, to run myApp.jar and profile it, a minimal invocation might be:

 java -agentpath:/opt/cprof/profiler_java_agent.so=-cprof_service=myApp -jar myApp.jar

Service name and version arguments

When you load the Profiler agent, you specify a service-name argument and an optional service-version argument to configure it.

The service name lets Profiler collect profiling data for all replicas of that service. The profiler service ensures a collection rate of one profile per minute, on average, for each service name across each combination service versions and zones.

For example, if you have a service with two versions running across replicas in three zones, the profiler will create an average of 6 profiles per minute for that service.

If you use different service names for your replicas, then your service will be profiled more often than necessary, with a correspondingly higher overhead.

When selecting a service name:

  • Choose a name that clearly represents the service in your application architecture. The choice of service name is less important if you only run a single service or application. It is more important if your application runs as a set of micro-services, for example.

  • Make sure to not use any process-specific values, like a process ID, in the service-name string.

  • The service-name string must match this regular expression:

    ^[a-z]([-a-z0-9_.]{0,253}[a-z0-9])?$

A good guideline is to use a static string like imageproc-service as the service name.

The service version is optional. If you specify the service version, Profiler can aggregate profiling information from multiple instances and display it correctly. It can be used to mark different versions of your services as they get deployed. The Profiler UI lets you filter the data by service version; this way, you can compare the performance of older and newer versions of the code.

The value of the service-version argument is a free-form string, but values for this argument typically look like version numbers, for example, 1.0.0 or 2.1.2.

Starting your program

Compute Engine

Start Java as you normally would to run your program, and add the the agent-configuration options:

java \
    -agentpath:/opt/cprof/profiler_java_agent.so=-cprof_service=myservice,-cprof_service_version=1.0.0 \
    [JAVA OPTIONS] -jar PATH/TO/YOUR/JARFILE [PROGRAM OPTIONS]

GKE

Modify the service container Dockerfile to start Java as you normally would to run your program, and add the agent-configuration options:

RUN java \
    -agentpath:/opt/cprof/profiler_java_agent.so=-cprof_service=myservice,-cprof_service_version=1.0.0 \
    [JAVA OPTIONS] -jar PATH/TO/YOUR/JARFILE [PROGRAM OPTIONS]

App Engine flexible environment

Modify the app.yaml configuration file to set the PROFILER_ENABLE environment variable. Then start your program as usual:

env_variables:
   PROFILER_ENABLE: true

See Defining environment variables for more information.

App Engine standard environment

Modify the app.yaml or the appengine-web.xml configuration file to include the GAE_PROFILER_MODE environment variable to instruct Stackdriver Profiler to collect CPU and heap profiles, one time per minute on average, across all instances of the same deployment:

app.yaml:

env_variables:
   GAE_PROFILER_MODE: cpu,heap

appengine-web.xml:

  <env-variables>
    <env-var name="GAE_PROFILER_MODE" value="cpu,heap" />
  </env-variables>

Then start your program as usual.

See Defining environment variables for more information.

Agent logging

The profiling agent can report debug information in its logs for configurations other than the App Engine standard environment. To enable this logging in the profiling agent, append the -logtostderr flag to the -agentpath configuration. This instructs the agent to log output to the standard-error stream. For example:

 java -agentpath:/opt/cprof/profiler_java_agent.so=-cprof_service=myApp,-logtostderr -jar myApp.jar

What's next

See Using the Stackdriver Profiler Interface for detailed information on different features of Profiler:

  • Learn about the Profiler controls.
  • Learn how to zoom in on specific frames.
  • Learn how to filter and focus the graph.
  • Learn how to compare profiles.
Was this page helpful? Let us know how we did:

Send feedback about...

Stackdriver Profiler