| 59 | |
| 60 | |
| 61 | Future<http::Response> Profiler::start( |
| 62 | const http::Request& request, |
| 63 | const Option<http::authentication::Principal>&) |
| 64 | { |
| 65 | #ifdef ENABLE_GPERFTOOLS |
| 66 | const Option<std::string> |
| 67 | enableProfiler = os::getenv("LIBPROCESS_ENABLE_PROFILER"); |
| 68 | if (enableProfiler.isNone() || enableProfiler.get() != "1") { |
| 69 | return http::BadRequest( |
| 70 | "The profiler is not enabled. To enable the profiler, libprocess " |
| 71 | "must be started with LIBPROCESS_ENABLE_PROFILER=1 in the " |
| 72 | "environment.\n"); |
| 73 | } |
| 74 | |
| 75 | if (started) { |
| 76 | return http::BadRequest("Profiler already started.\n"); |
| 77 | } |
| 78 | |
| 79 | LOG(INFO) << "Starting Profiler"; |
| 80 | |
| 81 | // WARNING: If using libunwind < 1.0.1, profiling should not be used, as |
| 82 | // there are reports of crashes. |
| 83 | // WARNING: If using libunwind 1.0.1, profiling should not be turned on |
| 84 | // when it's possible for new threads to be created. |
| 85 | // This may cause a deadlock. The workaround used in libprocess is described |
| 86 | // here: |
| 87 | // https://groups.google.com/d/topic/google-perftools/Df10Uy4Djrg/discussion |
| 88 | // NOTE: We have not tested this with libunwind > 1.0.1. |
| 89 | if (!ProfilerStart(PROFILE_FILE)) { |
| 90 | Try<std::string> error = |
| 91 | strings::format("Failed to start profiler: %s", os::strerror(errno)); |
| 92 | LOG(ERROR) << error.get(); |
| 93 | return http::InternalServerError(error.get()); |
| 94 | } |
| 95 | |
| 96 | started = true; |
| 97 | return http::OK("Profiler started.\n"); |
| 98 | #else |
| 99 | return http::BadRequest( |
| 100 | "Perftools is disabled. To enable perftools, " |
| 101 | "configure libprocess with --enable-perftools.\n"); |
| 102 | #endif |
| 103 | } |
| 104 | |
| 105 | |
| 106 | Future<http::Response> Profiler::stop( |