CheckerCPU: Re-factor CheckerCPU to be compatible with current gem5
[gem5.git] / src / cpu / activity.cc
1 /*
2 * Copyright (c) 2006 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 * Authors: Kevin Lim
29 */
30
31 #include <string>
32
33 #include "cpu/activity.hh"
34 #include "cpu/timebuf.hh"
35 #include "debug/Activity.hh"
36
37 using namespace std;
38
39 ActivityRecorder::ActivityRecorder(const string &name, int num_stages,
40 int longest_latency, int activity)
41 : _name(name), activityBuffer(longest_latency, 0),
42 longestLatency(longest_latency), activityCount(activity),
43 numStages(num_stages)
44 {
45 stageActive = new bool[numStages];
46 std::memset(stageActive, 0, numStages);
47 }
48
49 void
50 ActivityRecorder::activity()
51 {
52 // If we've already recorded activity for this cycle, we don't
53 // want to increment the count any more.
54 if (activityBuffer[0]) {
55 return;
56 }
57
58 activityBuffer[0] = true;
59
60 ++activityCount;
61
62 DPRINTF(Activity, "Activity: %i\n", activityCount);
63 }
64
65 void
66 ActivityRecorder::advance()
67 {
68 // If there's a 1 in the slot that is about to be erased once the
69 // time buffer advances, then decrement the activityCount.
70 if (activityBuffer[-longestLatency]) {
71 --activityCount;
72
73 assert(activityCount >= 0);
74
75 DPRINTF(Activity, "Activity: %i\n", activityCount);
76
77 if (activityCount == 0) {
78 DPRINTF(Activity, "No activity left!\n");
79 }
80 }
81
82 activityBuffer.advance();
83 }
84
85 void
86 ActivityRecorder::activateStage(const int idx)
87 {
88 // Increment the activity count if this stage wasn't already active.
89 if (!stageActive[idx]) {
90 ++activityCount;
91
92 stageActive[idx] = true;
93
94 DPRINTF(Activity, "Activity: %i\n", activityCount);
95 } else {
96 DPRINTF(Activity, "Stage %i already active.\n", idx);
97 }
98
99 // assert(activityCount < longestLatency + numStages + 1);
100 }
101
102 void
103 ActivityRecorder::deactivateStage(const int idx)
104 {
105 // Decrement the activity count if this stage was active.
106 if (stageActive[idx]) {
107 --activityCount;
108
109 stageActive[idx] = false;
110
111 DPRINTF(Activity, "Activity: %i\n", activityCount);
112 } else {
113 DPRINTF(Activity, "Stage %i already inactive.\n", idx);
114 }
115
116 assert(activityCount >= 0);
117 }
118
119 void
120 ActivityRecorder::reset()
121 {
122 activityCount = 0;
123 std::memset(stageActive, 0, numStages);
124 for (int i = 0; i < longestLatency + 1; ++i)
125 activityBuffer.advance();
126 }
127
128 void
129 ActivityRecorder::dump()
130 {
131 for (int i = 0; i <= longestLatency; ++i) {
132 cprintf("[Idx:%i %i] ", i, activityBuffer[-i]);
133 }
134
135 cprintf("\n");
136
137 for (int i = 0; i < numStages; ++i) {
138 cprintf("[Stage:%i %i]\n", i, stageActive[i]);
139 }
140
141 cprintf("\n");
142
143 cprintf("Activity count: %i\n", activityCount);
144 }
145
146 void
147 ActivityRecorder::validate()
148 {
149 int count = 0;
150 for (int i = 0; i <= longestLatency; ++i) {
151 if (activityBuffer[-i]) {
152 count++;
153 }
154 }
155
156 for (int i = 0; i < numStages; ++i) {
157 if (stageActive[i]) {
158 count++;
159 }
160 }
161
162 assert(count == activityCount);
163 }