P4C
The P4 Compiler
Loading...
Searching...
No Matches
alloc_trace.h
1/*
2 * SPDX-FileCopyrightText: 2023 Intel
3 * Copyright 2023-present Intel
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef LIB_ALLOC_TRACE_H_
9#define LIB_ALLOC_TRACE_H_
10
11#include <map>
12#include <ostream>
13
14#include "config.h"
15#include "exceptions.h"
16#include "gc.h"
17
18namespace P4 {
19
21 struct backtrace {
22 void *trace[ALLOC_TRACE_DEPTH];
23
24 backtrace(const backtrace &) = default;
25 explicit backtrace(void **bt) { memcpy(trace, bt, sizeof(trace)); }
26 bool operator<(const backtrace &a) const {
27 for (int i = 0; i < ALLOC_TRACE_DEPTH; ++i)
28 if (trace[i] != a.trace[i]) return trace[i] < a.trace[i];
29 return false;
30 }
31 bool operator==(const backtrace &a) const {
32 for (int i = 0; i < ALLOC_TRACE_DEPTH; ++i)
33 if (trace[i] != a.trace[i]) return false;
34 return true;
35 }
36 };
37 std::map<backtrace, std::map<size_t, int>> data;
38 void count(void **, size_t);
39 static void callback(void *t, void **bt, size_t sz) {
40 static_cast<AllocTrace *>(t)->count(bt, sz);
41 }
42
43 public:
44 void clear() { data.clear(); }
45#if HAVE_LIBGC
46 alloc_trace_cb_t start() { return set_alloc_trace(callback, this); }
47 void stop(alloc_trace_cb_t old) {
48 auto tmp = set_alloc_trace(old);
49 BUG_CHECK(tmp.fn == callback && tmp.arg == this, "AllocTrace stopped when not running");
50 }
51#else
52 alloc_trace_cb_t start() {
53 BUG("Can't trace allocations without garbage collection");
54 return alloc_trace_cb_t{};
55 }
56 void stop(alloc_trace_cb_t) {}
57#endif
58 friend std::ostream &operator<<(std::ostream &, const AllocTrace &);
59};
60
62#if HAVE_LIBGC
64 PauseTrace(const PauseTrace &) = delete;
65
66 public:
67 PauseTrace() { hold = set_alloc_trace(nullptr, nullptr); }
68 ~PauseTrace() { set_alloc_trace(hold); }
69#endif
70};
71
72} // namespace P4
73
74#endif /* LIB_ALLOC_TRACE_H_ */
Definition alloc_trace.h:20
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
Definition alloc_trace.h:61
Definition gc.h:27