xref: /aosp_15_r20/bionic/benchmarks/malloc_sql_benchmark.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker  * Copyright (C) 2018 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker  * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker  *
5*8d67ca89SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker  * are met:
8*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker  *    the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker  *    distribution.
14*8d67ca89SAndroid Build Coastguard Worker  *
15*8d67ca89SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker  * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker  */
28*8d67ca89SAndroid Build Coastguard Worker 
29*8d67ca89SAndroid Build Coastguard Worker #include <malloc.h>
30*8d67ca89SAndroid Build Coastguard Worker #include <stdlib.h>
31*8d67ca89SAndroid Build Coastguard Worker #include <unistd.h>
32*8d67ca89SAndroid Build Coastguard Worker 
33*8d67ca89SAndroid Build Coastguard Worker #include <benchmark/benchmark.h>
34*8d67ca89SAndroid Build Coastguard Worker #include "ScopedDecayTimeRestorer.h"
35*8d67ca89SAndroid Build Coastguard Worker #include "util.h"
36*8d67ca89SAndroid Build Coastguard Worker 
37*8d67ca89SAndroid Build Coastguard Worker #if defined(__BIONIC__)
38*8d67ca89SAndroid Build Coastguard Worker 
39*8d67ca89SAndroid Build Coastguard Worker enum AllocEnum : uint8_t {
40*8d67ca89SAndroid Build Coastguard Worker   MALLOC = 0,
41*8d67ca89SAndroid Build Coastguard Worker   CALLOC,
42*8d67ca89SAndroid Build Coastguard Worker   MEMALIGN,
43*8d67ca89SAndroid Build Coastguard Worker   REALLOC,
44*8d67ca89SAndroid Build Coastguard Worker   FREE,
45*8d67ca89SAndroid Build Coastguard Worker };
46*8d67ca89SAndroid Build Coastguard Worker 
47*8d67ca89SAndroid Build Coastguard Worker struct MallocEntry {
48*8d67ca89SAndroid Build Coastguard Worker   AllocEnum type;
49*8d67ca89SAndroid Build Coastguard Worker   size_t idx;
50*8d67ca89SAndroid Build Coastguard Worker   size_t size;
51*8d67ca89SAndroid Build Coastguard Worker   size_t arg2;
52*8d67ca89SAndroid Build Coastguard Worker };
53*8d67ca89SAndroid Build Coastguard Worker 
BenchmarkMalloc(MallocEntry entries[],size_t total_entries,size_t max_allocs)54*8d67ca89SAndroid Build Coastguard Worker void BenchmarkMalloc(MallocEntry entries[], size_t total_entries, size_t max_allocs) {
55*8d67ca89SAndroid Build Coastguard Worker   void* ptrs[max_allocs];
56*8d67ca89SAndroid Build Coastguard Worker 
57*8d67ca89SAndroid Build Coastguard Worker   for (size_t i = 0; i < total_entries; i++) {
58*8d67ca89SAndroid Build Coastguard Worker     switch (entries[i].type) {
59*8d67ca89SAndroid Build Coastguard Worker     case MALLOC:
60*8d67ca89SAndroid Build Coastguard Worker       ptrs[entries[i].idx] = malloc(entries[i].size);
61*8d67ca89SAndroid Build Coastguard Worker       // Touch at least one byte of the allocation to make sure that
62*8d67ca89SAndroid Build Coastguard Worker       // PSS for this allocation is counted.
63*8d67ca89SAndroid Build Coastguard Worker       reinterpret_cast<uint8_t*>(ptrs[entries[i].idx])[0] = 10;
64*8d67ca89SAndroid Build Coastguard Worker       break;
65*8d67ca89SAndroid Build Coastguard Worker     case CALLOC:
66*8d67ca89SAndroid Build Coastguard Worker       ptrs[entries[i].idx] = calloc(entries[i].arg2, entries[i].size);
67*8d67ca89SAndroid Build Coastguard Worker       // Touch at least one byte of the allocation to make sure that
68*8d67ca89SAndroid Build Coastguard Worker       // PSS for this allocation is counted.
69*8d67ca89SAndroid Build Coastguard Worker       reinterpret_cast<uint8_t*>(ptrs[entries[i].idx])[0] = 20;
70*8d67ca89SAndroid Build Coastguard Worker       break;
71*8d67ca89SAndroid Build Coastguard Worker     case MEMALIGN:
72*8d67ca89SAndroid Build Coastguard Worker       ptrs[entries[i].idx] = memalign(entries[i].arg2, entries[i].size);
73*8d67ca89SAndroid Build Coastguard Worker       // Touch at least one byte of the allocation to make sure that
74*8d67ca89SAndroid Build Coastguard Worker       // PSS for this allocation is counted.
75*8d67ca89SAndroid Build Coastguard Worker       reinterpret_cast<uint8_t*>(ptrs[entries[i].idx])[0] = 30;
76*8d67ca89SAndroid Build Coastguard Worker       break;
77*8d67ca89SAndroid Build Coastguard Worker     case REALLOC:
78*8d67ca89SAndroid Build Coastguard Worker       if (entries[i].arg2 == 0) {
79*8d67ca89SAndroid Build Coastguard Worker         ptrs[entries[i].idx] = realloc(nullptr, entries[i].size);
80*8d67ca89SAndroid Build Coastguard Worker       } else {
81*8d67ca89SAndroid Build Coastguard Worker         ptrs[entries[i].idx] = realloc(ptrs[entries[i].arg2 - 1], entries[i].size);
82*8d67ca89SAndroid Build Coastguard Worker       }
83*8d67ca89SAndroid Build Coastguard Worker       // Touch at least one byte of the allocation to make sure that
84*8d67ca89SAndroid Build Coastguard Worker       // PSS for this allocation is counted.
85*8d67ca89SAndroid Build Coastguard Worker       reinterpret_cast<uint8_t*>(ptrs[entries[i].idx])[0] = 40;
86*8d67ca89SAndroid Build Coastguard Worker       break;
87*8d67ca89SAndroid Build Coastguard Worker     case FREE:
88*8d67ca89SAndroid Build Coastguard Worker       free(ptrs[entries[i].idx]);
89*8d67ca89SAndroid Build Coastguard Worker       break;
90*8d67ca89SAndroid Build Coastguard Worker     }
91*8d67ca89SAndroid Build Coastguard Worker   }
92*8d67ca89SAndroid Build Coastguard Worker }
93*8d67ca89SAndroid Build Coastguard Worker 
94*8d67ca89SAndroid Build Coastguard Worker // This codifies playing back a single threaded trace of the allocations
95*8d67ca89SAndroid Build Coastguard Worker // when running the SQLite BenchMark app.
96*8d67ca89SAndroid Build Coastguard Worker // Instructions for recreating:
97*8d67ca89SAndroid Build Coastguard Worker //   - Enable malloc debug
98*8d67ca89SAndroid Build Coastguard Worker //       setprop wrap.com.wtsang02.sqliteutil "LIBC_DEBUG_MALLOC_OPTIONS=record_allocs logwrapper"
99*8d67ca89SAndroid Build Coastguard Worker //   - Start the SQLite BenchMark app
100*8d67ca89SAndroid Build Coastguard Worker //   - Dump allocs using the signal to get rid of non sql allocs(kill -47 <SQLITE_PID>)
101*8d67ca89SAndroid Build Coastguard Worker //   - Run the benchmark.
102*8d67ca89SAndroid Build Coastguard Worker //   - Dump allocs using the signal again.
103*8d67ca89SAndroid Build Coastguard Worker //   - Find the thread that has the most allocs and run the helper script
104*8d67ca89SAndroid Build Coastguard Worker //       bionic/libc/malloc_debug/tools/gen_malloc.pl -i <THREAD_ID> g_sql_entries kMaxSqlAllocSlots < <ALLOC_FILE> > malloc_sql.h
105*8d67ca89SAndroid Build Coastguard Worker #include "malloc_sql.h"
106*8d67ca89SAndroid Build Coastguard Worker 
BM_malloc_sql_trace_default(benchmark::State & state)107*8d67ca89SAndroid Build Coastguard Worker static void BM_malloc_sql_trace_default(benchmark::State& state) {
108*8d67ca89SAndroid Build Coastguard Worker   ScopedDecayTimeRestorer restorer;
109*8d67ca89SAndroid Build Coastguard Worker 
110*8d67ca89SAndroid Build Coastguard Worker   // The default is expected to be a zero decay time.
111*8d67ca89SAndroid Build Coastguard Worker   mallopt(M_DECAY_TIME, 0);
112*8d67ca89SAndroid Build Coastguard Worker 
113*8d67ca89SAndroid Build Coastguard Worker   for (auto _ : state) {
114*8d67ca89SAndroid Build Coastguard Worker     BenchmarkMalloc(g_sql_entries, sizeof(g_sql_entries) / sizeof(MallocEntry),
115*8d67ca89SAndroid Build Coastguard Worker                     kMaxSqlAllocSlots);
116*8d67ca89SAndroid Build Coastguard Worker   }
117*8d67ca89SAndroid Build Coastguard Worker }
118*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_malloc_sql_trace_default);
119*8d67ca89SAndroid Build Coastguard Worker 
BM_malloc_sql_trace_decay1(benchmark::State & state)120*8d67ca89SAndroid Build Coastguard Worker static void BM_malloc_sql_trace_decay1(benchmark::State& state) {
121*8d67ca89SAndroid Build Coastguard Worker   ScopedDecayTimeRestorer restorer;
122*8d67ca89SAndroid Build Coastguard Worker 
123*8d67ca89SAndroid Build Coastguard Worker   mallopt(M_DECAY_TIME, 1);
124*8d67ca89SAndroid Build Coastguard Worker 
125*8d67ca89SAndroid Build Coastguard Worker   for (auto _ : state) {
126*8d67ca89SAndroid Build Coastguard Worker     BenchmarkMalloc(g_sql_entries, sizeof(g_sql_entries) / sizeof(MallocEntry),
127*8d67ca89SAndroid Build Coastguard Worker                     kMaxSqlAllocSlots);
128*8d67ca89SAndroid Build Coastguard Worker   }
129*8d67ca89SAndroid Build Coastguard Worker }
130*8d67ca89SAndroid Build Coastguard Worker BIONIC_BENCHMARK(BM_malloc_sql_trace_decay1);
131*8d67ca89SAndroid Build Coastguard Worker 
132*8d67ca89SAndroid Build Coastguard Worker #endif
133