]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/malloctrace.hh
auth: switch circleci mssql image
[thirdparty/pdns.git] / pdns / malloctrace.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #pragma once
23 #include <string>
24 #include <atomic>
25 #include <stdint.h>
26 #include <mutex>
27 #include <map>
28 #include <vector>
29
30 /* please do NOT add PowerDNS specific includes/things to this file, we're trying
31 to make it useful for other projects as well! */
32
33 /* Goal: you can compile this in safely, but it won't do anything unless PDNS_TRACE_MEMORY is defined. */
34
35 class MallocTracer
36 {
37 public:
38 void* malloc (size_t size);
39 void free(void*);
40 uint64_t getAllocs(const std::string& = std::string()) const { return d_allocs; }
41 uint64_t getAllocFlux(const std::string& = std::string()) const { return d_allocflux; }
42 uint64_t getTotAllocated(const std::string& = std::string()) const { return d_totAllocated; }
43 uint64_t getNumOut() { std::lock_guard<std::mutex> lock(d_mut); return d_sizes.size(); }
44 struct AllocStats
45 {
46 int count;
47 std::map<unsigned int, unsigned int> sizes;
48 };
49 typedef std::vector<std::pair<MallocTracer::AllocStats,
50 std::vector<void*> > > allocators_t;
51 allocators_t topAllocators(int num=-1);
52 std::string topAllocatorsString(int num=-1);
53 void clearAllocators();
54
55 private:
56 static std::vector<void*> makeBacktrace();
57 std::atomic<uint64_t> d_allocs{0}, d_allocflux{0}, d_totAllocated{0};
58 std::map<std::vector<void*>, AllocStats> d_stats;
59 std::map<void*, size_t> d_sizes;
60 std::mutex d_mut;
61 };
62
63 extern MallocTracer* g_mtracer;