Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 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" |
Dynamic Tools Team | 3599770 | 2019-10-28 15:06:10 -0700 | [diff] [blame] | 13 | #include "list.h" |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 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 | |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 49 | LocalStats *Next; |
| 50 | LocalStats *Prev; |
Dynamic Tools Team | 3599770 | 2019-10-28 15:06:10 -0700 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | atomic_uptr StatsArray[StatCount]; |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | // Global stats, used for aggregation and querying. |
| 57 | class GlobalStats : public LocalStats { |
| 58 | public: |
Dynamic Tools Team | 3599770 | 2019-10-28 15:06:10 -0700 | [diff] [blame] | 59 | void initLinkerInitialized() {} |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 60 | void init() { |
Dynamic Tools Team | 08b690a | 2020-04-10 13:41:12 -0700 | [diff] [blame] | 61 | LocalStats::init(); |
| 62 | Mutex.init(); |
| 63 | StatsList = {}; |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 64 | initLinkerInitialized(); |
| 65 | } |
| 66 | |
| 67 | void link(LocalStats *S) { |
| 68 | ScopedLock L(Mutex); |
Dynamic Tools Team | 3599770 | 2019-10-28 15:06:10 -0700 | [diff] [blame] | 69 | StatsList.push_back(S); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void unlink(LocalStats *S) { |
| 73 | ScopedLock L(Mutex); |
Dynamic Tools Team | 3599770 | 2019-10-28 15:06:10 -0700 | [diff] [blame] | 74 | StatsList.remove(S); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 75 | for (uptr I = 0; I < StatCount; I++) |
| 76 | add(static_cast<StatType>(I), S->get(static_cast<StatType>(I))); |
| 77 | } |
| 78 | |
| 79 | void get(uptr *S) const { |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 80 | ScopedLock L(Mutex); |
Dynamic Tools Team | 3599770 | 2019-10-28 15:06:10 -0700 | [diff] [blame] | 81 | for (uptr I = 0; I < StatCount; I++) |
| 82 | S[I] = LocalStats::get(static_cast<StatType>(I)); |
| 83 | for (const auto &Stats : StatsList) { |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 84 | for (uptr I = 0; I < StatCount; I++) |
Dynamic Tools Team | 3599770 | 2019-10-28 15:06:10 -0700 | [diff] [blame] | 85 | S[I] += Stats.get(static_cast<StatType>(I)); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 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 | |
Dynamic Tools Team | 83eaa51 | 2020-01-09 11:43:16 -0800 | [diff] [blame] | 92 | void disable() { Mutex.lock(); } |
| 93 | void enable() { Mutex.unlock(); } |
| 94 | |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 95 | private: |
| 96 | mutable HybridMutex Mutex; |
Dynamic Tools Team | 3599770 | 2019-10-28 15:06:10 -0700 | [diff] [blame] | 97 | DoublyLinkedList<LocalStats> StatsList; |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } // namespace scudo |
| 101 | |
| 102 | #endif // SCUDO_STATS_H_ |