1 //===-- stats.h -------------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 #define SCUDO_STATS_H_
12 #include "atomic_helpers.h"
20 // Memory allocator statistics
21 enum StatType { StatAllocated, StatFree, StatMapped, StatCount };
23 typedef uptr StatCounters[StatCount];
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.
32 void initLinkerInitialized() {}
33 void init() { memset(this, 0, sizeof(*this)); }
35 void add(StatType I, uptr V) {
36 V += atomic_load_relaxed(&StatsArray[I]);
37 atomic_store_relaxed(&StatsArray[I], V);
40 void sub(StatType I, uptr V) {
41 V = atomic_load_relaxed(&StatsArray[I]) - V;
42 atomic_store_relaxed(&StatsArray[I], V);
45 void set(StatType I, uptr V) { atomic_store_relaxed(&StatsArray[I], V); }
47 uptr get(StatType I) const { return atomic_load_relaxed(&StatsArray[I]); }
53 atomic_uptr StatsArray[StatCount];
56 // Global stats, used for aggregation and querying.
57 class GlobalStats : public LocalStats {
59 void initLinkerInitialized() {}
61 memset(this, 0, sizeof(*this));
62 initLinkerInitialized();
65 void link(LocalStats *S) {
67 StatsList.push_back(S);
70 void unlink(LocalStats *S) {
73 for (uptr I = 0; I < StatCount; I++)
74 add(static_cast<StatType>(I), S->get(static_cast<StatType>(I)));
77 void get(uptr *S) const {
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));
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;
90 void disable() { Mutex.lock(); }
91 void enable() { Mutex.unlock(); }
94 mutable HybridMutex Mutex;
95 DoublyLinkedList<LocalStats> StatsList;
100 #endif // SCUDO_STATS_H_