stat_control.cc revision 9983
1695SN/A/*
29262Ssascha.bischoff@arm.com * Copyright (c) 2012 ARM Limited
39262Ssascha.bischoff@arm.com * All rights reserved
49262Ssascha.bischoff@arm.com *
59262Ssascha.bischoff@arm.com * The license below extends only to copyright in the software and shall
69262Ssascha.bischoff@arm.com * not be construed as granting a license to any other intellectual
79262Ssascha.bischoff@arm.com * property including but not limited to intellectual property relating
89262Ssascha.bischoff@arm.com * to a hardware implementation of the functionality of the software
99262Ssascha.bischoff@arm.com * licensed hereunder.  You may use the software subject to the license
109262Ssascha.bischoff@arm.com * terms below provided that you ensure that this notice is replicated
119262Ssascha.bischoff@arm.com * unmodified and in its entirety in all distributions of the software,
129262Ssascha.bischoff@arm.com * modified or unmodified, in source code or in binary form.
139262Ssascha.bischoff@arm.com *
141762SN/A * Copyright (c) 2004-2005 The Regents of The University of Michigan
159983Sstever@gmail.com * Copyright (c) 2013 Advanced Micro Devices, Inc.
169983Sstever@gmail.com * Copyright (c) 2013 Mark D. Hill and David A. Wood
17695SN/A * All rights reserved.
18695SN/A *
19695SN/A * Redistribution and use in source and binary forms, with or without
20695SN/A * modification, are permitted provided that the following conditions are
21695SN/A * met: redistributions of source code must retain the above copyright
22695SN/A * notice, this list of conditions and the following disclaimer;
23695SN/A * redistributions in binary form must reproduce the above copyright
24695SN/A * notice, this list of conditions and the following disclaimer in the
25695SN/A * documentation and/or other materials provided with the distribution;
26695SN/A * neither the name of the copyright holders nor the names of its
27695SN/A * contributors may be used to endorse or promote products derived from
28695SN/A * this software without specific prior written permission.
29695SN/A *
30695SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31695SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32695SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33695SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34695SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35695SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36695SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37695SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38695SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39695SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40695SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
412665Ssaidi@eecs.umich.edu *
422665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
439262Ssascha.bischoff@arm.com *          Sascha Bischoff
44695SN/A */
45695SN/A
46695SN/A// This file will contain default statistics for the simulator that
47695SN/A// don't really belong to a specific simulator object
48695SN/A
49695SN/A#include <fstream>
50695SN/A#include <iostream>
51695SN/A#include <list>
52695SN/A
53695SN/A#include "base/callback.hh"
54695SN/A#include "base/hostinfo.hh"
55695SN/A#include "base/statistics.hh"
56695SN/A#include "base/time.hh"
571717SN/A#include "cpu/base.hh"
589983Sstever@gmail.com#include "sim/global_event.hh"
597822Ssteve.reinhardt@amd.com#include "sim/stat_control.hh"
60695SN/A
61695SN/Ausing namespace std;
62695SN/A
63729SN/AStats::Formula simSeconds;
647068Snate@binkert.orgStats::Value simTicks;
658720SAli.Saidi@ARM.comStats::Value finalTick;
667068Snate@binkert.orgStats::Value simFreq;
67695SN/A
68729SN/Anamespace Stats {
69695SN/A
70695SN/ATime statTime(true);
71695SN/ATick startTick;
72695SN/A
739983Sstever@gmail.comGlobalEvent *dumpEvent;
749262Ssascha.bischoff@arm.com
754078Sbinkertn@umich.edustruct SimTicksReset : public Callback
76695SN/A{
77695SN/A    void process()
78695SN/A    {
797840Snate@binkert.org        statTime.setTimer();
807823Ssteve.reinhardt@amd.com        startTick = curTick();
81695SN/A    }
82695SN/A};
83695SN/A
84695SN/Adouble
85695SN/AstatElapsedTime()
86695SN/A{
877840Snate@binkert.org    Time now;
887840Snate@binkert.org    now.setTimer();
897840Snate@binkert.org
90695SN/A    Time elapsed = now - statTime;
917840Snate@binkert.org    return elapsed;
92695SN/A}
93695SN/A
941020SN/ATick
951020SN/AstatElapsedTicks()
961020SN/A{
977823Ssteve.reinhardt@amd.com    return curTick() - startTick;
981020SN/A}
991020SN/A
1008720SAli.Saidi@ARM.comTick
1018720SAli.Saidi@ARM.comstatFinalTick()
1028720SAli.Saidi@ARM.com{
1038720SAli.Saidi@ARM.com    return curTick();
1048720SAli.Saidi@ARM.com}
1058720SAli.Saidi@ARM.com
106695SN/ASimTicksReset simTicksReset;
107695SN/A
1085883Snate@binkert.orgstruct Global
1095883Snate@binkert.org{
1105883Snate@binkert.org    Stats::Formula hostInstRate;
1118834Satgutier@umich.edu    Stats::Formula hostOpRate;
1125883Snate@binkert.org    Stats::Formula hostTickRate;
1135883Snate@binkert.org    Stats::Value hostMemory;
1145883Snate@binkert.org    Stats::Value hostSeconds;
1155883Snate@binkert.org
1165883Snate@binkert.org    Stats::Value simInsts;
1178834Satgutier@umich.edu    Stats::Value simOps;
1185883Snate@binkert.org
1195883Snate@binkert.org    Global();
1205883Snate@binkert.org};
1215883Snate@binkert.org
1225883Snate@binkert.orgGlobal::Global()
123695SN/A{
124695SN/A    simInsts
1258834Satgutier@umich.edu        .functor(BaseCPU::numSimulatedInsts)
126695SN/A        .name("sim_insts")
127695SN/A        .desc("Number of instructions simulated")
128695SN/A        .precision(0)
129695SN/A        .prereq(simInsts)
130695SN/A        ;
131695SN/A
1328834Satgutier@umich.edu    simOps
1338834Satgutier@umich.edu        .functor(BaseCPU::numSimulatedOps)
1348834Satgutier@umich.edu        .name("sim_ops")
1358834Satgutier@umich.edu        .desc("Number of ops (including micro ops) simulated")
1368834Satgutier@umich.edu        .precision(0)
1378834Satgutier@umich.edu        .prereq(simOps)
1388834Satgutier@umich.edu        ;
1398834Satgutier@umich.edu
140695SN/A    simSeconds
141695SN/A        .name("sim_seconds")
142695SN/A        .desc("Number of seconds simulated")
143695SN/A        ;
144695SN/A
145707SN/A    simFreq
1467064Snate@binkert.org        .scalar(SimClock::Frequency)
147707SN/A        .name("sim_freq")
148707SN/A        .desc("Frequency of simulated ticks")
149707SN/A        ;
150707SN/A
151695SN/A    simTicks
1521020SN/A        .functor(statElapsedTicks)
153695SN/A        .name("sim_ticks")
154695SN/A        .desc("Number of ticks simulated")
155695SN/A        ;
156695SN/A
1578720SAli.Saidi@ARM.com    finalTick
1588720SAli.Saidi@ARM.com        .functor(statFinalTick)
1598720SAli.Saidi@ARM.com        .name("final_tick")
1608720SAli.Saidi@ARM.com        .desc("Number of ticks from beginning of simulation \
1618720SAli.Saidi@ARM.com(restored from checkpoints and never reset)")
1628720SAli.Saidi@ARM.com        ;
1638720SAli.Saidi@ARM.com
164695SN/A    hostInstRate
165695SN/A        .name("host_inst_rate")
166695SN/A        .desc("Simulator instruction rate (inst/s)")
167695SN/A        .precision(0)
168695SN/A        .prereq(simInsts)
169695SN/A        ;
170695SN/A
1718834Satgutier@umich.edu    hostOpRate
1728834Satgutier@umich.edu        .name("host_op_rate")
1738834Satgutier@umich.edu        .desc("Simulator op (including micro ops) rate (op/s)")
1748834Satgutier@umich.edu        .precision(0)
1758834Satgutier@umich.edu        .prereq(simOps)
1768834Satgutier@umich.edu        ;
1778834Satgutier@umich.edu
178695SN/A    hostMemory
179707SN/A        .functor(memUsage)
180695SN/A        .name("host_mem_usage")
181695SN/A        .desc("Number of bytes of host memory used")
182695SN/A        .prereq(hostMemory)
183695SN/A        ;
184695SN/A
185695SN/A    hostSeconds
186707SN/A        .functor(statElapsedTime)
187695SN/A        .name("host_seconds")
188695SN/A        .desc("Real time elapsed on the host")
189695SN/A        .precision(2)
190695SN/A        ;
191695SN/A
192695SN/A    hostTickRate
193695SN/A        .name("host_tick_rate")
194695SN/A        .desc("Simulator tick rate (ticks/s)")
195695SN/A        .precision(0)
196695SN/A        ;
197695SN/A
198707SN/A    simSeconds = simTicks / simFreq;
199695SN/A    hostInstRate = simInsts / hostSeconds;
2008834Satgutier@umich.edu    hostOpRate = simOps / hostSeconds;
201695SN/A    hostTickRate = simTicks / hostSeconds;
202695SN/A
203695SN/A    registerResetCallback(&simTicksReset);
204695SN/A}
205695SN/A
2065883Snate@binkert.orgvoid
2075883Snate@binkert.orginitSimStats()
2085883Snate@binkert.org{
2095883Snate@binkert.org    static Global global;
2105883Snate@binkert.org}
2115883Snate@binkert.org
2129262Ssascha.bischoff@arm.com/**
2139262Ssascha.bischoff@arm.com * Event to dump and/or reset the statistics.
2149262Ssascha.bischoff@arm.com */
2159983Sstever@gmail.comclass StatEvent : public GlobalEvent
216695SN/A{
2174078Sbinkertn@umich.edu  private:
2184078Sbinkertn@umich.edu    bool dump;
2194078Sbinkertn@umich.edu    bool reset;
220695SN/A    Tick repeat;
221695SN/A
222695SN/A  public:
2239983Sstever@gmail.com    StatEvent(Tick _when, bool _dump, bool _reset, Tick _repeat)
2249983Sstever@gmail.com        : GlobalEvent(_when, Stat_Event_Pri, 0),
2258581Ssteve.reinhardt@amd.com          dump(_dump), reset(_reset), repeat(_repeat)
2264078Sbinkertn@umich.edu    {
2274078Sbinkertn@umich.edu    }
2284078Sbinkertn@umich.edu
2294078Sbinkertn@umich.edu    virtual void
2304078Sbinkertn@umich.edu    process()
2314078Sbinkertn@umich.edu    {
2324078Sbinkertn@umich.edu        if (dump)
2334078Sbinkertn@umich.edu            Stats::dump();
2344078Sbinkertn@umich.edu
2354078Sbinkertn@umich.edu        if (reset)
2364078Sbinkertn@umich.edu            Stats::reset();
2374078Sbinkertn@umich.edu
2385606Snate@binkert.org        if (repeat) {
2397823Ssteve.reinhardt@amd.com            Stats::schedStatEvent(dump, reset, curTick() + repeat, repeat);
2405606Snate@binkert.org        }
2414078Sbinkertn@umich.edu    }
2429983Sstever@gmail.com
2439983Sstever@gmail.com    const char *description() const { return "GlobalStatEvent"; }
244695SN/A};
245695SN/A
2464078Sbinkertn@umich.eduvoid
2477822Ssteve.reinhardt@amd.comschedStatEvent(bool dump, bool reset, Tick when, Tick repeat)
248695SN/A{
2499983Sstever@gmail.com    // simQuantum is being added to the time when the stats would be
2509983Sstever@gmail.com    // dumped so as to ensure that this event happens only after the next
2519983Sstever@gmail.com    // sync amongst the event queues.  Asingle event queue simulation
2529983Sstever@gmail.com    // should remain unaffected.
2539983Sstever@gmail.com    dumpEvent = new StatEvent(when + simQuantum, dump, reset, repeat);
2549262Ssascha.bischoff@arm.com}
2559262Ssascha.bischoff@arm.com
2569262Ssascha.bischoff@arm.comvoid
2579450Ssascha.bischoff@arm.comperiodicStatDump(Tick period)
2589262Ssascha.bischoff@arm.com{
2599262Ssascha.bischoff@arm.com    /*
2609262Ssascha.bischoff@arm.com     * If the period is set to 0, then we do not want to dump periodically,
2619262Ssascha.bischoff@arm.com     * thus we deschedule the event. Else, if the period is not 0, but the event
2629262Ssascha.bischoff@arm.com     * has already been scheduled, we need to get rid of the old event before we
2639262Ssascha.bischoff@arm.com     * create a new one, as the old event will no longer be moved forward in the
2649262Ssascha.bischoff@arm.com     * event that we resume from a checkpoint.
2659262Ssascha.bischoff@arm.com     */
2669262Ssascha.bischoff@arm.com    if (dumpEvent != NULL && (period == 0 || dumpEvent->scheduled())) {
2679262Ssascha.bischoff@arm.com        // Event should AutoDelete, so we do not need to free it.
2689983Sstever@gmail.com        dumpEvent->deschedule();
2699262Ssascha.bischoff@arm.com    }
2709262Ssascha.bischoff@arm.com
2719262Ssascha.bischoff@arm.com    /*
2729262Ssascha.bischoff@arm.com     * If the period is not 0, we schedule the event. If this is called with a
2739262Ssascha.bischoff@arm.com     * period that is less than the current tick, then we shift the first dump
2749262Ssascha.bischoff@arm.com     * by curTick. This ensures that we do not schedule the event is the past.
2759262Ssascha.bischoff@arm.com     */
2769262Ssascha.bischoff@arm.com    if (period != 0) {
2779262Ssascha.bischoff@arm.com        // Schedule the event
2789262Ssascha.bischoff@arm.com        if (period >= curTick()) {
2799262Ssascha.bischoff@arm.com            schedStatEvent(true, true, (Tick)period, (Tick)period);
2809262Ssascha.bischoff@arm.com        } else {
2819262Ssascha.bischoff@arm.com            schedStatEvent(true, true, (Tick)period + curTick(), (Tick)period);
2829262Ssascha.bischoff@arm.com        }
2839262Ssascha.bischoff@arm.com    }
2849262Ssascha.bischoff@arm.com}
2859262Ssascha.bischoff@arm.com
2869262Ssascha.bischoff@arm.comvoid
2879262Ssascha.bischoff@arm.comupdateEvents()
2889262Ssascha.bischoff@arm.com{
2899262Ssascha.bischoff@arm.com    /*
2909262Ssascha.bischoff@arm.com     * If the dumpEvent has been scheduled, but is scheduled in the past, then
2919262Ssascha.bischoff@arm.com     * we need to shift the event to be at a valid point in time. Therefore, we
2929262Ssascha.bischoff@arm.com     * shift the event by curTick.
2939262Ssascha.bischoff@arm.com     */
2949262Ssascha.bischoff@arm.com    if (dumpEvent != NULL &&
2959262Ssascha.bischoff@arm.com        (dumpEvent->scheduled() && dumpEvent->when() < curTick())) {
2969262Ssascha.bischoff@arm.com        // shift by curTick() and reschedule
2979262Ssascha.bischoff@arm.com        Tick _when = dumpEvent->when();
2989983Sstever@gmail.com        dumpEvent->reschedule(_when + curTick());
2999262Ssascha.bischoff@arm.com    }
300695SN/A}
301695SN/A
3027811Ssteve.reinhardt@amd.com} // namespace Stats
303