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