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
4911793Sbrandon.potter@amd.com#include "sim/stat_control.hh"
5011793Sbrandon.potter@amd.com
51695SN/A#include <fstream>
52695SN/A#include <iostream>
53695SN/A#include <list>
54695SN/A
55695SN/A#include "base/callback.hh"
56695SN/A#include "base/hostinfo.hh"
57695SN/A#include "base/statistics.hh"
58695SN/A#include "base/time.hh"
591717SN/A#include "cpu/base.hh"
609983Sstever@gmail.com#include "sim/global_event.hh"
61695SN/A
62695SN/Ausing namespace std;
63695SN/A
64729SN/AStats::Formula simSeconds;
657068Snate@binkert.orgStats::Value simTicks;
668720SAli.Saidi@ARM.comStats::Value finalTick;
677068Snate@binkert.orgStats::Value simFreq;
68695SN/A
69729SN/Anamespace Stats {
70695SN/A
71695SN/ATime statTime(true);
72695SN/ATick startTick;
73695SN/A
749983Sstever@gmail.comGlobalEvent *dumpEvent;
759262Ssascha.bischoff@arm.com
764078Sbinkertn@umich.edustruct SimTicksReset : public Callback
77695SN/A{
78695SN/A    void process()
79695SN/A    {
807840Snate@binkert.org        statTime.setTimer();
817823Ssteve.reinhardt@amd.com        startTick = curTick();
82695SN/A    }
83695SN/A};
84695SN/A
85695SN/Adouble
86695SN/AstatElapsedTime()
87695SN/A{
887840Snate@binkert.org    Time now;
897840Snate@binkert.org    now.setTimer();
907840Snate@binkert.org
91695SN/A    Time elapsed = now - statTime;
927840Snate@binkert.org    return elapsed;
93695SN/A}
94695SN/A
951020SN/ATick
961020SN/AstatElapsedTicks()
971020SN/A{
987823Ssteve.reinhardt@amd.com    return curTick() - startTick;
991020SN/A}
1001020SN/A
1018720SAli.Saidi@ARM.comTick
1028720SAli.Saidi@ARM.comstatFinalTick()
1038720SAli.Saidi@ARM.com{
1048720SAli.Saidi@ARM.com    return curTick();
1058720SAli.Saidi@ARM.com}
1068720SAli.Saidi@ARM.com
107695SN/ASimTicksReset simTicksReset;
108695SN/A
1095883Snate@binkert.orgstruct Global
1105883Snate@binkert.org{
1115883Snate@binkert.org    Stats::Formula hostInstRate;
1128834Satgutier@umich.edu    Stats::Formula hostOpRate;
1135883Snate@binkert.org    Stats::Formula hostTickRate;
1145883Snate@binkert.org    Stats::Value hostMemory;
1155883Snate@binkert.org    Stats::Value hostSeconds;
1165883Snate@binkert.org
1175883Snate@binkert.org    Stats::Value simInsts;
1188834Satgutier@umich.edu    Stats::Value simOps;
1195883Snate@binkert.org
1205883Snate@binkert.org    Global();
1215883Snate@binkert.org};
1225883Snate@binkert.org
1235883Snate@binkert.orgGlobal::Global()
124695SN/A{
125695SN/A    simInsts
1268834Satgutier@umich.edu        .functor(BaseCPU::numSimulatedInsts)
127695SN/A        .name("sim_insts")
128695SN/A        .desc("Number of instructions simulated")
129695SN/A        .precision(0)
130695SN/A        .prereq(simInsts)
131695SN/A        ;
132695SN/A
1338834Satgutier@umich.edu    simOps
1348834Satgutier@umich.edu        .functor(BaseCPU::numSimulatedOps)
1358834Satgutier@umich.edu        .name("sim_ops")
1368834Satgutier@umich.edu        .desc("Number of ops (including micro ops) simulated")
1378834Satgutier@umich.edu        .precision(0)
1388834Satgutier@umich.edu        .prereq(simOps)
1398834Satgutier@umich.edu        ;
1408834Satgutier@umich.edu
141695SN/A    simSeconds
142695SN/A        .name("sim_seconds")
143695SN/A        .desc("Number of seconds simulated")
144695SN/A        ;
145695SN/A
146707SN/A    simFreq
1477064Snate@binkert.org        .scalar(SimClock::Frequency)
148707SN/A        .name("sim_freq")
149707SN/A        .desc("Frequency of simulated ticks")
150707SN/A        ;
151707SN/A
152695SN/A    simTicks
1531020SN/A        .functor(statElapsedTicks)
154695SN/A        .name("sim_ticks")
155695SN/A        .desc("Number of ticks simulated")
156695SN/A        ;
157695SN/A
1588720SAli.Saidi@ARM.com    finalTick
1598720SAli.Saidi@ARM.com        .functor(statFinalTick)
1608720SAli.Saidi@ARM.com        .name("final_tick")
16110367SAndrew.Bardsley@arm.com        .desc("Number of ticks from beginning of simulation "
16210367SAndrew.Bardsley@arm.com              "(restored from checkpoints and never reset)")
1638720SAli.Saidi@ARM.com        ;
1648720SAli.Saidi@ARM.com
165695SN/A    hostInstRate
166695SN/A        .name("host_inst_rate")
167695SN/A        .desc("Simulator instruction rate (inst/s)")
168695SN/A        .precision(0)
169695SN/A        .prereq(simInsts)
170695SN/A        ;
171695SN/A
1728834Satgutier@umich.edu    hostOpRate
1738834Satgutier@umich.edu        .name("host_op_rate")
1748834Satgutier@umich.edu        .desc("Simulator op (including micro ops) rate (op/s)")
1758834Satgutier@umich.edu        .precision(0)
1768834Satgutier@umich.edu        .prereq(simOps)
1778834Satgutier@umich.edu        ;
1788834Satgutier@umich.edu
179695SN/A    hostMemory
180707SN/A        .functor(memUsage)
181695SN/A        .name("host_mem_usage")
182695SN/A        .desc("Number of bytes of host memory used")
183695SN/A        .prereq(hostMemory)
184695SN/A        ;
185695SN/A
186695SN/A    hostSeconds
187707SN/A        .functor(statElapsedTime)
188695SN/A        .name("host_seconds")
189695SN/A        .desc("Real time elapsed on the host")
190695SN/A        .precision(2)
191695SN/A        ;
192695SN/A
193695SN/A    hostTickRate
194695SN/A        .name("host_tick_rate")
195695SN/A        .desc("Simulator tick rate (ticks/s)")
196695SN/A        .precision(0)
197695SN/A        ;
198695SN/A
199707SN/A    simSeconds = simTicks / simFreq;
200695SN/A    hostInstRate = simInsts / hostSeconds;
2018834Satgutier@umich.edu    hostOpRate = simOps / hostSeconds;
202695SN/A    hostTickRate = simTicks / hostSeconds;
203695SN/A
204695SN/A    registerResetCallback(&simTicksReset);
205695SN/A}
206695SN/A
2075883Snate@binkert.orgvoid
2085883Snate@binkert.orginitSimStats()
2095883Snate@binkert.org{
2105883Snate@binkert.org    static Global global;
2115883Snate@binkert.org}
2125883Snate@binkert.org
2139262Ssascha.bischoff@arm.com/**
2149262Ssascha.bischoff@arm.com * Event to dump and/or reset the statistics.
2159262Ssascha.bischoff@arm.com */
2169983Sstever@gmail.comclass StatEvent : public GlobalEvent
217695SN/A{
2184078Sbinkertn@umich.edu  private:
2194078Sbinkertn@umich.edu    bool dump;
2204078Sbinkertn@umich.edu    bool reset;
221695SN/A    Tick repeat;
222695SN/A
223695SN/A  public:
2249983Sstever@gmail.com    StatEvent(Tick _when, bool _dump, bool _reset, Tick _repeat)
2259983Sstever@gmail.com        : GlobalEvent(_when, Stat_Event_Pri, 0),
2268581Ssteve.reinhardt@amd.com          dump(_dump), reset(_reset), repeat(_repeat)
2274078Sbinkertn@umich.edu    {
2284078Sbinkertn@umich.edu    }
2294078Sbinkertn@umich.edu
2304078Sbinkertn@umich.edu    virtual void
2314078Sbinkertn@umich.edu    process()
2324078Sbinkertn@umich.edu    {
2334078Sbinkertn@umich.edu        if (dump)
2344078Sbinkertn@umich.edu            Stats::dump();
2354078Sbinkertn@umich.edu
2364078Sbinkertn@umich.edu        if (reset)
2374078Sbinkertn@umich.edu            Stats::reset();
2384078Sbinkertn@umich.edu
2395606Snate@binkert.org        if (repeat) {
2407823Ssteve.reinhardt@amd.com            Stats::schedStatEvent(dump, reset, curTick() + repeat, repeat);
2415606Snate@binkert.org        }
2424078Sbinkertn@umich.edu    }
2439983Sstever@gmail.com
2449983Sstever@gmail.com    const char *description() const { return "GlobalStatEvent"; }
245695SN/A};
246695SN/A
2474078Sbinkertn@umich.eduvoid
2487822Ssteve.reinhardt@amd.comschedStatEvent(bool dump, bool reset, Tick when, Tick repeat)
249695SN/A{
2509983Sstever@gmail.com    // simQuantum is being added to the time when the stats would be
2519983Sstever@gmail.com    // dumped so as to ensure that this event happens only after the next
2529983Sstever@gmail.com    // sync amongst the event queues.  Asingle event queue simulation
2539983Sstever@gmail.com    // should remain unaffected.
2549983Sstever@gmail.com    dumpEvent = new StatEvent(when + simQuantum, dump, reset, repeat);
2559262Ssascha.bischoff@arm.com}
2569262Ssascha.bischoff@arm.com
2579262Ssascha.bischoff@arm.comvoid
2589450Ssascha.bischoff@arm.comperiodicStatDump(Tick period)
2599262Ssascha.bischoff@arm.com{
2609262Ssascha.bischoff@arm.com    /*
2619262Ssascha.bischoff@arm.com     * If the period is set to 0, then we do not want to dump periodically,
2629262Ssascha.bischoff@arm.com     * thus we deschedule the event. Else, if the period is not 0, but the event
2639262Ssascha.bischoff@arm.com     * has already been scheduled, we need to get rid of the old event before we
2649262Ssascha.bischoff@arm.com     * create a new one, as the old event will no longer be moved forward in the
2659262Ssascha.bischoff@arm.com     * event that we resume from a checkpoint.
2669262Ssascha.bischoff@arm.com     */
2679262Ssascha.bischoff@arm.com    if (dumpEvent != NULL && (period == 0 || dumpEvent->scheduled())) {
2689262Ssascha.bischoff@arm.com        // Event should AutoDelete, so we do not need to free it.
2699983Sstever@gmail.com        dumpEvent->deschedule();
2709262Ssascha.bischoff@arm.com    }
2719262Ssascha.bischoff@arm.com
2729262Ssascha.bischoff@arm.com    /*
2739262Ssascha.bischoff@arm.com     * If the period is not 0, we schedule the event. If this is called with a
2749262Ssascha.bischoff@arm.com     * period that is less than the current tick, then we shift the first dump
2759262Ssascha.bischoff@arm.com     * by curTick. This ensures that we do not schedule the event is the past.
2769262Ssascha.bischoff@arm.com     */
2779262Ssascha.bischoff@arm.com    if (period != 0) {
2789262Ssascha.bischoff@arm.com        // Schedule the event
2799262Ssascha.bischoff@arm.com        if (period >= curTick()) {
2809262Ssascha.bischoff@arm.com            schedStatEvent(true, true, (Tick)period, (Tick)period);
2819262Ssascha.bischoff@arm.com        } else {
2829262Ssascha.bischoff@arm.com            schedStatEvent(true, true, (Tick)period + curTick(), (Tick)period);
2839262Ssascha.bischoff@arm.com        }
2849262Ssascha.bischoff@arm.com    }
2859262Ssascha.bischoff@arm.com}
2869262Ssascha.bischoff@arm.com
2879262Ssascha.bischoff@arm.comvoid
2889262Ssascha.bischoff@arm.comupdateEvents()
2899262Ssascha.bischoff@arm.com{
2909262Ssascha.bischoff@arm.com    /*
2919262Ssascha.bischoff@arm.com     * If the dumpEvent has been scheduled, but is scheduled in the past, then
2929262Ssascha.bischoff@arm.com     * we need to shift the event to be at a valid point in time. Therefore, we
2939262Ssascha.bischoff@arm.com     * shift the event by curTick.
2949262Ssascha.bischoff@arm.com     */
2959262Ssascha.bischoff@arm.com    if (dumpEvent != NULL &&
2969262Ssascha.bischoff@arm.com        (dumpEvent->scheduled() && dumpEvent->when() < curTick())) {
2979262Ssascha.bischoff@arm.com        // shift by curTick() and reschedule
2989262Ssascha.bischoff@arm.com        Tick _when = dumpEvent->when();
2999983Sstever@gmail.com        dumpEvent->reschedule(_when + curTick());
3009262Ssascha.bischoff@arm.com    }
301695SN/A}
302695SN/A
3037811Ssteve.reinhardt@amd.com} // namespace Stats
304