blob: 6ba505e602a72d1d415bde5de185d7dbd26d16bc [file] [log] [blame]
Christopher Ferris5610d5a2023-11-14 15:04:50 -08001/*
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
Elliott Hughes0ec50d82025-05-09 13:58:56 -070034#include <atomic>
35
Christopher Ferris5610d5a2023-11-14 15:04:50 -080036#include "Config.h"
37#include "LogAllocatorStats.h"
38#include "debug_log.h"
39
40namespace LogAllocatorStats {
41
42static std::atomic_bool g_call_mallopt = {};
43
44static void CallMalloptLogStats(int, struct siginfo*, void*) {
45 g_call_mallopt = true;
46}
47
Christopher Ferrisc1341e12024-07-11 19:48:08 -070048void Log() {
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
Christopher Ferris5610d5a2023-11-14 15:04:50 -080055void CheckIfShouldLog() {
56 bool expected = true;
57 if (g_call_mallopt.compare_exchange_strong(expected, false)) {
Christopher Ferrisc1341e12024-07-11 19:48:08 -070058 Log();
Christopher Ferris5610d5a2023-11-14 15:04:50 -080059 }
60}
61
62bool Initialize(const Config& config) {
63 struct sigaction64 log_stats_act = {};
64 log_stats_act.sa_sigaction = CallMalloptLogStats;
65 log_stats_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
66 if (sigaction64(config.log_allocator_stats_signal(), &log_stats_act, nullptr) != 0) {
67 error_log("Unable to set up log allocator stats signal function: %s", strerror(errno));
68 return false;
69 }
70
71 if (config.options() & VERBOSE) {
72 info_log("%s: Run: 'kill -%d %d' to log allocator stats.", getprogname(),
73 config.log_allocator_stats_signal(), getpid());
74 }
75
76 return true;
77}
78
79} // namespace LogAllocatorStats