def check_env(): """Check if there is appropriate environment on this system""" if os.path.exists(PY_CACHE): shutil.rmtree(PY_CACHE) # check cpu vendor id if not vendor_check(): parser_lib.print_red("Please run this tools on {}!".format(CPU_VENDOR)) sys.exit(1) # check if required tools are exists for excute in BIN_LIST: res = subprocess.Popen("which {}".format(excute), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) line = res.stdout.readline().decode('ascii') if not line: parser_lib.print_yel("'{}' not found, please install it!".format(excute)) sys.exit(1) if excute == 'cpuid': res = subprocess.Popen("cpuid -v", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) line = res.stdout.readline().decode('ascii') version = line.split()[2] if int(version) < 20170122: parser_lib.print_yel("Need CPUID version >= 20170122") sys.exit(1) if os.path.exists(OUTPUT): shutil.rmtree(OUTPUT)
def store_cx_data(sysnode1, sysnode2, config): """This will get Cx data of power and store it to PackedCx""" i = 0 state_cpus = {} with open(sysnode1, 'r') as acpi_idle: idle_driver = acpi_idle.read(32) if idle_driver.find("acpi_idle") == -1: if idle_driver.find("intel_idle") == 0: parser_lib.print_red("The tool need to run with acpi_idle driver, " + "please add intel_idle.max_cstate=0 in kernel " + "cmdline to fall back to acpi_idle driver", err=True) else: parser_lib.print_red("acpi_idle driver is not found.", err=True) sys.exit(1) files = os.listdir(sysnode2) for d_path in files: if os.path.isdir(sysnode2+d_path): state_cpus[d_path] = sysnode2+d_path state_cpus = sorted(state_cpus.keys()) del state_cpus[0] cpu_state = ['desc', 'latency', 'power'] acpi_hw_type = ['HLT', 'MWAIT', 'IOPORT'] cx_state = defaultdict(dict) for state in state_cpus: i += 1 for item in cpu_state: cx_data_file = open(sysnode2+state+'/'+item, 'r') cx_state[state][item] = cx_data_file.read().strip() cx_state[state]['type'] = i if cx_state[state][cpu_state[0]].find(acpi_hw_type[0]) != -1 or \ cx_state[state][cpu_state[0]].find(acpi_hw_type[1]) != -1: PackedCx.space_id_8b = SPACE_ID[0x7F] if cx_state[state][cpu_state[0]].find(acpi_hw_type[0]) != -1: PackedCx.bit_width_8b = 0 PackedCx.bit_offset_8b = 0 PackedCx.access_size_8b = 0 PackedCx.address_64b = 0 else: PackedCx.bit_width_8b = 1 PackedCx.bit_offset_8b = 2 PackedCx.access_size_8b = 1 PackedCx.address_64b = cx_state[state][cpu_state[0]].split()[3] elif cx_state[state][cpu_state[0]].find(acpi_hw_type[2]) != -1: PackedCx.space_id_8b = SPACE_ID[1] PackedCx.bit_width_8b = 8 PackedCx.bit_offset_8b = 0 PackedCx.access_size_8b = 0 PackedCx.address_64b = cx_state[state][cpu_state[0]].split()[2] print("\t\t{{{{{}, 0x{:0>2X}U, 0x{:0>2X}U, 0x{:0>2X}U, ".format( PackedCx.space_id_8b, PackedCx.bit_width_8b, PackedCx.bit_offset_8b, PackedCx.access_size_8b), file=config, end="") print("0x{:0>2X}UL}}, 0x{:0>2X}U, 0x{:0>2X}U, 0x{:0>2X}U}},".format( int(str(PackedCx.address_64b), 16), cx_state[state]['type'], int(cx_state[state][cpu_state[1]]), int(cx_state[state][cpu_state[2]])), file=config)
def check_env(): """Check if there is appropriate environment on this system""" if os.path.exists(PY_CACHE): shutil.rmtree(PY_CACHE) # check cpu msr file cpu_dirs = "/dev/cpu" if check_msr_files(cpu_dirs): res = subprocess.Popen("modprobe msr", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) err_msg = res.stderr.readline().decode('ascii') if err_msg: parser_lib.print_red("{}".format(err_msg), err=True) exit(-1) msr_info = check_msr_files(cpu_dirs) if msr_info: for cpu_num in msr_info: parser_lib.print_red("Missing CPU msr file in the {}/{}/".format( cpu_dirs, cpu_num), err=True) parser_lib.print_red( "Missing CPU msr file, please check the value of CONFIG_X86_MSR in the kernel config.", err=True) exit(-1) # check cpu vendor id if not vendor_check(): parser_lib.print_red("Please run this tools on {}!".format(CPU_VENDOR)) sys.exit(1) if os.path.exists(OUTPUT): shutil.rmtree(OUTPUT)
def store_mmcfg_base_data(mmcfg_node, config): print("\t/* PCI mmcfg base of MCFG */", file=config) with open(mmcfg_node, 'rb') as mmcfg: mmcfg.read(MCFG_OFFSET) mmcfg_len_obj = mmcfg.read(DWORD_LEN) mmcfg_len_int = int.from_bytes(mmcfg_len_obj, 'little') if mmcfg_len_int > MCFG_ENTRY1_OFFSET: parser_lib.print_red("Multiple PCI segment groups is not supported!", err=True) sys.exit(1) mmcfg.seek(MCFG_ENTRY0_BASE_OFFSET, 0) mmcfg_base_addr_obj = mmcfg.read(DWORD_LEN) mmcfg_base_addr = int.from_bytes(mmcfg_base_addr_obj, 'little') print("\t#define DEFAULT_PCI_MMCFG_BASE {}UL".format(hex(mmcfg_base_addr)), file=config)
def store_px_data(sysnode, config): """This will get Px data of power and store it to px data :param sysnode: the path of system power state, such as: /sys/devices/system/cpu/ :param config: file pointer that opened for writing board config information """ px_tmp = PxPkg() px_data = {} try: with open(sysnode + 'cpu0/cpufreq/scaling_driver', 'r') as f_node: freq_driver = f_node.read() if freq_driver.find("acpi-cpufreq") == -1: parser_lib.print_yel("The Px data for ACRN relies on " +\ "acpi-cpufreq driver but it is not found, ", warn=True, end=False) if freq_driver.find("intel_pstate") == 0: print("please add intel_pstate=disable in kernel " +\ "cmdline to fall back to acpi-cpufreq driver") else: parser_lib.print_yel( "please make sure ACPI Pstate is enabled in BIOS.", warn=True) print("\t/* Px data is not available */", file=config) return except IOError: parser_lib.print_yel("No scaling_driver found.", warn=True) print("\t/* Px data is not available */", file=config) return try: with open(sysnode + 'cpufreq/boost', 'r') as f_node: boost = f_node.read() except IOError: boost = 0 parser_lib.print_yel("CPU turbo is not enabled!") with open(sysnode + 'cpu0/cpufreq/scaling_available_frequencies', 'r') as f_node: freqs = f_node.read() with open(sysnode + 'cpu0/cpufreq/cpuinfo_transition_latency') as f_node: latency = int(f_node.read().strip()) latency = latency // 1000 i = 0 p_cnt = 0 for freq in freqs.split(): if boost != 0 and i == 0: try: subprocess.check_call('/usr/sbin/rdmsr 0x1ad', shell=True, stdout=subprocess.PIPE) except subprocess.CalledProcessError: parser_lib.print_red("MSR 0x1ad not support in this platform!", err=True) sys.exit(1) res = subprocess.Popen('/usr/sbin/rdmsr 0x1ad', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) result = res.stdout.readline().strip() #max_ratio_cpu = result[-2:] ctl_state = int(result[-2:], 16) << 8 i += 1 else: ctl_state = int(freq) // 100000 << 8 px_tmp.core_freq = int(int(freq) / 1000) px_tmp.power = 0 px_tmp.trans_latency = latency px_tmp.bus_latency = latency px_tmp.control = ctl_state px_tmp.status = ctl_state px_data[freq] = px_tmp print("\t{{0x{:0>2X}UL, 0x{:0>2X}UL, 0x{:0>2X}UL, ".format( px_data[freq].core_freq, px_data[freq].power, px_data[freq].trans_latency), file=config, end="") print("0x{:0>2X}UL, 0x{:0>6X}UL, 0x{:0>6X}UL}},\t/* P{} */".format( px_data[freq].bus_latency, px_data[freq].control, px_data[freq].status, p_cnt), file=config) p_cnt += 1
def check_permission(): """Check if it is root permission""" if os.getuid(): parser_lib.print_red( "You need run this tool with root privileges (sudo)!") sys.exit(1)
def check_permission(): """Check if it is root permission""" if os.getuid(): parser_lib.print_red("You need run with sudo!") sys.exit(1)
def store_px_data(sysnode, config): """This will get Px data of power and store it to px data""" px_tmp = PxPkg() px_data = {} with open(sysnode+'cpu0/cpufreq/scaling_driver', 'r') as f_node: freq_driver = f_node.read() if freq_driver.find("acpi-cpufreq") == -1: if freq_driver.find("intel_pstate") == 0: parser_lib.print_red("The tool need to run with acpi_cpufreq driver, " + "please add intel_pstate=disable in kernel cmdline " + "to fall back to acpi-cpufreq driver.", err=True) else: parser_lib.print_red("acpi-cpufreq driver is not found.", err=True) sys.exit(1) try: with open(sysnode+'cpufreq/boost', 'r') as f_node: boost = f_node.read() except IOError: boost = 0 parser_lib.print_yel("CPU turbo is not enabled!") with open(sysnode + 'cpu0/cpufreq/scaling_available_frequencies', 'r') as f_node: freqs = f_node.read() with open(sysnode + 'cpu0/cpufreq/cpuinfo_transition_latency') as f_node: latency = int(f_node.read().strip()) latency = latency//1000 i = 0 p_cnt = 0 for freq in freqs.split(): if boost != 0 and i == 0: try: subprocess.check_call('/usr/sbin/rdmsr 0x1ad', shell=True, stdout=subprocess.PIPE) except subprocess.CalledProcessError: parser_lib.print_red("MSR 0x1ad not support in this platform!", err=True) sys.exit(1) res = subprocess.Popen('/usr/sbin/rdmsr 0x1ad', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) result = res.stdout.readline().strip() #max_ratio_cpu = result[-2:] ctl_state = int(result[-2:], 16) << 8 i += 1 else: ctl_state = int(freq)//100000 << 8 px_tmp.core_freq = int(int(freq) / 1000) px_tmp.power = 0 px_tmp.trans_latency = latency px_tmp.bus_latency = latency px_tmp.control = ctl_state px_tmp.status = ctl_state px_data[freq] = px_tmp print("\t\t{{0x{:0>2X}UL, 0x{:0>2X}UL, 0x{:0>2X}UL, ".format( px_data[freq].core_freq, px_data[freq].power, px_data[freq].trans_latency), file=config, end="") print("0x{:0>2X}UL, 0x{:0>6X}UL, 0x{:0>6X}UL}}, /* P{} */".format( px_data[freq].bus_latency, px_data[freq].control, px_data[freq].status, p_cnt), file=config) p_cnt += 1