activity.hh revision 8229:78bf55f23338
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#ifndef __CPU_ACTIVITY_HH__
32#define __CPU_ACTIVITY_HH__
33
34#include "base/trace.hh"
35#include "cpu/timebuf.hh"
36
37/**
38 * ActivityRecorder helper class that informs the CPU if it can switch
39 * over to being idle or not.  It works by having a time buffer as
40 * long as any time buffer in the CPU, and the CPU and all of its
41 * stages inform the ActivityRecorder when they write to any time
42 * buffer.  The ActivityRecorder marks a 1 in the "0" slot of the time
43 * buffer any time a stage writes to a time buffer, and it advances
44 * its time buffer at the same time as all other stages.  The
45 * ActivityRecorder also records if a stage has activity to do next
46 * cycle.  The recorder keeps a count of these two.  Thus any time the
47 * count is non-zero, there is either communication still in flight,
48 * or activity that still must be done, meaning that the CPU can not
49 * idle.  If count is zero, then the CPU can safely idle as it has no
50 * more outstanding work to do.
51 */
52class ActivityRecorder
53{
54  public:
55    ActivityRecorder(const std::string &name, int num_stages,
56                     int longest_latency, int count);
57
58    /** Records that there is activity this cycle. */
59    void activity();
60
61    /** Advances the activity buffer, decrementing the activityCount
62     *  if active communication just left the time buffer, and
63     *  determining if there is no activity.
64     */
65    void advance();
66
67    /** Marks a stage as active. */
68    void activateStage(const int idx);
69
70    /** Deactivates a stage. */
71    void deactivateStage(const int idx);
72
73    /** Returns how many things are active within the recorder. */
74    int getActivityCount() { return activityCount; }
75
76    /** Sets the count to a starting value.  Can be used to disable
77     * the idling option.
78     */
79    void setActivityCount(int count)
80    { activityCount = count; }
81
82    /** Returns if the CPU should be active. */
83    bool active() { return activityCount; }
84
85    /** Clears the time buffer and the activity count. */
86    void reset();
87
88    /** Debug function to dump the contents of the time buffer. */
89    void dump();
90
91    /** Debug function to ensure that the activity count matches the
92     * contents of the time buffer.
93     */
94    void validate();
95
96  private:
97    // provide name() for DPRINTF.
98    std::string _name;
99    const std::string &name() { return _name; }
100
101    /** Time buffer that tracks if any cycles has active communication
102     *  in them.  It should be as long as the longest communication
103     *  latency in the system.  Each time any time buffer is written,
104     *  the activity buffer should also be written to. The
105     *  activityBuffer is advanced along with all the other time
106     *  buffers, so it should have a 1 somewhere in it only if there
107     *  is active communication in a time buffer.
108     */
109    TimeBuffer<bool> activityBuffer;
110
111    /** Longest latency time buffer in the CPU. */
112    int longestLatency;
113
114    /** Tracks how many stages and cycles of time buffer have
115     *  activity. Stages increment this count when they switch to
116     *  active, and decrement it when they switch to
117     *  inactive. Whenever a cycle that previously had no information
118     *  is written in the time buffer, this is incremented. When a
119     *  cycle that had information exits the time buffer due to age,
120     *  this count is decremented. When the count is 0, there is no
121     *  activity in the CPU, and it can be descheduled.
122     */
123    int activityCount;
124
125    /** Number of stages that can be marked as active or inactive. */
126    int numStages;
127
128    /** Records which stages are active/inactive. */
129    bool *stageActive;
130};
131
132#endif // __CPU_ACTIVITY_HH__
133