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
CheckIfShouldLog()46 void CheckIfShouldLog() {
47 bool expected = true;
48 if (g_call_mallopt.compare_exchange_strong(expected, false)) {
49 info_log("Logging allocator stats...");
50 if (mallopt(M_LOG_STATS, 0) == 0) {
51 error_log("mallopt(M_LOG_STATS, 0) call failed.");
52 }
53 }
54 }
55
Initialize(const Config & config)56 bool Initialize(const Config& config) {
57 struct sigaction64 log_stats_act = {};
58 log_stats_act.sa_sigaction = CallMalloptLogStats;
59 log_stats_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
60 if (sigaction64(config.log_allocator_stats_signal(), &log_stats_act, nullptr) != 0) {
61 error_log("Unable to set up log allocator stats signal function: %s", strerror(errno));
62 return false;
63 }
64
65 if (config.options() & VERBOSE) {
66 info_log("%s: Run: 'kill -%d %d' to log allocator stats.", getprogname(),
67 config.log_allocator_stats_signal(), getpid());
68 }
69
70 return true;
71 }
72
73 } // namespace LogAllocatorStats
74