parameters: (self)
docstring: Monitors CPU usage of containers.
  
      Otput is stored in self.output_path.
      Also, keeps track of minimum and maximum memory usage (for the machine).
docstring_summary: Monitors CPU usage of containers.
docstring_tokens: Monitors CPU usage of containers .
function: def _monitor(self):
      """Monitors CPU usage of containers.
  
      Otput is stored in self.output_path.
      Also, keeps track of minimum and maximum memory usage (for the machine).
      """
      # Ubuntu systems typically mount cpuacct cgroup in /sys/fs/cgroup/cpu,cpuacct,
      # but this can vary by OS distribution.
      all_cgroups = subprocess.check_output(
          "findmnt -n -o TARGET -t cgroup --source cgroup".split()
      ).split("\n")
      cpuacct_root = [c for c in all_cgroups if "cpuacct" in c][0]
      memory_root = [c for c in all_cgroups if "memory" in c][0]
      logging.info("Using cgroups: cpuacct %s, memory %s", cpuacct_root, memory_root)
      self.min_memory_usage_gb = None
      self.max_memory_usage_gb = None
  
      with file(self.output_path, "w") as output:
        while self.keep_monitoring:
          # Use a single timestamp for a given round of monitoring.
          now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
          for c in self.containers:
            cpu = self._metrics_from_stat_file(cpuacct_root, c, "cpuacct.stat")
            memory = self._metrics_from_stat_file(memory_root, c, "memory.stat")
            if cpu:
              output.write("%s %s cpu %s\n" % (now, c.id, cpu))
            if memory:
              output.write("%s %s memory %s\n" % (now, c.id, memory))
          output.flush()
  
          # Machine-wide memory usage
          m = used_memory()
          if self.min_memory_usage_gb is None:
            self.min_memory_usage_gb, self.max_memory_usage_gb = m, m
          else:
            self.min_memory_usage_gb = min(self.min_memory_usage_gb, m)
            self.max_memory_usage_gb = max(self.max_memory_usage_gb, m)
          time.sleep(self.frequency_seconds)
function_tokens: def _monitor ( self ) : # Ubuntu systems typically mount cpuacct cgroup in /sys/fs/cgroup/cpu,cpuacct, # but this can vary by OS distribution. all_cgroups = subprocess . check_output ( "findmnt -n -o TARGET -t cgroup --source cgroup" . split ( ) ) . split ( "\n" ) cpuacct_root = [ c for c in all_cgroups if "cpuacct" in c ] [ 0 ] memory_root = [ c for c in all_cgroups if "memory" in c ] [ 0 ] logging . info ( "Using cgroups: cpuacct %s, memory %s" , cpuacct_root , memory_root ) self . min_memory_usage_gb = None self . max_memory_usage_gb = None with file ( self . output_path , "w" ) as output : while self . keep_monitoring : # Use a single timestamp for a given round of monitoring. now = datetime . datetime . now ( ) . strftime ( "%Y-%m-%d %H:%M:%S.%f" ) for c in self . containers : cpu = self . _metrics_from_stat_file ( cpuacct_root , c , "cpuacct.stat" ) memory = self . _metrics_from_stat_file ( memory_root , c , "memory.stat" ) if cpu : output . write ( "%s %s cpu %s\n" % ( now , c . id , cpu ) ) if memory : output . write ( "%s %s memory %s\n" % ( now , c . id , memory ) ) output . flush ( ) # Machine-wide memory usage m = used_memory ( ) if self . min_memory_usage_gb is None : self . min_memory_usage_gb , self . max_memory_usage_gb = m , m else : self . min_memory_usage_gb = min ( self . min_memory_usage_gb , m ) self . max_memory_usage_gb = max ( self . max_memory_usage_gb , m ) time . sleep ( self . frequency_seconds )