gem5 [DEVELOP-FOR-25.0]
Loading...
Searching...
No Matches
probe.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 ARM Limited
3 * Copyright (c) 2022-2023 The University of Edinburgh
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2020 Inria
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
60
61#ifndef __SIM_PROBE_PROBE_HH__
62#define __SIM_PROBE_PROBE_HH__
63
64#include <string>
65#include <vector>
66
67#include "base/compiler.hh"
68#include "base/named.hh"
69#include "base/trace.hh"
70
71namespace gem5
72{
73
75class ProbeManager;
76class ProbeListener;
77
86namespace probing
87{
88/* Note: This is only here for documentation purposes, new probe
89 * points should normally be declared in their own header files. See
90 * for example pmu.hh.
91 */
92}
93
102{
103 public:
104 ProbeListener(std::string _name) : name(std::move(_name)) {}
105
106 virtual ~ProbeListener() = default;
107 ProbeListener(const ProbeListener& other) = delete;
108 ProbeListener& operator=(const ProbeListener& other) = delete;
109 ProbeListener(ProbeListener&& other) noexcept = delete;
110 ProbeListener &operator=(ProbeListener &&other) noexcept = delete;
111 const std::string &getName() const { return name; }
112
113 protected:
114 const std::string name;
115};
116
123{
124 protected:
125 const std::string name;
126 public:
127 ProbePoint(ProbeManager *manager, const std::string &name);
128 virtual ~ProbePoint() {}
129
130 virtual void addListener(ProbeListener *listener) = 0;
131 virtual void removeListener(ProbeListener *listener) = 0;
132 const std::string &getName() const { return name; }
133};
134
136{
139 void operator()(ProbeListener *listener) const;
140
141 private:
143};
144
154template <typename Listener = ProbeListener>
155using ProbeListenerPtr = std::unique_ptr<Listener, ProbeListenerCleanup>;
156
161class ProbeManager : public Named
162{
163 private:
166
167 public:
168 ProbeManager(const std::string &obj_name) : Named(obj_name) {}
169 virtual ~ProbeManager() {}
170
178 bool addListener(std::string_view point_name, ProbeListener &listener);
179
188 bool removeListener(std::string_view point_name, ProbeListener &listener);
189
194 void addPoint(ProbePoint &point);
195 ProbePoint *getFirstProbePoint(std::string_view point_name) const;
196
197 template <typename Listener, typename... Args>
199 {
201 new Listener(std::forward<Args>(args)...),
203 addListener(result->getName(), *result);
204 return result;
205 }
206};
207
208inline void
210{
211 manager->removeListener(listener->getName(), *listener);
212 delete listener;
213}
214
222template <class Arg>
224{
225 public:
227 virtual void notify(const Arg &val) = 0;
228};
229
237template <class T, class Arg>
239{
240 private:
242 void (T::* function)(const Arg &);
243
244 public:
250 ProbeListenerArg(T *obj, std::string name, void (T::*func)(const Arg &))
251 : ProbeListenerArgBase<Arg>(std::move(name)),
252 object(obj),
253 function(func)
254 {}
255
261 void notify(const Arg &val) override { (object->*function)(val); }
262};
263
271template <typename Arg>
273{
276
277 public:
278 ProbePointArg(ProbeManager *manager, std::string name)
279 : ProbePoint(manager, name)
280 {
281 }
282
290 bool hasListeners() const { return listeners.size() > 0; }
291
296 void
297 addListener(ProbeListener *l_base) override
298 {
299 auto *l = dynamic_cast<ProbeListenerArgBase<Arg> *>(l_base);
300 panic_if(!l, "Wrong type of listener: expected %s got %s",
302 typeid(*l_base).name());
303 // check listener not already added
304 if (std::find(listeners.begin(), listeners.end(), l) ==
305 listeners.end()) {
306 listeners.push_back(l);
307 }
308 }
309
314 void
316 {
317 auto *l = dynamic_cast<ProbeListenerArgBase<Arg> *>(l_base);
318 panic_if(!l, "Wrong type of listener expected %s got %s",
320 typeid(*l_base).name());
321 listeners.erase(std::remove(listeners.begin(), listeners.end(), l),
322 listeners.end());
323 }
324
329 void
330 notify(const Arg &arg)
331 {
332 for (auto *l : listeners) {
333 l->notify(arg);
334 }
335 }
336};
337
338
350template <class Arg>
352{
353 typedef std::function<void(const Arg &)> NotifyFunction;
354 private:
356
357 public:
364 ProbeListenerArgFunc(const std::string &name, const NotifyFunction &func)
365 : ProbeListenerArgBase<Arg>(name), function(func)
366 {}
367
373 void notify(const Arg &val) override { function(val); }
374};
375
376
377} // namespace gem5
378
379#endif//__SIM_PROBE_PROBE_HH__
Named(std::string_view name_)
Definition named.hh:57
ProbeListenerArgBase is used to define the base interface to a ProbeListenerArg (i....
Definition probe.hh:224
ProbeListenerArgBase(std::string name)
Definition probe.hh:226
virtual void notify(const Arg &val)=0
std::function< void(const Arg &)> NotifyFunction
Definition probe.hh:353
ProbeListenerArgFunc(const std::string &name, const NotifyFunction &func)
Definition probe.hh:364
void notify(const Arg &val) override
called when the ProbePoint calls notify.
Definition probe.hh:373
NotifyFunction function
Definition probe.hh:355
void(LocalInstTracker::* function)(const uint64_t &)
Definition probe.hh:242
void notify(const Arg &val) override
called when the ProbePoint calls notify.
Definition probe.hh:261
ProbeListenerArg(T *obj, std::string name, void(T::*func)(const Arg &))
Definition probe.hh:250
ProbeListener base class; here to simplify things like containers containing multiple types of ProbeL...
Definition probe.hh:102
ProbeListener & operator=(const ProbeListener &other)=delete
ProbeListener(std::string _name)
Definition probe.hh:104
virtual ~ProbeListener()=default
ProbeListener & operator=(ProbeListener &&other) noexcept=delete
ProbeListener(ProbeListener &&other) noexcept=delete
ProbeListener(const ProbeListener &other)=delete
const std::string & getName() const
Definition probe.hh:111
const std::string name
Definition probe.hh:114
ProbeManager is a conduit class that lives on each SimObject, and is used to match up probe listeners...
Definition probe.hh:162
bool addListener(std::string_view point_name, ProbeListener &listener)
Add a ProbeListener to the ProbePoint named by pointName.
Definition probe.cc:57
void addPoint(ProbePoint &point)
Add a ProbePoint to this SimObject ProbeManager.
Definition probe.cc:96
bool removeListener(std::string_view point_name, ProbeListener &listener)
Remove a ProbeListener from the ProbePoint named by pointName.
Definition probe.cc:76
std::vector< ProbePoint * > points
Vector for name look-up.
Definition probe.hh:165
ProbeListenerPtr< Listener > connect(Args &&...args)
Definition probe.hh:198
ProbeManager(const std::string &obj_name)
Definition probe.hh:168
virtual ~ProbeManager()
Definition probe.hh:169
ProbePoint * getFirstProbePoint(std::string_view point_name) const
Definition probe.cc:110
bool hasListeners() const
Informs whether any listeners are attached to this probe.
Definition probe.hh:290
void notify(const Arg &arg)
called at the ProbePoint call site, passes arg to each listener.
Definition probe.hh:330
ProbePointArg(ProbeManager *manager, std::string name)
Definition probe.hh:278
void removeListener(ProbeListener *l_base) override
remove a ProbeListener from this ProbePoints notify list.
Definition probe.hh:315
std::vector< ProbeListenerArgBase< PacketInfo > * > listeners
Definition probe.hh:275
void addListener(ProbeListener *l_base) override
adds a ProbeListener to this ProbePoints notify list.
Definition probe.hh:297
ProbePoint base class; again used to simplify use of ProbePoints in containers and used as to define ...
Definition probe.hh:123
virtual ~ProbePoint()
Definition probe.hh:128
virtual void removeListener(ProbeListener *listener)=0
const std::string name
Definition probe.hh:125
const std::string & getName() const
Definition probe.hh:132
ProbePoint(ProbeManager *manager, const std::string &name)
Definition probe.cc:48
virtual void addListener(ProbeListener *listener)=0
STL vector class.
Definition stl.hh:37
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:246
Bitfield< 0 > m
Bitfield< 5 > l
Bitfield< 63 > val
Definition misc.hh:804
Name space containing shared probe point declarations.
Definition mem.hh:50
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
std::unique_ptr< Listener, ProbeListenerCleanup > ProbeListenerPtr
This typedef should be used instead of a raw std::unique_ptr<> since we have to disconnect the listen...
Definition probe.hh:155
Overload hash function for BasicBlockRange type.
Definition binary32.hh:81
ProbeListenerCleanup(ProbeManager *m)
Definition probe.hh:138
void operator()(ProbeListener *listener) const
Definition probe.hh:209
ProbeManager * manager
Definition probe.hh:142

Generated on Mon May 26 2025 09:19:13 for gem5 by doxygen 1.13.2