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