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
|
/*
* Copyright (C) 2022 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 <unistd.h>
#include <string.h>
#include <map>
#include <atomic>
#include <utils/Log.h>
#include <androidfw/ResourceTimer.h>
// The following block allows compilation on windows, which does not have getuid().
#ifdef _WIN32
#ifdef ERROR
#undef ERROR
#endif
#define getuid() (getUidWindows_)
#endif
namespace android {
namespace {
#ifdef _WIN32
// A temporary to confuse lint into thinking that getuid() on windows might return something other
// than zero.
int getUidWindows_ = 0;
#endif
// The number of nanoseconds in a microsecond.
static const unsigned int US = 1000;
// The number of nanoseconds in a second.
static const unsigned int S = 1000 * 1000 * 1000;
// Return the difference between two timespec values. The difference is in nanoseconds. If the
// return value would exceed 2s (2^31 nanoseconds) then UINT_MAX is returned.
unsigned int diffInNs(timespec const &a, timespec const &b) {
timespec r = { 0, 0 };
r.tv_nsec = a.tv_nsec - b.tv_nsec;
if (r.tv_nsec < 0) {
r.tv_sec = -1;
r.tv_nsec += S;
}
r.tv_sec = r.tv_sec + (a.tv_sec - b.tv_sec);
if (r.tv_sec > 2) return UINT_MAX;
unsigned int result = (r.tv_sec * S) + r.tv_nsec;
if (result > 2 * S) return UINT_MAX;
return result;
}
}
ResourceTimer::ResourceTimer(Counter api)
: active_(enabled_.load()),
api_(api) {
if (active_) {
clock_gettime(CLOCK_MONOTONIC, &start_);
}
}
ResourceTimer::~ResourceTimer() {
record();
}
void ResourceTimer::enable() {
if (!enabled_.load()) counter_ = new GuardedTimer[ResourceTimer::counterSize];
enabled_.store(true);
}
void ResourceTimer::cancel() {
active_ = false;
}
void ResourceTimer::record() {
if (!active_) return;
struct timespec end;
clock_gettime(CLOCK_MONOTONIC, &end);
// Get the difference in microseconds.
const unsigned int ticks = diffInNs(end, start_);
ScopedTimer t(counter_[toIndex(api_)]);
t->record(ticks);
active_ = false;
}
bool ResourceTimer::copy(int counter, Timer &dst, bool reset) {
ScopedTimer t(counter_[counter]);
if (t->count == 0) {
dst.reset();
if (reset) t->reset();
return false;
}
Timer::copy(dst, *t, reset);
return true;
}
void ResourceTimer::reset() {
for (int i = 0; i < counterSize; i++) {
ScopedTimer t(counter_[i]);
t->reset();
}
}
ResourceTimer::Timer::Timer() {
// Ensure newly-created objects are zeroed.
memset(buckets, 0, sizeof(buckets));
reset();
}
ResourceTimer::Timer::~Timer() {
for (int d = 0; d < MaxDimension; d++) {
delete[] buckets[d];
}
}
void ResourceTimer::Timer::freeBuckets() {
for (int d = 0; d < MaxDimension; d++) {
delete[] buckets[d];
buckets[d] = 0;
}
}
void ResourceTimer::Timer::reset() {
count = total = mintime = maxtime = 0;
memset(largest, 0, sizeof(largest));
memset(&pvalues, 0, sizeof(pvalues));
// Zero the histogram, keeping any allocated dimensions.
for (int d = 0; d < MaxDimension; d++) {
if (buckets[d] != 0) memset(buckets[d], 0, sizeof(int) * MaxBuckets);
}
}
void ResourceTimer::Timer::copy(Timer &dst, Timer &src, bool reset) {
dst.freeBuckets();
dst = src;
// Clean up the histograms.
if (reset) {
// Do NOT free the src buckets because they being used by dst.
memset(src.buckets, 0, sizeof(src.buckets));
src.reset();
} else {
for (int d = 0; d < MaxDimension; d++) {
if (src.buckets[d] != nullptr) {
dst.buckets[d] = new int[MaxBuckets];
memcpy(dst.buckets[d], src.buckets[d], sizeof(int) * MaxBuckets);
}
}
}
}
void ResourceTimer::Timer::record(int ticks) {
// Record that the event happened.
count++;
total += ticks;
if (mintime == 0 || ticks < mintime) mintime = ticks;
if (ticks > maxtime) maxtime = ticks;
// Do not add oversized events to the histogram.
if (ticks != UINT_MAX) {
for (int d = 0; d < MaxDimension; d++) {
if (ticks < range[d]) {
if (buckets[d] == 0) {
buckets[d] = new int[MaxBuckets];
memset(buckets[d], 0, sizeof(int) * MaxBuckets);
}
if (ticks < width[d]) {
// Special case: never write to bucket 0 because it complicates the percentile logic.
// However, this is always the smallest possible value to it is very unlikely to ever
// affect any of the percentile results.
buckets[d][1]++;
} else {
buckets[d][ticks / width[d]]++;
}
break;
}
}
}
// The list of largest times is sorted with the biggest value at index 0 and the smallest at
// index MaxLargest-1. The incoming tick count should be added to the array only if it is
// larger than the current value at MaxLargest-1.
if (ticks > largest[Timer::MaxLargest-1]) {
for (size_t i = 0; i < Timer::MaxLargest; i++) {
if (ticks > largest[i]) {
if (i < Timer::MaxLargest-1) {
for (size_t j = Timer::MaxLargest - 1; j > i; j--) {
largest[j] = largest[j-1];
}
}
largest[i] = ticks;
break;
}
}
}
}
void ResourceTimer::Timer::Percentile::compute(
int cumulative, int current, int count, int width, int time) {
nominal = time;
nominal_actual = (cumulative * 100) / count;
floor = nominal - width;
floor_actual = ((cumulative - current) * 100) / count;
}
void ResourceTimer::Timer::compute() {
memset(&pvalues, 0, sizeof(pvalues));
float l50 = count / 2.0;
float l90 = (count * 9.0) / 10.0;
float l95 = (count * 95.0) / 100.0;
float l99 = (count * 99.0) / 100.0;
int sum = 0;
for (int d = 0; d < MaxDimension; d++) {
if (buckets[d] == 0) continue;
for (int j = 0; j < MaxBuckets && sum < count; j++) {
// Empty buckets don't contribute to the answers. Skip them.
if (buckets[d][j] == 0) continue;
sum += buckets[d][j];
// A word on indexing. j is never zero in the following lines. buckets[0][0] corresponds
// to a delay of 0us, which cannot happen. buckets[n][0], for n > 0 overlaps a value in
// buckets[n-1], and the code would have stopped there.
if (sum >= l50 && pvalues.p50.nominal == 0) {
pvalues.p50.compute(sum, buckets[d][j], count, width[d], j * width[d]);
}
if (sum >= l90 && pvalues.p90.nominal == 0) {
pvalues.p90.compute(sum, buckets[d][j], count, width[d], j * width[d]);
}
if (sum >= l95 && pvalues.p95.nominal == 0) {
pvalues.p95.compute(sum, buckets[d][j], count, width[d], j * width[d]);
}
if (sum >= l99 && pvalues.p99.nominal == 0) {
pvalues.p99.compute(sum, buckets[d][j], count, width[d], j * width[d]);
}
}
}
}
char const *ResourceTimer::toString(ResourceTimer::Counter counter) {
switch (counter) {
case Counter::GetResourceValue:
return "GetResourceValue";
case Counter::RetrieveAttributes:
return "RetrieveAttributes";
};
return "Unknown";
}
std::atomic<bool> ResourceTimer::enabled_(false);
std::atomic<ResourceTimer::GuardedTimer *> ResourceTimer::counter_(nullptr);
const int ResourceTimer::Timer::range[] = { 100 * US, 1000 * US, 10*1000 * US, 100*1000 * US };
const int ResourceTimer::Timer::width[] = { 1 * US, 10 * US, 100 * US, 1000 * US };
} // namespace android
|