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
19namespace scudo {
20
21// Memory allocator statistics
22enum StatType { StatAllocated, StatFree, StatMapped, StatCount };
23
24typedef 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.
31class LocalStats {
32public:
33 void init() {
34 for (uptr I = 0; I < StatCount; I++)
35 DCHECK_EQ(get(static_cast<StatType>(I)), 0U);
36 }
37
38 __attribute__((no_sanitize("unsigned-integer-overflow"))) void add(StatType I,
39 uptr V) {
40 V += atomic_load_relaxed(A: &StatsArray[I]);
41 atomic_store_relaxed(A: &StatsArray[I], V);
42 }
43
44 __attribute__((no_sanitize("unsigned-integer-overflow"))) void sub(StatType I,
45 uptr V) {
46 V = atomic_load_relaxed(A: &StatsArray[I]) - V;
47 atomic_store_relaxed(A: &StatsArray[I], V);
48 }
49
50 void set(StatType I, uptr V) { atomic_store_relaxed(A: &StatsArray[I], V); }
51
52 uptr get(StatType I) const { return atomic_load_relaxed(A: &StatsArray[I]); }
53
54 LocalStats *Next = nullptr;
55 LocalStats *Prev = nullptr;
56
57private:
58 atomic_uptr StatsArray[StatCount] = {};
59};
60
61// Global stats, used for aggregation and querying.
62class GlobalStats : public LocalStats {
63public:
64 void init() { LocalStats::init(); }
65
66 void link(LocalStats *S) EXCLUDES(Mutex) {
67 ScopedLock L(Mutex);
68 StatsList.push_back(X: S);
69 }
70
71 void unlink(LocalStats *S) EXCLUDES(Mutex) {
72 ScopedLock L(Mutex);
73 StatsList.remove(X: S);
74 for (uptr I = 0; I < StatCount; I++)
75 add(I: static_cast<StatType>(I), V: S->get(I: static_cast<StatType>(I)));
76 }
77
78 __attribute__((no_sanitize("unsigned-integer-overflow"))) void
79 get(uptr *S) const EXCLUDES(Mutex) {
80 ScopedLock L(Mutex);
81 for (uptr I = 0; I < StatCount; I++)
82 S[I] = LocalStats::get(I: static_cast<StatType>(I));
83 for (const auto &Stats : StatsList) {
84 for (uptr I = 0; I < StatCount; I++)
85 S[I] += Stats.get(I: static_cast<StatType>(I));
86 }
87 // All stats must be non-negative.
88 for (uptr I = 0; I < StatCount; I++)
89 S[I] = static_cast<sptr>(S[I]) >= 0 ? S[I] : 0;
90 }
91
92 void lock() ACQUIRE(Mutex) { Mutex.lock(); }
93 void unlock() RELEASE(Mutex) { Mutex.unlock(); }
94
95 void disable() ACQUIRE(Mutex) { lock(); }
96 void enable() RELEASE(Mutex) { unlock(); }
97
98private:
99 mutable HybridMutex Mutex;
100 DoublyLinkedList<LocalStats> StatsList GUARDED_BY(Mutex);
101};
102
103} // namespace scudo
104
105#endif // SCUDO_STATS_H_
106