]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/compiler-rt/lib/scudo/standalone/stats.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / compiler-rt / lib / scudo / standalone / stats.h
1 //===-- stats.h -------------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef SCUDO_STATS_H_
10 #define SCUDO_STATS_H_
11
12 #include "atomic_helpers.h"
13 #include "list.h"
14 #include "mutex.h"
15
16 #include <string.h>
17
18 namespace scudo {
19
20 // Memory allocator statistics
21 enum StatType { StatAllocated, StatFree, StatMapped, StatCount };
22
23 typedef uptr StatCounters[StatCount];
24
25 // Per-thread stats, live in per-thread cache. We use atomics so that the
26 // numbers themselves are consistent. But we don't use atomic_{add|sub} or a
27 // lock, because those are expensive operations , and we only care for the stats
28 // to be "somewhat" correct: eg. if we call GlobalStats::get while a thread is
29 // LocalStats::add'ing, this is OK, we will still get a meaningful number.
30 class LocalStats {
31 public:
32   void initLinkerInitialized() {}
33   void init() { memset(this, 0, sizeof(*this)); }
34
35   void add(StatType I, uptr V) {
36     V += atomic_load_relaxed(&StatsArray[I]);
37     atomic_store_relaxed(&StatsArray[I], V);
38   }
39
40   void sub(StatType I, uptr V) {
41     V = atomic_load_relaxed(&StatsArray[I]) - V;
42     atomic_store_relaxed(&StatsArray[I], V);
43   }
44
45   void set(StatType I, uptr V) { atomic_store_relaxed(&StatsArray[I], V); }
46
47   uptr get(StatType I) const { return atomic_load_relaxed(&StatsArray[I]); }
48
49   LocalStats *Next;
50   LocalStats *Prev;
51
52 private:
53   atomic_uptr StatsArray[StatCount];
54 };
55
56 // Global stats, used for aggregation and querying.
57 class GlobalStats : public LocalStats {
58 public:
59   void initLinkerInitialized() {}
60   void init() {
61     memset(this, 0, sizeof(*this));
62     initLinkerInitialized();
63   }
64
65   void link(LocalStats *S) {
66     ScopedLock L(Mutex);
67     StatsList.push_back(S);
68   }
69
70   void unlink(LocalStats *S) {
71     ScopedLock L(Mutex);
72     StatsList.remove(S);
73     for (uptr I = 0; I < StatCount; I++)
74       add(static_cast<StatType>(I), S->get(static_cast<StatType>(I)));
75   }
76
77   void get(uptr *S) const {
78     ScopedLock L(Mutex);
79     for (uptr I = 0; I < StatCount; I++)
80       S[I] = LocalStats::get(static_cast<StatType>(I));
81     for (const auto &Stats : StatsList) {
82       for (uptr I = 0; I < StatCount; I++)
83         S[I] += Stats.get(static_cast<StatType>(I));
84     }
85     // All stats must be non-negative.
86     for (uptr I = 0; I < StatCount; I++)
87       S[I] = static_cast<sptr>(S[I]) >= 0 ? S[I] : 0;
88   }
89
90   void disable() { Mutex.lock(); }
91   void enable() { Mutex.unlock(); }
92
93 private:
94   mutable HybridMutex Mutex;
95   DoublyLinkedList<LocalStats> StatsList;
96 };
97
98 } // namespace scudo
99
100 #endif // SCUDO_STATS_H_