xref: /aosp_15_r20/bionic/libc/malloc_debug/LogAllocatorStats.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <errno.h>
30 #include <malloc.h>
31 #include <signal.h>
32 #include <unistd.h>
33 
34 #include "Config.h"
35 #include "LogAllocatorStats.h"
36 #include "debug_log.h"
37 
38 namespace LogAllocatorStats {
39 
40 static std::atomic_bool g_call_mallopt = {};
41 
CallMalloptLogStats(int,struct siginfo *,void *)42 static void CallMalloptLogStats(int, struct siginfo*, void*) {
43   g_call_mallopt = true;
44 }
45 
Log()46 void Log() {
47   info_log("Logging allocator stats...");
48   if (mallopt(M_LOG_STATS, 0) == 0) {
49     error_log("mallopt(M_LOG_STATS, 0) call failed.");
50   }
51 }
52 
CheckIfShouldLog()53 void CheckIfShouldLog() {
54   bool expected = true;
55   if (g_call_mallopt.compare_exchange_strong(expected, false)) {
56     Log();
57   }
58 }
59 
Initialize(const Config & config)60 bool Initialize(const Config& config) {
61   struct sigaction64 log_stats_act = {};
62   log_stats_act.sa_sigaction = CallMalloptLogStats;
63   log_stats_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
64   if (sigaction64(config.log_allocator_stats_signal(), &log_stats_act, nullptr) != 0) {
65     error_log("Unable to set up log allocator stats signal function: %s", strerror(errno));
66     return false;
67   }
68 
69   if (config.options() & VERBOSE) {
70     info_log("%s: Run: 'kill -%d %d' to log allocator stats.", getprogname(),
71              config.log_allocator_stats_signal(), getpid());
72   }
73 
74   return true;
75 }
76 
77 }  // namespace LogAllocatorStats
78