P4C
The P4 Compiler
Loading...
Searching...
No Matches
alloc_trace.h
1/*
2Copyright 2023-present Intel
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef LIB_ALLOC_TRACE_H_
18#define LIB_ALLOC_TRACE_H_
19
20#include <map>
21#include <ostream>
22
23#include "config.h"
24#include "exceptions.h"
25#include "gc.h"
26
27namespace P4 {
28
30 struct backtrace {
31 void *trace[ALLOC_TRACE_DEPTH];
32
33 backtrace(const backtrace &) = default;
34 explicit backtrace(void **bt) { memcpy(trace, bt, sizeof(trace)); }
35 bool operator<(const backtrace &a) const {
36 for (int i = 0; i < ALLOC_TRACE_DEPTH; ++i)
37 if (trace[i] != a.trace[i]) return trace[i] < a.trace[i];
38 return false;
39 }
40 bool operator==(const backtrace &a) const {
41 for (int i = 0; i < ALLOC_TRACE_DEPTH; ++i)
42 if (trace[i] != a.trace[i]) return false;
43 return true;
44 }
45 };
46 std::map<backtrace, std::map<size_t, int>> data;
47 void count(void **, size_t);
48 static void callback(void *t, void **bt, size_t sz) {
49 static_cast<AllocTrace *>(t)->count(bt, sz);
50 }
51
52 public:
53 void clear() { data.clear(); }
54#if HAVE_LIBGC
55 alloc_trace_cb_t start() { return set_alloc_trace(callback, this); }
56 void stop(alloc_trace_cb_t old) {
57 auto tmp = set_alloc_trace(old);
58 BUG_CHECK(tmp.fn == callback && tmp.arg == this, "AllocTrace stopped when not running");
59 }
60#else
61 alloc_trace_cb_t start() {
62 BUG("Can't trace allocations without garbage collection");
63 return alloc_trace_cb_t{};
64 }
65 void stop(alloc_trace_cb_t) {}
66#endif
67 friend std::ostream &operator<<(std::ostream &, const AllocTrace &);
68};
69
71#if HAVE_LIBGC
73 PauseTrace(const PauseTrace &) = delete;
74
75 public:
76 PauseTrace() { hold = set_alloc_trace(nullptr, nullptr); }
77 ~PauseTrace() { set_alloc_trace(hold); }
78#endif
79};
80
81} // namespace P4
82
83#endif /* LIB_ALLOC_TRACE_H_ */
Definition alloc_trace.h:29
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition alloc_trace.h:70
Definition gc.h:27