1 /*
2 * Copyright (C) 2018 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 <malloc.h>
30 #include <stdlib.h>
31
32 #include <benchmark/benchmark.h>
33 #include "util.h"
34
35 #if defined(__BIONIC__)
36
37 enum AllocEnum : uint8_t {
38 MALLOC = 0,
39 CALLOC,
40 MEMALIGN,
41 REALLOC,
42 FREE,
43 };
44
45 struct MallocEntry {
46 AllocEnum type;
47 size_t idx;
48 size_t size;
49 size_t arg2;
50 };
51
BenchmarkMalloc(MallocEntry entries[],size_t total_entries,size_t max_allocs)52 void BenchmarkMalloc(MallocEntry entries[], size_t total_entries, size_t max_allocs) {
53 void* ptrs[max_allocs];
54
55 for (size_t i = 0; i < total_entries; i++) {
56 switch (entries[i].type) {
57 case MALLOC:
58 ptrs[entries[i].idx] = malloc(entries[i].size);
59 // Touch at least one byte of the allocation to make sure that
60 // PSS for this allocation is counted.
61 reinterpret_cast<uint8_t*>(ptrs[entries[i].idx])[0] = 10;
62 break;
63 case CALLOC:
64 ptrs[entries[i].idx] = calloc(entries[i].arg2, entries[i].size);
65 // Touch at least one byte of the allocation to make sure that
66 // PSS for this allocation is counted.
67 reinterpret_cast<uint8_t*>(ptrs[entries[i].idx])[0] = 20;
68 break;
69 case MEMALIGN:
70 ptrs[entries[i].idx] = memalign(entries[i].arg2, entries[i].size);
71 // Touch at least one byte of the allocation to make sure that
72 // PSS for this allocation is counted.
73 reinterpret_cast<uint8_t*>(ptrs[entries[i].idx])[0] = 30;
74 break;
75 case REALLOC:
76 if (entries[i].arg2 == 0) {
77 ptrs[entries[i].idx] = realloc(nullptr, entries[i].size);
78 } else {
79 ptrs[entries[i].idx] = realloc(ptrs[entries[i].arg2 - 1], entries[i].size);
80 }
81 // Touch at least one byte of the allocation to make sure that
82 // PSS for this allocation is counted.
83 reinterpret_cast<uint8_t*>(ptrs[entries[i].idx])[0] = 40;
84 break;
85 case FREE:
86 free(ptrs[entries[i].idx]);
87 break;
88 }
89 }
90 }
91
92 // This codifies playing back a single threaded trace of the allocations
93 // when running the SQLite BenchMark app.
94 // Instructions for recreating:
95 // - Enable malloc debug
96 // setprop wrap.com.wtsang02.sqliteutil "LIBC_DEBUG_MALLOC_OPTIONS=record_allocs logwrapper"
97 // - Start the SQLite BenchMark app
98 // - Dump allocs using the signal to get rid of non sql allocs(kill -47 <SQLITE_PID>)
99 // - Run the benchmark.
100 // - Dump allocs using the signal again.
101 // - Find the thread that has the most allocs and run the helper script
102 // bionic/libc/malloc_debug/tools/gen_malloc.pl -i <THREAD_ID> g_sql_entries kMaxSqlAllocSlots < <ALLOC_FILE> > malloc_sql.h
103 #include "malloc_sql.h"
104
BM_malloc_sql_trace_default(benchmark::State & state)105 static void BM_malloc_sql_trace_default(benchmark::State& state) {
106 // The default is expected to be a zero decay time.
107 mallopt(M_DECAY_TIME, 0);
108
109 for (auto _ : state) {
110 BenchmarkMalloc(g_sql_entries, sizeof(g_sql_entries) / sizeof(MallocEntry),
111 kMaxSqlAllocSlots);
112 }
113 }
114 BIONIC_BENCHMARK(BM_malloc_sql_trace_default);
115
BM_malloc_sql_trace_decay1(benchmark::State & state)116 static void BM_malloc_sql_trace_decay1(benchmark::State& state) {
117 mallopt(M_DECAY_TIME, 1);
118
119 for (auto _ : state) {
120 BenchmarkMalloc(g_sql_entries, sizeof(g_sql_entries) / sizeof(MallocEntry),
121 kMaxSqlAllocSlots);
122 }
123
124 mallopt(M_DECAY_TIME, 0);
125 }
126 BIONIC_BENCHMARK(BM_malloc_sql_trace_decay1);
127
128 #endif
129