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