xref: /freebsd/contrib/llvm-project/compiler-rt/lib/scudo/standalone/stats.h (revision e1e636193db45630c7881246d25902e57c43d24e)
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 #include "thread_annotations.h"
16 
17 #include <string.h>
18 
19 namespace scudo {
20 
21 // Memory allocator statistics
22 enum StatType { StatAllocated, StatFree, StatMapped, StatCount };
23 
24 typedef uptr StatCounters[StatCount];
25 
26 // Per-thread stats, live in per-thread cache. We use atomics so that the
27 // numbers themselves are consistent. But we don't use atomic_{add|sub} or a
28 // lock, because those are expensive operations , and we only care for the stats
29 // to be "somewhat" correct: eg. if we call GlobalStats::get while a thread is
30 // LocalStats::add'ing, this is OK, we will still get a meaningful number.
31 class LocalStats {
32 public:
33   void init() {
34     for (uptr I = 0; I < StatCount; I++)
35       DCHECK_EQ(get(static_cast<StatType>(I)), 0U);
36   }
37 
38   void add(StatType I, uptr V) {
39     V += atomic_load_relaxed(&StatsArray[I]);
40     atomic_store_relaxed(&StatsArray[I], V);
41   }
42 
43   void sub(StatType I, uptr V) {
44     V = atomic_load_relaxed(&StatsArray[I]) - V;
45     atomic_store_relaxed(&StatsArray[I], V);
46   }
47 
48   void set(StatType I, uptr V) { atomic_store_relaxed(&StatsArray[I], V); }
49 
50   uptr get(StatType I) const { return atomic_load_relaxed(&StatsArray[I]); }
51 
52   LocalStats *Next = nullptr;
53   LocalStats *Prev = nullptr;
54 
55 private:
56   atomic_uptr StatsArray[StatCount] = {};
57 };
58 
59 // Global stats, used for aggregation and querying.
60 class GlobalStats : public LocalStats {
61 public:
62   void init() { LocalStats::init(); }
63 
64   void link(LocalStats *S) EXCLUDES(Mutex) {
65     ScopedLock L(Mutex);
66     StatsList.push_back(S);
67   }
68 
69   void unlink(LocalStats *S) EXCLUDES(Mutex) {
70     ScopedLock L(Mutex);
71     StatsList.remove(S);
72     for (uptr I = 0; I < StatCount; I++)
73       add(static_cast<StatType>(I), S->get(static_cast<StatType>(I)));
74   }
75 
76   void get(uptr *S) const EXCLUDES(Mutex) {
77     ScopedLock L(Mutex);
78     for (uptr I = 0; I < StatCount; I++)
79       S[I] = LocalStats::get(static_cast<StatType>(I));
80     for (const auto &Stats : StatsList) {
81       for (uptr I = 0; I < StatCount; I++)
82         S[I] += Stats.get(static_cast<StatType>(I));
83     }
84     // All stats must be non-negative.
85     for (uptr I = 0; I < StatCount; I++)
86       S[I] = static_cast<sptr>(S[I]) >= 0 ? S[I] : 0;
87   }
88 
89   void lock() ACQUIRE(Mutex) { Mutex.lock(); }
90   void unlock() RELEASE(Mutex) { Mutex.unlock(); }
91 
92   void disable() ACQUIRE(Mutex) { lock(); }
93   void enable() RELEASE(Mutex) { unlock(); }
94 
95 private:
96   mutable HybridMutex Mutex;
97   DoublyLinkedList<LocalStats> StatsList GUARDED_BY(Mutex);
98 };
99 
100 } // namespace scudo
101 
102 #endif // SCUDO_STATS_H_
103