BPConfig.py revision 13774:a1be2a0c55f2
113669Sjavier.bueno@metempsy.com# Copyright (c) 2018 Metempsy Technology Consulting
213669Sjavier.bueno@metempsy.com# All rights reserved.
313669Sjavier.bueno@metempsy.com#
413669Sjavier.bueno@metempsy.com# Redistribution and use in source and binary forms, with or without
513669Sjavier.bueno@metempsy.com# modification, are permitted provided that the following conditions are
613669Sjavier.bueno@metempsy.com# met: redistributions of source code must retain the above copyright
713669Sjavier.bueno@metempsy.com# notice, this list of conditions and the following disclaimer;
813669Sjavier.bueno@metempsy.com# redistributions in binary form must reproduce the above copyright
913669Sjavier.bueno@metempsy.com# notice, this list of conditions and the following disclaimer in the
1013669Sjavier.bueno@metempsy.com# documentation and/or other materials provided with the distribution;
1113669Sjavier.bueno@metempsy.com# neither the name of the copyright holders nor the names of its
1213669Sjavier.bueno@metempsy.com# contributors may be used to endorse or promote products derived from
1313669Sjavier.bueno@metempsy.com# this software without specific prior written permission.
1413669Sjavier.bueno@metempsy.com#
1513669Sjavier.bueno@metempsy.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1613669Sjavier.bueno@metempsy.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1713669Sjavier.bueno@metempsy.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1813669Sjavier.bueno@metempsy.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1913669Sjavier.bueno@metempsy.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2013669Sjavier.bueno@metempsy.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2113669Sjavier.bueno@metempsy.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2213669Sjavier.bueno@metempsy.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2313669Sjavier.bueno@metempsy.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2413669Sjavier.bueno@metempsy.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2513669Sjavier.bueno@metempsy.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2613669Sjavier.bueno@metempsy.com#
2713669Sjavier.bueno@metempsy.com# Authors: Pau Cabre
2813669Sjavier.bueno@metempsy.com
2913669Sjavier.bueno@metempsy.com# This file is a copy of MemConfig.py / CpuConfig.py, but modified to
3013669Sjavier.bueno@metempsy.com# hanle branch predictors instead of memory controllers / CPUs
3113669Sjavier.bueno@metempsy.com
3213669Sjavier.bueno@metempsy.comfrom __future__ import print_function
3313669Sjavier.bueno@metempsy.comfrom __future__ import absolute_import
3413669Sjavier.bueno@metempsy.com
3513669Sjavier.bueno@metempsy.comfrom m5 import fatal
3613669Sjavier.bueno@metempsy.comimport m5.objects
3713669Sjavier.bueno@metempsy.comimport inspect
3813669Sjavier.bueno@metempsy.comimport sys
3913669Sjavier.bueno@metempsy.comfrom textwrap import TextWrapper
4013669Sjavier.bueno@metempsy.com
4113669Sjavier.bueno@metempsy.com# Dictionary of mapping names of real branch predictor models to classes.
4213669Sjavier.bueno@metempsy.com_bp_classes = {}
4313669Sjavier.bueno@metempsy.com
4413963Sodanrc@yahoo.com.br
4513669Sjavier.bueno@metempsy.comdef is_bp_class(cls):
4613669Sjavier.bueno@metempsy.com    """Determine if a class is a branch predictor that can be instantiated"""
4713669Sjavier.bueno@metempsy.com
4813669Sjavier.bueno@metempsy.com    # We can't use the normal inspect.isclass because the ParamFactory
4913669Sjavier.bueno@metempsy.com    # and ProxyFactory classes have a tendency to confuse it.
5013669Sjavier.bueno@metempsy.com    try:
5113669Sjavier.bueno@metempsy.com        return issubclass(cls, m5.objects.BranchPredictor) and \
5213669Sjavier.bueno@metempsy.com            not cls.abstract
5313669Sjavier.bueno@metempsy.com    except (TypeError, AttributeError):
5413669Sjavier.bueno@metempsy.com        return False
5513669Sjavier.bueno@metempsy.com
5613669Sjavier.bueno@metempsy.comdef get(name):
5713669Sjavier.bueno@metempsy.com    """Get a BP class from a user provided class name or alias."""
5813669Sjavier.bueno@metempsy.com
5913669Sjavier.bueno@metempsy.com    try:
6013669Sjavier.bueno@metempsy.com        bp_class = _bp_classes[name]
6113669Sjavier.bueno@metempsy.com        return bp_class
6213669Sjavier.bueno@metempsy.com    except KeyError:
6313669Sjavier.bueno@metempsy.com        print("%s is not a valid BP model." % (name,))
6413669Sjavier.bueno@metempsy.com        sys.exit(1)
6513669Sjavier.bueno@metempsy.com
6613669Sjavier.bueno@metempsy.comdef print_bp_list():
6713669Sjavier.bueno@metempsy.com    """Print a list of available BP classes."""
6813669Sjavier.bueno@metempsy.com
6913669Sjavier.bueno@metempsy.com    print("Available BranchPredictor classes:")
7013669Sjavier.bueno@metempsy.com    doc_wrapper = TextWrapper(initial_indent="\t\t", subsequent_indent="\t\t")
7113669Sjavier.bueno@metempsy.com    for name, cls in _bp_classes.items():
7213669Sjavier.bueno@metempsy.com        print("\t%s" % name)
7313963Sodanrc@yahoo.com.br
7413963Sodanrc@yahoo.com.br        # Try to extract the class documentation from the class help
7513669Sjavier.bueno@metempsy.com        # string.
7613669Sjavier.bueno@metempsy.com        doc = inspect.getdoc(cls)
7713669Sjavier.bueno@metempsy.com        if doc:
7813669Sjavier.bueno@metempsy.com            for line in doc_wrapper.wrap(doc):
7913669Sjavier.bueno@metempsy.com                print(line)
8013669Sjavier.bueno@metempsy.com
8113669Sjavier.bueno@metempsy.comdef bp_names():
8213669Sjavier.bueno@metempsy.com    """Return a list of valid Branch Predictor names."""
8313669Sjavier.bueno@metempsy.com    return list(_bp_classes.keys())
8413963Sodanrc@yahoo.com.br
8513963Sodanrc@yahoo.com.br# Add all BPs in the object hierarchy.
8613669Sjavier.bueno@metempsy.comfor name, cls in inspect.getmembers(m5.objects, is_bp_class):
8713669Sjavier.bueno@metempsy.com    _bp_classes[name] = cls
8813669Sjavier.bueno@metempsy.com
8913669Sjavier.bueno@metempsy.com