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