You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by al...@apache.org on 2018/04/18 04:22:05 UTC

[12/12] mesos git commit: Cleaned up method ordering in memory profiler.

Cleaned up method ordering in memory profiler.


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/963a289c
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/963a289c
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/963a289c

Branch: refs/heads/master
Commit: 963a289c6b3bfcec420092105c1d39837a261bbc
Parents: f2cb907
Author: Alexander Rukletsov <al...@apache.org>
Authored: Wed Apr 18 01:35:40 2018 +0200
Committer: Alexander Rukletsov <al...@apache.org>
Committed: Wed Apr 18 06:20:29 2018 +0200

----------------------------------------------------------------------
 3rdparty/libprocess/src/memory_profiler.cpp | 204 +++++++++++------------
 3rdparty/libprocess/src/memory_profiler.hpp |  28 ++--
 2 files changed, 113 insertions(+), 119 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/963a289c/3rdparty/libprocess/src/memory_profiler.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/memory_profiler.cpp b/3rdparty/libprocess/src/memory_profiler.cpp
index 2b3af3d..1991bbe 100644
--- a/3rdparty/libprocess/src/memory_profiler.cpp
+++ b/3rdparty/libprocess/src/memory_profiler.cpp
@@ -545,25 +545,16 @@ void MemoryProfiler::initialize()
         DOWNLOAD_GRAPH_HELP(),
         &MemoryProfiler::downloadGraph);
 
-  route("/state",
-        authenticationRealm,
-        STATE_HELP(),
-        &MemoryProfiler::state);
-
   route("/statistics",
         authenticationRealm,
         STATISTICS_HELP(),
         &MemoryProfiler::statistics);
-}
-
 
-MemoryProfiler::MemoryProfiler(const Option<string>& _authenticationRealm)
-  : ProcessBase("memory-profiler"),
-    authenticationRealm(_authenticationRealm),
-    jemallocRawProfile(RAW_PROFILE_FILENAME),
-    jeprofSymbolizedProfile(SYMBOLIZED_PROFILE_FILENAME),
-    jeprofGraph(GRAPH_FILENAME)
-{}
+  route("/state",
+        authenticationRealm,
+        STATE_HELP(),
+        &MemoryProfiler::state);
+}
 
 
 MemoryProfiler::ProfilingRun::ProfilingRun(
@@ -670,6 +661,14 @@ Try<Nothing> MemoryProfiler::DiskArtifact::generate(
 }
 
 
+MemoryProfiler::MemoryProfiler(const Option<string>& _authenticationRealm)
+  : ProcessBase("memory-profiler"),
+    authenticationRealm(_authenticationRealm),
+    jemallocRawProfile(RAW_PROFILE_FILENAME),
+    jeprofSymbolizedProfile(SYMBOLIZED_PROFILE_FILENAME),
+    jeprofGraph(GRAPH_FILENAME)
+{}
+
 
 // TODO(bevers): Add a query parameter to select json or html format.
 // TODO(bevers): Add a query parameter to configure the sampling interval.
@@ -802,79 +801,6 @@ Future<http::Response> MemoryProfiler::stop(
 }
 
 
-void MemoryProfiler::stopAndGenerateRawProfile()
-{
-  ASSERT(detectJemalloc());
-
-  VLOG(1) << "Attempting to stop current profiling run";
-
-  // If there is no current profiling run, there is nothing to do.
-  if (!currentRun.isSome()) {
-    return;
-  }
-
-  Try<bool> stopped = jemalloc::stopProfiling();
-
-  if (stopped.isError()) {
-    LOG(WARNING) << "Failed to stop memory profiling: " << stopped.error();
-
-    // Don't give up. Probably it will fail again in the future, but at least
-    // the problem will be clearly visible in the logs.
-    currentRun->extend(this, Seconds(5));
-
-    return;
-  }
-
-  // Heap profiling should not be active any more.
-  // We won't retry stopping and generating a profile after this point:
-  // We're not actively sampling any more, and if the user still cares
-  // about this profile they will get the data with the next run.
-  Try<bool> stillActive = jemalloc::profilingActive();
-  CHECK(stillActive.isError() || !stillActive.get());
-
-  time_t runId = currentRun->id;
-  Clock::cancel(currentRun->timer);
-  currentRun = None();
-
-  if (!stopped.get()) {
-    // This is a weird state to end up in, apparently something else in this
-    // process stopped profiling independently of us.
-    // If there was some valuable, un-dumped data it is still possible to get
-    // it by starting a new run.
-    LOG(WARNING)
-      << "Memory profiling unexpectedly inactive; not dumping profile. Ensure"
-      << " nothing else is interfacing with jemalloc in this process";
-    return;
-  }
-
-  Try<Nothing> generated = jemallocRawProfile.generate(
-      runId,
-      [this](const string& outputPath) -> Try<Nothing> {
-        // Make sure we actually have permissions to write to the file and that
-        // there is at least a little bit space left on the device.
-        const string data(DUMMY_FILE_SIZE, '\0');
-        Try<Nothing> written = os::write(outputPath, data);
-        if (written.isError()) {
-          return Error(written.error());
-        }
-
-        // Verify independently that the file was actually written.
-        Try<Bytes> size = os::stat::size(outputPath);
-        if (size.isError() || size.get() != DUMMY_FILE_SIZE) {
-          return Error(strings::format(
-              "Couldn't verify integrity of dump file %s", outputPath).get());
-        }
-
-        // Finally, do the real dump.
-        return jemalloc::dump(outputPath);
-      });
-
-  if (generated.isError()) {
-    LOG(WARNING) << "Could not dump profile: " + generated.error();
-  }
-}
-
-
 Future<http::Response> MemoryProfiler::downloadRaw(
     const http::Request& request,
     const Option<http::authentication::Principal>&)
@@ -908,7 +834,7 @@ Future<http::Response> MemoryProfiler::downloadRaw(
 }
 
 
-Future<http::Response> MemoryProfiler::downloadGraph(
+Future<http::Response> MemoryProfiler::downloadTextProfile(
     const http::Request& request,
     const Option<http::authentication::Principal>&)
 {
@@ -938,17 +864,19 @@ Future<http::Response> MemoryProfiler::downloadGraph(
     requestedId = rawId;
   }
 
-  // Generate the graph with the given id, or return the cached file on disk.
-  Try<Nothing> result = jeprofGraph.generate(
-      rawId,
-      [&](const string& outputPath) -> Try<Nothing> {
+  // Generate the profile with the given timestamp, or return the cached file
+  // on disk.
+  Try<Nothing> result = jeprofSymbolizedProfile.generate(
+      requestedId.get(),
+      [&](const string& outputPath) -> Try<Nothing>
+      {
         if (!(requestedId.get() == jemallocRawProfile.id().get())) {
           return Error("Requested version cannot be served");
         }
 
         return generateJeprofFile(
             jemallocRawProfile.path(),
-            "--svg",
+            "--text",
             outputPath);
       });
 
@@ -957,11 +885,11 @@ Future<http::Response> MemoryProfiler::downloadGraph(
         "Could not generate file: " + result.error() + ".\n");
   }
 
-  return jeprofGraph.asHttp();
+  return jeprofSymbolizedProfile.asHttp();
 }
 
 
-Future<http::Response> MemoryProfiler::downloadTextProfile(
+Future<http::Response> MemoryProfiler::downloadGraph(
     const http::Request& request,
     const Option<http::authentication::Principal>&)
 {
@@ -991,19 +919,17 @@ Future<http::Response> MemoryProfiler::downloadTextProfile(
     requestedId = rawId;
   }
 
-  // Generate the profile with the given timestamp, or return the cached file
-  // on disk.
-  Try<Nothing> result = jeprofSymbolizedProfile.generate(
-      requestedId.get(),
-      [&](const string& outputPath) -> Try<Nothing>
-      {
+  // Generate the graph with the given id, or return the cached file on disk.
+  Try<Nothing> result = jeprofGraph.generate(
+      rawId,
+      [&](const string& outputPath) -> Try<Nothing> {
         if (!(requestedId.get() == jemallocRawProfile.id().get())) {
           return Error("Requested version cannot be served");
         }
 
         return generateJeprofFile(
             jemallocRawProfile.path(),
-            "--text",
+            "--svg",
             outputPath);
       });
 
@@ -1012,7 +938,7 @@ Future<http::Response> MemoryProfiler::downloadTextProfile(
         "Could not generate file: " + result.error() + ".\n");
   }
 
-  return jeprofSymbolizedProfile.asHttp();
+  return jeprofGraph.asHttp();
 }
 
 
@@ -1138,4 +1064,76 @@ Future<http::Response> MemoryProfiler::state(
 }
 
 
+void MemoryProfiler::stopAndGenerateRawProfile()
+{
+  ASSERT(detectJemalloc());
+
+  VLOG(1) << "Attempting to stop current profiling run";
+
+  // If there is no current profiling run, there is nothing to do.
+  if (!currentRun.isSome()) {
+    return;
+  }
+
+  Try<bool> stopped = jemalloc::stopProfiling();
+
+  if (stopped.isError()) {
+    LOG(WARNING) << "Failed to stop memory profiling: " << stopped.error();
+
+    // Don't give up. Probably it will fail again in the future, but at least
+    // the problem will be clearly visible in the logs.
+    currentRun->extend(this, Seconds(5));
+
+    return;
+  }
+
+  // Heap profiling should not be active any more.
+  // We won't retry stopping and generating a profile after this point:
+  // We're not actively sampling any more, and if the user still cares
+  // about this profile they will get the data with the next run.
+  Try<bool> stillActive = jemalloc::profilingActive();
+  CHECK(stillActive.isError() || !stillActive.get());
+
+  time_t runId = currentRun->id;
+  Clock::cancel(currentRun->timer);
+  currentRun = None();
+
+  if (!stopped.get()) {
+    // This is a weird state to end up in, apparently something else in this
+    // process stopped profiling independently of us.
+    // If there was some valuable, un-dumped data it is still possible to get
+    // it by starting a new run.
+    LOG(WARNING)
+      << "Memory profiling unexpectedly inactive; not dumping profile. Ensure"
+      << " nothing else is interfacing with jemalloc in this process";
+    return;
+  }
+
+  Try<Nothing> generated = jemallocRawProfile.generate(
+      runId,
+      [this](const string& outputPath) -> Try<Nothing> {
+        // Make sure we actually have permissions to write to the file and that
+        // there is at least a little bit space left on the device.
+        const string data(DUMMY_FILE_SIZE, '\0');
+        Try<Nothing> written = os::write(outputPath, data);
+        if (written.isError()) {
+          return Error(written.error());
+        }
+
+        // Verify independently that the file was actually written.
+        Try<Bytes> size = os::stat::size(outputPath);
+        if (size.isError() || size.get() != DUMMY_FILE_SIZE) {
+          return Error(strings::format(
+              "Couldn't verify integrity of dump file %s", outputPath).get());
+        }
+
+        // Finally, do the real dump.
+        return jemalloc::dump(outputPath);
+      });
+
+  if (generated.isError()) {
+    LOG(WARNING) << "Could not dump profile: " + generated.error();
+  }
+}
+
 } // namespace process {

http://git-wip-us.apache.org/repos/asf/mesos/blob/963a289c/3rdparty/libprocess/src/memory_profiler.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/memory_profiler.hpp b/3rdparty/libprocess/src/memory_profiler.hpp
index 5cbe507..dc98998 100644
--- a/3rdparty/libprocess/src/memory_profiler.hpp
+++ b/3rdparty/libprocess/src/memory_profiler.hpp
@@ -10,8 +10,8 @@
 // See the License for the specific language governing permissions and
 // limitations under the License
 
-#ifndef __PROCESS_MEMORYPROFILER_HPP__
-#define __PROCESS_MEMORYPROFILER_HPP__
+#ifndef __PROCESS_MEMORY_PROFILER_HPP__
+#define __PROCESS_MEMORY_PROFILER_HPP__
 
 #include <functional>
 #include <string>
@@ -25,18 +25,16 @@
 #include <stout/path.hpp>
 #include <stout/try.hpp>
 
-
 namespace process {
 
-// This class provides support for taking advantage of the introspection
-// and memory profiling capabilities of the jemalloc memory allocator.
+// This class provides support for memory profiling and introspection
+// using the capabilities of the jemalloc memory allocator.
 //
 // For user-facing documentation on how to use the facilities provided
 // by this class, see `docs/memory-profiling.md` in the mesos repository.
 //
 // For more details about the implementation, see the comments
 // in `memory_profiler.cpp`.
-
 class MemoryProfiler : public Process<MemoryProfiler>
 {
 public:
@@ -49,11 +47,11 @@ protected:
 private:
   static const std::string START_HELP();
   static const std::string STOP_HELP();
-  static const std::string STATISTICS_HELP();
-  static const std::string STATE_HELP();
   static const std::string DOWNLOAD_RAW_HELP();
   static const std::string DOWNLOAD_TEXT_HELP();
   static const std::string DOWNLOAD_GRAPH_HELP();
+  static const std::string STATISTICS_HELP();
+  static const std::string STATE_HELP();
 
   // HTTP endpoints.
   // Refer to the `HELP()` messages for detailed documentation.
@@ -73,13 +71,13 @@ private:
       const http::Request& request,
       const Option<http::authentication::Principal>&);
 
-  // Generates and returns a call graph in svg format.
-  Future<http::Response> downloadGraph(
+  // Generates and returns a symbolized heap profile.
+  Future<http::Response> downloadTextProfile(
       const http::Request& request,
       const Option<http::authentication::Principal>&);
 
-  // Generates and returns a symbolized heap profile.
-  Future<http::Response> downloadTextProfile(
+  // Generates and returns a call graph in svg format.
+  Future<http::Response> downloadGraph(
       const http::Request& request,
       const Option<http::authentication::Principal>&);
 
@@ -93,7 +91,7 @@ private:
     const http::Request& request,
     const Option<http::authentication::Principal>&);
 
-  // Internal functions and data members.
+  // Internal functions, helper classes, and data members.
 
   // Deactivates data collection and attempts to dump the raw profile to disk.
   void stopAndGenerateRawProfile();
@@ -150,12 +148,10 @@ private:
   };
 
   DiskArtifact jemallocRawProfile;
-
   DiskArtifact jeprofSymbolizedProfile;
-
   DiskArtifact jeprofGraph;
 };
 
 } // namespace process {
 
-#endif // __PROCESS_MEMORYPROFILER_HPP__
+#endif // __PROCESS_MEMORY_PROFILER_HPP__