tools/testing/selftests/turbostat/added_perf_counters.py

Source file repositories/reference/linux-study-clean/tools/testing/selftests/turbostat/added_perf_counters.py

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/turbostat/added_perf_counters.py
Extension
.py
Size
5323 bytes
Lines
179
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: tools
Status
atlas-only

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

#!/bin/env python3
# SPDX-License-Identifier: GPL-2.0

import subprocess
from shutil import which
from os import pread

class PerfCounterInfo:
	def __init__(self, subsys, event):
		self.subsys = subsys
		self.event = event

	def get_perf_event_name(self):
		return f'{self.subsys}/{self.event}/'

	def get_turbostat_perf_id(self, counter_scope, counter_type, column_name):
		return f'perf/{self.subsys}/{self.event},{counter_scope},{counter_type},{column_name}'

PERF_COUNTERS_CANDIDATES = [
	PerfCounterInfo('msr', 'mperf'),
	PerfCounterInfo('msr', 'aperf'),
	PerfCounterInfo('msr', 'tsc'),
	PerfCounterInfo('cstate_core', 'c1-residency'),
	PerfCounterInfo('cstate_core', 'c6-residency'),
	PerfCounterInfo('cstate_core', 'c7-residency'),
	PerfCounterInfo('cstate_pkg', 'c2-residency'),
	PerfCounterInfo('cstate_pkg', 'c3-residency'),
	PerfCounterInfo('cstate_pkg', 'c6-residency'),
	PerfCounterInfo('cstate_pkg', 'c7-residency'),
	PerfCounterInfo('cstate_pkg', 'c8-residency'),
	PerfCounterInfo('cstate_pkg', 'c9-residency'),
	PerfCounterInfo('cstate_pkg', 'c10-residency'),
]
present_perf_counters = []

def check_perf_access():
	perf = which('perf')
	if perf is None:
		print('SKIP: Could not find perf binary, thus could not determine perf access.')
		return False

	def has_perf_counter_access(counter_name):
		proc_perf = subprocess.run([perf, 'stat', '-e', counter_name, '--timeout', '10'],
							 capture_output = True)

		if proc_perf.returncode != 0:
			print(f'SKIP: Could not read {counter_name} perf counter.')
			return False

		if b'<not supported>' in proc_perf.stderr:
			print(f'SKIP: Could not read {counter_name} perf counter.')
			return False

		return True

	for counter in PERF_COUNTERS_CANDIDATES:
		if has_perf_counter_access(counter.get_perf_event_name()):
			present_perf_counters.append(counter)

	if len(present_perf_counters) == 0:
		print('SKIP: Could not read any perf counter.')
		return False

	if len(present_perf_counters) != len(PERF_COUNTERS_CANDIDATES):
		print(f'WARN: Could not access all of the counters - some will be left untested')

	return True

if not check_perf_access():
	exit(0)

Annotation

Implementation Notes