Monitors CPU usage of containers. Otput is stored in self.output_path. Also, keeps track of minimum and maximum memory usage (for the machine).
(self)
| 171 | return None |
| 172 | |
| 173 | def _monitor(self): |
| 174 | """Monitors CPU usage of containers. |
| 175 | |
| 176 | Otput is stored in self.output_path. |
| 177 | Also, keeps track of minimum and maximum memory usage (for the machine). |
| 178 | """ |
| 179 | # Ubuntu systems typically mount cpuacct cgroup in /sys/fs/cgroup/cpu,cpuacct, |
| 180 | # but this can vary by OS distribution. |
| 181 | all_cgroups = subprocess.check_output( |
| 182 | "findmnt -n -o TARGET -t cgroup --source cgroup".split(), universal_newlines=True |
| 183 | ).split("\n") |
| 184 | cpuacct_root = [c for c in all_cgroups if "cpuacct" in c][0] |
| 185 | memory_root = [c for c in all_cgroups if "memory" in c][0] |
| 186 | logging.info("Using cgroups: cpuacct %s, memory %s", cpuacct_root, memory_root) |
| 187 | self.min_memory_usage_gb = None |
| 188 | self.max_memory_usage_gb = None |
| 189 | |
| 190 | with open(self.output_path, "w") as output: |
| 191 | while self.keep_monitoring: |
| 192 | # Use a single timestamp for a given round of monitoring. |
| 193 | now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f") |
| 194 | for c in self.containers: |
| 195 | cpu = self._metrics_from_stat_file(cpuacct_root, c, "cpuacct.stat") |
| 196 | memory = self._metrics_from_stat_file(memory_root, c, "memory.stat") |
| 197 | if cpu: |
| 198 | output.write("%s %s cpu %s\n" % (now, c.id, cpu)) |
| 199 | if memory: |
| 200 | output.write("%s %s memory %s\n" % (now, c.id, memory)) |
| 201 | output.flush() |
| 202 | |
| 203 | # Machine-wide memory usage |
| 204 | m = used_memory() |
| 205 | if self.min_memory_usage_gb is None: |
| 206 | self.min_memory_usage_gb, self.max_memory_usage_gb = m, m |
| 207 | else: |
| 208 | self.min_memory_usage_gb = min(self.min_memory_usage_gb, m) |
| 209 | self.max_memory_usage_gb = max(self.max_memory_usage_gb, m) |
| 210 | time.sleep(self.frequency_seconds) |
| 211 | |
| 212 | |
| 213 | class Timeline(object): |
nothing calls this directly
no test coverage detected