Apache Mesos
process.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 __STOUT_OS_PROCESS_HPP__
14 #define __STOUT_OS_PROCESS_HPP__
15 
16 #include <sys/types.h> // For pid_t.
17 
18 #include <list>
19 #include <ostream>
20 #include <sstream>
21 #include <string>
22 
23 #include <stout/bytes.hpp>
24 #include <stout/duration.hpp>
25 #include <stout/none.hpp>
26 #include <stout/option.hpp>
27 #include <stout/strings.hpp>
28 
29 
30 namespace os {
31 
32 struct Process
33 {
34  Process(pid_t _pid,
35  pid_t _parent,
36  pid_t _group,
37  const Option<pid_t>& _session,
38  const Option<Bytes>& _rss,
39  const Option<Duration>& _utime,
40  const Option<Duration>& _stime,
41  const std::string& _command,
42  bool _zombie)
43  : pid(_pid),
44  parent(_parent),
45  group(_group),
46  session(_session),
47  rss(_rss),
48  utime(_utime),
49  stime(_stime),
50  command(_command),
51  zombie(_zombie) {}
52 
53  const pid_t pid;
54  const pid_t parent;
55  const pid_t group;
60  const std::string command;
61  const bool zombie;
62 
63  // TODO(bmahler): Add additional data as needed.
64 
65  bool operator<(const Process& p) const { return pid < p.pid; }
66  bool operator<=(const Process& p) const { return pid <= p.pid; }
67  bool operator>(const Process& p) const { return pid > p.pid; }
68  bool operator>=(const Process& p) const { return pid >= p.pid; }
69  bool operator==(const Process& p) const { return pid == p.pid; }
70  bool operator!=(const Process& p) const { return pid != p.pid; }
71 };
72 
73 
75 {
76 public:
77  // Returns a process subtree rooted at the specified PID, or none if
78  // the specified pid could not be found in this process tree.
80  {
81  if (process.pid == pid) {
82  return *this;
83  }
84 
85  foreach (const ProcessTree& tree, children) {
86  Option<ProcessTree> option = tree.find(pid);
87  if (option.isSome()) {
88  return option;
89  }
90  }
91 
92  return None();
93  }
94 
95  // Checks if the specified pid is contained in this process tree.
96  bool contains(pid_t pid) const
97  {
98  return find(pid).isSome();
99  }
100 
101  operator Process() const
102  {
103  return process;
104  }
105 
106  operator pid_t() const
107  {
108  return process.pid;
109  }
110 
112  const std::list<ProcessTree> children;
113 
114 private:
115  friend struct Fork;
116  friend Try<ProcessTree> pstree(pid_t, const std::list<Process>&);
117 
118  ProcessTree(
119  const Process& _process,
120  const std::list<ProcessTree>& _children)
121  : process(_process),
122  children(_children) {}
123 };
124 
125 
126 inline std::ostream& operator<<(std::ostream& stream, const ProcessTree& tree)
127 {
128  if (tree.children.empty()) {
129  stream << "--- " << tree.process.pid << " ";
130  if (tree.process.zombie) {
131  stream << "(" << tree.process.command << ")";
132  } else {
133  stream << tree.process.command;
134  }
135  } else {
136  stream << "-+- " << tree.process.pid << " ";
137  if (tree.process.zombie) {
138  stream << "(" << tree.process.command << ")";
139  } else {
140  stream << tree.process.command;
141  }
142  size_t size = tree.children.size();
143  foreach (const ProcessTree& child, tree.children) {
144  std::ostringstream out;
145  out << child;
146  stream << "\n";
147  if (--size != 0) {
148  stream << " |" << strings::replace(out.str(), "\n", "\n |");
149  } else {
150  stream << " \\" << strings::replace(out.str(), "\n", "\n ");
151  }
152  }
153  }
154  return stream;
155 }
156 
157 } // namespace os {
158 
159 
160 // An overload of stringify for printing a list of process trees
161 // (since printing a process tree is rather particular).
162 inline std::string stringify(const std::list<os::ProcessTree>& list)
163 {
164  std::ostringstream out;
165  out << "[ " << std::endl;
166  std::list<os::ProcessTree>::const_iterator iterator = list.begin();
167  while (iterator != list.end()) {
168  out << stringify(*iterator);
169  if (++iterator != list.end()) {
170  out << std::endl << std::endl;
171  }
172  }
173  out << std::endl << "]";
174  return out.str();
175 }
176 
177 #endif // __STOUT_OS_PROCESS_HPP__
Definition: fork.hpp:113
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:130
Definition: check.hpp:33
const std::list< ProcessTree > children
Definition: process.hpp:112
bool contains(pid_t pid) const
Definition: process.hpp:96
const Process process
Definition: process.hpp:111
const pid_t group
Definition: process.hpp:55
bool operator!=(const Process &p) const
Definition: process.hpp:70
const Option< Bytes > rss
Definition: process.hpp:57
Definition: posix_signalhandler.hpp:23
const Option< pid_t > session
Definition: process.hpp:56
bool isSome() const
Definition: option.hpp:116
DWORD pid_t
Definition: windows.hpp:181
Definition: process.hpp:32
const pid_t pid
Definition: process.hpp:53
const bool zombie
Definition: process.hpp:61
bool operator>=(const Process &p) const
Definition: process.hpp:68
Option< ProcessTree > find(pid_t pid) const
Definition: process.hpp:79
const Option< Duration > utime
Definition: process.hpp:58
std::string stringify(const std::list< os::ProcessTree > &list)
Definition: process.hpp:162
std::string replace(const std::string &s, const std::string &from, const std::string &to)
Definition: strings.hpp:113
bool operator<(const Process &p) const
Definition: process.hpp:65
Try< std::vector< Entry > > list(const std::string &hierarchy, const std::string &cgroup)
bool operator<=(const Process &p) const
Definition: process.hpp:66
const pid_t parent
Definition: process.hpp:54
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
Definition: grp.hpp:26
Definition: none.hpp:27
const std::string command
Definition: process.hpp:60
bool operator>(const Process &p) const
Definition: process.hpp:67
std::set< pid_t > children(pid_t, const std::list< Process > &, bool)
Definition: os.hpp:217
Definition: executor.hpp:48
const Option< Duration > stime
Definition: process.hpp:59
Try< ProcessTree > pstree(pid_t pid, const std::list< Process > &processes)
Definition: pstree.hpp:37
std::ostream & operator<<(std::ostream &stream, const ProcessTree &tree)
Definition: process.hpp:126
bool operator==(const Process &p) const
Definition: process.hpp:69
Definition: process.hpp:74
Try< std::list< std::string > > find(const std::string &directory, const std::string &pattern)
Definition: find.hpp:37
Process(pid_t _pid, pid_t _parent, pid_t _group, const Option< pid_t > &_session, const Option< Bytes > &_rss, const Option< Duration > &_utime, const Option< Duration > &_stime, const std::string &_command, bool _zombie)
Definition: process.hpp:34