Skip to content

Commit 69d8edf

Browse files
committed
feat(tests): read msrs in C
Rewrite `msr_reader.sh` script in C to speed up manual validation of msr baselines. Bash script run in ~1 minute. C version runs in 0.2 seconds. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent eec0ca1 commit 69d8edf

File tree

4 files changed

+52
-48
lines changed

4 files changed

+52
-48
lines changed

tests/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,17 @@ def waitpkg_bin(test_fc_session_root_path):
258258
yield waitpkg_bin_path
259259

260260

261+
@pytest.fixture(scope="session")
262+
def msr_reader_bin(test_fc_session_root_path):
263+
"""Build a binary that reads msrs"""
264+
msr_reader_bin_path = os.path.join(test_fc_session_root_path, "msr_reader")
265+
build_tools.gcc_compile(
266+
"data/msr/msr_reader.c",
267+
msr_reader_bin_path,
268+
)
269+
yield msr_reader_bin_path
270+
271+
261272
@pytest.fixture
262273
def bin_seccomp_paths():
263274
"""Build jailers and jailed binaries to test seccomp.

tests/data/msr/msr_reader.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// Helper script used to read MSR values from ranges known to contain MSRs.
5+
6+
#include <fcntl.h>
7+
#include <stdint.h>
8+
#include <stdio.h>
9+
#include <unistd.h>
10+
11+
void print_msr(int msr_fd, uint64_t msr) {
12+
uint64_t value;
13+
if (pread(msr_fd, &value, sizeof(value), msr) == sizeof(value))
14+
printf("0x%llx,0x%llx\n", msr, value);
15+
}
16+
17+
int main() {
18+
int msr_fd = open("/dev/cpu/0/msr", O_RDONLY);
19+
if (msr_fd < 0)
20+
return -1;
21+
22+
printf("MSR_ADDR,VALUE\n");
23+
for (uint64_t msr = 0; msr <= 0xFFF; msr++)
24+
print_msr(msr_fd, msr);
25+
for (uint64_t msr = 0x10000; msr <= 0x10FFF; msr++)
26+
print_msr(msr_fd, msr);
27+
for (uint64_t msr = 0xC0000000; msr <= 0xC0011030; msr++)
28+
print_msr(msr_fd, msr);
29+
30+
print_msr(msr_fd, 0x400000000);
31+
print_msr(msr_fd, 0x2000000000);
32+
print_msr(msr_fd, 0x4000000000);
33+
print_msr(msr_fd, 0x8000000000);
34+
print_msr(msr_fd, 0x1000000000000);
35+
print_msr(msr_fd, 0x3c000000000000);
36+
print_msr(msr_fd, 0x80000000000000);
37+
print_msr(msr_fd, 0x40000000000000);
38+
}

tests/data/msr/msr_reader.sh

Lines changed: 0 additions & 45 deletions
This file was deleted.

tests/integration_tests/functional/test_cpu_features_x86_64.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def test_brand_string(uvm_plain_any):
256256
@pytest.mark.timeout(900)
257257
@pytest.mark.nonci
258258
def test_cpu_rdmsr(
259-
microvm_factory, cpu_template_any, guest_kernel, rootfs, results_dir
259+
msr_reader_bin, microvm_factory, cpu_template_any, guest_kernel, rootfs, results_dir
260260
):
261261
"""
262262
Test MSRs that are available to the guest.
@@ -300,8 +300,8 @@ def test_cpu_rdmsr(
300300
vm.basic_config(vcpu_count=vcpus, mem_size_mib=guest_mem_mib)
301301
vm.set_cpu_template(cpu_template_any)
302302
vm.start()
303-
vm.ssh.scp_put(DATA_FILES / "msr_reader.sh", "/tmp/msr_reader.sh")
304-
_, stdout, stderr = vm.ssh.run("/tmp/msr_reader.sh", timeout=None)
303+
vm.ssh.scp_put(msr_reader_bin, "/tmp/msr_reader")
304+
_, stdout, stderr = vm.ssh.run("/tmp/msr_reader")
305305
assert stderr == ""
306306

307307
# Load results read from the microvm

0 commit comments

Comments
 (0)