stat_control.cc revision 11793:ef606668d247
14123Sbinkertn@umich.edu/*
24123Sbinkertn@umich.edu * Copyright (c) 2012 ARM Limited
39983Sstever@gmail.com * All rights reserved
49983Sstever@gmail.com *
54123Sbinkertn@umich.edu * The license below extends only to copyright in the software and shall
64123Sbinkertn@umich.edu * not be construed as granting a license to any other intellectual
74123Sbinkertn@umich.edu * property including but not limited to intellectual property relating
84123Sbinkertn@umich.edu * to a hardware implementation of the functionality of the software
94123Sbinkertn@umich.edu * licensed hereunder.  You may use the software subject to the license
104123Sbinkertn@umich.edu * terms below provided that you ensure that this notice is replicated
114123Sbinkertn@umich.edu * unmodified and in its entirety in all distributions of the software,
124123Sbinkertn@umich.edu * modified or unmodified, in source code or in binary form.
134123Sbinkertn@umich.edu *
144123Sbinkertn@umich.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan
154123Sbinkertn@umich.edu * Copyright (c) 2013 Advanced Micro Devices, Inc.
164123Sbinkertn@umich.edu * Copyright (c) 2013 Mark D. Hill and David A. Wood
174123Sbinkertn@umich.edu * All rights reserved.
184123Sbinkertn@umich.edu *
194123Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
204123Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
214123Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
224123Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
234123Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
244123Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
254123Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
264123Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
274123Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
284123Sbinkertn@umich.edu * this software without specific prior written permission.
294123Sbinkertn@umich.edu *
304123Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
314123Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
324123Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
334123Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
349983Sstever@gmail.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
359983Sstever@gmail.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
369983Sstever@gmail.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
374123Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
384123Sbinkertn@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
396216Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
404123Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
419356Snilay@cs.wisc.edu *
424123Sbinkertn@umich.edu * Authors: Nathan Binkert
434123Sbinkertn@umich.edu *          Sascha Bischoff
444123Sbinkertn@umich.edu */
456216Snate@binkert.org
464123Sbinkertn@umich.edu// This file will contain default statistics for the simulator that
479983Sstever@gmail.com// don't really belong to a specific simulator object
489983Sstever@gmail.com
499983Sstever@gmail.com#include "sim/stat_control.hh"
509983Sstever@gmail.com
519983Sstever@gmail.com#include <fstream>
529983Sstever@gmail.com#include <iostream>
539983Sstever@gmail.com#include <list>
549983Sstever@gmail.com
559983Sstever@gmail.com#include "base/callback.hh"
569983Sstever@gmail.com#include "base/hostinfo.hh"
579983Sstever@gmail.com#include "base/statistics.hh"
589983Sstever@gmail.com#include "base/time.hh"
599983Sstever@gmail.com#include "cpu/base.hh"
609983Sstever@gmail.com#include "sim/global_event.hh"
619983Sstever@gmail.com
629983Sstever@gmail.comusing namespace std;
639983Sstever@gmail.com
649983Sstever@gmail.comStats::Formula simSeconds;
659983Sstever@gmail.comStats::Value simTicks;
669983Sstever@gmail.comStats::Value finalTick;
679983Sstever@gmail.comStats::Value simFreq;
689983Sstever@gmail.com
699983Sstever@gmail.comnamespace Stats {
709983Sstever@gmail.com
719983Sstever@gmail.comTime statTime(true);
729983Sstever@gmail.comTick startTick;
739983Sstever@gmail.com
7410756SCurtis.Dunham@arm.comGlobalEvent *dumpEvent;
7510756SCurtis.Dunham@arm.com
7610756SCurtis.Dunham@arm.comstruct SimTicksReset : public Callback
7710756SCurtis.Dunham@arm.com{
7810756SCurtis.Dunham@arm.com    void process()
7910756SCurtis.Dunham@arm.com    {
8010756SCurtis.Dunham@arm.com        statTime.setTimer();
8110756SCurtis.Dunham@arm.com        startTick = curTick();
824123Sbinkertn@umich.edu    }
834123Sbinkertn@umich.edu};
844123Sbinkertn@umich.edu
854123Sbinkertn@umich.edudouble
864123Sbinkertn@umich.edustatElapsedTime()
879983Sstever@gmail.com{
884123Sbinkertn@umich.edu    Time now;
894123Sbinkertn@umich.edu    now.setTimer();
909983Sstever@gmail.com
919983Sstever@gmail.com    Time elapsed = now - statTime;
929983Sstever@gmail.com    return elapsed;
939983Sstever@gmail.com}
949983Sstever@gmail.com
959983Sstever@gmail.comTick
969983Sstever@gmail.comstatElapsedTicks()
979983Sstever@gmail.com{
989983Sstever@gmail.com    return curTick() - startTick;
999983Sstever@gmail.com}
1009983Sstever@gmail.com
1019983Sstever@gmail.comTick
1029983Sstever@gmail.comstatFinalTick()
1039983Sstever@gmail.com{
1049983Sstever@gmail.com    return curTick();
1059983Sstever@gmail.com}
1069983Sstever@gmail.com
1079983Sstever@gmail.comSimTicksReset simTicksReset;
1089983Sstever@gmail.com
1097823Ssteve.reinhardt@amd.comstruct Global
1104123Sbinkertn@umich.edu{
1119174Satgutier@umich.edu    Stats::Formula hostInstRate;
1129174Satgutier@umich.edu    Stats::Formula hostOpRate;
1139174Satgutier@umich.edu    Stats::Formula hostTickRate;
1144123Sbinkertn@umich.edu    Stats::Value hostMemory;
1154123Sbinkertn@umich.edu    Stats::Value hostSeconds;
11610756SCurtis.Dunham@arm.com
1179983Sstever@gmail.com    Stats::Value simInsts;
1189983Sstever@gmail.com    Stats::Value simOps;
1199983Sstever@gmail.com
1209983Sstever@gmail.com    Global();
1219983Sstever@gmail.com};
1229983Sstever@gmail.com
1239983Sstever@gmail.comGlobal::Global()
12410101Sandreas@sandberg.pp.se{
1259983Sstever@gmail.com    simInsts
1269983Sstever@gmail.com        .functor(BaseCPU::numSimulatedInsts)
1279983Sstever@gmail.com        .name("sim_insts")
1289983Sstever@gmail.com        .desc("Number of instructions simulated")
1299983Sstever@gmail.com        .precision(0)
1309983Sstever@gmail.com        .prereq(simInsts)
1319983Sstever@gmail.com        ;
1329983Sstever@gmail.com
1339983Sstever@gmail.com    simOps
1349983Sstever@gmail.com        .functor(BaseCPU::numSimulatedOps)
1359983Sstever@gmail.com        .name("sim_ops")
1369983Sstever@gmail.com        .desc("Number of ops (including micro ops) simulated")
1379983Sstever@gmail.com        .precision(0)
1389983Sstever@gmail.com        .prereq(simOps)
1399983Sstever@gmail.com        ;
1409983Sstever@gmail.com
1419983Sstever@gmail.com    simSeconds
1429983Sstever@gmail.com        .name("sim_seconds")
1439983Sstever@gmail.com        .desc("Number of seconds simulated")
1449983Sstever@gmail.com        ;
1459983Sstever@gmail.com
1469983Sstever@gmail.com    simFreq
1479983Sstever@gmail.com        .scalar(SimClock::Frequency)
1489983Sstever@gmail.com        .name("sim_freq")
1499983Sstever@gmail.com        .desc("Frequency of simulated ticks")
1509983Sstever@gmail.com        ;
1519983Sstever@gmail.com
1529983Sstever@gmail.com    simTicks
1539983Sstever@gmail.com        .functor(statElapsedTicks)
1549983Sstever@gmail.com        .name("sim_ticks")
1559983Sstever@gmail.com        .desc("Number of ticks simulated")
1569983Sstever@gmail.com        ;
1579983Sstever@gmail.com
1589983Sstever@gmail.com    finalTick
1599983Sstever@gmail.com        .functor(statFinalTick)
1609983Sstever@gmail.com        .name("final_tick")
1619983Sstever@gmail.com        .desc("Number of ticks from beginning of simulation "
1629983Sstever@gmail.com              "(restored from checkpoints and never reset)")
1639983Sstever@gmail.com        ;
1649983Sstever@gmail.com
1659983Sstever@gmail.com    hostInstRate
1669983Sstever@gmail.com        .name("host_inst_rate")
1679983Sstever@gmail.com        .desc("Simulator instruction rate (inst/s)")
1689983Sstever@gmail.com        .precision(0)
1699983Sstever@gmail.com        .prereq(simInsts)
1709983Sstever@gmail.com        ;
1719983Sstever@gmail.com
1729983Sstever@gmail.com    hostOpRate
1739983Sstever@gmail.com        .name("host_op_rate")
1749983Sstever@gmail.com        .desc("Simulator op (including micro ops) rate (op/s)")
1759983Sstever@gmail.com        .precision(0)
1769983Sstever@gmail.com        .prereq(simOps)
1779983Sstever@gmail.com        ;
1789983Sstever@gmail.com
1799983Sstever@gmail.com    hostMemory
1809983Sstever@gmail.com        .functor(memUsage)
1819983Sstever@gmail.com        .name("host_mem_usage")
1829983Sstever@gmail.com        .desc("Number of bytes of host memory used")
1839983Sstever@gmail.com        .prereq(hostMemory)
1849983Sstever@gmail.com        ;
1859983Sstever@gmail.com
1869983Sstever@gmail.com    hostSeconds
1874123Sbinkertn@umich.edu        .functor(statElapsedTime)
1884123Sbinkertn@umich.edu        .name("host_seconds")
1894123Sbinkertn@umich.edu        .desc("Real time elapsed on the host")
1904123Sbinkertn@umich.edu        .precision(2)
1919983Sstever@gmail.com        ;
1929983Sstever@gmail.com
1934123Sbinkertn@umich.edu    hostTickRate
1944123Sbinkertn@umich.edu        .name("host_tick_rate")
1959983Sstever@gmail.com        .desc("Simulator tick rate (ticks/s)")
19610153Sandreas@sandberg.pp.se        .precision(0)
19710153Sandreas@sandberg.pp.se        ;
19810153Sandreas@sandberg.pp.se
1994123Sbinkertn@umich.edu    simSeconds = simTicks / simFreq;
2007822Ssteve.reinhardt@amd.com    hostInstRate = simInsts / hostSeconds;
2014123Sbinkertn@umich.edu    hostOpRate = simOps / hostSeconds;
2024123Sbinkertn@umich.edu    hostTickRate = simTicks / hostSeconds;
2034123Sbinkertn@umich.edu
2044123Sbinkertn@umich.edu    registerResetCallback(&simTicksReset);
20510670SCurtis.Dunham@arm.com}
20610670SCurtis.Dunham@arm.com
20710670SCurtis.Dunham@arm.comvoid
20810670SCurtis.Dunham@arm.cominitSimStats()
20910670SCurtis.Dunham@arm.com{
2104123Sbinkertn@umich.edu    static Global global;
2114123Sbinkertn@umich.edu}
2124123Sbinkertn@umich.edu
2134123Sbinkertn@umich.edu/**
2144123Sbinkertn@umich.edu * Event to dump and/or reset the statistics.
2154123Sbinkertn@umich.edu */
2164123Sbinkertn@umich.educlass StatEvent : public GlobalEvent
2174123Sbinkertn@umich.edu{
2184123Sbinkertn@umich.edu  private:
2194123Sbinkertn@umich.edu    bool dump;
22010670SCurtis.Dunham@arm.com    bool reset;
22110670SCurtis.Dunham@arm.com    Tick repeat;
22210670SCurtis.Dunham@arm.com
22310670SCurtis.Dunham@arm.com  public:
22410670SCurtis.Dunham@arm.com    StatEvent(Tick _when, bool _dump, bool _reset, Tick _repeat)
2254123Sbinkertn@umich.edu        : GlobalEvent(_when, Stat_Event_Pri, 0),
2264123Sbinkertn@umich.edu          dump(_dump), reset(_reset), repeat(_repeat)
2274123Sbinkertn@umich.edu    {
2284123Sbinkertn@umich.edu    }
229
230    virtual void
231    process()
232    {
233        if (dump)
234            Stats::dump();
235
236        if (reset)
237            Stats::reset();
238
239        if (repeat) {
240            Stats::schedStatEvent(dump, reset, curTick() + repeat, repeat);
241        }
242    }
243
244    const char *description() const { return "GlobalStatEvent"; }
245};
246
247void
248schedStatEvent(bool dump, bool reset, Tick when, Tick repeat)
249{
250    // simQuantum is being added to the time when the stats would be
251    // dumped so as to ensure that this event happens only after the next
252    // sync amongst the event queues.  Asingle event queue simulation
253    // should remain unaffected.
254    dumpEvent = new StatEvent(when + simQuantum, dump, reset, repeat);
255}
256
257void
258periodicStatDump(Tick period)
259{
260    /*
261     * If the period is set to 0, then we do not want to dump periodically,
262     * thus we deschedule the event. Else, if the period is not 0, but the event
263     * has already been scheduled, we need to get rid of the old event before we
264     * create a new one, as the old event will no longer be moved forward in the
265     * event that we resume from a checkpoint.
266     */
267    if (dumpEvent != NULL && (period == 0 || dumpEvent->scheduled())) {
268        // Event should AutoDelete, so we do not need to free it.
269        dumpEvent->deschedule();
270    }
271
272    /*
273     * If the period is not 0, we schedule the event. If this is called with a
274     * period that is less than the current tick, then we shift the first dump
275     * by curTick. This ensures that we do not schedule the event is the past.
276     */
277    if (period != 0) {
278        // Schedule the event
279        if (period >= curTick()) {
280            schedStatEvent(true, true, (Tick)period, (Tick)period);
281        } else {
282            schedStatEvent(true, true, (Tick)period + curTick(), (Tick)period);
283        }
284    }
285}
286
287void
288updateEvents()
289{
290    /*
291     * If the dumpEvent has been scheduled, but is scheduled in the past, then
292     * we need to shift the event to be at a valid point in time. Therefore, we
293     * shift the event by curTick.
294     */
295    if (dumpEvent != NULL &&
296        (dumpEvent->scheduled() && dumpEvent->when() < curTick())) {
297        // shift by curTick() and reschedule
298        Tick _when = dumpEvent->when();
299        dumpEvent->reschedule(_when + curTick());
300    }
301}
302
303} // namespace Stats
304