This project has retired. For details please refer to its Attic page.
YourKitProfiler xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.utils;
20  
21  import org.apache.log4j.Logger;
22  
23  import com.yourkit.api.Controller;
24  import com.yourkit.api.ProfilingModes;
25  
26  /**
27   * Helper for YourKit profiling from within the code.
28   *
29   * See the following for information about usage:
30   *  - http://www.yourkit.com/docs/95/help/api.jsp
31   *  - http://www.yourkit.com/docs/95/api/index.html
32   *
33   * This class is a simple helper around the API mentioned above
34   * followed by any amount of snapshotX calls and finally
35   * {@link YourKitContext#stop()}.
36   * See also {@link YourKitContext}.
37   *
38   * As of 05/2013 YourKit is not publishing their API jars to Maven, but their
39   * license allows us to do it, so we have setup a repository to do this.
40   * See https://github.com/facebook/sonatype-yourkit for more info.
41   */
42  public class YourKitProfiler {
43    /** Logger */
44    private static final Logger LOG = Logger.getLogger(YourKitProfiler.class);
45    /** Record every ALLOCATION_RECORDING_INTERVAL'th allocation */
46    private static final int ALLOCATION_RECORDING_INTERVAL = 1000;
47  
48    /** Don't construct, allow inheritance */
49    protected YourKitProfiler() { }
50  
51    /**
52     * Create a YourKit controller and do some or all of
53     * {@link Controller#enableExceptionTelemetry()}
54     * {@link Controller#startCPUProfiling(long, String, String)}
55     * {@link Controller#startAllocationRecording(boolean, int, boolean,
56     * int, boolean, boolean)}
57     * based on boolean config options passed as method parameters
58     *
59     * @param enableStackTelemetry      enable stack telementry
60     * @param enableCPUProfilling       enable CPU profilling
61     * @param enableAllocationRecording enable allocation recording
62     *
63     * @return profiler context, or null if controller cannot be created
64     */
65    public static YourKitContext startProfile(boolean enableStackTelemetry,
66                                              boolean enableCPUProfilling,
67                                              boolean enableAllocationRecording) {
68      Controller controller;
69      try {
70        controller = new Controller();
71        // CHECKSTYLE: stop IllegalCatch
72      } catch (Exception e) {
73        // CHECKSTYLE: resume IllegalCatch
74        LOG.info("Failed to set up YourKit controller", e);
75        return null;
76      }
77  
78      try {
79        if (enableStackTelemetry) {
80          controller.enableStackTelemetry();
81          LOG.info("Enabled Yourkit stack telemetry");
82        }
83        // CHECKSTYLE: stop IllegalCatch
84      } catch (Exception e) {
85        // CHECKSTYLE: resume IllegalCatch
86        LOG.info("Failed to enable YourKit stack telemetry", e);
87      }
88  
89      try {
90        if (enableCPUProfilling) {
91          controller.startCPUProfiling(ProfilingModes.CPU_SAMPLING,
92            Controller.DEFAULT_FILTERS, Controller.DEFAULT_WALLTIME_SPEC);
93          LOG.info("Started YourKit CPU profiling");
94        }
95        // CHECKSTYLE: stop IllegalCatch
96      } catch (Exception e) {
97        // CHECKSTYLE: resume IllegalCatch
98        LOG.info("Failed to start YourKit CPU profiling", e);
99      }
100 
101     try {
102       if (enableAllocationRecording) {
103         controller.startAllocationRecording(true, ALLOCATION_RECORDING_INTERVAL,
104             false, -1, true, false);
105         LOG.info("Started YourKit allocation recording");
106       }
107       // CHECKSTYLE: stop IllegalCatch
108     } catch (Exception e) {
109       // CHECKSTYLE: resume IllegalCatch
110       LOG.info("Failed to start YourKit allocation recording", e);
111     }
112 
113     return new YourKitContext(controller);
114   }
115 }