def get_cpus(container_name): """Get the set of cpus in a container.""" file_name = cpus_path(container_name) if os.path.exists(file_name): return rangelist_to_set(utils.read_one_line(file_name)) else: return set()
def get_mem_nodes(container_name): "Return mem nodes now available to a container, both exclusive & shared""" file_name = mems_path(container_name) if os.path.exists(file_name): return rangelist_to_set(utils.read_one_line(file_name)) else: return set()
def my_container_name(): """Get current process's inherited or self-built container name. The container is within /dev/cpuset or /dev/cgroup, or '' for root container.""" name = utils.read_one_line('/proc/%i/cpuset' % os.getpid()) return name[1:] # strip leading /
def get_boot_numa(): """Get boot-time numa=fake=xyz option for current boot. For example, numa=fake=nnn, numa=fake=nnnM, or nothing """ label = 'numa=fake=' for arg in utils.read_one_line('/proc/cmdline').split(): if arg.startswith(label): return arg[len(label):] return ''
def create_container_via_memcg(name, parent, bytes, cpus): # create container via direct memcg cgroup writes os.mkdir(full_path(name)) nodes = utils.read_one_line(mems_path(parent)) utils.write_one_line(mems_path(name), nodes) # inherit parent's nodes utils.write_one_line(memory_path(name)+'.limit_in_bytes', str(bytes)) utils.write_one_line(cpus_path(name), ','.join(map(str, cpus))) logging.debug('Created container %s directly via memcg,' ' has %d cpus and %s bytes', name, len(cpus), utils.human_format(container_bytes(name)))
def is_tracing(self): """Is tracing on? Returns: True if tracing enabled and at least one event is enabled. """ fname = os.path.join(self._TRACE_ROOT, 'tracing_on') result = int(utils.read_one_line(fname).strip()) if result == 1 and len(self._events) > 0: return True return False
def container_bytes(name): """Get the memory limit for a given container, in bytes.""" if fake_numa_containers: return nodes_avail_mbytes(get_mem_nodes(name)) << 20 else: while True: file = memory_path(name) + '.limit_in_bytes' limit = int(utils.read_one_line(file)) if limit < NO_LIMIT: return limit if name == SUPER_ROOT: return root_container_bytes name = os.path.dirname(name)
def _gather_vboot_times(self, results): """Read and report firmware internal timestamps. The firmware for all Chrome platforms except Mario records the ticks since power on at selected times during startup. These timestamps can be extracted from the `crossystem` command. If the timestamps are available, convert the tick times to seconds and record the following keyvals in `results`: * seconds_power_on_to_lf_start * seconds_power_on_to_lf_end * seconds_power_on_to_lk_start * seconds_power_on_to_lk_end The frequency of the recorded tick timestamps is determined by reading `_CPU_FREQ_FILE` and is recorded in the keyval mhz_primary_cpu. @param results Keyvals dictionary. """ try: khz = int(utils.read_one_line(self._CPU_FREQ_FILE)) except IOError: logging.info( 'Test is unable to read "%s", not calculating the ' 'vboot times.', self._CPU_FREQ_FILE) return hertz = khz * 1000.0 results['mhz_primary_cpu'] = khz / 1000.0 try: out = utils.system_output('crossystem') except error.CmdError: logging.info('Unable to run crossystem, not calculating the vboot ' 'times.') return # Parse the crossystem output, we are looking for vdat_timers items = out.splitlines() for item in items: times_re = re.compile(r'LF=(\d+),(\d+) LK=(\d+),(\d+)') match = re.findall(times_re, item) if match: times = map(lambda s: round(float(s) / hertz, 2), match[0]) results['seconds_power_on_to_lf_start'] = times[0] results['seconds_power_on_to_lf_end'] = times[1] results['seconds_power_on_to_lk_start'] = times[2] results['seconds_power_on_to_lk_end'] = times[3]
def my_container(): """Get current task's cgroup names, across all cgroup hierarchies.""" container = {} # maps cgroup-subsystems to mount-relative cgroup paths filename = '/proc/self/cgroup' if os.path.exists(filename): for hierarchy in open(filename).readlines(): # eg 'number:oom,blockio,net,cpuacct,cpu,cpuset:/sys' junknum, subsystems, cgroup_name = hierarchy.split(':') cgroup_name = cgroup_name[1:-1] # strip leading / and newline for subsystem in subsystems.split(','): container[subsystem] = cgroup_name else: filename = '/proc/self/cpuset' cgroup_name = utils.read_one_line(filename)[1:] # strip leading / container['cpuset'] = cgroup_name return container
def _onoff(self, val): """Enable/Disable tracing. Arguments: val: integer, 1 for on, 0 for off Raises: error.TestFail: If unable to enable/disable tracing boolean of tracing on/off status """ utils.write_one_line(self._TRACE_EN_PATH, val) fname = os.path.join(self._TRACE_ROOT, 'tracing_on') result = int(utils.read_one_line(fname).strip()) if not result == val: raise error.TestFail("Unable to %sable tracing" % 'en' if val == 1 else 'dis')
def flush(self): """Flush trace buffer. Raises: error.TestFail: If unable to flush """ self.off() fname = os.path.join(self._TRACE_ROOT, 'free_buffer') utils.write_one_line(fname, 1) self._buffer_ptr = 0 fname = os.path.join(self._TRACE_ROOT, 'buffer_size_kb') result = utils.read_one_line(fname).strip() if result is '0': return True return False
def my_container(): """Get current task's cgroup names, across all cgroup hierarchies.""" container = {} # maps cgroup-subsystems to mount-relative cgroup paths filename = '/proc/self/cgroup' if os.path.exists(filename): for hierarchy in open(filename).readlines(): # eg 'number:oom,blockio,net,cpuacct,cpu,cpuset:/sys' junknum, subsystems, cgroup_name = hierarchy.split(':') cgroup_name = cgroup_name[1:-1] # strip leading / and newline for subsystem in subsystems.split(','): container[subsystem] = cgroup_name else: filename = '/proc/self/cpuset' cgroup_name = utils.read_one_line(filename)[1:] # strip leading / container['cpuset'] = cgroup_name return container
def enable_blkio_and_cfq(device): """Enable blkio and cfq, when not done by boot command.""" # Ensure that the required device is valid block device. disk = os.path.join('/sys/block', device) if not os.path.exists(disk): raise error.Error('Machine does not have disk device ' + device) # Ensure the io cgroup is mounted. if not cgroup.mount_point(BLKIO_CGROUP_NAME): raise error.Error('Kernel not compiled with blkio support') # Enable cfq scheduling on the block device. file = os.path.join(disk, 'queue/scheduler') if '[cfq]' in utils.read_one_line(file): logging.debug('cfq scheduler is already enabled on drive %s', device) return logging.info('Enabling cfq scheduler on drive %s', device) utils.write_one_line(file, 'cfq')
def enable_blkio_and_cfq(device): """Enable blkio and cfq, when not done by boot command.""" # Ensure that the required device is valid block device. disk = os.path.join('/sys/block', device) if not os.path.exists(disk): raise error.Error('Machine does not have disk device ' + device) # Ensure the io cgroup is mounted. if not cgroup.mount_point(BLKIO_CGROUP_NAME): raise error.Error('Kernel not compiled with blkio support') # Enable cfq scheduling on the block device. file = os.path.join(disk, 'queue/scheduler') if '[cfq]' in utils.read_one_line(file): logging.debug('cfq scheduler is already enabled on drive %s', device) return logging.info('Enabling cfq scheduler on drive %s', device) utils.write_one_line(file, 'cfq')
def _gather_firmware_boot_time(self, results): """Read and report firmware startup time. The boot process writes the firmware startup time to the file named in `_FIRMWARE_TIME_FILE`. Read the time from that file, and record it in `results` as the keyval seconds_power_on_to_kernel. @param results Keyvals dictionary. """ try: # If the firmware boot time is not available, the file # will not exist. data = utils.read_one_line(self._FIRMWARE_TIME_FILE) except IOError: return firmware_time = float(data) boot_time = results['seconds_kernel_to_login'] results['seconds_power_on_to_kernel'] = firmware_time results['seconds_power_on_to_login'] = ( round(firmware_time + boot_time, 2))
def uptime_secs(): results = utils.read_one_line("/proc/uptime") return float(results.split()[0])