|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# This program is free software; you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License as published by |
| 5 | +# the Free Software Foundation; either version 2 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | +# |
| 8 | +# This program is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | +# |
| 12 | +# See LICENSE for more details. |
| 13 | +# |
| 14 | +# Copyright: 2025 IBM |
| 15 | +# Author: Tejas Manhas <Tejas.Manhas1@ibm.com> |
| 16 | +import re |
| 17 | +from avocado import Test |
| 18 | +from avocado.utils import distro, process, genio |
| 19 | +from avocado.utils.software_manager.manager import SoftwareManager |
| 20 | + |
| 21 | + |
| 22 | +class datatype_profiling(Test): |
| 23 | + """ |
| 24 | + This is a test class for datatype profiling that checks datatype offset etc. |
| 25 | + """ |
| 26 | + |
| 27 | + def setUp(self): |
| 28 | + ''' |
| 29 | + Install the basic packages to support perf |
| 30 | + ''' |
| 31 | + detected_distro = distro.detect() |
| 32 | + if 'ppc64' not in detected_distro.arch: |
| 33 | + self.cancel('This test is not supported on %s architecture' |
| 34 | + % detected_distro.arch) |
| 35 | + if 'PowerNV' in genio.read_file('/proc/cpuinfo'): |
| 36 | + self.cancel('This test is only supported on LPAR') |
| 37 | + process.run("dmesg -C") |
| 38 | + self.check_mem_event_availability() |
| 39 | + self.check_dependencies() |
| 40 | + |
| 41 | + def run_cmd(self, cmd): |
| 42 | + """ |
| 43 | + run command on SUT as root |
| 44 | + """ |
| 45 | + |
| 46 | + result = process.run(cmd, shell=True, ignore_status=True) |
| 47 | + output = result.stdout_text + result.stderr_text |
| 48 | + return output |
| 49 | + |
| 50 | + def check_mem_event_availability(self): |
| 51 | + try: |
| 52 | + output = self.run_cmd( |
| 53 | + "perf mem record -e list") |
| 54 | + except Exception as e: |
| 55 | + self.log.info(f"Command failed: {e}") |
| 56 | + return False |
| 57 | + if not re.search(r"ldlat-loads\s*:\s*available", output): |
| 58 | + self.cancel("Required memory event 'ldlat-loads' not available") |
| 59 | + |
| 60 | + if not re.search(r"ldlat-stores\s*:\s*available", output): |
| 61 | + self.cancel("Required memory event 'ldlat-stores' not available") |
| 62 | + |
| 63 | + def check_dependencies(self): |
| 64 | + """ |
| 65 | + Check if required debug packages for current kernel are installed |
| 66 | + and perf events are available. |
| 67 | + """ |
| 68 | + |
| 69 | + detected_distro = distro.detect() |
| 70 | + is_rhel = False |
| 71 | + is_sles = False |
| 72 | + if "rhel" in detected_distro.name.lower(): |
| 73 | + is_rhel = True |
| 74 | + if "suse" in detected_distro.name.lower(): |
| 75 | + is_sles = True |
| 76 | + # Define base package names (without versions) |
| 77 | + base_packages = ["perf"] |
| 78 | + |
| 79 | + # Add debuginfo names based on distro |
| 80 | + if is_rhel: |
| 81 | + base_packages += [ |
| 82 | + "kernel-tools-libs", |
| 83 | + "kernel-tools", |
| 84 | + "kernel-headers", |
| 85 | + "kernel-devel", |
| 86 | + "kernel", |
| 87 | + "kernel-core", |
| 88 | + "kernel-modules", |
| 89 | + "kernel-modules-extra", |
| 90 | + "kernel-modules-core", |
| 91 | + "kernel-debuginfo", |
| 92 | + "kernel-debug-debuginfo", |
| 93 | + "kernel-debuginfo-common-ppc64le" |
| 94 | + ] |
| 95 | + elif is_sles: |
| 96 | + base_packages += [ |
| 97 | + "kernel-default-debuginfo", |
| 98 | + "kernel-debug-debuginfo", |
| 99 | + "kernel-default-devel", |
| 100 | + "kernel-devel", |
| 101 | + "kernel-default" |
| 102 | + ] |
| 103 | + else: |
| 104 | + self.cancel("Unsupported Linux distribution") |
| 105 | + |
| 106 | + smm = SoftwareManager() |
| 107 | + for package in base_packages: |
| 108 | + if not smm.check_installed(package) and not smm.install(package): |
| 109 | + self.cancel('%s is needed for the test to be run' % package) |
| 110 | + |
| 111 | + def check_perf_report_headers(self, cmd): |
| 112 | + """ |
| 113 | + Checks for expected headers in perf report output. |
| 114 | + """ |
| 115 | + output = self.run_cmd(cmd) |
| 116 | + expected_headers = ["Symbol", "Data Type", "Data Type Offset"] |
| 117 | + for header in expected_headers: |
| 118 | + if header not in output: |
| 119 | + self.fail( |
| 120 | + f"Missing expected header '{header}' in perf report output") |
| 121 | + |
| 122 | + def check_perf_annotate_headers(self, cmd, check_insn_stat=False): |
| 123 | + """ |
| 124 | + Checks for expected headers in perf annotate output. |
| 125 | + """ |
| 126 | + self.run_cmd(f"{cmd} > out") |
| 127 | + output = self.run_cmd("cat out") |
| 128 | + |
| 129 | + if check_insn_stat: |
| 130 | + # First check for Name/opcode, Good, Bad headers |
| 131 | + expected_insn_headers = ["Name/opcode", "Good", "Bad"] |
| 132 | + for header in expected_insn_headers: |
| 133 | + if header not in output: |
| 134 | + self.fail( |
| 135 | + f"Missing expected header '{header}' in perf annotate insn stat output") |
| 136 | + |
| 137 | + # Now check for 'offset' and 'size' in each Annotate type block |
| 138 | + sections = output.split("Annotate type:") |
| 139 | + for section in sections[1:]: |
| 140 | + if "offset" not in section or "size" not in section: |
| 141 | + self.fail( |
| 142 | + "Missing 'offset' or 'size' header in perf annotate output") |
| 143 | + |
| 144 | + def test_datatype_profiling(self): |
| 145 | + """ |
| 146 | + Test to verify perf data type profiling feature. |
| 147 | + Steps: |
| 148 | + 1. Verify perf report headers. |
| 149 | + 2. Verify perf annotate data type headers. |
| 150 | + 3. Verify perf annotate instruction stats headers. |
| 151 | + Repeat above for: |
| 152 | + a. perf mem record -a sleep 10 |
| 153 | + b. perf record -a -e mem-loads sleep 5 |
| 154 | + c. perf record -c 1 -e mem-stores sleep 5 |
| 155 | + """ |
| 156 | + |
| 157 | + record_cmds = [ |
| 158 | + "perf mem record -a -o /tmp/perf_output.data sleep 10", |
| 159 | + "perf record -a -e mem-loads -o /tmp/perf_output.data sleep 5", |
| 160 | + "perf record -c 1 -e mem-stores -o /tmp/perf_output.data sleep 5" |
| 161 | + ] |
| 162 | + |
| 163 | + for cmd in record_cmds: |
| 164 | + self.run_cmd(cmd) |
| 165 | + |
| 166 | + self.check_perf_report_headers( |
| 167 | + "perf report -i /tmp/perf_output.data -s symbol,type,typeoff") |
| 168 | + self.check_perf_annotate_headers( |
| 169 | + "perf annotate -i /tmp/perf_output.data --data-type") |
| 170 | + self.check_perf_annotate_headers( |
| 171 | + "perf annotate -i /tmp/perf_output.data --data-type --insn-stat", |
| 172 | + check_insn_stat=True) |
| 173 | + |
| 174 | + def tearDown(self): |
| 175 | + """ |
| 176 | + tear down function to clear dmesg and data files. |
| 177 | + """ |
| 178 | + process.run("dmesg -T") |
| 179 | + self.run_cmd("rm -rf /tmp/perf_output.data") |
| 180 | + self.run_cmd("rm -rf out") |
0 commit comments