Profiling Java code

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

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

Setting up 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 (requires App Engine SDK version 1.9.64 or later)

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 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

For GKE, modify the service container Dockerfile to create a directory in which to install 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

When you use the Google Java 8 and Java 9 base images, the Profiler agent is pre-installed. If you use a base image from a different source or if you use a different supported version of Java, then you need to install the agent.

For example, the following Dockerfile for Java 11 applications loads the openjdk:11-slim image and installs the Profiler agent:

FROM openjdk:11-slim

COPY . .
RUN apt-get update && apt-get install -y curl wget

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

ENTRYPOINT ["java", "-agentpath:/opt/cprof/profiler_java_agent.so=[OPTION1],[OPTION2]"]

App Engine standard environment

When you use the Google Java 8 runtime base image, the Profiler agent is pre-installed. If you use a base image from a different source or if you use a different supported version of Java, then you need to install the agent.

For example, the following Dockerfile for Java 11 applications loads the openjdk:11-slim image and installs the Profiler agent:

FROM openjdk:11-slim

COPY . .
RUN apt-get update && apt-get install -y curl wget

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

ENTRYPOINT ["java", "-agentpath:/opt/cprof/profiler_java_agent.so=[OPTION1],[OPTION2]"]

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. For naming restrictions, go to Service name and version arguments.
  • -cprof_service_version: (optional) The version of the service being profiled
  • -cprof_gce_metadata_server_retry_count: (optional) The maximum number of times the agent is to retry contacting the GCP metadata server. By default, the agent doesn't perform retries. If the agent isn't able to contact the GCP metadata server, then the agent initialization fails and profile data isn't available.
  • -cprof_gce_metadata_server_retry_sleep_sec: (optional) The number of seconds the agent is to wait between retries.

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

To run myApp.jar and to instruct the agent to retry contacting the GCP metadata server up to 100 times at 5s intervals, a minimal invocation might be:

java -agentpath:/opt/cprof/profiler_java_agent.so=-cprof_service=myapp,-cprof_gce_metadata_server_retry_sleep_sec=5,-cprof_gce_metadata_server_retry_count=100 -jar myApp.jar

Heap profiling

To enable heap profiling for Java 11, add -cprof_enable_heap_sampling to your -agentpath. For example, to run myapp.jar and profile it with heap profiling enabled, your invocation might be:

 java -agentpath:/opt/cprof/profiler_java_agent.so=-cprof_service=myapp,-cprof_enable_heap_sampling -jar myapp.jar

Go to Found a bug in a Stackdriver product? for a list of known bugs and for information on filing a bug.

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 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 logging information for App Engine flexible environment, Compute Engine, and GKE. The profiling agent supports the following logging levels:

  • 0: Log all messages. Default logging level.
  • 1: Log warning, error, and fatal messages.
  • 2: Log error and fatal messages.
  • 3: Log only fatal messages and stop the application.

To enable writing logs to standard error with the default logging level, append -logtostderr to the -agentpath configuration.

To set the logging level to log only error and fatal messages, append -minloglevel=2 to the -agentpath configuration.

For example, to enable logging of error and fatal messages to standard error, append -logtostderr and ‑minloglevel=2 to the -agentpath configuration:

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

What's next

To learn about the Profiler graph and controls, go to Using the Stackdriver Profiler Interface. For advanced information, go to the following:

Оцените, насколько информация на этой странице была вам полезна:

Оставить отзыв о...

Текущей странице
Stackdriver Profiler
Нужна помощь? Обратитесь в службу поддержки.