1e8d8bef9SDimitry Andric //===-- memprof_stats.cpp ------------------------------------------------===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric //
9e8d8bef9SDimitry Andric // This file is a part of MemProfiler, a memory profiler.
10e8d8bef9SDimitry Andric //
11e8d8bef9SDimitry Andric // Code related to statistics collected by MemProfiler.
12e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
13e8d8bef9SDimitry Andric #include "memprof_stats.h"
14e8d8bef9SDimitry Andric #include "memprof_interceptors.h"
15e8d8bef9SDimitry Andric #include "memprof_internal.h"
16e8d8bef9SDimitry Andric #include "memprof_thread.h"
17e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_allocator_interface.h"
18e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_mutex.h"
19e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_stackdepot.h"
20e8d8bef9SDimitry Andric
21e8d8bef9SDimitry Andric namespace __memprof {
22e8d8bef9SDimitry Andric
MemprofStats()23e8d8bef9SDimitry Andric MemprofStats::MemprofStats() { Clear(); }
24e8d8bef9SDimitry Andric
Clear()25e8d8bef9SDimitry Andric void MemprofStats::Clear() {
26e8d8bef9SDimitry Andric if (REAL(memset))
27e8d8bef9SDimitry Andric return (void)REAL(memset)(this, 0, sizeof(MemprofStats));
28e8d8bef9SDimitry Andric internal_memset(this, 0, sizeof(MemprofStats));
29e8d8bef9SDimitry Andric }
30e8d8bef9SDimitry Andric
PrintMallocStatsArray(const char * prefix,uptr (& array)[kNumberOfSizeClasses])31e8d8bef9SDimitry Andric static void PrintMallocStatsArray(const char *prefix,
32e8d8bef9SDimitry Andric uptr (&array)[kNumberOfSizeClasses]) {
33e8d8bef9SDimitry Andric Printf("%s", prefix);
34e8d8bef9SDimitry Andric for (uptr i = 0; i < kNumberOfSizeClasses; i++) {
35e8d8bef9SDimitry Andric if (!array[i])
36e8d8bef9SDimitry Andric continue;
37e8d8bef9SDimitry Andric Printf("%zu:%zu; ", i, array[i]);
38e8d8bef9SDimitry Andric }
39e8d8bef9SDimitry Andric Printf("\n");
40e8d8bef9SDimitry Andric }
41e8d8bef9SDimitry Andric
Print()42e8d8bef9SDimitry Andric void MemprofStats::Print() {
43e8d8bef9SDimitry Andric Printf("Stats: %zuM malloced (%zuM for overhead) by %zu calls\n",
44e8d8bef9SDimitry Andric malloced >> 20, malloced_overhead >> 20, mallocs);
45e8d8bef9SDimitry Andric Printf("Stats: %zuM realloced by %zu calls\n", realloced >> 20, reallocs);
46e8d8bef9SDimitry Andric Printf("Stats: %zuM freed by %zu calls\n", freed >> 20, frees);
47e8d8bef9SDimitry Andric Printf("Stats: %zuM really freed by %zu calls\n", really_freed >> 20,
48e8d8bef9SDimitry Andric real_frees);
49e8d8bef9SDimitry Andric Printf("Stats: %zuM (%zuM-%zuM) mmaped; %zu maps, %zu unmaps\n",
50e8d8bef9SDimitry Andric (mmaped - munmaped) >> 20, mmaped >> 20, munmaped >> 20, mmaps,
51e8d8bef9SDimitry Andric munmaps);
52e8d8bef9SDimitry Andric
53e8d8bef9SDimitry Andric PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size);
54e8d8bef9SDimitry Andric Printf("Stats: malloc large: %zu\n", malloc_large);
55e8d8bef9SDimitry Andric }
56e8d8bef9SDimitry Andric
MergeFrom(const MemprofStats * stats)57e8d8bef9SDimitry Andric void MemprofStats::MergeFrom(const MemprofStats *stats) {
58e8d8bef9SDimitry Andric uptr *dst_ptr = reinterpret_cast<uptr *>(this);
59e8d8bef9SDimitry Andric const uptr *src_ptr = reinterpret_cast<const uptr *>(stats);
60e8d8bef9SDimitry Andric uptr num_fields = sizeof(*this) / sizeof(uptr);
61e8d8bef9SDimitry Andric for (uptr i = 0; i < num_fields; i++)
62e8d8bef9SDimitry Andric dst_ptr[i] += src_ptr[i];
63e8d8bef9SDimitry Andric }
64e8d8bef9SDimitry Andric
65*349cc55cSDimitry Andric static Mutex print_lock;
66e8d8bef9SDimitry Andric
67e8d8bef9SDimitry Andric static MemprofStats unknown_thread_stats(LINKER_INITIALIZED);
68e8d8bef9SDimitry Andric static MemprofStats dead_threads_stats(LINKER_INITIALIZED);
69*349cc55cSDimitry Andric static Mutex dead_threads_stats_lock;
70e8d8bef9SDimitry Andric // Required for malloc_zone_statistics() on OS X. This can't be stored in
71e8d8bef9SDimitry Andric // per-thread MemprofStats.
72e8d8bef9SDimitry Andric static uptr max_malloced_memory;
73e8d8bef9SDimitry Andric
MergeThreadStats(ThreadContextBase * tctx_base,void * arg)74e8d8bef9SDimitry Andric static void MergeThreadStats(ThreadContextBase *tctx_base, void *arg) {
75e8d8bef9SDimitry Andric MemprofStats *accumulated_stats = reinterpret_cast<MemprofStats *>(arg);
76e8d8bef9SDimitry Andric MemprofThreadContext *tctx = static_cast<MemprofThreadContext *>(tctx_base);
77e8d8bef9SDimitry Andric if (MemprofThread *t = tctx->thread)
78e8d8bef9SDimitry Andric accumulated_stats->MergeFrom(&t->stats());
79e8d8bef9SDimitry Andric }
80e8d8bef9SDimitry Andric
GetAccumulatedStats(MemprofStats * stats)81e8d8bef9SDimitry Andric static void GetAccumulatedStats(MemprofStats *stats) {
82e8d8bef9SDimitry Andric stats->Clear();
83e8d8bef9SDimitry Andric {
84e8d8bef9SDimitry Andric ThreadRegistryLock l(&memprofThreadRegistry());
85e8d8bef9SDimitry Andric memprofThreadRegistry().RunCallbackForEachThreadLocked(MergeThreadStats,
86e8d8bef9SDimitry Andric stats);
87e8d8bef9SDimitry Andric }
88e8d8bef9SDimitry Andric stats->MergeFrom(&unknown_thread_stats);
89e8d8bef9SDimitry Andric {
90*349cc55cSDimitry Andric Lock lock(&dead_threads_stats_lock);
91e8d8bef9SDimitry Andric stats->MergeFrom(&dead_threads_stats);
92e8d8bef9SDimitry Andric }
93e8d8bef9SDimitry Andric // This is not very accurate: we may miss allocation peaks that happen
94e8d8bef9SDimitry Andric // between two updates of accumulated_stats_. For more accurate bookkeeping
95e8d8bef9SDimitry Andric // the maximum should be updated on every malloc(), which is unacceptable.
96e8d8bef9SDimitry Andric if (max_malloced_memory < stats->malloced) {
97e8d8bef9SDimitry Andric max_malloced_memory = stats->malloced;
98e8d8bef9SDimitry Andric }
99e8d8bef9SDimitry Andric }
100e8d8bef9SDimitry Andric
FlushToDeadThreadStats(MemprofStats * stats)101e8d8bef9SDimitry Andric void FlushToDeadThreadStats(MemprofStats *stats) {
102*349cc55cSDimitry Andric Lock lock(&dead_threads_stats_lock);
103e8d8bef9SDimitry Andric dead_threads_stats.MergeFrom(stats);
104e8d8bef9SDimitry Andric stats->Clear();
105e8d8bef9SDimitry Andric }
106e8d8bef9SDimitry Andric
GetCurrentThreadStats()107e8d8bef9SDimitry Andric MemprofStats &GetCurrentThreadStats() {
108e8d8bef9SDimitry Andric MemprofThread *t = GetCurrentThread();
109e8d8bef9SDimitry Andric return (t) ? t->stats() : unknown_thread_stats;
110e8d8bef9SDimitry Andric }
111e8d8bef9SDimitry Andric
PrintAccumulatedStats()112e8d8bef9SDimitry Andric static void PrintAccumulatedStats() {
113e8d8bef9SDimitry Andric MemprofStats stats;
114e8d8bef9SDimitry Andric GetAccumulatedStats(&stats);
115e8d8bef9SDimitry Andric // Use lock to keep reports from mixing up.
116*349cc55cSDimitry Andric Lock lock(&print_lock);
117e8d8bef9SDimitry Andric stats.Print();
118*349cc55cSDimitry Andric StackDepotStats stack_depot_stats = StackDepotGetStats();
119e8d8bef9SDimitry Andric Printf("Stats: StackDepot: %zd ids; %zdM allocated\n",
120*349cc55cSDimitry Andric stack_depot_stats.n_uniq_ids, stack_depot_stats.allocated >> 20);
121e8d8bef9SDimitry Andric PrintInternalAllocatorStats();
122e8d8bef9SDimitry Andric }
123e8d8bef9SDimitry Andric
124e8d8bef9SDimitry Andric } // namespace __memprof
125e8d8bef9SDimitry Andric
126e8d8bef9SDimitry Andric // ---------------------- Interface ---------------- {{{1
127e8d8bef9SDimitry Andric using namespace __memprof;
128e8d8bef9SDimitry Andric
__sanitizer_get_current_allocated_bytes()129e8d8bef9SDimitry Andric uptr __sanitizer_get_current_allocated_bytes() {
130e8d8bef9SDimitry Andric MemprofStats stats;
131e8d8bef9SDimitry Andric GetAccumulatedStats(&stats);
132e8d8bef9SDimitry Andric uptr malloced = stats.malloced;
133e8d8bef9SDimitry Andric uptr freed = stats.freed;
134e8d8bef9SDimitry Andric // Return sane value if malloced < freed due to racy
135e8d8bef9SDimitry Andric // way we update accumulated stats.
136e8d8bef9SDimitry Andric return (malloced > freed) ? malloced - freed : 1;
137e8d8bef9SDimitry Andric }
138e8d8bef9SDimitry Andric
__sanitizer_get_heap_size()139e8d8bef9SDimitry Andric uptr __sanitizer_get_heap_size() {
140e8d8bef9SDimitry Andric MemprofStats stats;
141e8d8bef9SDimitry Andric GetAccumulatedStats(&stats);
142e8d8bef9SDimitry Andric return stats.mmaped - stats.munmaped;
143e8d8bef9SDimitry Andric }
144e8d8bef9SDimitry Andric
__sanitizer_get_free_bytes()145e8d8bef9SDimitry Andric uptr __sanitizer_get_free_bytes() {
146e8d8bef9SDimitry Andric MemprofStats stats;
147e8d8bef9SDimitry Andric GetAccumulatedStats(&stats);
148e8d8bef9SDimitry Andric uptr total_free = stats.mmaped - stats.munmaped + stats.really_freed;
149e8d8bef9SDimitry Andric uptr total_used = stats.malloced;
150e8d8bef9SDimitry Andric // Return sane value if total_free < total_used due to racy
151e8d8bef9SDimitry Andric // way we update accumulated stats.
152e8d8bef9SDimitry Andric return (total_free > total_used) ? total_free - total_used : 1;
153e8d8bef9SDimitry Andric }
154e8d8bef9SDimitry Andric
__sanitizer_get_unmapped_bytes()155e8d8bef9SDimitry Andric uptr __sanitizer_get_unmapped_bytes() { return 0; }
156e8d8bef9SDimitry Andric
__memprof_print_accumulated_stats()157e8d8bef9SDimitry Andric void __memprof_print_accumulated_stats() { PrintAccumulatedStats(); }
158