sanitizer_allocator_stats.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //===-- sanitizer_allocator_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. // Part of the Sanitizer Allocator.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_ALLOCATOR_H
  13. #error This file must be included inside sanitizer_allocator.h
  14. #endif
  15. // Memory allocator statistics
  16. enum AllocatorStat {
  17. AllocatorStatAllocated,
  18. AllocatorStatMapped,
  19. AllocatorStatCount
  20. };
  21. typedef uptr AllocatorStatCounters[AllocatorStatCount];
  22. // Per-thread stats, live in per-thread cache.
  23. class AllocatorStats {
  24. public:
  25. void Init() { internal_memset(this, 0, sizeof(*this)); }
  26. void Add(AllocatorStat i, uptr v) {
  27. atomic_fetch_add(&stats_[i], v, memory_order_relaxed);
  28. }
  29. void Sub(AllocatorStat i, uptr v) {
  30. atomic_fetch_sub(&stats_[i], v, memory_order_relaxed);
  31. }
  32. void Set(AllocatorStat i, uptr v) {
  33. atomic_store(&stats_[i], v, memory_order_relaxed);
  34. }
  35. uptr Get(AllocatorStat i) const {
  36. return atomic_load(&stats_[i], memory_order_relaxed);
  37. }
  38. private:
  39. friend class AllocatorGlobalStats;
  40. AllocatorStats *next_;
  41. AllocatorStats *prev_;
  42. atomic_uintptr_t stats_[AllocatorStatCount];
  43. };
  44. // Global stats, used for aggregation and querying.
  45. class AllocatorGlobalStats : public AllocatorStats {
  46. public:
  47. void Init() {
  48. internal_memset(this, 0, sizeof(*this));
  49. }
  50. void Register(AllocatorStats *s) {
  51. SpinMutexLock l(&mu_);
  52. LazyInit();
  53. s->next_ = next_;
  54. s->prev_ = this;
  55. next_->prev_ = s;
  56. next_ = s;
  57. }
  58. void Unregister(AllocatorStats *s) {
  59. SpinMutexLock l(&mu_);
  60. s->prev_->next_ = s->next_;
  61. s->next_->prev_ = s->prev_;
  62. for (int i = 0; i < AllocatorStatCount; i++)
  63. Add(AllocatorStat(i), s->Get(AllocatorStat(i)));
  64. }
  65. void Get(AllocatorStatCounters s) const {
  66. internal_memset(s, 0, AllocatorStatCount * sizeof(uptr));
  67. SpinMutexLock l(&mu_);
  68. const AllocatorStats *stats = this;
  69. for (; stats;) {
  70. for (int i = 0; i < AllocatorStatCount; i++)
  71. s[i] += stats->Get(AllocatorStat(i));
  72. stats = stats->next_;
  73. if (stats == this)
  74. break;
  75. }
  76. // All stats must be non-negative.
  77. for (int i = 0; i < AllocatorStatCount; i++)
  78. s[i] = ((sptr)s[i]) >= 0 ? s[i] : 0;
  79. }
  80. private:
  81. void LazyInit() {
  82. if (!next_) {
  83. next_ = this;
  84. prev_ = this;
  85. }
  86. }
  87. mutable StaticSpinMutex mu_;
  88. };