kernel_stats.cc revision 1070
1/*
2 * Copyright (c) 2003 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <map>
30#include <stack>
31#include <string>
32
33#include "arch/alpha/osfpal.hh"
34#include "base/trace.hh"
35#include "base/statistics.hh"
36#include "base/stats/bin.hh"
37#include "cpu/exec_context.hh"
38#include "cpu/pc_event.hh"
39#include "cpu/static_inst.hh"
40#include "kern/kernel_stats.hh"
41#include "kern/linux/linux_syscalls.hh"
42
43using namespace std;
44using namespace Stats;
45
46namespace Kernel {
47
48const char *modestr[] = { "kernel", "user", "idle" };
49
50Statistics::Statistics(ExecContext *context)
51    : xc(context), idleProcess((Addr)-1), themode(kernel), lastModeTick(0),
52      iplLast(0), iplLastTick(0)
53{
54}
55
56void
57Statistics::regStats(const string &_name)
58{
59    myname = _name;
60
61    _arm
62        .name(name() + ".inst.arm")
63        .desc("number of arm instructions executed")
64        ;
65
66    _quiesce
67        .name(name() + ".inst.quiesce")
68        .desc("number of quiesce instructions executed")
69        ;
70
71    _ivlb
72        .name(name() + ".inst.ivlb")
73        .desc("number of ivlb instructions executed")
74        ;
75
76    _ivle
77        .name(name() + ".inst.ivle")
78        .desc("number of ivle instructions executed")
79        ;
80
81    _hwrei
82        .name(name() + ".inst.hwrei")
83        .desc("number of hwrei instructions executed")
84        ;
85
86    _iplCount
87        .init(32)
88        .name(name() + ".ipl_count")
89        .desc("number of times we switched to this ipl")
90        .flags(total | pdf | nozero | nonan)
91        ;
92
93    _iplGood
94        .init(32)
95        .name(name() + ".ipl_good")
96        .desc("number of times we switched to this ipl from a different ipl")
97        .flags(total | pdf | nozero | nonan)
98        ;
99
100    _iplTicks
101        .init(32)
102        .name(name() + ".ipl_ticks")
103        .desc("number of cycles we spent at this ipl")
104        .flags(total | pdf | nozero | nonan)
105        ;
106
107    _iplUsed
108        .name(name() + ".ipl_used")
109        .desc("fraction of swpipl calls that actually changed the ipl")
110        .flags(total | nozero | nonan)
111        ;
112
113    _iplUsed = _iplGood / _iplCount;
114
115    _callpal
116        .init(256)
117        .name(name() + ".callpal")
118        .desc("number of callpals executed")
119        .flags(total | pdf | nozero | nonan)
120        ;
121
122    for (int i = 0; i < PAL::NumCodes; ++i) {
123        const char *str = PAL::name(i);
124        if (str)
125            _callpal.subname(i, str);
126    }
127
128    _syscall
129        .init(SystemCalls<Tru64>::Number)
130        .name(name() + ".syscall")
131        .desc("number of syscalls executed")
132        .flags(total | pdf | nozero | nonan)
133        ;
134
135    for (int i = 0; i < SystemCalls<Tru64>::Number; ++i) {
136        const char *str = SystemCalls<Tru64>::name(i);
137        if (str) {
138            _syscall.subname(i, str);
139        }
140    }
141
142    _faults
143        .init(Num_Faults)
144        .name(name() + ".faults")
145        .desc("number of faults")
146        .flags(total | pdf | nozero | nonan)
147        ;
148
149    for (int i = 1; i < Num_Faults; ++i) {
150        const char *str = FaultName(i);
151        if (str)
152            _faults.subname(i, str);
153    }
154
155    _mode
156        .init(3)
157        .name(name() + ".mode_switch")
158        .desc("number of protection mode switches")
159        ;
160
161    for (int i = 0; i < 3; ++i)
162        _mode.subname(i, modestr[i]);
163
164    _modeGood
165        .init(3)
166        .name(name() + ".mode_good")
167        ;
168
169    for (int i = 0; i < 3; ++i)
170        _modeGood.subname(i, modestr[i]);
171
172    _modeFraction
173        .name(name() + ".mode_switch_good")
174        .desc("fraction of useful protection mode switches")
175        .flags(total)
176        ;
177
178    for (int i = 0; i < 3; ++i)
179        _modeFraction.subname(i, modestr[i]);
180
181    _modeFraction = _modeGood / _mode;
182
183    _modeTicks
184        .init(3)
185        .name(name() + ".mode_ticks")
186        .desc("number of ticks spent at the given mode")
187        .flags(pdf)
188        ;
189    for (int i = 0; i < 3; ++i)
190        _modeTicks.subname(i, modestr[i]);
191
192    _swap_context
193        .name(name() + ".swap_context")
194        .desc("number of times the context was actually changed")
195        ;
196}
197
198void
199Statistics::setIdleProcess(Addr idlepcbb)
200{
201    assert(themode == kernel);
202    idleProcess = idlepcbb;
203    themode = idle;
204    changeMode(themode);
205}
206
207void
208Statistics::changeMode(cpu_mode newmode)
209{
210    _mode[newmode]++;
211
212    if (newmode == themode)
213        return;
214
215    DPRINTF(Context, "old mode=%-8s new mode=%-8s\n",
216            modestr[themode], modestr[newmode]);
217
218    _modeGood[newmode]++;
219    _modeTicks[themode] += curTick - lastModeTick;
220
221    xc->system->kernelBinning->changeMode(newmode);
222
223    lastModeTick = curTick;
224    themode = newmode;
225}
226
227void
228Statistics::swpipl(int ipl)
229{
230    assert(ipl >= 0 && ipl <= 0x1f && "invalid IPL\n");
231
232    _iplCount[ipl]++;
233
234    if (ipl == iplLast)
235        return;
236
237    _iplGood[ipl]++;
238    _iplTicks[iplLast] += curTick - iplLastTick;
239    iplLastTick = curTick;
240    iplLast = ipl;
241}
242
243void
244Statistics::mode(bool usermode)
245{
246    Addr pcbb = xc->regs.ipr[AlphaISA::IPR_PALtemp23];
247
248    cpu_mode newmode = usermode ? user : kernel;
249    if (newmode == kernel && pcbb == idleProcess)
250        newmode = idle;
251
252    changeMode(newmode);
253}
254
255void
256Statistics::context(Addr oldpcbb, Addr newpcbb)
257{
258    assert(themode != user);
259
260    _swap_context++;
261    changeMode(newpcbb == idleProcess ? idle : kernel);
262}
263
264void
265Statistics::callpal(int code)
266{
267    if (!PAL::name(code))
268        return;
269
270    _callpal[code]++;
271
272    switch (code) {
273      case PAL::callsys: {
274          int number = xc->regs.intRegFile[0];
275          if (SystemCalls<Tru64>::validSyscallNumber(number)) {
276              int cvtnum = SystemCalls<Tru64>::convert(number);
277              _syscall[cvtnum]++;
278          }
279      } break;
280
281      case PAL::swpctx:
282        if (xc->system->kernelBinning)
283            xc->system->kernelBinning->palSwapContext(xc);
284        break;
285    }
286}
287
288void
289Statistics::serialize(ostream &os)
290{
291    int exemode = themode;
292    SERIALIZE_SCALAR(exemode);
293}
294
295void
296Statistics::unserialize(Checkpoint *cp, const string &section)
297{
298    int exemode;
299    UNSERIALIZE_SCALAR(exemode);
300    themode = (cpu_mode)exemode;
301}
302
303/* end namespace Kernel */ }
304