2 * Copyright (c) 2006-2009 The Regents of The University of Michigan
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.
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.
31 #ifndef __BASE__CP_ANNOTATE_HH__
32 #define __BASE__CP_ANNOTATE_HH__
39 #include "base/loader/symtab.hh"
40 #include "base/hashmap.hh"
41 #include "base/trace.hh"
42 #include "base/types.hh"
43 #include "config/cp_annotate.hh"
44 #include "config/the_isa.hh"
45 #include "sim/serialize.hh"
46 #include "sim/system.hh"
49 #include "params/CPA.hh"
70 static CPA *cpa() { return NULL; }
71 static bool available() { return false; }
72 bool enabled() { return false; }
73 void swSmBegin(ThreadContext *tc) { return; }
74 void swSmEnd(ThreadContext *tc) { return; }
75 void swExplictBegin(ThreadContext *tc) { return; }
76 void swAutoBegin(ThreadContext *tc, Addr next_pc) { return; }
77 void swEnd(ThreadContext *tc) { return; }
78 void swQ(ThreadContext *tc) { return; }
79 void swDq(ThreadContext *tc) { return; }
80 void swPq(ThreadContext *tc) { return; }
81 void swRq(ThreadContext *tc) { return; }
82 void swWf(ThreadContext *tc) { return; }
83 void swWe(ThreadContext *tc) { return; }
84 void swSq(ThreadContext *tc) { return; }
85 void swAq(ThreadContext *tc) { return; }
86 void swLink(ThreadContext *tc) { return; }
87 void swIdentify(ThreadContext *tc) { return; }
88 uint64_t swGetId(ThreadContext *tc) { return 0; }
89 void swSyscallLink(ThreadContext *tc) { return; }
90 void hwBegin(flags f, System *sys, uint64_t frame, std::string sm,
91 std::string st) { return; }
92 void hwQ(flags f, System *sys, uint64_t frame, std::string sm,
93 std::string q, uint64_t qid, System *q_sys = NULL,
94 int32_t count = 1) { return; }
95 void hwDq(flags f, System *sys, uint64_t frame, std::string sm,
96 std::string q, uint64_t qid, System *q_sys = NULL,
97 int32_t count = 1) { return; }
98 void hwPq(flags f, System *sys, uint64_t frame, std::string sm,
99 std::string q, uint64_t qid, System *q_sys = NULL,
100 int32_t count = 1) { return; }
101 void hwRq(flags f, System *sys, uint64_t frame, std::string sm,
102 std::string q, uint64_t qid, System *q_sys = NULL,
103 int32_t count = 1) { return; }
104 void hwWf(flags f, System *sys, uint64_t frame, std::string sm,
105 std::string q, uint64_t qid, System *q_sys = NULL,
106 int32_t count = 1) { return; }
107 void hwWe(flags f, System *sys, uint64_t frame, std::string sm,
108 std::string q, uint64_t qid, System *q_sys = NULL,
109 int32_t count = 1) { return; }
112 class CPA : SimObject
115 typedef CPAParams Params;
117 /** The known operations that are written to the annotation output file. */
120 OP_WAIT_EMPTY = 0x02,
124 OP_SIZE_QUEUE = 0x08,
131 /** Flags for the various options.*/
135 /* operation was done on hardware */
137 /* operation should cause a warning when encountered */
139 /* Queue like a stack, not a queue */
141 /* Mark HW state as waiting for some non-resource constraint
142 * (e.g. wait because SM only starts after 10 items are queued) */
144 /* operation is linking to another state machine */
146 /* queue should be completely cleared/reset before executing this
157 return dynamic_cast<const Params *>(_params);
160 /* struct that is written to the annotation output file */
161 struct AnnotateData : public RefCounted {
173 void serialize(std::ostream &os);
174 void unserialize(Checkpoint *cp, const std::string §ion);
178 typedef RefCountingPtr<AnnotateData> AnnDataPtr;
180 /* header for the annotation file */
181 struct AnnotateHeader {
192 std::vector<uint64_t> annotateIdx;
194 // number of state machines encountered in the simulation
196 // number of states encountered in the simulation
198 // number of states/queues for a given state machine/system respectively
199 std::vector<int> numSt, numQ;
200 // number of systems in the simulation
202 // number of queues in the state machine
204 // maximum connection id assigned so far
207 // Convert state strings into state ids
208 typedef m5::hash_map<std::string, int> SCache;
209 typedef std::vector<SCache> StCache;
211 // Convert sm and queue name,id into queue id
212 typedef std::pair<std::string, uint64_t> Id;
213 typedef m5::hash_map<Id, int> IdHCache;
214 typedef std::vector<IdHCache> IdCache;
216 // Hold mapping of sm and queues to output python
217 typedef std::vector<std::pair<int, Id> > IdMap;
219 // System pointer to name,id
220 typedef std::map<System*, std::pair<std::string, int> > NameCache;
222 // array of systems each of which is a stack of running sm
223 typedef std::pair<int, uint64_t> StackId;
224 typedef std::map<StackId, std::vector<int> > SmStack;
226 // map of each context and if it's currently in explict state mode
227 // states are not automatically updated until it leaves
228 typedef std::map<StackId, bool> SwExpl;
230 typedef std::map<int,int> IMap;
231 // List of annotate records have not been written/completed yet
232 typedef std::list<AnnDataPtr> AnnotateList;
234 // Maintain link state information
235 typedef std::map<int, int> LinkMap;
238 typedef m5::hash_map<Id, AnnDataPtr> ScHCache;
239 typedef std::vector<ScHCache> ScCache;
244 // vector indexed by queueid to find current number of elements and bytes
245 std::vector<int> qSize;
246 std::vector<int32_t> qBytes;
249 // Turn state machine string into state machine id (small int)
250 // Used for outputting key to convert id back into string
252 // Turn state machine id, state name into state id (small int)
254 // turn system, queue, and queue identify into qid (small int)
255 // turn system, state, and context into state machine id (small int)
256 IdCache qCache, smCache;
257 //Link state machines accross system calls
259 // System pointer to name,id
261 // Stack of state machines currently nested (should unwind correctly)
263 // Map of currently outstanding links
265 // If the state machine is currently exculding automatic changes
267 // Last state that a given state machine was in
269 // Hold mapping of sm and queues to output python
271 // Items still in queue, used for sanity checking
272 std::vector<AnnotateList> qData;
274 void doDq(System *sys, int flags, int cpu, int sm, std::string q, int qi,
276 void doQ(System *sys, int flags, int cpu, int sm, std::string q, int qi,
279 void doSwSmEnd(System *sys, int cpuid, std::string sm, uint64_t frame);
281 // Turn a system id, state machine string, state machine id into a small int
282 // for annotation output
284 getSm(int sysi, std::string si, uint64_t id)
287 Id smid = Id(si, id);
289 smi = smCache[sysi-1][smid];
291 smCache[sysi-1][smid] = smi = ++numSm;
293 smMap.push_back(std::make_pair<int, Id>(sysi, smid));
298 // Turn a state machine string, state string into a small int
299 // for annotation output
301 getSt(std::string sm, std::string s)
307 smi = smtCache[sm] = ++numSmt;
309 while (stCache.size() < smi) {
310 //stCache.resize(sm);
311 stCache.push_back(SCache());
314 //assert(stCache.size() == sm);
315 //assert(numSt.size() == sm);
316 sti = stCache[smi-1][s];
318 stCache[smi-1][s] = sti = ++numSt[smi-1];
322 // Turn state machine pointer into a smal int for annotation output
326 NameCache::iterator i = nameCache.find(s);
327 if (i == nameCache.end()) {
328 nameCache[s] = std::make_pair<std::string,int>(s->name(), ++numSys);
329 i = nameCache.find(s);
330 // might need to put smstackid into map here, but perhaps not
331 //smStack.push_back(std::vector<int>());
332 //swExpl.push_back(false);
334 qCache.push_back(IdHCache());
335 smCache.push_back(IdHCache());
336 scLinks.push_back(ScHCache());
338 return i->second.second;
341 // Turn queue name, and queue context into small int for
344 getQ(int sys, std::string q, uint64_t id)
349 qi = qCache[sys-1][qid];
351 qi = qCache[sys-1][qid] = ++numQs;
355 qData.push_back(AnnotateList());
357 qMap.push_back(std::make_pair<int, Id>(sys, qid));
362 void swBegin(System *sys, int cpuid, std::string st, uint64_t frame,
363 bool expl = false, int flags = FL_NONE);
365 AnnDataPtr add(int t, int f, int c, int sm, int stq, int32_t data=0);
371 /** Only allow one CPA object in a system. It doesn't make sense to have
372 * more that one per simulation because if a part of the system was
373 * important it would have annotations and queues, and with more than one
374 * object none of the sanity checking for queues will work. */
379 std::map<std::string, SymbolTable*> userApp;
382 static CPA *cpa() { return _cpa; }
383 void swSmBegin(ThreadContext *tc);
384 void swSmEnd(ThreadContext *tc);
385 void swExplictBegin(ThreadContext *tc);
386 void swAutoBegin(ThreadContext *tc, Addr next_pc);
387 void swEnd(ThreadContext *tc);
388 void swQ(ThreadContext *tc);
389 void swDq(ThreadContext *tc);
390 void swPq(ThreadContext *tc);
391 void swRq(ThreadContext *tc);
392 void swWf(ThreadContext *tc);
393 void swWe(ThreadContext *tc);
394 void swSq(ThreadContext *tc);
395 void swAq(ThreadContext *tc);
396 void swLink(ThreadContext *tc);
397 void swIdentify(ThreadContext *tc);
398 uint64_t swGetId(ThreadContext *tc);
399 void swSyscallLink(ThreadContext *tc);
401 inline void hwBegin(flags f, System *sys, uint64_t frame, std::string sm,
407 int sysi = getSys(sys);
408 int smi = getSm(sysi, sm, frame);
409 add(OP_BEGIN, FL_HW | f, 0, smi, getSt(sm, st));
411 warn("BAD state encountered: at cycle %d: %s\n", curTick(), st);
414 inline void hwQ(flags f, System *sys, uint64_t frame, std::string sm,
415 std::string q, uint64_t qid, System *q_sys = NULL, int32_t count = 1)
420 int sysi = getSys(sys);
421 int qi = getQ(q_sys ? getSys(q_sys) : sysi, q, qid);
422 DPRINTFS(AnnotateQ, sys,
423 "hwQ: %s[%#x] cur size %d %d bytes: %d adding: %d\n",
424 q, qid, qSize[qi-1], qData[qi-1].size(), qBytes[qi-1], count);
425 doQ(sys, FL_HW | f, 0, getSm(sysi, sm, frame), q, qi, count);
429 inline void hwDq(flags f, System *sys, uint64_t frame, std::string sm,
430 std::string q, uint64_t qid, System *q_sys = NULL, int32_t count = 1)
435 int sysi = getSys(sys);
436 int qi = getQ(q_sys ? getSys(q_sys) : sysi, q, qid);
437 DPRINTFS(AnnotateQ, sys,
438 "hwDQ: %s[%#x] cur size %d %d bytes: %d removing: %d\n",
439 q, qid, qSize[qi-1], qData[qi-1].size(), qBytes[qi-1], count);
440 doDq(sys, FL_HW | f, 0, getSm(sysi,sm, frame), q, qi, count);
443 inline void hwPq(flags f, System *sys, uint64_t frame, std::string sm,
444 std::string q, uint64_t qid, System *q_sys = NULL, int32_t count = 1)
449 int sysi = getSys(sys);
450 int qi = getQ(q_sys ? getSys(q_sys) : sysi, q, qid);
451 DPRINTFS(AnnotateQ, sys,
452 "hwPQ: %s[%#x] cur size %d %d bytes: %d peeking: %d\n",
453 q, qid, qSize[qi-1], qData[qi-1].size(), qBytes[qi-1], count);
454 add(OP_PEEK, FL_HW | f, 0, getSm(sysi, sm, frame), qi, count);
457 inline void hwRq(flags f, System *sys, uint64_t frame, std::string sm,
458 std::string q, uint64_t qid, System *q_sys = NULL, int32_t count = 1)
463 int sysi = getSys(sys);
464 int qi = getQ(q_sys ? getSys(q_sys) : sysi, q, qid);
465 DPRINTFS(AnnotateQ, sys,
466 "hwRQ: %s[%#x] cur size %d %d bytes: %d reserving: %d\n",
467 q, qid, qSize[qi-1], qData[qi-1].size(), qBytes[qi-1], count);
468 add(OP_RESERVE, FL_HW | f, 0, getSm(sysi, sm, frame), qi, count);
471 inline void hwWf(flags f, System *sys, uint64_t frame, std::string sm,
472 std::string q, uint64_t qid, System *q_sys = NULL, int32_t count = 1)
477 int sysi = getSys(sys);
478 int qi = getQ(q_sys ? getSys(q_sys) : sysi, q, qid);
479 add(OP_WAIT_FULL, FL_HW | f, 0, getSm(sysi, sm, frame), qi, count);
482 inline void hwWe(flags f, System *sys, uint64_t frame, std::string sm,
483 std::string q, uint64_t qid, System *q_sys = NULL, int32_t count = 1)
488 int sysi = getSys(sys);
489 int qi = getQ(q_sys ? getSys(q_sys) : sysi, q, qid);
490 add(OP_WAIT_EMPTY, FL_HW | f, 0, getSm(sysi, sm, frame), qi, count);
497 // This code is ISA specific and will need to be changed
498 // if the annotation code is used for something other than Alpha
499 inline uint64_t getFrame(ThreadContext *tc)
500 { return (tc->readMiscRegNoEffect(TheISA::IPR_PALtemp23) &
503 static bool available() { return true; }
516 void serialize(std::ostream &os);
517 void unserialize(Checkpoint *cp, const std::string §ion);
520 #endif // !CP_ANNOTATE
522 #endif //__BASE__CP_ANNOTATE_HH__