Apache Mesos
memory_profiler.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License
12 
13 #ifndef __PROCESS_MEMORY_PROFILER_HPP__
14 #define __PROCESS_MEMORY_PROFILER_HPP__
15 
16 #include <functional>
17 #include <string>
18 
19 #include <process/future.hpp>
20 #include <process/http.hpp>
21 #include <process/process.hpp>
22 
23 #include <stout/nothing.hpp>
24 #include <stout/option.hpp>
25 #include <stout/path.hpp>
26 #include <stout/try.hpp>
27 
28 namespace process {
29 
30 // This class provides support for memory profiling and introspection
31 // using the capabilities of the jemalloc memory allocator.
32 //
33 // For user-facing documentation on how to use the facilities provided
34 // by this class, see `docs/memory-profiling.md` in the mesos repository.
35 //
36 // For more details about the implementation, see the comments
37 // in `memory_profiler.cpp`.
38 class MemoryProfiler : public Process<MemoryProfiler>
39 {
40 public:
41  MemoryProfiler(const Option<std::string>& authenticationRealm);
42  ~MemoryProfiler() override {}
43 
44 protected:
45  void initialize() override;
46 
47 private:
48  static const std::string START_HELP();
49  static const std::string STOP_HELP();
50  static const std::string DOWNLOAD_RAW_HELP();
51  static const std::string DOWNLOAD_TEXT_HELP();
52  static const std::string DOWNLOAD_GRAPH_HELP();
53  static const std::string STATISTICS_HELP();
54  static const std::string STATE_HELP();
55 
56  // HTTP endpoints.
57  // Refer to the `HELP()` messages for detailed documentation.
58 
59  // Starts memory profiling.
61  const http::Request& request,
63 
64  // Stops memory profiling and dumps collected data.
66  const http::Request& request,
68 
69  // Returns a raw heap profile.
70  Future<http::Response> downloadRawProfile(
71  const http::Request& request,
73 
74  // Generates and returns a symbolized heap profile.
75  Future<http::Response> downloadSymbolizedProfile(
76  const http::Request& request,
78 
79  // Generates and returns a call graph in svg format.
80  Future<http::Response> downloadGraphProfile(
81  const http::Request& request,
83 
84  // Shows memory allocation statistics.
85  Future<http::Response> statistics(
86  const http::Request& request,
88 
89  // Shows the configuration of the memory-profiler process.
91  const http::Request& request,
93 
94  // Internal functions, helper classes, and data members.
95 
96  // Deactivates data collection and attempts to dump the raw profile to disk.
97  void stopAndGenerateRawProfile();
98 
99  // The authentication realm that the profiler's HTTP endpoints will be
100  // installed into.
101  Option<std::string> authenticationRealm;
102 
103  // Stores information about the current profiling run.
104  // When the timer reaches zero, a dump of the collected
105  // data will be attempted.
106  class ProfilingRun
107  {
108  public:
109  ProfilingRun(MemoryProfiler*, time_t, const Duration&);
110  void extend(MemoryProfiler*, const Duration&);
111 
112  time_t id;
113  Timer timer;
114  };
115 
116  Option<ProfilingRun> currentRun;
117 
118  // Represents a file on the filesystem that is generated as the result
119  // of running some action.
120  class DiskArtifact
121  {
122  public:
123  static Try<DiskArtifact> create(
124  const std::string& filename,
125  time_t timestamp,
126  std::function<Try<Nothing>(const std::string& outputPath)> generator);
127 
128  const time_t getId() const;
129 
130  std::string getPath() const;
131 
132  // Generates an error response if the file doesn't exist, or a download
133  // if it does.
134  http::Response asHttp() const;
135 
136  private:
137  DiskArtifact(const std::string& path, time_t id);
138 
139  std::string path;
140  time_t id;
141  };
142 
143  // This profile is obtained by telling jemalloc to dump its stats to a file.
144  Try<DiskArtifact> rawProfile = Error("Not yet generated");
145 
146  // These profiles are obtained by running `jeprof` on the `raw` profile.
147  Try<DiskArtifact> symbolizedProfile = Error("Not yet generated");
148  Try<DiskArtifact> graphProfile = Error("Not yet generated");
149 };
150 
151 } // namespace process {
152 
153 #endif // __PROCESS_MEMORY_PROFILER_HPP__
Definition: path.hpp:29
Definition: errorbase.hpp:36
Future< Response > request(const Request &request, bool streamedResponse=false)
Asynchronously sends an HTTP request to the process and returns the HTTP response once the entire res...
Definition: uuid.hpp:33
MemoryProfiler(const Option< std::string > &authenticationRealm)
Definition: duration.hpp:32
Definition: http.hpp:533
Definition: memory_profiler.hpp:38
Definition: timer.hpp:30
Definition: executor.hpp:48
Definition: http.hpp:612
Try< Nothing > create(const std::string &hierarchy, const std::string &cgroup, bool recursive=false)
~MemoryProfiler() override
Definition: memory_profiler.hpp:42
void initialize() override
Invoked when a process gets spawned.
Definition: process.hpp:505