Best Python code snippet using avocado_python
gen-trex-cfg.py
Source:gen-trex-cfg.py
1#!/usr/bin/python32import yaml3import argparse4import logging5import sys6from pathlib import Path7import subprocess8import re9import os10TOOLBOX_HOME = os.environ.get('TOOLBOX_HOME')11if TOOLBOX_HOME is None:12 print("This script requires libraries that are provided by the toolbox project.")13 print("Toolbox can be acquired from https://github.com/perftool-incubator/toolbox and")14 print("then use 'export TOOLBOX_HOME=/path/to/toolbox' so that it can be located.")15 exit(1)16else:17 p = Path(TOOLBOX_HOME) / 'python'18 if not p.exists() or not p.is_dir():19 print("ERROR: <TOOLBOX_HOME>/python ('%s') does not exist!" % (p))20 exit(2)21 sys.path.append(str(p))22from toolbox.system_cpu_topology import *23class t_global(object):24 args = None25 cfg = [26 {27 'version': 2,28 'c': 14,29 'interfaces': [],30 'limit_memory': None,31 'port_info': [],32 'port_bandwidth_gb': None,33 'platform': {34 'master_thread_id': None,35 'latency_thread_id': None,36 'dual_if': []37 }38 }39 ]40 log_debug_format = '[%(module)s %(funcName)s:%(lineno)d]\n[%(asctime)s][%(levelname) 8s] %(message)s'41 log_normal_format = '%(message)s'42 log = None43 device_pairs = []44 numa_nodes = {}45 system_cpus = None46def process_options():47 parser = argparse.ArgumentParser(description='Custon TRex config generator');48 parser.add_argument('--device',49 dest = 'devices',50 help = 'Use one or more times to specify devices to use.',51 action = 'append',52 required = True,53 type = str)54 parser.add_argument('--cpu',55 dest = 'cpus',56 help = 'Use one or more times to specify CPUs to use.',57 action = 'append',58 required = True,59 type = int)60 parser.add_argument('--log-level',61 dest = 'log_level',62 help = 'Control how much logging output should be generated',63 default = 'normal',64 choices = [ 'normal', 'debug' ])65 parser.add_argument('--memory-limit',66 dest = 'memory_limit',67 help = 'Limit TRex to X MB of memory so it does not consume all available hugepages',68 default = 2048,69 type = int)70 parser.add_argument('--use-l2',71 dest = 'use_l2',72 help = 'Should TRex run in L2 mode (as opposed to L3)',73 default = 'no',74 choices = [ 'no', 'yes' ])75 parser.add_argument('--use-smt',76 dest = 'use_smt',77 help = 'Should TRex use or ignore SMT CPU threads',78 default = 'no',79 choices = [ 'no', 'yes' ])80 parser.add_argument('--trex-binary',81 dest = 'trex_binary',82 help = 'Path to TRex binary which is used to query devices for L2 mode',83 default = '/opt/trex/current/t-rex-64',84 type = str)85 parser.add_argument('--output-file',86 dest = 'output_file',87 help = 'Where to write the resulting YAML config file',88 default = None,89 type = str)90 t_global.args = parser.parse_args()91 if t_global.args.log_level == 'debug':92 logging.basicConfig(level = logging.DEBUG, format = t_global.log_debug_format, stream = sys.stdout)93 elif t_global.args.log_level == 'normal':94 logging.basicConfig(level = logging.INFO, format = t_global.log_normal_format, stream = sys.stdout)95 t_global.log = logging.getLogger(__file__)96def main():97 process_options()98 if t_global.args.use_l2 == 'yes':99 path = Path(t_global.args.trex_binary)100 if not path.exists():101 t_global.log.error("User supplied TRex binary (%s) does not exist!" % (t_global.args.trex_binary))102 return(10)103 if not path.is_file():104 t_global.log.error("User supplied TRex binary (%s) is not a file!" % (t_global.args.trex_binary))105 return(11)106 if len(t_global.args.devices) < 2:107 t_global.log.error('You must specify at least 2 devices')108 return(1)109 elif len(t_global.args.devices) % 2 != 0:110 t_global.log.error('You must specify devices in pairs')111 return(2)112 # import 'simple' user defined options113 t_global.cfg[0]['limit_memory'] = t_global.args.memory_limit114 # build device pairs115 pair_started = False116 pair = None117 pair_idx = 1118 for dev in t_global.args.devices:119 # verify that the device exists (use lspci because we need some information from it anyway)120 result = subprocess.run(['lspci', '-mm', '-s', dev], stdout=subprocess.PIPE, stderr=subprocess.PIPE)121 if result.returncode == 0:122 if len(result.stdout.rstrip()) == 0:123 t_global.log.error("You specified an invalid device [%s]" % (dev))124 return(15)125 t_global.log.debug("Device %s is a valid PCI device: %s" % (dev, result.stdout.decode('utf-8').rstrip()))126 speed = None127 # figure out the speed of interface, if possible -- logic derived from TRex dpdk_setup_ports.py128 #129 #85:00.1 "Ethernet controller" "Intel Corporation" "Ethernet Controller XXV710 for 25GbE SFP28" -r02 "Intel Corporation" "Ethernet Network Adapter XXV710"130 m = re.search(r"([0-9]+)Gb", result.stdout.decode('utf-8'))131 if m:132 speed = int(m.group(1))133 else:134 #82:00.0 "Ethernet controller" "Intel Corporation" "82599ES 10-Gigabit SFI/SFP+ Network Connection" -r01 "Intel Corporation" "Ethernet Server Adapter X520-2"135 m = re.search(r"([0-9]+)-Gigabit", result.stdout.decode('utf-8'))136 if m:137 speed = int(m.group(1))138 else:139 #03:00.0 "Ethernet controller" "Intel Corporation" "Ethernet Connection X552/X557-AT 10GBASE-T" "Super Micro Computer Inc" "Device 15ad"140 #04:00.0 "Ethernet controller" "Intel Corporation" "Ethernet Controller X710/X557-AT 10GBASE-T" -r02 "Intel Corporation" "Ethernet Converged Network Adapter X710-T4"141 m = re.search(r"([0-9]+)GBASE", result.stdout.decode('utf-8'))142 if m:143 speed = int(m.group(1))144 if speed is not None:145 t_global.log.debug("Device %s has speed %dGb" % (dev, speed))146 if t_global.cfg[0]['port_bandwidth_gb'] is None or speed < t_global.cfg[0]['port_bandwidth_gb']:147 t_global.cfg[0]['port_bandwidth_gb'] = speed148 t_global.log.debug("Setting global config port_bandwidth_gb to %dGb" % (speed))149 else:150 t_global.log.error("You specified an invalid device [%s]" % (dev))151 return(14)152 if pair_started:153 pair.append(dev)154 port_info = None155 if t_global.args.use_l2 == 'yes':156 port_info = [157 {158 'src_mac': None,159 'dst_mac': None160 },161 {162 'src_mac': None,163 'dst_mac': None164 }165 ]166 else:167 port_info = [168 {169 'ip': "%d.%d.%d.%d" % (pair_idx, pair_idx, pair_idx, pair_idx),170 'default_gw': "%d.%d.%d.%d" % (pair_idx+1, pair_idx+1, pair_idx+1, pair_idx+1)171 },172 {173 'default_gw': "%d.%d.%d.%d" % (pair_idx, pair_idx, pair_idx, pair_idx),174 'ip': "%d.%d.%d.%d" % (pair_idx+1, pair_idx+1, pair_idx+1, pair_idx+1)175 }176 ]177 t_global.device_pairs.append( { 'devs': pair, 'port_info': port_info, 'cpus': [] } )178 pair_started = False179 pair_idx += 2180 else:181 pair = [ dev ]182 pair_started = True183 # default case if port speed could not be resolved for the devices184 if t_global.cfg[0]['port_bandwidth_gb'] is None:185 t_global.cfg[0]['port_bandwidth_gb'] = 10186 t_global.log.debug("device pairs: %s" % (t_global.device_pairs))187 # find the device's numa node and ensure they are the same between pair devices188 for pair in t_global.device_pairs:189 t_global.log.debug("pair: %s" % (pair))190 for dev in pair['devs']:191 t_global.log.debug("dev: %s" % (dev))192 path = Path('/sys/bus/pci/devices/' + dev)193 if path.exists() and path.is_dir():194 path = path / 'numa_node'195 if path.exists() and path.is_file():196 with path.open() as fh:197 if 'node' in pair:198 node = int(fh.readline().rstrip())199 if node == -1:200 t_global.log.warning("Device %s has an invalid NUMA node [%s], assuming it should be 0" % (dev, node))201 node = 0202 if pair['node'] != node:203 t_global.log.error("Device pair %s are not on the same NUMA node" % (pair['devs']))204 return(3)205 else:206 pair['node'] = int(fh.readline().rstrip())207 if pair['node'] == -1:208 t_global.log.warning("Device '%s' has an invalid NUMA node [%s], assuming it should be 0" % (dev, pair['node']))209 pair['node'] = 0210 else:211 t_global.log.error("Could not determine NUMA node for device %s" % (dev))212 return(16)213 else:214 t_global.log.error("Could not find /sys/bus/pci/devices/%s" % (dev))215 return(17)216 t_global.log.debug("device pairs: %s" % (t_global.device_pairs))217 t_global.system_cpus = system_cpu_topology(log = t_global.log)218 t_global.log.debug("cpus: %s" % (t_global.args.cpus))219 if t_global.args.use_smt == 'no':220 t_global.log.debug("Checking for SMT siblings to disable")221 # remove all but 1 SMT sibling from the list of requested CPUs222 for idx in range(0, len(t_global.args.cpus)):223 if t_global.args.cpus[idx] is None:224 continue225 siblings = t_global.system_cpus.get_thread_siblings(t_global.args.cpus[idx])226 for sibling in siblings:227 try:228 sibling_idx = t_global.args.cpus.index(sibling)229 t_global.args.cpus[sibling_idx] = None230 t_global.log.debug("Disabling CPU %d because it is an SMT sibling to CPU %d" % (sibling, t_global.args.cpus[idx]))231 except ValueError:232 pass233 t_global.log.debug("cpus: %s" % (t_global.args.cpus))234 # validate the the user requested CPUs are availble235 online_cpus = t_global.system_cpus.get_online_cpus()236 for cpu in t_global.args.cpus:237 if cpu is None:238 continue239 try:240 online_cpus.index(cpu)241 except ValueError:242 t_global.log.error("Requested CPU %d is not an online CPU on this system" % (cpu))243 return(4)244 # determine the NUMA node for the CPUs we are using245 for cpu in t_global.args.cpus:246 if cpu is None:247 continue248 node = t_global.system_cpus.get_cpu_node(cpu)249 if not node in t_global.numa_nodes:250 t_global.log.debug("Adding NUMA node %d" % (node))251 t_global.numa_nodes[node] = { 'cpus': [], 'devices': False }252 t_global.numa_nodes[node]['cpus'].append(cpu)253 t_global.log.debug("Adding CPU %d to NUMA node %d" % (cpu, node))254 t_global.log.debug("numa nodes: %s" % (t_global.numa_nodes))255 # make sure that node local CPU resources are available to every device pair256 for pair in t_global.device_pairs:257 if not pair['node'] in t_global.numa_nodes:258 t_global.log.error("Device pair %s is on a numa node (%d) that you did not provide any CPUs for!" % ('|'.join(pair['devs']), pair['node']))259 return(5)260 else:261 t_global.numa_nodes[pair['node']]['devices'] = True262 t_global.log.debug("Found device pair %s on NUMA node %d" % ('|'.join(pair['devs']), pair['node']))263 t_global.log.debug("numa nodes: %s" % (t_global.numa_nodes))264 # remove numa nodes (and their cpus) which are not node local to any device265 for node in list(t_global.numa_nodes):266 if not t_global.numa_nodes[node]['devices']:267 t_global.log.debug("Removing NUMA node %d and it's CPUs [%s] because it has no node local devices" % (node, ",".join([str(int) for int in t_global.numa_nodes[node]['cpus']])))268 del t_global.numa_nodes[node]269 t_global.log.debug("numa nodes: %s" % (t_global.numa_nodes))270 # populate the global resources271 if len(t_global.numa_nodes) == 1:272 for node in t_global.numa_nodes:273 try:274 master_thread_id = t_global.numa_nodes[node]['cpus'].pop()275 t_global.cfg[0]['platform']['master_thread_id'] = master_thread_id276 t_global.log.debug("Setting master_thread_id to CPU %d from NUMA node %d" % (master_thread_id, node))277 latency_thread_id = t_global.numa_nodes[node]['cpus'].pop()278 t_global.cfg[0]['platform']['latency_thread_id'] = latency_thread_id279 t_global.log.debug("Setting latency_thread_id to CPU %d from NUMA node %d" % (latency_thread_id, node))280 except IndexError:281 t_global.log.error("You do not have enough CPUs to fullfill the base requirements")282 return(7)283 else:284 max_cpus = -1285 nodes = []286 for node in t_global.numa_nodes:287 num_cpus = len(t_global.numa_nodes[node]['cpus'])288 if num_cpus > max_cpus:289 max_cpus = num_cpus290 nodes = [ node ]291 elif num_cpus == max_cpus:292 nodes.append(node)293 t_global.log.debug("NUMA nodes %s have the most CPUs (%d)" % (','.join(str(x) for x in nodes), max_cpus))294 if len(nodes) == 1:295 # one node has more cpus than any other node, pull the cpus from that node296 try:297 master_thread_id = t_global.numa_nodes[nodes[0]]['cpus'].pop()298 t_global.cfg[0]['platform']['master_thread_id'] = master_thread_id299 t_global.log.debug("Setting master_thread_id to CPU %d from NUMA node %d" % (master_thread_id, nodes[0]))300 latency_thread_id = t_global.numa_nodes[nodes[0]]['cpus'].pop()301 t_global.cfg[0]['platform']['latency_thread_id'] = latency_thread_id302 t_global.log.debug("Setting latency_thread_id to CPU %d from NUMA node %d" % (latency_thread_id, nodes[0]))303 except IndexError:304 t_global.log.error("You do not have enough CPUs to fullfill the base requirements")305 return(6)306 elif len(nodes) > 1:307 # at least two nodes have the same number of cpus which is the most, pull 1 cpu from each of the first two308 try:309 master_thread_id = t_global.numa_nodes[nodes[0]]['cpus'].pop()310 t_global.cfg[0]['platform']['master_thread_id'] = master_thread_id311 t_global.log.debug("Setting master_thread_id to CPU %d from NUMA node %d" % (master_thread_id, nodes[0]))312 latency_thread_id = t_global.numa_nodes[nodes[1]]['cpus'].pop()313 t_global.cfg[0]['platform']['latency_thread_id'] = latency_thread_id314 t_global.log.debug("Setting latency_thread_id to CPU %d from NUMA node %d" % (latency_thread_id, nodes[1]))315 except IndexError:316 t_global.log.error("You do not have enough CPUs to fullfill the base requirements")317 return(12)318 else:319 t_global.log.error("Fatal error, how did I get here?")320 return(13)321 t_global.log.debug("numa nodes: %s" % (t_global.numa_nodes))322 # determine how many cpus each device pair should be allocated323 for node in t_global.numa_nodes:324 node_pair_counter = 0325 for pair in t_global.device_pairs:326 if pair['node'] == node:327 node_pair_counter += 1328 t_global.log.debug("NUMA node %d has %d device pairs" % (node, node_pair_counter))329 node_cpus_per_pair = int(len(t_global.numa_nodes[node]['cpus']) / node_pair_counter)330 if node_cpus_per_pair == 0:331 t_global.log.error("You do not have enough CPUs for this device configuration!")332 return(8)333 else:334 if node_cpus_per_pair < t_global.cfg[0]['c']:335 t_global.log.debug("Limiting CPUs per device pair to %d due to resources required on NUMA node %d" % (node_cpus_per_pair, node))336 t_global.cfg[0]['c'] = node_cpus_per_pair337 t_global.log.debug("numa nodes: %s" % (t_global.numa_nodes))338 # allocate the cpus to the device pair(s)339 for pair in t_global.device_pairs:340 for cpu_idx in range(t_global.cfg[0]['c']):341 cpu = t_global.numa_nodes[pair['node']]['cpus'].pop()342 pair['cpus'].append(cpu)343 t_global.log.debug("Assigning CPU %d from NUMA node %d to device pair %s" % (cpu, pair['node'], '|'.join(pair['devs'])))344 t_global.log.debug("numa nodes: %s" % (t_global.numa_nodes))345 t_global.log.debug("device pairs: %s" % (t_global.device_pairs))346 # if in 'use_l2' mode, determine device mac addresses now; this347 # has been deferred as long as possible since it is slow, waiting348 # until all other checks pass reduces the impact if there are349 # errors elsewhere350 if t_global.args.use_l2 == 'yes':351 for pair in t_global.device_pairs:352 for dev in pair['devs']:353 result = subprocess.run([t_global.args.trex_binary, '--dump-interfaces', dev], stdout=subprocess.PIPE, stderr=subprocess.PIPE)354 #Starting TRex v2.82 please wait ...355 #Showing interfaces info.356 #PCI: 0000:86:00.1 - MAC: 3C:FD:FE:B8:96:49 - Driver: net_i40e357 m = re.search(r"PCI:\s+([0-9a-fA-F:\.]+)\s+-\s+MAC:\s+([0-9a-fA-F:]+)\s.*", result.stdout.decode('utf-8'))358 if m:359 t_global.log.debug("Found MAC address %s for device %s" % (m.group(2), m.group(1)))360 dev_idx = pair['devs'].index(dev)361 if dev_idx == 0:362 pair['port_info'][0]['src_mac'] = m.group(2)363 pair['port_info'][1]['dst_mac'] = m.group(2)364 else:365 pair['port_info'][1]['src_mac'] = m.group(2)366 pair['port_info'][0]['dst_mac'] = m.group(2)367 else:368 t_global.log.error("Failed to discover MAC address for device %s" % (dev))369 return(9)370 t_global.log.debug("device pairs: %s" % (t_global.device_pairs))371 # add the device pairs to the cfg372 for pair in t_global.device_pairs:373 # interfaces374 t_global.cfg[0]['interfaces'].extend(pair['devs'])375 # port info376 t_global.cfg[0]['port_info'].extend(pair['port_info'])377 # dual_if378 if_obj = {379 'socket': pair['node'],380 'threads': pair['cpus']381 }382 t_global.cfg[0]['platform']['dual_if'].append(if_obj)383 if t_global.args.output_file is None:384 print("%s" % (yaml.dump(t_global.cfg, default_flow_style=False, indent=4, encoding=None)))385 else:386 t_global.log.info("Writing TRex config to %s" % (t_global.args.output_file))387 with open(t_global.args.output_file, 'w') as file:388 yaml.dump(t_global.cfg, default_flow_style=False, indent=4, encoding=None, stream=file)389 return(0)390if __name__ == "__main__":...
mpstat2node.py
Source:mpstat2node.py
1#!/usr/bin/python32# -*- coding: utf-8 -*-3"""4Read mpstat.log file and convert it by aggregating values across NUMA nodes.5Read aggregation of CPUs from output of lscpu command.6Copyright 2017, Jarmila Hladká7Copyright 2020, Jirka Hladky, hladky DOT jiri AT gmail DOT com8License:9This program is free software: you can redistribute it and/or modify10it under the terms of the GNU General Public License as published by11the Free Software Foundation, either version 3 of the License, or12(at your option) any later version.13This program is distributed in the hope that it will be useful,14but WITHOUT ANY WARRANTY; without even the implied warranty of15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16GNU General Public License for more details.17You should have received a copy of the GNU General Public License18along with this program. If not, see <http://www.gnu.org/licenses/>.19"""20from sys import stdin, stdout, stderr, exit21import re22def get_input():23 """24 Parse and validate input line.25 """26 from argparse import ArgumentParser27 import os28 # Parse input line:29 usage = """30./mpstat2node.py --lscpu <(lscpu) < mpstat.log31or32mpstat -P ALL 5 | ./mpstat2node.py --lscpu <(lscpu)33Process mpstat output on the fly for the already running mpstat process:341) mpstat -P ALL 5 > mpstat.txt352) tail -f -n +1 mpstat.txt | mpstat2node.py --lscpu <(lscpu)36Retrieve mpstat output from another machine:371) Collect mpstat data38lscpu > lscpu.txt39mpstat -P ALL 5 > mpstat.txt402) Aggreagate mpstat data41./mpstat2node.py --lscpu lscpu.txt < mpstat.txt42"""43 description = """Converts output of mpstat -P ALL <time_interval>44 by aggregating values for NUMA node."""45 parser = ArgumentParser(description=description, usage=usage)46 parser.add_argument("--lscpu", help="Path to lscpu file.", required=True)47 args = parser.parse_args()48 # Return lscpu file:49 if args.lscpu:50 # Check if the file/link/pipe is readable:51 if os.access(args.lscpu, os.R_OK):52 return args.lscpu53 else:54 stderr.write("File {0} doesn't exist!\n".format(args.lscpu))55 exit(1)56 # If lscpu file isn't specified:57 else:58 stderr.write("Specify path to lscpu file using '--lscpu' option!\n")59 exit(1)60def CPU_NUMA(lscpu):61 """62 Make a dictionary of CPU's association with NUMA nodes.63 """64 with open(lscpu) as lscpufile:65 cpu_numa = {}66 NUMA_re = re.compile(r'NUMA.*CPU\(s\):')67 numa_nodes_set = set()68 for line in lscpufile:69 # Find number of CPUs and NUMA nodes:70 if line[:7] == 'CPU(s):':71 cpu_nb = int(line[7:])72 elif line[:13] == 'NUMA node(s):':73 nodes_nb = int(line[13:])74 # Find NUMA nodes associated with CPUs:75 elif NUMA_re.search(line):76 words = line.split()77 cpus = words[-1].split(',')78 numa_node = int(words[1][4:])79 numa_nodes_set.add(numa_node)80 for cpu in cpus:81 if '-' in cpu:82 w = cpu.split('-')83 for i in range(int(w[0]), int(w[1]) + 1):84 cpu_numa[str(i)] = numa_node85 else:86 cpu_numa[cpu] = numa_node87 # Check if all CPUs are associated with NUMA node:88 if len(cpu_numa) != cpu_nb:89 stderr.write("Error in CPU - node association!\n")90 exit(1)91 # Create list of NUMA nodes sorted by value92 numa_nodes = sorted(numa_nodes_set)93 # Check if nodes_nb == len(numa_nodes)94 if nodes_nb != len(numa_nodes):95 stderr.write("Warning - nodes_nb " + str(nodes_nb) + " is different from len(numa_nodes) " + str(len(numa_nodes)) + " !\n")96 stderr.write(" numa_nodes: " + " ".join(numa_nodes) + "\n")97 # Number of cpus on nodes:98 cpu_on_node = {}99 for node in numa_nodes:100 cpu_on_node[node] = list(cpu_numa.values()).count(node)101 return cpu_numa, cpu_on_node, cpu_nb, numa_nodes102def modify_mpstat_output(cpu_numa, cpu_on_node, cpu_nb, nodes_nb):103 """104 Read mpstat output from stdin and output average activities105 among nodes.106 Ignore any lines at the top of the file starting with #107 """108 line = stdin.readline()109 if not line:110 stderr.write("WARN: Unexpected end of input file!\n")111 exit(1)112 line_count = 1113 while line.startswith("#"):114 stderr.write(line)115 line = stdin.readline()116 line_count += 1117 # Print the first line with the system info118 stdout.write(line)119 # Next line should be empty120 line = stdin.readline()121 if not line:122 stderr.write("WARN: Unexpected end of input file!\n")123 exit(1)124 line_count += 1125 stdout.write(line)126 if line != '\n':127 stderr.write("WARN: Expecting line number " + str(line_count) +128 " to be empty, but it's not.\n")129 # Loop over time reports.130 # Subsequent reports are separated by blank line:131 while True:132 status = average_over_node(cpu_numa, cpu_on_node, cpu_nb, numa_nodes, line_count)133 if status in ("END", "EOF"):134 break135 # Read and print final time statistics for nodes:136 if status == "END":137 average_over_node(cpu_numa, cpu_on_node, cpu_nb, numa_nodes, line_count)138def average_over_node(cpu_numa, cpu_on_node, cpu_nb, numa_nodes, line_count):139 """140 Read and print average statistics for one time interval report:141 """142 # Print description of columns:143 columns = stdin.readline()144 line_count += 1145 # Check for final time averages at the end of file:146 if columns == '\n':147 stdout.write('\n')148 return 'END'149 # Number of displayed CPU statistic values:150 STAT_COLUMNS = columns.count('%')151 # Write revised column labels:152 stdout.write('{0}{1}{2}'.format(columns[:12], 'NODE', columns[16:]))153 stdout.write(stdin.readline())154 line_count += 1155 # statistics - dictionary; key is the NUMA node number, value is the list of columns156 statistics = {}157 for key in numa_nodes:158 statistics[key] = [0.0] * STAT_COLUMNS159 # statistics = [[0.0 for j in range(len(numa_nodes))] for i in range(STAT_COLUMNS)]160 # Read statistics for CPUs:161 for i in range(cpu_nb):162 line = stdin.readline()163 line_count += 1164 words = line[11:].split()165 cpu = words[0]166 for col in range(STAT_COLUMNS):167 # print repr(words[col + 1])168 # try:169 statistics[cpu_numa[cpu]][col] += float(words[col + 1].strip('\0'))170 # except Exception as e:171 # print str(e)172 # print "col, cpu, node", col,cpu,cpu_numa[cpu]173 # print line174 # Statistics over nodes:175 for node in numa_nodes:176 output = '{0}{1:5d}'.format(line[:11], node)177 for col in range(STAT_COLUMNS):178 # print "node, col", node, col179 # print statistics[node][col]180 # print cpu_on_node[node]181 average = statistics[node][col]/cpu_on_node[node]182 output += '{0:8.2f}'.format(average)183 output += '\n'184 stdout.write(output)185 # Check for end of file without average values:186 next_line = stdin.readline()187 if next_line != '\n':188 return 'EOF'189 # If not end of file print blank line:190 stdout.write(next_line)191 line_count += 1192if __name__ == "__main__":193 cpu_numa, cpu_on_node, cpu_nb, numa_nodes = CPU_NUMA(get_input())...
numa_opts.py
Source:numa_opts.py
1from autotest.client.shared import error2from virttest import qemu_vm3import logging4logger = logging.getLogger(__name__)5dbg = logger.debug6def run_numa_opts(test, params, env):7 """8 Simple test to check if NUMA options are being parsed properly9 This _does not_ test if NUMA information is being properly exposed to the10 guest.11 """12 dbg("starting numa_opts test...")13 # work around a test runner bug that makes it override test-specific "mem"14 # and "smp" options unconditionally, so we override them manually if15 # necessary, using the mem_override/smp_override options:16 mem_override = params.get("mem_override")17 if mem_override:18 params["mem"] = mem_override19 smp_override = params.get("smp_override")20 if smp_override:21 params["smp"] = smp_override22 # we start the VM manually because of the mem/smp workaround above:23 vm = env.get_vm(params["main_vm"])24 vm.create(params=params)25 numa = vm.monitors[0].info_numa()26 dbg("info numa reply: %r", numa)27 numa_nodes = params.get("numa_nodes")28 if numa_nodes:29 numa_nodes = int(params.get("numa_nodes"))30 if len(numa) != numa_nodes:31 raise error.TestFail(32 "Wrong number of numa nodes: %d. Expected: %d" %33 (len(numa), numa_nodes))34 for nodenr, node in enumerate(numa):35 size = params.get("numa_node%d_size" % (nodenr))36 if size is not None:37 size = int(size)38 if size != numa[nodenr][0]:39 raise error.TestFail(40 "Wrong size of numa node %d: %d. Expected: %d" %41 (nodenr, numa[nodenr][0], size))42 cpus = params.get("numa_node%d_cpus" % (nodenr))43 if cpus is not None:44 cpus = set([int(v) for v in cpus.split()])45 if cpus != numa[nodenr][1]:46 raise error.TestFail(47 "Wrong CPU set on numa node %d: %s. Expected: %s" %...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!