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