summaryrefslogtreecommitdiff
path: root/memory_replay/TraceBenchmark.cpp
blob: da8867219a84b55f541bb31bf481de47e72acf1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <err.h>
#include <inttypes.h>
#include <malloc.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <algorithm>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#include <android-base/file.h>
#include <android-base/strings.h>
#include <benchmark/benchmark.h>

#include "Alloc.h"
#include "File.h"
#include "Utils.h"

struct TraceAllocEntry {
  TraceAllocEntry(AllocEnum type, size_t idx, size_t size, size_t last_arg)
      : type(type), idx(idx), size(size) {
    u.old_idx = last_arg;
  }
  AllocEnum type;
  size_t idx;
  size_t size;
  union {
    size_t old_idx = 0;
    size_t align;
    size_t n_elements;
  } u;
};

static size_t GetIndex(std::stack<size_t>& free_indices, size_t* max_index) {
  if (free_indices.empty()) {
    return (*max_index)++;
  }
  size_t index = free_indices.top();
  free_indices.pop();
  return index;
}

static std::vector<TraceAllocEntry>* GetTraceData(const char* filename, size_t* max_ptrs) {
  // Only keep last trace encountered cached.
  static std::string cached_filename;
  static std::vector<TraceAllocEntry> cached_entries;
  static size_t cached_max_ptrs;

  if (cached_filename == filename) {
    *max_ptrs = cached_max_ptrs;
    return &cached_entries;
  }

  cached_entries.clear();
  cached_max_ptrs = 0;
  cached_filename = filename;

  std::string content(ZipGetContents(filename));
  if (content.empty()) {
    errx(1, "Internal Error: Empty zip file %s", filename);
  }
  std::vector<std::string> lines(android::base::Split(content, "\n"));

  *max_ptrs = 0;
  std::stack<size_t> free_indices;
  std::unordered_map<uint64_t, size_t> ptr_to_index;
  std::vector<TraceAllocEntry>* entries = &cached_entries;
  for (const std::string& line : lines) {
    if (line.empty()) {
      continue;
    }
    AllocEntry entry;
    AllocGetData(line, &entry);

    switch (entry.type) {
      case MALLOC: {
        size_t idx = GetIndex(free_indices, max_ptrs);
        ptr_to_index[entry.ptr] = idx;
        entries->emplace_back(MALLOC, idx, entry.size, 0);
        break;
      }
      case CALLOC: {
        size_t idx = GetIndex(free_indices, max_ptrs);
        ptr_to_index[entry.ptr] = idx;
        entries->emplace_back(CALLOC, idx, entry.u.n_elements, entry.size);
        break;
      }
      case MEMALIGN: {
        size_t idx = GetIndex(free_indices, max_ptrs);
        ptr_to_index[entry.ptr] = idx;
        entries->emplace_back(MEMALIGN, idx, entry.size, entry.u.align);
        break;
      }
      case REALLOC: {
        size_t old_pointer_idx = 0;
        if (entry.u.old_ptr != 0) {
          auto idx_entry = ptr_to_index.find(entry.u.old_ptr);
          if (idx_entry == ptr_to_index.end()) {
            errx(1, "File Error: Failed to find realloc pointer %" PRIu64, entry.u.old_ptr);
          }
          old_pointer_idx = idx_entry->second;
          free_indices.push(old_pointer_idx);
        }
        size_t idx = GetIndex(free_indices, max_ptrs);
        ptr_to_index[entry.ptr] = idx;
        entries->emplace_back(REALLOC, idx, entry.size, old_pointer_idx + 1);
        break;
      }
      case FREE:
        if (entry.ptr != 0) {
          auto idx_entry = ptr_to_index.find(entry.ptr);
          if (idx_entry == ptr_to_index.end()) {
            errx(1, "File Error: Unable to find free pointer %" PRIu64, entry.ptr);
          }
          free_indices.push(idx_entry->second);
          entries->emplace_back(FREE, idx_entry->second + 1, 0, 0);
        } else {
          entries->emplace_back(FREE, 0, 0, 0);
        }
        break;
      case THREAD_DONE:
        // Ignore these.
        break;
    }
  }

  cached_max_ptrs = *max_ptrs;
  return entries;
}

static void RunTrace(benchmark::State& state, std::vector<TraceAllocEntry>& entries,
                     size_t max_ptrs) {
  std::vector<void*> ptrs(max_ptrs, nullptr);

  int pagesize = getpagesize();
  void* ptr;
  uint64_t total_ns = 0;
  uint64_t start_ns;
  for (auto& entry : entries) {
    switch (entry.type) {
      case MALLOC:
        start_ns = Nanotime();
        ptr = malloc(entry.size);
        if (ptr == nullptr) {
          errx(1, "malloc returned nullptr");
        }
        MakeAllocationResident(ptr, entry.size, pagesize);
        total_ns += Nanotime() - start_ns;

        if (ptrs[entry.idx] != nullptr) {
          errx(1, "Internal Error: malloc pointer being replaced is not nullptr");
        }
        ptrs[entry.idx] = ptr;
        break;

      case CALLOC:
        start_ns = Nanotime();
        ptr = calloc(entry.u.n_elements, entry.size);
        if (ptr == nullptr) {
          errx(1, "calloc returned nullptr");
        }
        MakeAllocationResident(ptr, entry.size, pagesize);
        total_ns += Nanotime() - start_ns;

        if (ptrs[entry.idx] != nullptr) {
          errx(1, "Internal Error: calloc pointer being replaced is not nullptr");
        }
        ptrs[entry.idx] = ptr;
        break;

      case MEMALIGN:
        start_ns = Nanotime();
        ptr = memalign(entry.u.align, entry.size);
        if (ptr == nullptr) {
          errx(1, "memalign returned nullptr");
        }
        MakeAllocationResident(ptr, entry.size, pagesize);
        total_ns += Nanotime() - start_ns;

        if (ptrs[entry.idx] != nullptr) {
          errx(1, "Internal Error: memalign pointer being replaced is not nullptr");
        }
        ptrs[entry.idx] = ptr;
        break;

      case REALLOC:
        start_ns = Nanotime();
        if (entry.u.old_idx == 0) {
          ptr = realloc(nullptr, entry.size);
        } else {
          ptr = realloc(ptrs[entry.u.old_idx - 1], entry.size);
          ptrs[entry.u.old_idx - 1] = nullptr;
        }
        if (entry.size > 0) {
          if (ptr == nullptr) {
            errx(1, "realloc returned nullptr");
          }
          MakeAllocationResident(ptr, entry.size, pagesize);
        }
        total_ns += Nanotime() - start_ns;

        if (ptrs[entry.idx] != nullptr) {
          errx(1, "Internal Error: realloc pointer being replaced is not nullptr");
        }
        ptrs[entry.idx] = ptr;
        break;

      case FREE:
        if (entry.idx != 0) {
          ptr = ptrs[entry.idx - 1];
          ptrs[entry.idx - 1] = nullptr;
        } else {
          ptr = nullptr;
        }
        start_ns = Nanotime();
        free(ptr);
        total_ns += Nanotime() - start_ns;
        break;

      case THREAD_DONE:
        break;
    }
  }
  state.SetIterationTime(total_ns / double(1000000000.0));

  std::for_each(ptrs.begin(), ptrs.end(), [](void* ptr) { free(ptr); });
}

// Run a trace as if all of the allocations occurred in a single thread.
// This is not completely realistic, but it is a possible worst case that
// could happen in an app.
static void BenchmarkTrace(benchmark::State& state, const char* filename) {
  std::string full_filename(android::base::GetExecutableDirectory() + "/traces/" + filename);
  size_t max_ptrs;
  std::vector<TraceAllocEntry>* entries = GetTraceData(full_filename.c_str(), &max_ptrs);
  if (entries == nullptr) {
    errx(1, "ERROR: Failed to get trace data for %s.", full_filename.c_str());
  }

#if defined(__BIONIC__)
  // Need to set the decay time the same as how an app would operate.
  mallopt(M_DECAY_TIME, 1);
#endif

  for (auto _ : state) {
    RunTrace(state, *entries, max_ptrs);
  }
}

#define BENCH_OPTIONS                 \
  UseManualTime()                     \
      ->Unit(benchmark::kMicrosecond) \
      ->MinTime(15.0)                 \
      ->Repetitions(4)                \
      ->ReportAggregatesOnly(true)

static void BM_angry_birds2(benchmark::State& state) {
  BenchmarkTrace(state, "angry_birds2.zip");
}
BENCHMARK(BM_angry_birds2)->BENCH_OPTIONS;

static void BM_camera(benchmark::State& state) {
  BenchmarkTrace(state, "camera.zip");
}
BENCHMARK(BM_camera)->BENCH_OPTIONS;

static void BM_candy_crush_saga(benchmark::State& state) {
  BenchmarkTrace(state, "candy_crush_saga.zip");
}
BENCHMARK(BM_candy_crush_saga)->BENCH_OPTIONS;

void BM_gmail(benchmark::State& state) {
  BenchmarkTrace(state, "gmail.zip");
}
BENCHMARK(BM_gmail)->BENCH_OPTIONS;

void BM_maps(benchmark::State& state) {
  BenchmarkTrace(state, "maps.zip");
}
BENCHMARK(BM_maps)->BENCH_OPTIONS;

void BM_photos(benchmark::State& state) {
  BenchmarkTrace(state, "photos.zip");
}
BENCHMARK(BM_photos)->BENCH_OPTIONS;

void BM_pubg(benchmark::State& state) {
  BenchmarkTrace(state, "pubg.zip");
}
BENCHMARK(BM_pubg)->BENCH_OPTIONS;

void BM_surfaceflinger(benchmark::State& state) {
  BenchmarkTrace(state, "surfaceflinger.zip");
}
BENCHMARK(BM_surfaceflinger)->BENCH_OPTIONS;

void BM_system_server(benchmark::State& state) {
  BenchmarkTrace(state, "system_server.zip");
}
BENCHMARK(BM_system_server)->BENCH_OPTIONS;

void BM_systemui(benchmark::State& state) {
  BenchmarkTrace(state, "systemui.zip");
}
BENCHMARK(BM_systemui)->BENCH_OPTIONS;

void BM_youtube(benchmark::State& state) {
  BenchmarkTrace(state, "youtube.zip");
}
BENCHMARK(BM_youtube)->BENCH_OPTIONS;

int main(int argc, char** argv) {
  std::vector<char*> args;
  args.push_back(argv[0]);

  // Look for the --cpu=XX option.
  for (int i = 1; i < argc; i++) {
    if (strncmp(argv[i], "--cpu=", 6) == 0) {
      char* endptr;
      int cpu = strtol(&argv[i][6], &endptr, 10);
      if (argv[i][0] == '\0' || endptr == nullptr || *endptr != '\0') {
        printf("Invalid format of --cpu option, '%s' must be an integer value.\n", argv[i] + 6);
        return 1;
      }
      cpu_set_t cpuset;
      CPU_ZERO(&cpuset);
      CPU_SET(cpu, &cpuset);
      if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0) {
        if (errno == EINVAL) {
          printf("Invalid cpu %d\n", cpu);
          return 1;
        }
        perror("sched_setaffinity failed");
        return 1;
      }
      printf("Locking to cpu %d\n", cpu);
    } else {
      args.push_back(argv[i]);
    }
  }

  argc = args.size();
  ::benchmark::Initialize(&argc, args.data());
  if (::benchmark::ReportUnrecognizedArguments(argc, args.data())) return 1;
  ::benchmark::RunSpecifiedBenchmarks();
}