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    WALL("wall"),
078    ALLOC("alloc"),
079    LOCK("lock"),
080    PAGE_FAULTS("page-faults"),
081    CONTEXT_SWITCHES("context-switches"),
082    CYCLES("cycles"),
083    INSTRUCTIONS("instructions"),
084    CACHE_REFERENCES("cache-references"),
085    CACHE_MISSES("cache-misses"),
086    BRANCHES("branches"),
087    BRANCH_MISSES("branch-misses"),
088    BUS_CYCLES("bus-cycles"),
089    L1_DCACHE_LOAD_MISSES("L1-dcache-load-misses"),
090    LLC_LOAD_MISSES("LLC-load-misses"),
091    DTLB_LOAD_MISSES("dTLB-load-misses"),
092    MEM_BREAKPOINT("mem:breakpoint"),
093    TRACE_TRACEPOINT("trace:tracepoint"),;
094
095    private final String internalName;
096
097    Event(final String internalName) {
098      this.internalName = internalName;
099    }
100
101    public String getInternalName() {
102      return internalName;
103    }
104
105    public static Event fromInternalName(final String name) {
106      for (Event event : values()) {
107        if (event.getInternalName().equalsIgnoreCase(name)) {
108          return event;
109        }
110      }
111
112      return null;
113    }
114  }
115
116  enum Output {
117    SUMMARY,
118    TRACES,
119    FLAT,
120    COLLAPSED,
121    // No SVG in 2.x asyncprofiler.
122    SVG,
123    TREE,
124    JFR,
125    // In 2.x asyncprofiler, this is how you get flamegraphs.
126    HTML
127  }
128
129  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "SE_TRANSIENT_FIELD_NOT_RESTORED",
130      justification = "This class is never serialized nor restored.")
131  private transient Lock profilerLock = new ReentrantLock();
132  private transient volatile Process process;
133  private String asyncProfilerHome;
134  private Integer pid;
135
136  public ProfileServlet() {
137    this.asyncProfilerHome = getAsyncProfilerHome();
138    this.pid = ProcessUtils.getPid();
139    LOG.info("Servlet process PID: " + pid + " asyncProfilerHome: " + asyncProfilerHome);
140  }
141
142  @Override
143  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
144    throws IOException {
145    if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(), req, resp)) {
146      resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
147      setResponseHeader(resp);
148      resp.getWriter().write("Unauthorized: Instrumentation access is not allowed!");
149      return;
150    }
151
152    // make sure async profiler home is set
153    if (asyncProfilerHome == null || asyncProfilerHome.trim().isEmpty()) {
154      resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
155      setResponseHeader(resp);
156      resp.getWriter()
157        .write("ASYNC_PROFILER_HOME env is not set.\n\n"
158          + "Please ensure the prerequsites for the Profiler Servlet have been installed and the\n"
159          + "environment is properly configured. For more information please see\n"
160          + "http://hbase.apache.org/book.html#profiler\n");
161      return;
162    }
163
164    // if pid is explicitly specified, use it else default to current process
165    pid = getInteger(req, "pid", pid);
166
167    // if pid is not specified in query param and if current process pid cannot be determined
168    if (pid == null) {
169      resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
170      setResponseHeader(resp);
171      resp.getWriter()
172        .write("'pid' query parameter unspecified or unable to determine PID of current process.");
173      return;
174    }
175
176    final int duration = getInteger(req, "duration", DEFAULT_DURATION_SECONDS);
177    final Output output = getOutput(req);
178    final Event event = getEvent(req);
179    final Long interval = getLong(req, "interval");
180    final Integer jstackDepth = getInteger(req, "jstackdepth", null);
181    final Long bufsize = getLong(req, "bufsize");
182    final boolean thread = req.getParameterMap().containsKey("thread");
183    final boolean simple = req.getParameterMap().containsKey("simple");
184    final Integer width = getInteger(req, "width", null);
185    final Integer height = getInteger(req, "height", null);
186    final Double minwidth = getMinWidth(req);
187    final boolean reverse = req.getParameterMap().containsKey("reverse");
188
189    if (process == null || !process.isAlive()) {
190      try {
191        int lockTimeoutSecs = 3;
192        if (profilerLock.tryLock(lockTimeoutSecs, TimeUnit.SECONDS)) {
193          try {
194            File outputFile =
195              new File(OUTPUT_DIR, "async-prof-pid-" + pid + "-" + event.name().toLowerCase() + "-"
196                + ID_GEN.incrementAndGet() + "." + output.name().toLowerCase());
197            List<String> cmd = new ArrayList<>();
198            cmd.add(asyncProfilerHome + PROFILER_SCRIPT);
199            cmd.add("-e");
200            cmd.add(event.getInternalName());
201            cmd.add("-d");
202            cmd.add("" + duration);
203            cmd.add("-o");
204            cmd.add(output.name().toLowerCase());
205            cmd.add("-f");
206            cmd.add(outputFile.getAbsolutePath());
207            if (interval != null) {
208              cmd.add("-i");
209              cmd.add(interval.toString());
210            }
211            if (jstackDepth != null) {
212              cmd.add("-j");
213              cmd.add(jstackDepth.toString());
214            }
215            if (bufsize != null) {
216              cmd.add("-b");
217              cmd.add(bufsize.toString());
218            }
219            if (thread) {
220              cmd.add("-t");
221            }
222            if (simple) {
223              cmd.add("-s");
224            }
225            if (width != null) {
226              cmd.add("--width");
227              cmd.add(width.toString());
228            }
229            if (height != null) {
230              cmd.add("--height");
231              cmd.add(height.toString());
232            }
233            if (minwidth != null) {
234              cmd.add("--minwidth");
235              cmd.add(minwidth.toString());
236            }
237            if (reverse) {
238              cmd.add("--reverse");
239            }
240            cmd.add(pid.toString());
241            process = ProcessUtils.runCmdAsync(cmd);
242
243            // set response and set refresh header to output location
244            setResponseHeader(resp);
245            resp.setStatus(HttpServletResponse.SC_ACCEPTED);
246            String relativeUrl = "/prof-output-hbase/" + outputFile.getName();
247            resp.getWriter()
248              .write("Started [" + event.getInternalName()
249                + "] profiling. This page will automatically redirect to " + relativeUrl + " after "
250                + duration + " seconds. "
251                + "If empty diagram and Linux 4.6+, see 'Basic Usage' section on the Async "
252                + "Profiler Home Page, https://github.com/jvm-profiling-tools/async-profiler."
253                + "\n\nCommand:\n" + Joiner.on(" ").join(cmd));
254
255            // to avoid auto-refresh by ProfileOutputServlet, refreshDelay can be specified
256            // via url param
257            int refreshDelay = getInteger(req, "refreshDelay", 0);
258
259            // instead of sending redirect, set auto-refresh so that browsers will refresh
260            // with redirected url
261            resp.setHeader("Refresh", (duration + refreshDelay) + ";" + relativeUrl);
262            resp.getWriter().flush();
263          } finally {
264            profilerLock.unlock();
265          }
266        } else {
267          setResponseHeader(resp);
268          resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
269          resp.getWriter()
270            .write("Unable to acquire lock. Another instance of profiler might be running.");
271          LOG.warn("Unable to acquire lock in " + lockTimeoutSecs
272            + " seconds. Another instance of profiler might be running.");
273        }
274      } catch (InterruptedException e) {
275        LOG.warn("Interrupted while acquiring profile lock.", e);
276        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
277      }
278    } else {
279      setResponseHeader(resp);
280      resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
281      resp.getWriter().write("Another instance of profiler is already running.");
282    }
283  }
284
285  private Integer getInteger(final HttpServletRequest req, final String param,
286    final Integer defaultValue) {
287    final String value = req.getParameter(param);
288    if (value != null) {
289      try {
290        return Integer.valueOf(value);
291      } catch (NumberFormatException e) {
292        return defaultValue;
293      }
294    }
295    return defaultValue;
296  }
297
298  private Long getLong(final HttpServletRequest req, final String param) {
299    final String value = req.getParameter(param);
300    if (value != null) {
301      try {
302        return Long.valueOf(value);
303      } catch (NumberFormatException e) {
304        return null;
305      }
306    }
307    return null;
308  }
309
310  private Double getMinWidth(final HttpServletRequest req) {
311    final String value = req.getParameter("minwidth");
312    if (value != null) {
313      try {
314        return Double.valueOf(value);
315      } catch (NumberFormatException e) {
316        return null;
317      }
318    }
319    return null;
320  }
321
322  private Event getEvent(final HttpServletRequest req) {
323    final String eventArg = req.getParameter("event");
324    if (eventArg != null) {
325      Event event = Event.fromInternalName(eventArg);
326      return event == null ? Event.CPU : event;
327    }
328    return Event.CPU;
329  }
330
331  private Output getOutput(final HttpServletRequest req) {
332    final String outputArg = req.getParameter("output");
333    if (req.getParameter("output") != null) {
334      try {
335        return Output.valueOf(outputArg.trim().toUpperCase());
336      } catch (IllegalArgumentException e) {
337        return Output.HTML;
338      }
339    }
340    return Output.HTML;
341  }
342
343  static void setResponseHeader(final HttpServletResponse response) {
344    response.setHeader(ACCESS_CONTROL_ALLOW_METHODS, ALLOWED_METHODS);
345    response.setHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
346    response.setContentType(CONTENT_TYPE_TEXT);
347  }
348
349  static String getAsyncProfilerHome() {
350    String asyncProfilerHome = System.getenv(ASYNC_PROFILER_HOME_ENV);
351    // if ENV is not set, see if -Dasync.profiler.home=/path/to/async/profiler/home is set
352    if (asyncProfilerHome == null || asyncProfilerHome.trim().isEmpty()) {
353      asyncProfilerHome = System.getProperty(ASYNC_PROFILER_HOME_SYSTEM_PROPERTY);
354    }
355
356    return asyncProfilerHome;
357  }
358
359  public static class DisabledServlet extends HttpServlet {
360
361    private static final long serialVersionUID = 1L;
362
363    @Override
364    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
365      throws IOException {
366      resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
367      setResponseHeader(resp);
368      resp.getWriter()
369        .write("The profiler servlet was disabled at startup.\n\n"
370          + "Please ensure the prerequisites for the Profiler Servlet have been installed and the\n"
371          + "environment is properly configured. For more information please see\n"
372          + "http://hbase.apache.org/book.html#profiler\n");
373      return;
374    }
375
376  }
377
378}