113876Sjavier.bueno@metempsy.com# Copyright (c) 2018 Metempsy Technology Consulting
213876Sjavier.bueno@metempsy.com# All rights reserved.
313876Sjavier.bueno@metempsy.com#
413876Sjavier.bueno@metempsy.com# Redistribution and use in source and binary forms, with or without
513876Sjavier.bueno@metempsy.com# modification, are permitted provided that the following conditions are
613876Sjavier.bueno@metempsy.com# met: redistributions of source code must retain the above copyright
713876Sjavier.bueno@metempsy.com# notice, this list of conditions and the following disclaimer;
813876Sjavier.bueno@metempsy.com# redistributions in binary form must reproduce the above copyright
913876Sjavier.bueno@metempsy.com# notice, this list of conditions and the following disclaimer in the
1013876Sjavier.bueno@metempsy.com# documentation and/or other materials provided with the distribution;
1113876Sjavier.bueno@metempsy.com# neither the name of the copyright holders nor the names of its
1213876Sjavier.bueno@metempsy.com# contributors may be used to endorse or promote products derived from
1313876Sjavier.bueno@metempsy.com# this software without specific prior written permission.
1413876Sjavier.bueno@metempsy.com#
1513876Sjavier.bueno@metempsy.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1613876Sjavier.bueno@metempsy.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1713876Sjavier.bueno@metempsy.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1813876Sjavier.bueno@metempsy.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1913876Sjavier.bueno@metempsy.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2013876Sjavier.bueno@metempsy.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2113876Sjavier.bueno@metempsy.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2213876Sjavier.bueno@metempsy.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2313876Sjavier.bueno@metempsy.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2413876Sjavier.bueno@metempsy.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2513876Sjavier.bueno@metempsy.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2613876Sjavier.bueno@metempsy.com#
2713876Sjavier.bueno@metempsy.com# Authors: Pau Cabre
2813876Sjavier.bueno@metempsy.com
2913876Sjavier.bueno@metempsy.com# This file is a copy of MemConfig.py / CpuConfig.py, but modified to
3013876Sjavier.bueno@metempsy.com# hanle branch predictors instead of memory controllers / CPUs
3113876Sjavier.bueno@metempsy.com
3213876Sjavier.bueno@metempsy.comfrom __future__ import print_function
3313876Sjavier.bueno@metempsy.comfrom __future__ import absolute_import
3413876Sjavier.bueno@metempsy.com
3513876Sjavier.bueno@metempsy.comfrom m5 import fatal
3613876Sjavier.bueno@metempsy.comimport m5.objects
3713876Sjavier.bueno@metempsy.comimport inspect
3813876Sjavier.bueno@metempsy.comimport sys
3913876Sjavier.bueno@metempsy.comfrom textwrap import TextWrapper
4013876Sjavier.bueno@metempsy.com
4113876Sjavier.bueno@metempsy.com# Dictionary of mapping names of real branch predictor models to classes.
4213876Sjavier.bueno@metempsy.com_hwp_classes = {}
4313876Sjavier.bueno@metempsy.com
4413876Sjavier.bueno@metempsy.com
4513876Sjavier.bueno@metempsy.comdef is_hwp_class(cls):
4613876Sjavier.bueno@metempsy.com    """Determine if a class is a prefetcher that can be instantiated"""
4713876Sjavier.bueno@metempsy.com
4813876Sjavier.bueno@metempsy.com    # We can't use the normal inspect.isclass because the ParamFactory
4913876Sjavier.bueno@metempsy.com    # and ProxyFactory classes have a tendency to confuse it.
5013876Sjavier.bueno@metempsy.com    try:
5113876Sjavier.bueno@metempsy.com        return issubclass(cls, m5.objects.BasePrefetcher) and \
5213876Sjavier.bueno@metempsy.com            not cls.abstract
5313876Sjavier.bueno@metempsy.com    except (TypeError, AttributeError):
5413876Sjavier.bueno@metempsy.com        return False
5513876Sjavier.bueno@metempsy.com
5613876Sjavier.bueno@metempsy.comdef get(name):
5713876Sjavier.bueno@metempsy.com    """Get a HWP class from a user provided class name or alias."""
5813876Sjavier.bueno@metempsy.com
5913876Sjavier.bueno@metempsy.com    try:
6013876Sjavier.bueno@metempsy.com        hwp_class = _hwp_classes[name]
6113876Sjavier.bueno@metempsy.com        return hwp_class
6213876Sjavier.bueno@metempsy.com    except KeyError:
6313876Sjavier.bueno@metempsy.com        print("%s is not a valid HWP model." % (name,))
6413876Sjavier.bueno@metempsy.com        sys.exit(1)
6513876Sjavier.bueno@metempsy.com
6613876Sjavier.bueno@metempsy.comdef print_hwp_list():
6713876Sjavier.bueno@metempsy.com    """Print a list of available HWP classes."""
6813876Sjavier.bueno@metempsy.com
6913876Sjavier.bueno@metempsy.com    print("Available Hardware Prefetcher classes:")
7013876Sjavier.bueno@metempsy.com    doc_wrapper = TextWrapper(initial_indent="\t\t", subsequent_indent="\t\t")
7113876Sjavier.bueno@metempsy.com    for name, cls in _hwp_classes.items():
7213876Sjavier.bueno@metempsy.com        print("\t%s" % name)
7313876Sjavier.bueno@metempsy.com
7413876Sjavier.bueno@metempsy.com        # Try to extract the class documentation from the class help
7513876Sjavier.bueno@metempsy.com        # string.
7613876Sjavier.bueno@metempsy.com        doc = inspect.getdoc(cls)
7713876Sjavier.bueno@metempsy.com        if doc:
7813876Sjavier.bueno@metempsy.com            for line in doc_wrapper.wrap(doc):
7913876Sjavier.bueno@metempsy.com                print(line)
8013876Sjavier.bueno@metempsy.com
8113876Sjavier.bueno@metempsy.comdef hwp_names():
8213876Sjavier.bueno@metempsy.com    """Return a list of valid Hardware Prefetcher names."""
8313876Sjavier.bueno@metempsy.com    return list(_hwp_classes.keys())
8413876Sjavier.bueno@metempsy.com
8513876Sjavier.bueno@metempsy.com# Add all HWPs in the object hierarchy.
8613876Sjavier.bueno@metempsy.comfor name, cls in inspect.getmembers(m5.objects, is_hwp_class):
8713876Sjavier.bueno@metempsy.com    _hwp_classes[name] = cls
8813876Sjavier.bueno@metempsy.com
89