stdlib,tests: Add StatTester SimObject and Scalar tests

This SimObject can be used to quickly test the statistics are
functioning correctly. The SimObject schedules a single event which sets
the statistics to values dependent on the SimObject params.

With this commit the "Scalar" stats have a StatTester subclass that can
be used for testing. More can be added as required.

Tests are included to check our Scalar SimStat functionality.

Change-Id: I78fa5d9a0c3fc7115bd6c6d3410a5436aaa47f55
This commit is contained in:
Bobby R. Bruce
2024-03-25 22:59:20 -07:00
parent dd5a30d41e
commit 2771061207
7 changed files with 481 additions and 2 deletions

View File

@@ -0,0 +1,31 @@
# Copyright (c) 2024 The Regents of the University of California
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer;
# redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution;
# neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Import ("*")
if env['CONF']['USE_TEST_OBJECTS']:
SimObject('StatTester.py', sim_objects=['StatTester', 'ScalarStatTester'])
Source('stat_tester.cc')

View File

@@ -0,0 +1,46 @@
# Copyright (c) 2024 The Regents of the University of California
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met: redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer;
# redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution;
# neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.params import *
from m5.SimObject import SimObject
class StatTester(SimObject):
type = "StatTester"
abstract = True
cxx_header = "test_objects/stat_tester.hh"
cxx_class = "gem5::StatTester"
name = Param.String("stat", "Name of the stat.")
description = Param.String("", "The descriptionof the stat.")
class ScalarStatTester(StatTester):
type = "ScalarStatTester"
cxx_header = "test_objects/stat_tester.hh"
cxx_class = "gem5::ScalarStatTester"
value = Param.Float("The scalar stat's value.")

View File

@@ -0,0 +1,54 @@
/*
* Copyright (c) 2024 The Regents of the University of California
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "test_objects/stat_tester.hh"
#include "base/stats/group.hh"
namespace gem5
{
void
ScalarStatTester::setStats()
{
stats.scalar = params.value;
}
ScalarStatTester::ScalarStatTesterStats::ScalarStatTesterStats(
statistics::Group *parent,
const ScalarStatTesterParams &params
) : statistics::Group(parent),
scalar(this,
params.name.c_str(),
statistics::units::Count::get(),
params.description.c_str()
)
{
}
} // namespace gem5

View File

@@ -0,0 +1,119 @@
/*
* Copyright (c) 2024 The Regents of the University of California
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met: redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer;
* redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution;
* neither the name of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __STAT_TESTER_HH__
#define __STAT_TESTER_HH__
#include "base/statistics.hh"
#include "params/ScalarStatTester.hh"
#include "params/StatTester.hh"
#include "sim/sim_object.hh"
namespace gem5
{
/**
* This classes are used to test the stats system from setting through to
* output. The value of the stats are set via the parameters and when
* included in a simulation will create these stats and update them at Tick
* zero.
*
* Example usage
* -------------
*
* import m5
* from m5.objects import (
* Root,
* ScalarStatTester,
* )
*
* stat_tester = ScalarStatTester()
*
* stat_tester.value = 42
* stat_tester.name = "example"
* root = Root(full_system=False)
* root.stat_tester = stat_tester
*
* m5.instantiate()
* m5.simulate()
*
* When the above is run, the m5out/stats.txt file will contain the
* following:
*
* ---------- Begin Simulation Statistics ----------
* simSeconds 18446744.073710
* simTicks 18446744073709551616
* finalTick 18446744073709551616
* simFreq 1000000000000
* hostSeconds 0.00
* hostTickRate 101355736668733798612992
* hostMemory 403847696
* stat_tester.example 42
* ---------- End Simulation Statistics ----------
*/
class StatTester : public SimObject
{
private:
EventFunctionWrapper event;
public:
StatTester(const StatTesterParams &p) :
SimObject(p),
event([this]{ setStats(); }, name() + ".event")
{}
void startup() override { schedule(event, curTick()); }
protected:
virtual void setStats(){}
};
class ScalarStatTester : public StatTester
{
private:
ScalarStatTesterParams params;
public:
ScalarStatTester(const ScalarStatTesterParams &p) :
StatTester(p), params(p), stats(this, p) {}
protected:
void setStats() override;
struct ScalarStatTesterStats : public statistics::Group
{
ScalarStatTesterStats(
statistics::Group *parent,
const ScalarStatTesterParams &params
);
statistics::Scalar scalar;
} stats;
};
} // namespace gem5
#endif // __STAT_TESTER_HH__