001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.http;
019
020import java.io.File;
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.concurrent.TimeUnit;
025import java.util.concurrent.atomic.AtomicInteger;
026import java.util.concurrent.locks.Lock;
027import java.util.concurrent.locks.ReentrantLock;
028import javax.servlet.http.HttpServlet;
029import javax.servlet.http.HttpServletRequest;
030import javax.servlet.http.HttpServletResponse;
031import org.apache.hadoop.hbase.util.ProcessUtils;
032import org.apache.yetus.audience.InterfaceAudience;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036import org.apache.hbase.thirdparty.com.google.common.base.Joiner;
037
038/**
039 * Servlet that runs async-profiler as web-endpoint. Following options from async-profiler can be
040 * specified as query paramater. // -e event profiling event: cpu|alloc|lock|cache-misses etc. // -d
041 * duration run profiling for 'duration' seconds (integer) // -i interval sampling interval in
042 * nanoseconds (long) // -j jstackdepth maximum Java stack depth (integer) // -b bufsize frame
043 * buffer size (long) // -t profile different threads separately // -s simple class names instead of
044 * FQN // -o fmt[,fmt...] output format: summary|traces|flat|collapsed|svg|tree|jfr|html // --width
045 * px SVG width pixels (integer) // --height px SVG frame height pixels (integer) // --minwidth px
046 * skip frames smaller than px (double) // --reverse generate stack-reversed FlameGraph / Call tree
047 * Example: - To collect 30 second CPU profile of current process (returns FlameGraph svg) curl
048 * "http://localhost:10002/prof" - To collect 1 minute CPU profile of current process and output in
049 * tree format (html) curl "http://localhost:10002/prof?output=tree&duration=60" - To collect 30
050 * second heap allocation profile of current process (returns FlameGraph svg) curl
051 * "http://localhost:10002/prof?event=alloc" - To collect lock contention profile of current process
052 * (returns FlameGraph svg) curl "http://localhost:10002/prof?event=lock" Following event types are
053 * supported (default is 'cpu') (NOTE: not all OS'es support all events) // Perf events: // cpu //
054 * page-faults // context-switches // cycles // instructions // cache-references // cache-misses //
055 * branches // branch-misses // bus-cycles // L1-dcache-load-misses // LLC-load-misses //
056 * dTLB-load-misses // mem:breakpoint // trace:tracepoint // Java events: // alloc // lock
057 */
058@InterfaceAudience.Private
059public class ProfileServlet extends HttpServlet {
060
061  private static final long serialVersionUID = 1L;
062  private static final Logger LOG = LoggerFactory.getLogger(ProfileServlet.class);
063
064  private static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods";
065  private static final String ALLOWED_METHODS = "GET";
066  private static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
067  private static final String CONTENT_TYPE_TEXT = "text/plain; charset=utf-8";
068  private static final String ASYNC_PROFILER_HOME_ENV = "ASYNC_PROFILER_HOME";
069  private static final String ASYNC_PROFILER_HOME_SYSTEM_PROPERTY = "async.profiler.home";
070  private static final String PROFILER_SCRIPT = "/profiler.sh";
071  private static final int DEFAULT_DURATION_SECONDS = 10;
072  private static final AtomicInteger ID_GEN = new AtomicInteger(0);
073  static final String OUTPUT_DIR = System.getProperty("java.io.tmpdir") + "/prof-output-hbase";
074
075  enum Event {
076    CPU("cpu"),
077    ALLOC("alloc"),
078    LOCK("lock"),
079    PAGE_FAULTS("page-faults"),
080    CONTEXT_SWITCHES("context-switches"),
081    CYCLES("cycles"),
082    INSTRUCTIONS("instructions"),
083    CACHE_REFERENCES("cache-references"),
084    CACHE_MISSES("cache-misses"),
085    BRANCHES("branches"),
086    BRANCH_MISSES("branch-misses"),
087    BUS_CYCLES("bus-cycles"),
088    L1_DCACHE_LOAD_MISSES("L1-dcache-load-misses"),
089    LLC_LOAD_MISSES("LLC-load-misses"),
090    DTLB_LOAD_MISSES("dTLB-load-misses"),
091    MEM_BREAKPOINT("mem:breakpoint"),
092    TRACE_TRACEPOINT("trace:tracepoint"),;
093
094    private final String internalName;
095
096    Event(final String internalName) {
097      this.internalName = internalName;
098    }
099
100    public String getInternalName() {
101      return internalName;
102    }
103
104    public static Event fromInternalName(final String name) {
105      for (Event event : values()) {
106        if (event.getInternalName().equalsIgnoreCase(name)) {
107          return event;
108        }
109      }
110
111      return null;
112    }
113  }
114
115  enum Output {
116    SUMMARY,
117    TRACES,
118    FLAT,
119    COLLAPSED,
120    // No SVG in 2.x asyncprofiler.
121    SVG,
122    TREE,
123    JFR,
124    // In 2.x asyncprofiler, this is how you get flamegraphs.
125    HTML
126  }
127
128  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
129      justification = "This class is never serialized nor restored.")
130  private transient Lock profilerLock = new ReentrantLock();
131  private transient volatile Process process;
132  private String asyncProfilerHome;
133  private Integer pid;
134
135  public ProfileServlet() {
136    this.asyncProfilerHome = getAsyncProfilerHome();
137    this.pid = ProcessUtils.getPid();
138    LOG.info("Servlet process PID: " + pid + " asyncProfilerHome: " + asyncProfilerHome);
139  }
140
141  @Override
142  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
143    throws IOException {
144    if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(), req, resp)) {
145      resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
146      setResponseHeader(resp);
147      resp.getWriter().write("Unauthorized: Instrumentation access is not allowed!");
148      return;
149    }
150
151    // make sure async profiler home is set
152    if (asyncProfilerHome == null || asyncProfilerHome.trim().isEmpty()) {
153      resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
154      setResponseHeader(resp);
155      resp.getWriter()
156        .write("ASYNC_PROFILER_HOME env is not set.\n\n"
157          + "Please ensure the prerequsites for the Profiler Servlet have been installed and the\n"
158          + "environment is properly configured. For more information please see\n"
159          + "http://hbase.apache.org/book.html#profiler\n");
160      return;
161    }
162
163    // if pid is explicitly specified, use it else default to current process
164    pid = getInteger(req, "pid", pid);
165
166    // if pid is not specified in query param and if current process pid cannot be determined
167    if (pid == null) {
168      resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
169      setResponseHeader(resp);
170      resp.getWriter()
171        .write("'pid' query parameter unspecified or unable to determine PID of current process.");
172      return;
173    }
174
175    final int duration = getInteger(req, "duration", DEFAULT_DURATION_SECONDS);
176    final Output output = getOutput(req);
177    final Event event = getEvent(req);
178    final Long interval = getLong(req, "interval");
179    final Integer jstackDepth = getInteger(req, "jstackdepth", null);
180    final Long bufsize = getLong(req, "bufsize");
181    final boolean thread = req.getParameterMap().containsKey("thread");
182    final boolean simple = req.getParameterMap().containsKey("simple");
183    final Integer width = getInteger(req, "width", null);
184    final Integer height = getInteger(req, "height", null);
185    final Double minwidth = getMinWidth(req);
186    final boolean reverse = req.getParameterMap().containsKey("reverse");
187
188    if (process == null || !process.isAlive()) {
189      try {
190        int lockTimeoutSecs = 3;
191        if (profilerLock.tryLock(lockTimeoutSecs, TimeUnit.SECONDS)) {
192          try {
193            File outputFile =
194              new File(OUTPUT_DIR, "async-prof-pid-" + pid + "-" + event.name().toLowerCase() + "-"
195                + ID_GEN.incrementAndGet() + "." + output.name().toLowerCase());
196            List<String> cmd = new ArrayList<>();
197            cmd.add(asyncProfilerHome + PROFILER_SCRIPT);
198            cmd.add("-e");
199            cmd.add(event.getInternalName());
200            cmd.add("-d");
201            cmd.add("" + duration);
202            cmd.add("-o");
203            cmd.add(output.name().toLowerCase());
204            cmd.add("-f");
205            cmd.add(outputFile.getAbsolutePath());
206            if (interval != null) {
207              cmd.add("-i");
208              cmd.add(interval.toString());
209            }
210            if (jstackDepth != null) {
211              cmd.add("-j");
212              cmd.add(jstackDepth.toString());
213            }
214            if (bufsize != null) {
215              cmd.add("-b");
216              cmd.add(bufsize.toString());
217            }
218            if (thread) {
219              cmd.add("-t");
220            }
221            if (simple) {
222              cmd.add("-s");
223            }
224            if (width != null) {
225              cmd.add("--width");
226              cmd.add(width.toString());
227            }
228            if (height != null) {
229              cmd.add("--height");
230              cmd.add(height.toString());
231            }
232            if (minwidth != null) {
233              cmd.add("--minwidth");
234              cmd.add(minwidth.toString());
235            }
236            if (reverse) {
237              cmd.add("--reverse");
238            }
239            cmd.add(pid.toString());
240            process = ProcessUtils.runCmdAsync(cmd);
241
242            // set response and set refresh header to output location
243            setResponseHeader(resp);
244            resp.setStatus(HttpServletResponse.SC_ACCEPTED);
245            String relativeUrl = "/prof-output-hbase/" + outputFile.getName();
246            resp.getWriter()
247              .write("Started [" + event.getInternalName()
248                + "] profiling. This page will automatically redirect to " + relativeUrl + " after "
249                + duration + " seconds.\n\nCommand:\n" + Joiner.on(" ").join(cmd));
250
251            // to avoid auto-refresh by ProfileOutputServlet, refreshDelay can be specified
252            // via url param
253            int refreshDelay = getInteger(req, "refreshDelay", 0);
254
255            // instead of sending redirect, set auto-refresh so that browsers will refresh
256            // with redirected url
257            resp.setHeader("Refresh", (duration + refreshDelay) + ";" + relativeUrl);
258            resp.getWriter().flush();
259          } finally {
260            profilerLock.unlock();
261          }
262        } else {
263          setResponseHeader(resp);
264          resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
265          resp.getWriter()
266            .write("Unable to acquire lock. Another instance of profiler might be running.");
267          LOG.warn("Unable to acquire lock in " + lockTimeoutSecs
268            + " seconds. Another instance of profiler might be running.");
269        }
270      } catch (InterruptedException e) {
271        LOG.warn("Interrupted while acquiring profile lock.", e);
272        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
273      }
274    } else {
275      setResponseHeader(resp);
276      resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
277      resp.getWriter().write("Another instance of profiler is already running.");
278    }
279  }
280
281  private Integer getInteger(final HttpServletRequest req, final String param,
282    final Integer defaultValue) {
283    final String value = req.getParameter(param);
284    if (value != null) {
285      try {
286        return Integer.valueOf(value);
287      } catch (NumberFormatException e) {
288        return defaultValue;
289      }
290    }
291    return defaultValue;
292  }
293
294  private Long getLong(final HttpServletRequest req, final String param) {
295    final String value = req.getParameter(param);
296    if (value != null) {
297      try {
298        return Long.valueOf(value);
299      } catch (NumberFormatException e) {
300        return null;
301      }
302    }
303    return null;
304  }
305
306  private Double getMinWidth(final HttpServletRequest req) {
307    final String value = req.getParameter("minwidth");
308    if (value != null) {
309      try {
310        return Double.valueOf(value);
311      } catch (NumberFormatException e) {
312        return null;
313      }
314    }
315    return null;
316  }
317
318  private Event getEvent(final HttpServletRequest req) {
319    final String eventArg = req.getParameter("event");
320    if (eventArg != null) {
321      Event event = Event.fromInternalName(eventArg);
322      return event == null ? Event.CPU : event;
323    }
324    return Event.CPU;
325  }
326
327  private Output getOutput(final HttpServletRequest req) {
328    final String outputArg = req.getParameter("output");
329    if (req.getParameter("output") != null) {
330      try {
331        return Output.valueOf(outputArg.trim().toUpperCase());
332      } catch (IllegalArgumentException e) {
333        return Output.HTML;
334      }
335    }
336    return Output.HTML;
337  }
338
339  static void setResponseHeader(final HttpServletResponse response) {
340    response.setHeader(ACCESS_CONTROL_ALLOW_METHODS, ALLOWED_METHODS);
341    response.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
342    response.setContentType(CONTENT_TYPE_TEXT);
343  }
344
345  static String getAsyncProfilerHome() {
346    String asyncProfilerHome = System.getenv(ASYNC_PROFILER_HOME_ENV);
347    // if ENV is not set, see if -Dasync.profiler.home=/path/to/async/profiler/home is set
348    if (asyncProfilerHome == null || asyncProfilerHome.trim().isEmpty()) {
349      asyncProfilerHome = System.getProperty(ASYNC_PROFILER_HOME_SYSTEM_PROPERTY);
350    }
351
352    return asyncProfilerHome;
353  }
354
355  public static class DisabledServlet extends HttpServlet {
356
357    private static final long serialVersionUID = 1L;
358
359    @Override
360    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
361      throws IOException {
362      resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
363      setResponseHeader(resp);
364      resp.getWriter()
365        .write("The profiler servlet was disabled at startup.\n\n"
366          + "Please ensure the prerequisites for the Profiler Servlet have been installed and the\n"
367          + "environment is properly configured. For more information please see\n"
368          + "http://hbase.apache.org/book.html#profiler\n");
369      return;
370    }
371
372  }
373
374}