tools/testing/selftests/turbostat/smi_aperf_mperf.py

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/turbostat/smi_aperf_mperf.py
Extension
.py
Size
4467 bytes
Lines
158
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

# CDLL calls dlopen underneath.
# Calling it with None (null), we get handle to the our own image (python interpreter).
# We hope to find sched_getcpu() inside ;]
# This is a bit ugly, but helps shipping working software, so..
try:
	import ctypes

	this_image = ctypes.CDLL(None)
	BASE_CPU = this_image.sched_getcpu()
except:
	BASE_CPU = 0 # If we fail, set to 0 and pray it's not offline.

MSR_IA32_MPERF = 0x000000e7
MSR_IA32_APERF = 0x000000e8

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, assuming no access.')
			return False

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

		return True

	if not has_perf_counter_access('msr/mperf/'):
		return False
	if not has_perf_counter_access('msr/aperf/'):
		return False
	if not has_perf_counter_access('msr/smi/'):
		return False

	return True

def check_msr_access():
	try:
		file_msr = open(f'/dev/cpu/{BASE_CPU}/msr', 'rb')
	except:
		return False

	if len(pread(file_msr.fileno(), 8, MSR_IA32_MPERF)) != 8:
		return False

	if len(pread(file_msr.fileno(), 8, MSR_IA32_APERF)) != 8:
		return False

	return True

has_perf_access = check_perf_access()
has_msr_access = check_msr_access()

turbostat_counter_source_opts = ['']

Annotation

Implementation Notes