FileSystemConfig.py revision 13885
113883Sdavid.hashe@amd.com# Copyright (c) 2015 Advanced Micro Devices, Inc. 213883Sdavid.hashe@amd.com# All rights reserved 313883Sdavid.hashe@amd.com# 413883Sdavid.hashe@amd.com# Redistribution and use in source and binary forms, with or without 513883Sdavid.hashe@amd.com# modification, are permitted provided that the following conditions are 613883Sdavid.hashe@amd.com# met: redistributions of source code must retain the above copyright 713883Sdavid.hashe@amd.com# notice, this list of conditions and the following disclaimer; 813883Sdavid.hashe@amd.com# redistributions in binary form must reproduce the above copyright 913883Sdavid.hashe@amd.com# notice, this list of conditions and the following disclaimer in the 1013883Sdavid.hashe@amd.com# documentation and/or other materials provided with the distribution; 1113883Sdavid.hashe@amd.com# neither the name of the copyright holders nor the names of its 1213883Sdavid.hashe@amd.com# contributors may be used to endorse or promote products derived from 1313883Sdavid.hashe@amd.com# this software without specific prior written permission. 1413883Sdavid.hashe@amd.com# 1513883Sdavid.hashe@amd.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1613883Sdavid.hashe@amd.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1713883Sdavid.hashe@amd.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1813883Sdavid.hashe@amd.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1913883Sdavid.hashe@amd.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2013883Sdavid.hashe@amd.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2113883Sdavid.hashe@amd.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2213883Sdavid.hashe@amd.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2313883Sdavid.hashe@amd.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2413883Sdavid.hashe@amd.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2513883Sdavid.hashe@amd.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2613883Sdavid.hashe@amd.com# 2713883Sdavid.hashe@amd.com# Authors: David Hashe 2813883Sdavid.hashe@amd.com 2913883Sdavid.hashe@amd.comfrom __future__ import print_function 3013883Sdavid.hashe@amd.com 3113883Sdavid.hashe@amd.comimport m5 3213883Sdavid.hashe@amd.comfrom m5.objects import * 3313883Sdavid.hashe@amd.comfrom m5.util.convert import * 3413883Sdavid.hashe@amd.com 3513883Sdavid.hashe@amd.comimport operator, os, platform, getpass 3613883Sdavid.hashe@amd.comfrom os import mkdir, makedirs, getpid, listdir, stat, access 3713883Sdavid.hashe@amd.comfrom pwd import getpwuid 3813883Sdavid.hashe@amd.comfrom os.path import join as joinpath 3913883Sdavid.hashe@amd.comfrom os.path import isdir 4013883Sdavid.hashe@amd.comfrom shutil import rmtree, copyfile 4113883Sdavid.hashe@amd.com 4213883Sdavid.hashe@amd.comdef hex_mask(terms): 4313883Sdavid.hashe@amd.com dec_mask = reduce(operator.or_, [2**i for i in terms], 0) 4413883Sdavid.hashe@amd.com return "%08x" % dec_mask 4513883Sdavid.hashe@amd.com 4613883Sdavid.hashe@amd.comdef file_append(path, contents): 4713883Sdavid.hashe@amd.com with open(joinpath(*path), 'a') as f: 4813883Sdavid.hashe@amd.com f.write(str(contents)) 4913883Sdavid.hashe@amd.com 5013883Sdavid.hashe@amd.comdef replace_tree(path): 5113883Sdavid.hashe@amd.com if isdir(path): 5213883Sdavid.hashe@amd.com rmtree(path) 5313883Sdavid.hashe@amd.com mkdir(path) 5413883Sdavid.hashe@amd.com 5513883Sdavid.hashe@amd.comdef config_filesystem(options): 5613883Sdavid.hashe@amd.com fsdir = joinpath(m5.options.outdir, 'fs') 5713883Sdavid.hashe@amd.com replace_tree(fsdir) 5813883Sdavid.hashe@amd.com 5913883Sdavid.hashe@amd.com # Set up /proc 6013883Sdavid.hashe@amd.com procdir = joinpath(fsdir, 'proc') 6113883Sdavid.hashe@amd.com mkdir(procdir) 6213883Sdavid.hashe@amd.com 6313883Sdavid.hashe@amd.com for i in xrange(options.num_cpus): 6413885Sdavid.hashe@amd.com one_cpu = 'processor : %d\n' % (i) + \ 6513883Sdavid.hashe@amd.com 'vendor_id : Generic\n' + \ 6613883Sdavid.hashe@amd.com 'cpu family : 0\n' + \ 6713883Sdavid.hashe@amd.com 'model : 0\n' + \ 6813883Sdavid.hashe@amd.com 'model name : Generic\n' + \ 6913883Sdavid.hashe@amd.com 'stepping : 0\n' + \ 7013883Sdavid.hashe@amd.com 'cpu MHz : %0.3d\n' \ 7113883Sdavid.hashe@amd.com % (toFrequency(options.cpu_clock)/mega) + \ 7213883Sdavid.hashe@amd.com 'cache size: : %dK\n' \ 7313883Sdavid.hashe@amd.com % (toMemorySize(options.l2_size)/kibi) + \ 7413883Sdavid.hashe@amd.com 'physical id : 0\n' + \ 7513883Sdavid.hashe@amd.com 'siblings : %s\n' \ 7613883Sdavid.hashe@amd.com % options.num_cpus + \ 7713883Sdavid.hashe@amd.com 'core id : %d\n' \ 7813883Sdavid.hashe@amd.com % i + \ 7913883Sdavid.hashe@amd.com 'cpu cores : %d\n' \ 8013883Sdavid.hashe@amd.com % options.num_cpus + \ 8113883Sdavid.hashe@amd.com 'fpu : yes\n' + \ 8213883Sdavid.hashe@amd.com 'fpu exception : yes\n' + \ 8313883Sdavid.hashe@amd.com 'cpuid level : 1\n' + \ 8413883Sdavid.hashe@amd.com 'wp : yes\n' + \ 8513883Sdavid.hashe@amd.com 'flags : fpu\n' + \ 8613883Sdavid.hashe@amd.com 'cache alignment : %d\n' \ 8713883Sdavid.hashe@amd.com % options.cacheline_size + \ 8813883Sdavid.hashe@amd.com '\n' 8913883Sdavid.hashe@amd.com file_append((procdir, 'cpuinfo'), one_cpu) 9013883Sdavid.hashe@amd.com 9113883Sdavid.hashe@amd.com file_append((procdir, 'stat'), 'cpu 0 0 0 0 0 0 0\n') 9213883Sdavid.hashe@amd.com for i in xrange(options.num_cpus): 9313883Sdavid.hashe@amd.com file_append((procdir, 'stat'), 'cpu%d 0 0 0 0 0 0 0\n' % i) 9413883Sdavid.hashe@amd.com 9513883Sdavid.hashe@amd.com # Set up /sys 9613883Sdavid.hashe@amd.com sysdir = joinpath(fsdir, 'sys') 9713883Sdavid.hashe@amd.com mkdir(sysdir) 9813883Sdavid.hashe@amd.com 9913883Sdavid.hashe@amd.com # Set up /sys/devices/system/cpu 10013883Sdavid.hashe@amd.com cpudir = joinpath(sysdir, 'devices', 'system', 'cpu') 10113883Sdavid.hashe@amd.com makedirs(cpudir) 10213883Sdavid.hashe@amd.com 10313883Sdavid.hashe@amd.com file_append((cpudir, 'online'), '0-%d' % (options.num_cpus-1)) 10413883Sdavid.hashe@amd.com file_append((cpudir, 'possible'), '0-%d' % (options.num_cpus-1)) 10513883Sdavid.hashe@amd.com 10613883Sdavid.hashe@amd.com # Set up /tmp 10713883Sdavid.hashe@amd.com tmpdir = joinpath(fsdir, 'tmp') 10813883Sdavid.hashe@amd.com replace_tree(tmpdir) 10913883Sdavid.hashe@amd.com 11013883Sdavid.hashe@amd.comdef register_node(cpu_list, mem, node_number): 11113883Sdavid.hashe@amd.com nodebasedir = joinpath(m5.options.outdir, 'fs', 'sys', 'devices', 11213883Sdavid.hashe@amd.com 'system', 'node') 11313883Sdavid.hashe@amd.com 11413883Sdavid.hashe@amd.com nodedir = joinpath(nodebasedir,'node%d' % node_number) 11513883Sdavid.hashe@amd.com makedirs(nodedir) 11613883Sdavid.hashe@amd.com 11713883Sdavid.hashe@amd.com file_append((nodedir, 'cpumap'), hex_mask(cpu_list)) 11813883Sdavid.hashe@amd.com file_append((nodedir, 'meminfo'), 11913883Sdavid.hashe@amd.com 'Node %d MemTotal: %dkB' % (node_number, 12013883Sdavid.hashe@amd.com toMemorySize(str(mem))/kibi)) 12113883Sdavid.hashe@amd.com 12213883Sdavid.hashe@amd.comdef register_cpu(physical_package_id, core_siblings, 12313883Sdavid.hashe@amd.com core_id, thread_siblings): 12413883Sdavid.hashe@amd.com cpudir = joinpath(m5.options.outdir, 'fs', 'sys', 'devices', 'system', 12513883Sdavid.hashe@amd.com 'cpu', 'cpu%d' % core_id) 12613883Sdavid.hashe@amd.com 12713883Sdavid.hashe@amd.com if not isdir(joinpath(cpudir, 'topology')): 12813883Sdavid.hashe@amd.com makedirs(joinpath(cpudir, 'topology')) 12913883Sdavid.hashe@amd.com if not isdir(joinpath(cpudir, 'cache')): 13013883Sdavid.hashe@amd.com makedirs(joinpath(cpudir, 'cache')) 13113883Sdavid.hashe@amd.com 13213883Sdavid.hashe@amd.com file_append((cpudir, 'online'), '1') 13313883Sdavid.hashe@amd.com file_append((cpudir, 'topology', 'physical_package_id'), 13413883Sdavid.hashe@amd.com physical_package_id) 13513883Sdavid.hashe@amd.com file_append((cpudir, 'topology', 'core_siblings'), 13613883Sdavid.hashe@amd.com hex_mask(core_siblings)) 13713883Sdavid.hashe@amd.com file_append((cpudir, 'topology', 'core_id'), core_id) 13813883Sdavid.hashe@amd.com file_append((cpudir, 'topology', 'thread_siblings'), 13913883Sdavid.hashe@amd.com hex_mask(thread_siblings)) 14013883Sdavid.hashe@amd.com 14113883Sdavid.hashe@amd.comdef register_cache(level, idu_type, size, line_size, assoc, cpus): 14213883Sdavid.hashe@amd.com fsdir = joinpath(m5.options.outdir, 'fs') 14313883Sdavid.hashe@amd.com for i in cpus: 14413883Sdavid.hashe@amd.com cachedir = joinpath(fsdir, 'sys', 'devices', 'system', 'cpu', 14513883Sdavid.hashe@amd.com 'cpu%d' % i, 'cache') 14613883Sdavid.hashe@amd.com 14713883Sdavid.hashe@amd.com j = 0 14813883Sdavid.hashe@amd.com while isdir(joinpath(cachedir, 'index%d' % j)): 14913883Sdavid.hashe@amd.com j += 1 15013883Sdavid.hashe@amd.com indexdir = joinpath(cachedir, 'index%d' % j) 15113883Sdavid.hashe@amd.com makedirs(indexdir) 15213883Sdavid.hashe@amd.com 15313883Sdavid.hashe@amd.com file_append((indexdir, 'level'), level) 15413883Sdavid.hashe@amd.com file_append((indexdir, 'type'), idu_type) 15513883Sdavid.hashe@amd.com file_append((indexdir, 'size'), "%dK" % (toMemorySize(size)/kibi)) 15613883Sdavid.hashe@amd.com file_append((indexdir, 'coherency_line_size'), line_size) 15713883Sdavid.hashe@amd.com 15813883Sdavid.hashe@amd.com # Since cache size = number of indices * associativity * block size 15913883Sdavid.hashe@amd.com num_sets = toMemorySize(size) / int(assoc) * int(line_size) 16013883Sdavid.hashe@amd.com 16113883Sdavid.hashe@amd.com file_append((indexdir, 'number_of_sets'), num_sets) 16213883Sdavid.hashe@amd.com file_append((indexdir, 'physical_line_partition'), '1') 16313883Sdavid.hashe@amd.com file_append((indexdir, 'shared_cpu_map'), hex_mask(cpus)) 16413883Sdavid.hashe@amd.com 16513883Sdavid.hashe@amd.comdef redirect_paths(chroot): 16613883Sdavid.hashe@amd.com # Redirect filesystem syscalls from src to the first matching dests 16713883Sdavid.hashe@amd.com redirect_paths = [RedirectPath(app_path = "/proc", 16813883Sdavid.hashe@amd.com host_paths = ["%s/fs/proc" % m5.options.outdir]), 16913883Sdavid.hashe@amd.com RedirectPath(app_path = "/sys", 17013883Sdavid.hashe@amd.com host_paths = ["%s/fs/sys" % m5.options.outdir]), 17113883Sdavid.hashe@amd.com RedirectPath(app_path = "/tmp", 17213883Sdavid.hashe@amd.com host_paths = ["%s/fs/tmp" % m5.options.outdir]), 17313883Sdavid.hashe@amd.com RedirectPath(app_path = "/", 17413883Sdavid.hashe@amd.com host_paths = ["%s" % chroot])] 17513883Sdavid.hashe@amd.com return redirect_paths 176