58464696b668318ed83329d678f775d118ff26ae
[gem5.git] / src / sim / workload.hh
1 /*
2 * Copyright 2019 Google Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef __SIM_WORKLOAD_HH__
29 #define __SIM_WORKLOAD_HH__
30
31 #include "base/loader/object_file.hh"
32 #include "base/loader/symtab.hh"
33 #include "params/Workload.hh"
34 #include "sim/sim_object.hh"
35 #include "sim/stats.hh"
36
37 class System;
38 class ThreadContext;
39
40 class Workload : public SimObject
41 {
42 protected:
43 virtual Addr fixFuncEventAddr(Addr addr) const { return addr; }
44
45 struct WorkloadStats : public Stats::Group
46 {
47 struct InstStats: public Stats::Group
48 {
49 Stats::Scalar arm;
50 Stats::Scalar quiesce;
51
52 InstStats(Stats::Group *parent) : Stats::Group(parent, "inst"),
53 ADD_STAT(arm, UNIT_COUNT,
54 "number of arm instructions executed"),
55 ADD_STAT(quiesce, UNIT_COUNT,
56 "number of quiesce instructions executed")
57 {}
58
59 } instStats;
60
61 WorkloadStats(Workload *workload) : Stats::Group(workload),
62 instStats(workload)
63 {}
64 } stats;
65
66 public:
67 Workload(const WorkloadParams &_params) : SimObject(_params), stats(this)
68 {}
69
70 void recordQuiesce() { stats.instStats.quiesce++; }
71 void recordArm() { stats.instStats.arm++; }
72
73 System *system = nullptr;
74
75 virtual Addr getEntry() const = 0;
76 virtual Loader::Arch getArch() const = 0;
77
78 virtual const Loader::SymbolTable &symtab(ThreadContext *tc) = 0;
79 virtual bool insertSymbol(const Loader::Symbol &symbol) = 0;
80
81 virtual void
82 syscall(ThreadContext *tc)
83 {
84 panic("syscall() not implemented.");
85 }
86
87 virtual void
88 event(ThreadContext *tc)
89 {
90 warn("Unhandled workload event.");
91 }
92
93 /** @{ */
94 /**
95 * Add a function-based event to the given function, to be looked
96 * up in the specified symbol table.
97 *
98 * The ...OrPanic flavor of the method causes the simulator to
99 * panic if the symbol can't be found.
100 *
101 * @param symtab Symbol table to use for look up.
102 * @param lbl Function to hook the event to.
103 * @param desc Description to be passed to the event.
104 * @param args Arguments to be forwarded to the event constructor.
105 */
106 template <class T, typename... Args>
107 T *
108 addFuncEvent(const Loader::SymbolTable &symtab, const char *lbl,
109 const std::string &desc, Args... args)
110 {
111 auto it = symtab.find(lbl);
112 if (it == symtab.end())
113 return nullptr;
114
115 return new T(system, desc, fixFuncEventAddr(it->address),
116 std::forward<Args>(args)...);
117 }
118
119 template <class T>
120 T *
121 addFuncEvent(const Loader::SymbolTable &symtab, const char *lbl)
122 {
123 return addFuncEvent<T>(symtab, lbl, lbl);
124 }
125
126 template <class T, typename... Args>
127 T *
128 addFuncEventOrPanic(const Loader::SymbolTable &symtab, const char *lbl,
129 Args... args)
130 {
131 T *e = addFuncEvent<T>(symtab, lbl, std::forward<Args>(args)...);
132 panic_if(!e, "Failed to find symbol '%s'", lbl);
133 return e;
134 }
135 /** @} */
136 };
137
138 #endif // __SIM_WORKLOAD_HH__