blob: c9671e173bd0d5d61db3da28bb380115a7ea292c [file] [log] [blame]
Christopher Ferris33c03d32015-09-11 15:34:23 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <inttypes.h>
18#include <malloc.h>
19#include <stdint.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/param.h>
Christopher Ferriscd7d1082015-10-14 13:25:15 -070024#include <time.h>
Christopher Ferris33c03d32015-09-11 15:34:23 -070025
26#include <new>
27
28#include "Action.h"
29#include "Threads.h"
30#include "Pointers.h"
31
Christopher Ferriscd7d1082015-10-14 13:25:15 -070032static uint64_t nanotime() {
33 struct timespec t;
34 t.tv_sec = t.tv_nsec = 0;
35 clock_gettime(CLOCK_MONOTONIC, &t);
36 return static_cast<uint64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec;
37}
38
Christopher Ferris33c03d32015-09-11 15:34:23 -070039class EndThreadAction : public Action {
40 public:
41 EndThreadAction() {}
42
43 bool EndThread() override { return true; }
44
Christopher Ferriscd7d1082015-10-14 13:25:15 -070045 uint64_t Execute(Pointers*) override { return 0; }
Christopher Ferris33c03d32015-09-11 15:34:23 -070046};
47
48class AllocAction : public Action {
49 public:
50 AllocAction(uintptr_t key_pointer) : key_pointer_(key_pointer) {}
51
52 protected:
53 uintptr_t key_pointer_ = 0;
54 size_t size_ = 0;
55};
56
57class MallocAction : public AllocAction {
58 public:
59 MallocAction(uintptr_t key_pointer, const char* line) : AllocAction(key_pointer) {
60 if (sscanf(line, "%zu", &size_) != 1) {
61 is_error_ = true;
62 }
63 }
64
Christopher Ferriscd7d1082015-10-14 13:25:15 -070065 uint64_t Execute(Pointers* pointers) override {
66 uint64_t time_nsecs = nanotime();
Christopher Ferris33c03d32015-09-11 15:34:23 -070067 void* memory = malloc(size_);
Christopher Ferriscd7d1082015-10-14 13:25:15 -070068 time_nsecs = nanotime() - time_nsecs;
69
Christopher Ferris33c03d32015-09-11 15:34:23 -070070 memset(memory, 1, size_);
71 pointers->Add(key_pointer_, memory);
Christopher Ferriscd7d1082015-10-14 13:25:15 -070072
73 return time_nsecs;
Christopher Ferris33c03d32015-09-11 15:34:23 -070074 }
75};
76
77class CallocAction : public AllocAction {
78 public:
79 CallocAction(uintptr_t key_pointer, const char* line) : AllocAction(key_pointer) {
80 if (sscanf(line, "%zu %zu", &n_elements_, &size_) != 2) {
81 is_error_ = true;
82 }
83 }
84
Christopher Ferriscd7d1082015-10-14 13:25:15 -070085 uint64_t Execute(Pointers* pointers) override {
86 uint64_t time_nsecs = nanotime();
Christopher Ferris33c03d32015-09-11 15:34:23 -070087 void* memory = calloc(n_elements_, size_);
Christopher Ferriscd7d1082015-10-14 13:25:15 -070088 time_nsecs = nanotime() - time_nsecs;
89
Christopher Ferris33c03d32015-09-11 15:34:23 -070090 memset(memory, 0, n_elements_ * size_);
91 pointers->Add(key_pointer_, memory);
Christopher Ferriscd7d1082015-10-14 13:25:15 -070092
93 return time_nsecs;
Christopher Ferris33c03d32015-09-11 15:34:23 -070094 }
95
96 private:
97 size_t n_elements_ = 0;
98};
99
100class ReallocAction : public AllocAction {
101 public:
102 ReallocAction(uintptr_t key_pointer, const char* line) : AllocAction(key_pointer) {
103 if (sscanf(line, "%" SCNxPTR " %zu", &old_pointer_, &size_) != 2) {
104 is_error_ = true;
105 }
106 }
107
108 bool DoesFree() override { return old_pointer_ != 0; }
109
Christopher Ferriscd7d1082015-10-14 13:25:15 -0700110 uint64_t Execute(Pointers* pointers) override {
Christopher Ferris33c03d32015-09-11 15:34:23 -0700111 void* old_memory = nullptr;
112 if (old_pointer_ != 0) {
113 old_memory = pointers->Remove(old_pointer_);
114 }
Christopher Ferriscd7d1082015-10-14 13:25:15 -0700115
116 uint64_t time_nsecs = nanotime();
Christopher Ferris33c03d32015-09-11 15:34:23 -0700117 void* memory = realloc(old_memory, size_);
Christopher Ferriscd7d1082015-10-14 13:25:15 -0700118 time_nsecs = nanotime() - time_nsecs;
119
Christopher Ferris33c03d32015-09-11 15:34:23 -0700120 memset(memory, 1, size_);
121 pointers->Add(key_pointer_, memory);
Christopher Ferriscd7d1082015-10-14 13:25:15 -0700122
123 return time_nsecs;
Christopher Ferris33c03d32015-09-11 15:34:23 -0700124 }
125
126 private:
127 uintptr_t old_pointer_ = 0;
128};
129
130class MemalignAction : public AllocAction {
131 public:
132 MemalignAction(uintptr_t key_pointer, const char* line) : AllocAction(key_pointer) {
133 if (sscanf(line, "%zu %zu", &align_, &size_) != 2) {
134 is_error_ = true;
135 }
136 }
137
Christopher Ferriscd7d1082015-10-14 13:25:15 -0700138 uint64_t Execute(Pointers* pointers) override {
139 uint64_t time_nsecs = nanotime();
Christopher Ferris33c03d32015-09-11 15:34:23 -0700140 void* memory = memalign(align_, size_);
Christopher Ferriscd7d1082015-10-14 13:25:15 -0700141 time_nsecs = nanotime() - time_nsecs;
142
Christopher Ferris33c03d32015-09-11 15:34:23 -0700143 memset(memory, 1, size_);
144 pointers->Add(key_pointer_, memory);
Christopher Ferriscd7d1082015-10-14 13:25:15 -0700145
146 return time_nsecs;
Christopher Ferris33c03d32015-09-11 15:34:23 -0700147 }
148
149 private:
150 size_t align_ = 0;
151};
152
153class FreeAction : public AllocAction {
154 public:
155 FreeAction(uintptr_t key_pointer) : AllocAction(key_pointer) {
156 }
157
158 bool DoesFree() override { return key_pointer_ != 0; }
159
Christopher Ferriscd7d1082015-10-14 13:25:15 -0700160 uint64_t Execute(Pointers* pointers) override {
Christopher Ferris33c03d32015-09-11 15:34:23 -0700161 if (key_pointer_) {
162 void* memory = pointers->Remove(key_pointer_);
Christopher Ferriscd7d1082015-10-14 13:25:15 -0700163 uint64_t time_nsecs = nanotime();
Christopher Ferris33c03d32015-09-11 15:34:23 -0700164 free(memory);
Christopher Ferriscd7d1082015-10-14 13:25:15 -0700165 return nanotime() - time_nsecs;
Christopher Ferris33c03d32015-09-11 15:34:23 -0700166 }
Christopher Ferriscd7d1082015-10-14 13:25:15 -0700167 return 0;
Christopher Ferris33c03d32015-09-11 15:34:23 -0700168 }
169};
170
171size_t Action::MaxActionSize() {
172 size_t max = MAX(sizeof(EndThreadAction), sizeof(MallocAction));
173 max = MAX(max, sizeof(CallocAction));
174 max = MAX(max, sizeof(ReallocAction));
175 max = MAX(max, sizeof(MemalignAction));
176 return MAX(max, sizeof(FreeAction));
177}
178
179Action* Action::CreateAction(uintptr_t key_pointer, const char* type,
180 const char* line, void* action_memory) {
181 Action* action = nullptr;
182 if (strcmp(type, "malloc") == 0) {
183 action = new (action_memory) MallocAction(key_pointer, line);
184 } else if (strcmp(type, "free") == 0) {
185 action = new (action_memory) FreeAction(key_pointer);
186 } else if (strcmp(type, "calloc") == 0) {
187 action = new (action_memory) CallocAction(key_pointer, line);
188 } else if (strcmp(type, "realloc") == 0) {
189 action = new (action_memory) ReallocAction(key_pointer, line);
190 } else if (strcmp(type, "memalign") == 0) {
191 action = new (action_memory) MemalignAction(key_pointer, line);
192 } else if (strcmp(type, "thread_done") == 0) {
193 action = new (action_memory) EndThreadAction();
194 }
195
196 if (action == nullptr || action->IsError()) {
197 return nullptr;
198 }
199 return action;
200}