def cmd_collect_metadata(args): filename = args.output if filename and os.path.exists(filename): print("ERROR: The JSON file %r already exists" % filename) sys.exit(1) cpus = args.affinity if cpus: if not set_cpu_affinity(cpus): print("ERROR: failed to set the CPU affinity") sys.exit(1) else: cpus = get_isolated_cpus() if cpus: set_cpu_affinity(cpus) # ignore if set_cpu_affinity() failed run = pyperf.Run([1.0]) metadata = run.get_metadata() if metadata: print("Metadata:") for line in format_metadata(metadata): print(line) if filename: run = run._update_metadata({'name': 'metadata'}) bench = pyperf.Benchmark([run]) bench.dump(filename)
def _cpu_affinity(self): cpus = self.args.affinity if not cpus: # --affinity option is not set: detect isolated CPUs isolated = True cpus = get_isolated_cpus() if not cpus: # no isolated CPUs or unable to get the isolated CPUs return else: isolated = False cpus = parse_cpu_list(cpus) if set_cpu_affinity(cpus): if self.args.verbose: if isolated: text = ("Pin process to isolated CPUs: %s" % format_cpu_list(cpus)) else: text = ("Pin process to CPUs: %s" % format_cpu_list(cpus)) print(text) if isolated: self.args.affinity = format_cpu_list(cpus) else: if not isolated: print("ERROR: CPU affinity not available.", file=sys.stderr) print("Use Python 3.3 or newer, or install psutil dependency") sys.exit(1) elif not self.args.quiet: print("WARNING: unable to pin worker processes to " "isolated CPUs, CPU affinity not available") print("Use Python 3.3 or newer, or install psutil dependency")
def collect_cpu_config(metadata, cpus): nohz_full = read_first_line(sysfs_path('devices/system/cpu/nohz_full')) if nohz_full: nohz_full = parse_cpu_list(nohz_full) isolated = get_isolated_cpus() if isolated: isolated = set(isolated) configs = {} for cpu in cpus: config = get_cpu_config(cpu) if nohz_full and cpu in nohz_full: config.append('nohz_full') if isolated and cpu in isolated: config.append('isolated') if config: configs[cpu] = ', '.join(config) config = format_cpu_infos(configs) cpuidle = read_first_line('/sys/devices/system/cpu/cpuidle/current_driver') if cpuidle: config.append('idle:%s' % cpuidle) if not config: return metadata['cpu_config'] = '; '.join(config)
def check_get(line): def mock_open(*args, **kw): return io.StringIO(line) with mock.patch('pyperf._utils.open', create=True, side_effect=mock_open): return cpu_utils.get_isolated_cpus()
def check_isolcpus(self): isolated = get_isolated_cpus() if isolated: self.log_state('Isolated CPUs (%s/%s): %s' % (len(isolated), self.ncpu, format_cpu_list(isolated))) elif self.ncpu > 1: self.log_state('No CPU is isolated') self.advice('Use isolcpus=<cpu list> kernel parameter ' 'to isolate CPUs')
def test_get_isolated_cpus(self): BUILTIN_OPEN = 'builtins.open' if six.PY3 else '__builtin__.open' def check_get(line): with mock.patch(BUILTIN_OPEN) as mock_open: mock_file = mock_open.return_value mock_file.readline.return_value = line return cpu_utils.get_isolated_cpus() # no isolated CPU self.assertIsNone(check_get('')) # isolated CPUs self.assertEqual(check_get('1-2'), [1, 2]) # /sys/devices/system/cpu/isolated doesn't exist (ex: Windows) with mock.patch(BUILTIN_OPEN, side_effect=IOError): self.assertIsNone(cpu_utils.get_isolated_cpus())
def test_get_isolated_cpus(self): def check_get(line): def mock_open(*args, **kw): return io.StringIO(line) with mock.patch('pyperf._utils.open', create=True, side_effect=mock_open): return cpu_utils.get_isolated_cpus() # no isolated CPU self.assertIsNone(check_get('')) # isolated CPUs self.assertEqual(check_get('1-2'), [1, 2]) # /sys/devices/system/cpu/isolated doesn't exist (ex: Windows) with mock.patch('builtins.open', side_effect=IOError): self.assertIsNone(cpu_utils.get_isolated_cpus())
def init(self, args): if not self.operations: print("WARNING: no operation available for your platform") sys.exit() self.logical_cpu_count = get_logical_cpu_count() if not self.logical_cpu_count: print("ERROR: failed to get the number of logical CPUs") sys.exit(1) isolated = get_isolated_cpus() if isolated: self.cpus = tuple(isolated) elif args.affinity: self.cpus = tuple(args.affinity) else: self.cpus = tuple(range(self.logical_cpu_count)) # The list of cpus must be sorted to avoid useless write in operations assert sorted(self.cpus) == list(self.cpus) self.log_state("CPU: use %s logical CPUs: %s" % (len(self.cpus), format_cpu_list(self.cpus)))
def check_get(line): with mock.patch(BUILTIN_OPEN) as mock_open: mock_file = mock_open.return_value mock_file.readline.return_value = line return cpu_utils.get_isolated_cpus()