xref: /aosp_15_r20/system/extras/memory_replay/Alloc.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #include <malloc.h>
18*288bf522SAndroid Build Coastguard Worker #include <stdint.h>
19*288bf522SAndroid Build Coastguard Worker #include <stdio.h>
20*288bf522SAndroid Build Coastguard Worker #include <unistd.h>
21*288bf522SAndroid Build Coastguard Worker 
22*288bf522SAndroid Build Coastguard Worker #include <memory_trace/MemoryTrace.h>
23*288bf522SAndroid Build Coastguard Worker 
24*288bf522SAndroid Build Coastguard Worker #include "Alloc.h"
25*288bf522SAndroid Build Coastguard Worker #include "Pointers.h"
26*288bf522SAndroid Build Coastguard Worker #include "Utils.h"
27*288bf522SAndroid Build Coastguard Worker 
AllocDoesFree(const memory_trace::Entry & entry)28*288bf522SAndroid Build Coastguard Worker bool AllocDoesFree(const memory_trace::Entry& entry) {
29*288bf522SAndroid Build Coastguard Worker   switch (entry.type) {
30*288bf522SAndroid Build Coastguard Worker     case memory_trace::MALLOC:
31*288bf522SAndroid Build Coastguard Worker     case memory_trace::CALLOC:
32*288bf522SAndroid Build Coastguard Worker     case memory_trace::MEMALIGN:
33*288bf522SAndroid Build Coastguard Worker     case memory_trace::THREAD_DONE:
34*288bf522SAndroid Build Coastguard Worker       return false;
35*288bf522SAndroid Build Coastguard Worker 
36*288bf522SAndroid Build Coastguard Worker     case memory_trace::FREE:
37*288bf522SAndroid Build Coastguard Worker       return entry.ptr != 0;
38*288bf522SAndroid Build Coastguard Worker 
39*288bf522SAndroid Build Coastguard Worker     case memory_trace::REALLOC:
40*288bf522SAndroid Build Coastguard Worker       return entry.u.old_ptr != 0;
41*288bf522SAndroid Build Coastguard Worker   }
42*288bf522SAndroid Build Coastguard Worker }
43*288bf522SAndroid Build Coastguard Worker 
MallocExecute(const memory_trace::Entry & entry,Pointers * pointers)44*288bf522SAndroid Build Coastguard Worker static uint64_t MallocExecute(const memory_trace::Entry& entry, Pointers* pointers) {
45*288bf522SAndroid Build Coastguard Worker   int pagesize = getpagesize();
46*288bf522SAndroid Build Coastguard Worker   uint64_t time_nsecs = Nanotime();
47*288bf522SAndroid Build Coastguard Worker   void* memory = malloc(entry.size);
48*288bf522SAndroid Build Coastguard Worker   MakeAllocationResident(memory, entry.size, pagesize);
49*288bf522SAndroid Build Coastguard Worker   time_nsecs = Nanotime() - time_nsecs;
50*288bf522SAndroid Build Coastguard Worker 
51*288bf522SAndroid Build Coastguard Worker   pointers->Add(entry.ptr, memory);
52*288bf522SAndroid Build Coastguard Worker 
53*288bf522SAndroid Build Coastguard Worker   return time_nsecs;
54*288bf522SAndroid Build Coastguard Worker }
55*288bf522SAndroid Build Coastguard Worker 
CallocExecute(const memory_trace::Entry & entry,Pointers * pointers)56*288bf522SAndroid Build Coastguard Worker static uint64_t CallocExecute(const memory_trace::Entry& entry, Pointers* pointers) {
57*288bf522SAndroid Build Coastguard Worker   int pagesize = getpagesize();
58*288bf522SAndroid Build Coastguard Worker   uint64_t time_nsecs = Nanotime();
59*288bf522SAndroid Build Coastguard Worker   void* memory = calloc(entry.u.n_elements, entry.size);
60*288bf522SAndroid Build Coastguard Worker   MakeAllocationResident(memory, entry.u.n_elements * entry.size, pagesize);
61*288bf522SAndroid Build Coastguard Worker   time_nsecs = Nanotime() - time_nsecs;
62*288bf522SAndroid Build Coastguard Worker 
63*288bf522SAndroid Build Coastguard Worker   pointers->Add(entry.ptr, memory);
64*288bf522SAndroid Build Coastguard Worker 
65*288bf522SAndroid Build Coastguard Worker   return time_nsecs;
66*288bf522SAndroid Build Coastguard Worker }
67*288bf522SAndroid Build Coastguard Worker 
ReallocExecute(const memory_trace::Entry & entry,Pointers * pointers)68*288bf522SAndroid Build Coastguard Worker static uint64_t ReallocExecute(const memory_trace::Entry& entry, Pointers* pointers) {
69*288bf522SAndroid Build Coastguard Worker   void* old_memory = nullptr;
70*288bf522SAndroid Build Coastguard Worker   if (entry.u.old_ptr != 0) {
71*288bf522SAndroid Build Coastguard Worker     old_memory = pointers->Remove(entry.u.old_ptr);
72*288bf522SAndroid Build Coastguard Worker   }
73*288bf522SAndroid Build Coastguard Worker 
74*288bf522SAndroid Build Coastguard Worker   int pagesize = getpagesize();
75*288bf522SAndroid Build Coastguard Worker   uint64_t time_nsecs = Nanotime();
76*288bf522SAndroid Build Coastguard Worker   void* memory = realloc(old_memory, entry.size);
77*288bf522SAndroid Build Coastguard Worker   MakeAllocationResident(memory, entry.size, pagesize);
78*288bf522SAndroid Build Coastguard Worker   time_nsecs = Nanotime() - time_nsecs;
79*288bf522SAndroid Build Coastguard Worker 
80*288bf522SAndroid Build Coastguard Worker   pointers->Add(entry.ptr, memory);
81*288bf522SAndroid Build Coastguard Worker 
82*288bf522SAndroid Build Coastguard Worker   return time_nsecs;
83*288bf522SAndroid Build Coastguard Worker }
84*288bf522SAndroid Build Coastguard Worker 
MemalignExecute(const memory_trace::Entry & entry,Pointers * pointers)85*288bf522SAndroid Build Coastguard Worker static uint64_t MemalignExecute(const memory_trace::Entry& entry, Pointers* pointers) {
86*288bf522SAndroid Build Coastguard Worker   int pagesize = getpagesize();
87*288bf522SAndroid Build Coastguard Worker   uint64_t time_nsecs = Nanotime();
88*288bf522SAndroid Build Coastguard Worker   void* memory = memalign(entry.u.align, entry.size);
89*288bf522SAndroid Build Coastguard Worker   MakeAllocationResident(memory, entry.size, pagesize);
90*288bf522SAndroid Build Coastguard Worker   time_nsecs = Nanotime() - time_nsecs;
91*288bf522SAndroid Build Coastguard Worker 
92*288bf522SAndroid Build Coastguard Worker   pointers->Add(entry.ptr, memory);
93*288bf522SAndroid Build Coastguard Worker 
94*288bf522SAndroid Build Coastguard Worker   return time_nsecs;
95*288bf522SAndroid Build Coastguard Worker }
96*288bf522SAndroid Build Coastguard Worker 
FreeExecute(const memory_trace::Entry & entry,Pointers * pointers)97*288bf522SAndroid Build Coastguard Worker static uint64_t FreeExecute(const memory_trace::Entry& entry, Pointers* pointers) {
98*288bf522SAndroid Build Coastguard Worker   if (entry.ptr == 0) {
99*288bf522SAndroid Build Coastguard Worker     return 0;
100*288bf522SAndroid Build Coastguard Worker   }
101*288bf522SAndroid Build Coastguard Worker 
102*288bf522SAndroid Build Coastguard Worker   void* memory = pointers->Remove(entry.ptr);
103*288bf522SAndroid Build Coastguard Worker   uint64_t time_nsecs = Nanotime();
104*288bf522SAndroid Build Coastguard Worker   free(memory);
105*288bf522SAndroid Build Coastguard Worker   return Nanotime() - time_nsecs;
106*288bf522SAndroid Build Coastguard Worker }
107*288bf522SAndroid Build Coastguard Worker 
AllocExecute(const memory_trace::Entry & entry,Pointers * pointers)108*288bf522SAndroid Build Coastguard Worker uint64_t AllocExecute(const memory_trace::Entry& entry, Pointers* pointers) {
109*288bf522SAndroid Build Coastguard Worker   switch (entry.type) {
110*288bf522SAndroid Build Coastguard Worker     case memory_trace::MALLOC:
111*288bf522SAndroid Build Coastguard Worker       return MallocExecute(entry, pointers);
112*288bf522SAndroid Build Coastguard Worker     case memory_trace::CALLOC:
113*288bf522SAndroid Build Coastguard Worker       return CallocExecute(entry, pointers);
114*288bf522SAndroid Build Coastguard Worker     case memory_trace::REALLOC:
115*288bf522SAndroid Build Coastguard Worker       return ReallocExecute(entry, pointers);
116*288bf522SAndroid Build Coastguard Worker     case memory_trace::MEMALIGN:
117*288bf522SAndroid Build Coastguard Worker       return MemalignExecute(entry, pointers);
118*288bf522SAndroid Build Coastguard Worker     case memory_trace::FREE:
119*288bf522SAndroid Build Coastguard Worker       return FreeExecute(entry, pointers);
120*288bf522SAndroid Build Coastguard Worker     default:
121*288bf522SAndroid Build Coastguard Worker       return 0;
122*288bf522SAndroid Build Coastguard Worker   }
123*288bf522SAndroid Build Coastguard Worker }
124