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