FileSystemConfig.py revision 13952
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 6313952Sbrandon.potter@amd.com cpu_clock = '0' 6413952Sbrandon.potter@amd.com if hasattr(options, 'cpu_clock'): 6513952Sbrandon.potter@amd.com cpu_clock = options.cpu_clock 6613952Sbrandon.potter@amd.com cpu_clock = toFrequency(cpu_clock)/mega 6713952Sbrandon.potter@amd.com 6813952Sbrandon.potter@amd.com l2_size = '0' 6913952Sbrandon.potter@amd.com if hasattr(options, 'l2_size'): 7013952Sbrandon.potter@amd.com l2_size = options.l2_size 7113952Sbrandon.potter@amd.com l2_size = toMemorySize(l2_size)/kibi 7213952Sbrandon.potter@amd.com 7313952Sbrandon.potter@amd.com cacheline_size = '0' 7413952Sbrandon.potter@amd.com if hasattr(options, 'cacheline_size'): 7513952Sbrandon.potter@amd.com cacheline_size = options.cacheline_size 7613952Sbrandon.potter@amd.com 7713883Sdavid.hashe@amd.com for i in xrange(options.num_cpus): 7813885Sdavid.hashe@amd.com one_cpu = 'processor : %d\n' % (i) + \ 7913883Sdavid.hashe@amd.com 'vendor_id : Generic\n' + \ 8013883Sdavid.hashe@amd.com 'cpu family : 0\n' + \ 8113883Sdavid.hashe@amd.com 'model : 0\n' + \ 8213883Sdavid.hashe@amd.com 'model name : Generic\n' + \ 8313883Sdavid.hashe@amd.com 'stepping : 0\n' + \ 8413883Sdavid.hashe@amd.com 'cpu MHz : %0.3d\n' \ 8513952Sbrandon.potter@amd.com % cpu_clock + \ 8613883Sdavid.hashe@amd.com 'cache size: : %dK\n' \ 8713952Sbrandon.potter@amd.com % l2_size + \ 8813883Sdavid.hashe@amd.com 'physical id : 0\n' + \ 8913883Sdavid.hashe@amd.com 'siblings : %s\n' \ 9013883Sdavid.hashe@amd.com % options.num_cpus + \ 9113883Sdavid.hashe@amd.com 'core id : %d\n' \ 9213883Sdavid.hashe@amd.com % i + \ 9313883Sdavid.hashe@amd.com 'cpu cores : %d\n' \ 9413883Sdavid.hashe@amd.com % options.num_cpus + \ 9513883Sdavid.hashe@amd.com 'fpu : yes\n' + \ 9613883Sdavid.hashe@amd.com 'fpu exception : yes\n' + \ 9713883Sdavid.hashe@amd.com 'cpuid level : 1\n' + \ 9813883Sdavid.hashe@amd.com 'wp : yes\n' + \ 9913883Sdavid.hashe@amd.com 'flags : fpu\n' + \ 10013883Sdavid.hashe@amd.com 'cache alignment : %d\n' \ 10113952Sbrandon.potter@amd.com % cacheline_size + \ 10213883Sdavid.hashe@amd.com '\n' 10313883Sdavid.hashe@amd.com file_append((procdir, 'cpuinfo'), one_cpu) 10413883Sdavid.hashe@amd.com 10513883Sdavid.hashe@amd.com file_append((procdir, 'stat'), 'cpu 0 0 0 0 0 0 0\n') 10613883Sdavid.hashe@amd.com for i in xrange(options.num_cpus): 10713883Sdavid.hashe@amd.com file_append((procdir, 'stat'), 'cpu%d 0 0 0 0 0 0 0\n' % i) 10813883Sdavid.hashe@amd.com 10913883Sdavid.hashe@amd.com # Set up /sys 11013883Sdavid.hashe@amd.com sysdir = joinpath(fsdir, 'sys') 11113883Sdavid.hashe@amd.com mkdir(sysdir) 11213883Sdavid.hashe@amd.com 11313883Sdavid.hashe@amd.com # Set up /sys/devices/system/cpu 11413883Sdavid.hashe@amd.com cpudir = joinpath(sysdir, 'devices', 'system', 'cpu') 11513883Sdavid.hashe@amd.com makedirs(cpudir) 11613883Sdavid.hashe@amd.com 11713883Sdavid.hashe@amd.com file_append((cpudir, 'online'), '0-%d' % (options.num_cpus-1)) 11813883Sdavid.hashe@amd.com file_append((cpudir, 'possible'), '0-%d' % (options.num_cpus-1)) 11913883Sdavid.hashe@amd.com 12013883Sdavid.hashe@amd.com # Set up /tmp 12113883Sdavid.hashe@amd.com tmpdir = joinpath(fsdir, 'tmp') 12213883Sdavid.hashe@amd.com replace_tree(tmpdir) 12313883Sdavid.hashe@amd.com 12413883Sdavid.hashe@amd.comdef register_node(cpu_list, mem, node_number): 12513883Sdavid.hashe@amd.com nodebasedir = joinpath(m5.options.outdir, 'fs', 'sys', 'devices', 12613883Sdavid.hashe@amd.com 'system', 'node') 12713883Sdavid.hashe@amd.com 12813883Sdavid.hashe@amd.com nodedir = joinpath(nodebasedir,'node%d' % node_number) 12913883Sdavid.hashe@amd.com makedirs(nodedir) 13013883Sdavid.hashe@amd.com 13113883Sdavid.hashe@amd.com file_append((nodedir, 'cpumap'), hex_mask(cpu_list)) 13213883Sdavid.hashe@amd.com file_append((nodedir, 'meminfo'), 13313883Sdavid.hashe@amd.com 'Node %d MemTotal: %dkB' % (node_number, 13413883Sdavid.hashe@amd.com toMemorySize(str(mem))/kibi)) 13513883Sdavid.hashe@amd.com 13613883Sdavid.hashe@amd.comdef register_cpu(physical_package_id, core_siblings, 13713883Sdavid.hashe@amd.com core_id, thread_siblings): 13813883Sdavid.hashe@amd.com cpudir = joinpath(m5.options.outdir, 'fs', 'sys', 'devices', 'system', 13913883Sdavid.hashe@amd.com 'cpu', 'cpu%d' % core_id) 14013883Sdavid.hashe@amd.com 14113883Sdavid.hashe@amd.com if not isdir(joinpath(cpudir, 'topology')): 14213883Sdavid.hashe@amd.com makedirs(joinpath(cpudir, 'topology')) 14313883Sdavid.hashe@amd.com if not isdir(joinpath(cpudir, 'cache')): 14413883Sdavid.hashe@amd.com makedirs(joinpath(cpudir, 'cache')) 14513883Sdavid.hashe@amd.com 14613883Sdavid.hashe@amd.com file_append((cpudir, 'online'), '1') 14713883Sdavid.hashe@amd.com file_append((cpudir, 'topology', 'physical_package_id'), 14813883Sdavid.hashe@amd.com physical_package_id) 14913883Sdavid.hashe@amd.com file_append((cpudir, 'topology', 'core_siblings'), 15013883Sdavid.hashe@amd.com hex_mask(core_siblings)) 15113883Sdavid.hashe@amd.com file_append((cpudir, 'topology', 'core_id'), core_id) 15213883Sdavid.hashe@amd.com file_append((cpudir, 'topology', 'thread_siblings'), 15313883Sdavid.hashe@amd.com hex_mask(thread_siblings)) 15413883Sdavid.hashe@amd.com 15513883Sdavid.hashe@amd.comdef register_cache(level, idu_type, size, line_size, assoc, cpus): 15613883Sdavid.hashe@amd.com fsdir = joinpath(m5.options.outdir, 'fs') 15713883Sdavid.hashe@amd.com for i in cpus: 15813883Sdavid.hashe@amd.com cachedir = joinpath(fsdir, 'sys', 'devices', 'system', 'cpu', 15913883Sdavid.hashe@amd.com 'cpu%d' % i, 'cache') 16013883Sdavid.hashe@amd.com 16113883Sdavid.hashe@amd.com j = 0 16213883Sdavid.hashe@amd.com while isdir(joinpath(cachedir, 'index%d' % j)): 16313883Sdavid.hashe@amd.com j += 1 16413883Sdavid.hashe@amd.com indexdir = joinpath(cachedir, 'index%d' % j) 16513883Sdavid.hashe@amd.com makedirs(indexdir) 16613883Sdavid.hashe@amd.com 16713883Sdavid.hashe@amd.com file_append((indexdir, 'level'), level) 16813883Sdavid.hashe@amd.com file_append((indexdir, 'type'), idu_type) 16913883Sdavid.hashe@amd.com file_append((indexdir, 'size'), "%dK" % (toMemorySize(size)/kibi)) 17013883Sdavid.hashe@amd.com file_append((indexdir, 'coherency_line_size'), line_size) 17113883Sdavid.hashe@amd.com 17213883Sdavid.hashe@amd.com # Since cache size = number of indices * associativity * block size 17313883Sdavid.hashe@amd.com num_sets = toMemorySize(size) / int(assoc) * int(line_size) 17413883Sdavid.hashe@amd.com 17513883Sdavid.hashe@amd.com file_append((indexdir, 'number_of_sets'), num_sets) 17613883Sdavid.hashe@amd.com file_append((indexdir, 'physical_line_partition'), '1') 17713883Sdavid.hashe@amd.com file_append((indexdir, 'shared_cpu_map'), hex_mask(cpus)) 17813883Sdavid.hashe@amd.com 17913883Sdavid.hashe@amd.comdef redirect_paths(chroot): 18013883Sdavid.hashe@amd.com # Redirect filesystem syscalls from src to the first matching dests 18113883Sdavid.hashe@amd.com redirect_paths = [RedirectPath(app_path = "/proc", 18213883Sdavid.hashe@amd.com host_paths = ["%s/fs/proc" % m5.options.outdir]), 18313883Sdavid.hashe@amd.com RedirectPath(app_path = "/sys", 18413883Sdavid.hashe@amd.com host_paths = ["%s/fs/sys" % m5.options.outdir]), 18513883Sdavid.hashe@amd.com RedirectPath(app_path = "/tmp", 18613883Sdavid.hashe@amd.com host_paths = ["%s/fs/tmp" % m5.options.outdir]), 18713883Sdavid.hashe@amd.com RedirectPath(app_path = "/", 18813883Sdavid.hashe@amd.com host_paths = ["%s" % chroot])] 18913883Sdavid.hashe@amd.com return redirect_paths 190