info.py

来自「M5,一个功能强大的多处理器系统模拟器.很多针对处理器架构,性能的研究都使用它作」· Python 代码 · 共 768 行 · 第 1/2 页

PY
768
字号
# Copyright (c) 2003, 2004# The Regents of The University of Michigan# All Rights Reserved## This code is part of the M5 simulator.## Permission is granted to use, copy, create derivative works and# redistribute this software and such derivative works for any purpose,# so long as the copyright notice above, this grant of permission, and# the disclaimer below appear in all copies made; and so long as the# name of The University of Michigan is not used in any advertising or# publicity pertaining to the use or distribution of this software# without specific, written prior authorization.## THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE# UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND WITHOUT# WARRANTY BY THE UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER EXPRESS OR# IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE REGENTS OF# THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE FOR ANY DAMAGES,# INCLUDING DIRECT, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL# DAMAGES, WITH RESPECT TO ANY CLAIM ARISING OUT OF OR IN CONNECTION# WITH THE USE OF THE SOFTWARE, EVEN IF IT HAS BEEN OR IS HEREAFTER# ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.## Authors: Nathan L. Binkertfrom __future__ import divisionimport operator, re, typesclass ProxyError(Exception):    passdef unproxy(proxy):    if hasattr(proxy, '__unproxy__'):        return proxy.__unproxy__()    return proxydef scalar(stat):    stat = unproxy(stat)    assert(stat.__scalar__() != stat.__vector__())    return stat.__scalar__()def vector(stat):    stat = unproxy(stat)    assert(stat.__scalar__() != stat.__vector__())    return stat.__vector__()def value(stat, *args):    stat = unproxy(stat)    return stat.__value__(*args)def values(stat, run):    stat = unproxy(stat)    result = []    for i in xrange(len(stat)):        val = value(stat, run, i)        if val is None:            return None        result.append(val)    return resultdef total(stat, run):    return sum(values(stat, run))def len(stat):    stat = unproxy(stat)    return stat.__len__()class Value(object):    def __scalar__(self):        raise AttributeError, "must define __scalar__ for %s" % (type (self))    def __vector__(self):        raise AttributeError, "must define __vector__ for %s" % (type (self))    def __add__(self, other):        return BinaryProxy(operator.__add__, self, other)    def __sub__(self, other):        return BinaryProxy(operator.__sub__, self, other)    def __mul__(self, other):        return BinaryProxy(operator.__mul__, self, other)    def __div__(self, other):        return BinaryProxy(operator.__div__, self, other)    def __truediv__(self, other):        return BinaryProxy(operator.__truediv__, self, other)    def __floordiv__(self, other):        return BinaryProxy(operator.__floordiv__, self, other)    def __radd__(self, other):        return BinaryProxy(operator.__add__, other, self)    def __rsub__(self, other):        return BinaryProxy(operator.__sub__, other, self)    def __rmul__(self, other):        return BinaryProxy(operator.__mul__, other, self)    def __rdiv__(self, other):        return BinaryProxy(operator.__div__, other, self)    def __rtruediv__(self, other):        return BinaryProxy(operator.__truediv__, other, self)    def __rfloordiv__(self, other):        return BinaryProxy(operator.__floordiv__, other, self)    def __neg__(self):        return UnaryProxy(operator.__neg__, self)    def __pos__(self):        return UnaryProxy(operator.__pos__, self)    def __abs__(self):        return UnaryProxy(operator.__abs__, self)class Scalar(Value):    def __scalar__(self):        return True    def __vector__(self):        return False    def __value__(self, run):        raise AttributeError, '__value__ must be defined'class VectorItemProxy(Value):    def __init__(self, proxy, index):        self.proxy = proxy        self.index = index    def __scalar__(self):        return True    def __vector__(self):        return False    def __value__(self, run):        return value(self.proxy, run, self.index)class Vector(Value):    def __scalar__(self):        return False    def __vector__(self):        return True    def __value__(self, run, index):        raise AttributeError, '__value__ must be defined'    def __getitem__(self, index):        return VectorItemProxy(self, index)class ScalarConstant(Scalar):    def __init__(self, constant):        self.constant = constant    def __value__(self, run):        return self.constant    def __str__(self):        return str(self.constant)class VectorConstant(Vector):    def __init__(self, constant):        self.constant = constant    def __value__(self, run, index):        return self.constant[index]    def __len__(self):        return len(self.constant)    def __str__(self):        return str(self.constant)def WrapValue(value):    if isinstance(value, (int, long, float)):        return ScalarConstant(value)    if isinstance(value, (list, tuple)):        return VectorConstant(value)    if isinstance(value, Value):        return value    raise AttributeError, 'Only values can be wrapped'class Statistic(object):    def __getattr__(self, attr):        if attr in ('data', 'x', 'y'):            result = self.source.data(self, self.ticks)            self.data = result.data            self.x = result.x            self.y = result.y        return super(Statistic, self).__getattribute__(attr)    def __setattr__(self, attr, value):        if attr == 'stat':            raise AttributeError, '%s is read only' % stat        if attr in ('source', 'ticks'):            if getattr(self, attr) != value:                if hasattr(self, 'data'):                    delattr(self, 'data')        super(Statistic, self).__setattr__(attr, value)    def __str__(self):        return self.nameclass ValueProxy(Value):    def __getattr__(self, attr):        if attr == '__value__':            if scalar(self):                return self.__scalarvalue__            if vector(self):                return self.__vectorvalue__        if attr == '__len__':            if vector(self):                return self.__vectorlen__        return super(ValueProxy, self).__getattribute__(attr)class UnaryProxy(ValueProxy):    def __init__(self, op, arg):        self.op = op        self.arg = WrapValue(arg)    def __scalar__(self):        return scalar(self.arg)    def __vector__(self):        return vector(self.arg)    def __scalarvalue__(self, run):        val = value(self.arg, run)        if val is None:            return None        return self.op(val)    def __vectorvalue__(self, run, index):        val = value(self.arg, run, index)        if val is None:            return None        return self.op(val)    def __vectorlen__(self):        return len(unproxy(self.arg))    def __str__(self):        if self.op == operator.__neg__:            return '-%s' % str(self.arg)        if self.op == operator.__pos__:            return '+%s' % str(self.arg)        if self.op == operator.__abs__:            return 'abs(%s)' % self.argclass BinaryProxy(ValueProxy):    def __init__(self, op, arg0, arg1):        super(BinaryProxy, self).__init__()        self.op = op        self.arg0 = WrapValue(arg0)        self.arg1 = WrapValue(arg1)    def __scalar__(self):        return scalar(self.arg0) and scalar(self.arg1)    def __vector__(self):        return vector(self.arg0) or vector(self.arg1)    def __scalarvalue__(self, run):        val0 = value(self.arg0, run)        val1 = value(self.arg1, run)        if val0 is None or val1 is None:            return None        try:            return self.op(val0, val1)        except ZeroDivisionError:            return None    def __vectorvalue__(self, run, index):        if scalar(self.arg0):            val0 = value(self.arg0, run)        if vector(self.arg0):            val0 = value(self.arg0, run, index)        if scalar(self.arg1):            val1 = value(self.arg1, run)        if vector(self.arg1):            val1 = value(self.arg1, run, index)        if val0 is None or val1 is None:            return None        try:            return self.op(val0, val1)        except ZeroDivisionError:            return None    def __vectorlen__(self):        if vector(self.arg0) and scalar(self.arg1):            return len(self.arg0)        if scalar(self.arg0) and vector(self.arg1):            return len(self.arg1)        len0 = len(self.arg0)        len1 = len(self.arg1)        if len0 != len1:            raise AttributeError, \                  "vectors of different lengths %d != %d" % (len0, len1)        return len0    def __str__(self):        ops = { operator.__add__ : '+',                operator.__sub__ : '-',                operator.__mul__ : '*',                operator.__div__ : '/',                operator.__truediv__ : '/',                operator.__floordiv__ : '//' }        return '(%s %s %s)' % (str(self.arg0), ops[self.op], str(self.arg1))class Proxy(Value):    def __init__(self, name, dict):        self.name = name        self.dict = dict    def __unproxy__(self):        return unproxy(self.dict[self.name])    def __getitem__(self, index):        return ItemProxy(self, index)    def __getattr__(self, attr):        return AttrProxy(self, attr)    def __str__(self):        return str(self.dict[self.name])class ItemProxy(Proxy):    def __init__(self, proxy, index):        self.proxy = proxy        self.index = index    def __unproxy__(self):        return unproxy(unproxy(self.proxy)[self.index])    def __str__(self):        return '%s[%s]' % (self.proxy, self.index)class AttrProxy(Proxy):    def __init__(self, proxy, attr):        self.proxy = proxy        self.attr = attr    def __unproxy__(self):        proxy = unproxy(self.proxy)        try:            attr = getattr(proxy, self.attr)        except AttributeError, e:            raise ProxyError, e        return unproxy(attr)    def __str__(self):        return '%s.%s' % (self.proxy, self.attr)class ProxyGroup(object):    def __init__(self, dict=None, **kwargs):        self.__dict__['dict'] = {}        if dict is not None:            self.dict.update(dict)        if kwargs:            self.dict.update(kwargs)    def __getattr__(self, name):        return Proxy(name, self.dict)    def __setattr__(self, attr, value):        self.dict[attr] = valueclass ScalarStat(Statistic,Scalar):    def __value__(self, run):        if run not in self.data:            return None        return self.data[run][0][0]    def display(self, run=None):        import display        p = display.Print()        p.name = self.name        p.desc = self.desc        p.value = value(self, run)        p.flags = self.flags        p.precision = self.precision        if display.all or (self.flags & flags.printable):            p.display()

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?