blob: 63a27869b6a8f8ba1b86c6e8400de9666848c87d [file] [log] [blame]
Vishnu Nair00b90132021-11-05 14:03:40 -07001/*
2 * Copyright 2021 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#pragma once
18
19#include <android-base/file.h>
20#include <android-base/stringprintf.h>
21
22#include <log/log.h>
23#include <utils/Errors.h>
Vishnu Nair7891e962021-11-11 12:07:21 -080024#include <utils/Timers.h>
Vishnu Nair00b90132021-11-05 14:03:40 -070025#include <utils/Trace.h>
Vishnu Nair7891e962021-11-11 12:07:21 -080026#include <chrono>
Vishnu Nair00b90132021-11-05 14:03:40 -070027#include <queue>
28
29namespace android {
30
31class SurfaceFlinger;
32
33template <typename FileProto, typename EntryProto>
34class RingBuffer {
35public:
36 size_t size() const { return mSizeInBytes; }
37 size_t used() const { return mUsedInBytes; }
38 size_t frameCount() const { return mStorage.size(); }
39 void setSize(size_t newSize) { mSizeInBytes = newSize; }
40 EntryProto& front() { return mStorage.front(); }
41 const EntryProto& front() const { return mStorage.front(); }
42
43 void reset(size_t newSize) {
44 // use the swap trick to make sure memory is released
45 std::queue<EntryProto>().swap(mStorage);
46 mSizeInBytes = newSize;
47 mUsedInBytes = 0U;
48 }
49 void flush(FileProto& fileProto) {
50 fileProto.mutable_entry()->Reserve(static_cast<int>(mStorage.size()));
51 while (!mStorage.empty()) {
52 auto entry = fileProto.add_entry();
53 entry->Swap(&mStorage.front());
54 mStorage.pop();
55 }
56 }
57
58 status_t writeToFile(FileProto& fileProto, std::string filename) {
59 ATRACE_CALL();
60 std::string output;
Vishnu Nair7891e962021-11-11 12:07:21 -080061 if (!writeToString(fileProto, &output)) {
Vishnu Nair00b90132021-11-05 14:03:40 -070062 ALOGE("Could not serialize proto.");
63 return UNKNOWN_ERROR;
64 }
65
66 // -rw-r--r--
67 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
68 if (!android::base::WriteStringToFile(output, filename, mode, getuid(), getgid(), true)) {
69 ALOGE("Could not save the proto file.");
70 return PERMISSION_DENIED;
71 }
72 return NO_ERROR;
73 }
74
Vishnu Nair7891e962021-11-11 12:07:21 -080075 bool writeToString(FileProto& fileProto, std::string* outString) {
76 ATRACE_CALL();
77 flush(fileProto);
78 reset(mSizeInBytes);
79 return fileProto.SerializeToString(outString);
80 }
81
Vishnu Nair00b90132021-11-05 14:03:40 -070082 std::vector<EntryProto> emplace(EntryProto&& proto) {
83 std::vector<EntryProto> replacedEntries;
84 size_t protoSize = static_cast<size_t>(proto.ByteSize());
85 while (mUsedInBytes + protoSize > mSizeInBytes) {
86 if (mStorage.empty()) {
87 return {};
88 }
89 mUsedInBytes -= static_cast<size_t>(mStorage.front().ByteSize());
90 replacedEntries.emplace_back(mStorage.front());
91 mStorage.pop();
92 }
93 mUsedInBytes += protoSize;
94 mStorage.emplace();
95 mStorage.back().Swap(&proto);
96 return replacedEntries;
97 }
98
99 void dump(std::string& result) const {
100 std::chrono::milliseconds duration(0);
101 if (frameCount() > 0) {
102 duration = std::chrono::duration_cast<std::chrono::milliseconds>(
103 std::chrono::nanoseconds(systemTime() - front().elapsed_realtime_nanos()));
104 }
105 const int64_t durationCount = duration.count();
106 base::StringAppendF(&result,
107 " number of entries: %zu (%.2fMB / %.2fMB) duration: %" PRIi64 "ms\n",
Vishnu Nair7891e962021-11-11 12:07:21 -0800108 frameCount(), float(used()) / (1024.f * 1024.f),
109 float(size()) / (1024.f * 1024.f), durationCount);
Vishnu Nair00b90132021-11-05 14:03:40 -0700110 }
111
112private:
113 size_t mUsedInBytes = 0U;
114 size_t mSizeInBytes = 0U;
115 std::queue<EntryProto> mStorage;
116};
117
118} // namespace android