Apache Mesos
memory_test_helper.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __MEMORY_TEST_HELPER_HPP__
18 #define __MEMORY_TEST_HELPER_HPP__
19 
20 #include <process/subprocess.hpp>
21 
22 #include <stout/bytes.hpp>
23 #include <stout/option.hpp>
24 #include <stout/subcommand.hpp>
25 #include <stout/try.hpp>
26 
27 namespace mesos {
28 namespace internal {
29 namespace tests {
30 
31 // The abstraction for controlling the memory usage of a subprocess.
32 // TODO(chzhcn): Currently, this helper is only supposed to be used by
33 // one thread. Consider making it thread safe.
35 {
36 public:
37  static const char NAME[];
38 
40  ~MemoryTestHelper() override;
41 
42  // Spawns a subprocess.
43  // TODO(chzhcn): Consider returning a future instead of blocking.
45 
46  // Kill and reap the subprocess if exists.
47  // TODO(chzhcn): Consider returning a future instead of blocking.
48  void cleanup();
49 
50  // Returns the pid of the subprocess.
51  Try<pid_t> pid();
52 
53  // Allocate and lock specified page-aligned anonymous memory (RSS)
54  // in the subprocess. It uses mlock and memset to make sure
55  // allocated memory is mapped.
56  // TODO(chzhcn): Consider returning a future instead of blocking.
58 
59  // This function attempts to generate requested size of page cache
60  // in the subprocess by using a small buffer and writing it to disk
61  // multiple times.
62  // TODO(chzhcn): Consider returning a future instead of blocking.
64 
65 protected:
66  // The main function of the subprocess. It runs in a loop and
67  // executes commands passed from stdin.
68  int execute() override;
69 
70 private:
71  Try<Nothing> requestAndWait(const std::string& request);
72 
74 };
75 
76 } // namespace tests {
77 } // namespace internal {
78 } // namespace mesos {
79 
80 #endif // __MEMORY_TEST_HELPER_HPP__
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:130
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: check.hpp:33
static const char NAME[]
Definition: memory_test_helper.hpp:37
constexpr Bytes Megabytes(uint64_t value)
Definition: bytes.hpp:123
MemoryTestHelper()
Definition: memory_test_helper.hpp:39
Definition: subcommand.hpp:41
Definition: memory_test_helper.hpp:34
Definition: agent.hpp:25
Definition: attributes.hpp:24
Definition: bytes.hpp:30
Try< Nothing > increasePageCache(const Bytes &size=Megabytes(1))
Try< Nothing > increaseRSS(const Bytes &size)