| Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 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 |  | 
|  | 42 | static void CallMalloptLogStats(int, struct siginfo*, void*) { | 
|  | 43 | g_call_mallopt = true; | 
|  | 44 | } | 
|  | 45 |  | 
| Christopher Ferris | c1341e1 | 2024-07-11 19:48:08 -0700 | [diff] [blame] | 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 |  | 
| Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 53 | void CheckIfShouldLog() { | 
|  | 54 | bool expected = true; | 
|  | 55 | if (g_call_mallopt.compare_exchange_strong(expected, false)) { | 
| Christopher Ferris | c1341e1 | 2024-07-11 19:48:08 -0700 | [diff] [blame] | 56 | Log(); | 
| Christopher Ferris | 5610d5a | 2023-11-14 15:04:50 -0800 | [diff] [blame] | 57 | } | 
|  | 58 | } | 
|  | 59 |  | 
|  | 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 |