stat_control.cc revision 1762
112855Sgabeblack@google.com/*
212855Sgabeblack@google.com * Copyright (c) 2004-2005 The Regents of The University of Michigan
312855Sgabeblack@google.com * All rights reserved.
412855Sgabeblack@google.com *
512855Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
612855Sgabeblack@google.com * modification, are permitted provided that the following conditions are
712855Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
812855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
912855Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1012855Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
1112855Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
1212855Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
1312855Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
1412855Sgabeblack@google.com * this software without specific prior written permission.
1512855Sgabeblack@google.com *
1612855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712855Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812855Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912855Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012855Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112855Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212855Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312855Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412855Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512855Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612855Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2712855Sgabeblack@google.com */
2812855Sgabeblack@google.com
2912855Sgabeblack@google.com// This file will contain default statistics for the simulator that
3012855Sgabeblack@google.com// don't really belong to a specific simulator object
3112855Sgabeblack@google.com
3212855Sgabeblack@google.com#include <fstream>
3312855Sgabeblack@google.com#include <iostream>
3412855Sgabeblack@google.com#include <list>
3512855Sgabeblack@google.com
3612855Sgabeblack@google.com#include "base/callback.hh"
3712855Sgabeblack@google.com#include "base/hostinfo.hh"
3812855Sgabeblack@google.com#include "base/statistics.hh"
3912855Sgabeblack@google.com#include "base/str.hh"
4012855Sgabeblack@google.com#include "base/time.hh"
4112855Sgabeblack@google.com#include "base/stats/output.hh"
4212855Sgabeblack@google.com#include "cpu/base.hh"
4312855Sgabeblack@google.com#include "sim/eventq.hh"
4412855Sgabeblack@google.com#include "sim/sim_object.hh"
4512855Sgabeblack@google.com#include "sim/stat_control.hh"
4612855Sgabeblack@google.com#include "sim/root.hh"
4712855Sgabeblack@google.com
4812855Sgabeblack@google.comusing namespace std;
4912855Sgabeblack@google.com
5012855Sgabeblack@google.comStats::Formula hostInstRate;
5112855Sgabeblack@google.comStats::Formula hostTickRate;
5212855Sgabeblack@google.comStats::Value hostMemory;
5312855Sgabeblack@google.comStats::Value hostSeconds;
5412855Sgabeblack@google.com
5512855Sgabeblack@google.comStats::Value simTicks;
5612855Sgabeblack@google.comStats::Value simInsts;
5712855Sgabeblack@google.comStats::Value simFreq;
5812855Sgabeblack@google.comStats::Formula simSeconds;
5912855Sgabeblack@google.com
6012855Sgabeblack@google.comnamespace Stats {
61
62Time statTime(true);
63Tick startTick;
64
65class SimTicksReset : public Callback
66{
67  public:
68    void process()
69    {
70        statTime.set();
71        startTick = curTick;
72    }
73};
74
75double
76statElapsedTime()
77{
78    Time now(true);
79    Time elapsed = now - statTime;
80    return elapsed();
81}
82
83Tick
84statElapsedTicks()
85{
86    return curTick - startTick;
87}
88
89SimTicksReset simTicksReset;
90
91void
92InitSimStats()
93{
94    simInsts
95        .functor(BaseCPU::numSimulatedInstructions)
96        .name("sim_insts")
97        .desc("Number of instructions simulated")
98        .precision(0)
99        .prereq(simInsts)
100        ;
101
102    simSeconds
103        .name("sim_seconds")
104        .desc("Number of seconds simulated")
105        ;
106
107    simFreq
108        .scalar(Clock::Frequency)
109        .name("sim_freq")
110        .desc("Frequency of simulated ticks")
111        ;
112
113    simTicks
114        .functor(statElapsedTicks)
115        .name("sim_ticks")
116        .desc("Number of ticks simulated")
117        ;
118
119    hostInstRate
120        .name("host_inst_rate")
121        .desc("Simulator instruction rate (inst/s)")
122        .precision(0)
123        .prereq(simInsts)
124        ;
125
126    hostMemory
127        .functor(memUsage)
128        .name("host_mem_usage")
129        .desc("Number of bytes of host memory used")
130        .prereq(hostMemory)
131        ;
132
133    hostSeconds
134        .functor(statElapsedTime)
135        .name("host_seconds")
136        .desc("Real time elapsed on the host")
137        .precision(2)
138        ;
139
140    hostTickRate
141        .name("host_tick_rate")
142        .desc("Simulator tick rate (ticks/s)")
143        .precision(0)
144        ;
145
146    simSeconds = simTicks / simFreq;
147    hostInstRate = simInsts / hostSeconds;
148    hostTickRate = simTicks / hostSeconds;
149
150    registerResetCallback(&simTicksReset);
151}
152
153class StatEvent : public Event
154{
155  protected:
156    int flags;
157    Tick repeat;
158
159  public:
160    StatEvent(int _flags, Tick _when, Tick _repeat);
161    virtual void process();
162    virtual const char *description();
163};
164
165StatEvent::StatEvent(int _flags, Tick _when, Tick _repeat)
166    : Event(&mainEventQueue, Stat_Event_Pri),
167      flags(_flags), repeat(_repeat)
168{
169    setFlags(AutoDelete);
170    schedule(_when);
171}
172
173const char *
174StatEvent::description()
175{
176    return "Statistics dump and/or reset";
177}
178
179void
180StatEvent::process()
181{
182    if (flags & Stats::Dump)
183        DumpNow();
184
185    if (flags & Stats::Reset)
186        reset();
187
188    if (repeat)
189        schedule(curTick + repeat);
190}
191
192list<Output *> OutputList;
193
194void
195DumpNow()
196{
197    list<Output *>::iterator i = OutputList.begin();
198    list<Output *>::iterator end = OutputList.end();
199    for (; i != end; ++i) {
200        Output *output = *i;
201        if (!output->valid())
202            continue;
203
204        output->output();
205    }
206}
207
208void
209SetupEvent(int flags, Tick when, Tick repeat)
210{
211    new StatEvent(flags, when, repeat);
212}
213
214/* namespace Stats */ }
215
216extern "C" void
217debugDumpStats()
218{
219    Stats::DumpNow();
220}
221
222