blob: 281cd1951f660f33b37fb2cee67028f0dcda5303 [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(); }
Vishnu Nair0cc69e12021-11-18 09:05:49 -080042 const EntryProto& back() const { return mStorage.back(); }
Vishnu Nair00b90132021-11-05 14:03:40 -070043
Vishnu Nair0cc69e12021-11-18 09:05:49 -080044 void reset() {
Vishnu Nair00b90132021-11-05 14:03:40 -070045 // use the swap trick to make sure memory is released
Vishnu Nair0cc69e12021-11-18 09:05:49 -080046 std::deque<EntryProto>().swap(mStorage);
Vishnu Nair00b90132021-11-05 14:03:40 -070047 mUsedInBytes = 0U;
48 }
Vishnu Nair0cc69e12021-11-18 09:05:49 -080049
50 void writeToProto(FileProto& fileProto) {
51 fileProto.mutable_entry()->Reserve(static_cast<int>(mStorage.size()) +
52 fileProto.entry().size());
53 for (const EntryProto& entry : mStorage) {
54 EntryProto* entryProto = fileProto.add_entry();
55 *entryProto = entry;
Vishnu Nair00b90132021-11-05 14:03:40 -070056 }
57 }
58
59 status_t writeToFile(FileProto& fileProto, std::string filename) {
60 ATRACE_CALL();
Vishnu Nair0cc69e12021-11-18 09:05:49 -080061 writeToProto(fileProto);
Vishnu Nair00b90132021-11-05 14:03:40 -070062 std::string output;
Vishnu Nair0cc69e12021-11-18 09:05:49 -080063 if (!fileProto.SerializeToString(&output)) {
Vishnu Nair00b90132021-11-05 14:03:40 -070064 ALOGE("Could not serialize proto.");
65 return UNKNOWN_ERROR;
66 }
67
68 // -rw-r--r--
69 const mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
70 if (!android::base::WriteStringToFile(output, filename, mode, getuid(), getgid(), true)) {
71 ALOGE("Could not save the proto file.");
72 return PERMISSION_DENIED;
73 }
74 return NO_ERROR;
75 }
76
77 std::vector<EntryProto> emplace(EntryProto&& proto) {
78 std::vector<EntryProto> replacedEntries;
79 size_t protoSize = static_cast<size_t>(proto.ByteSize());
80 while (mUsedInBytes + protoSize > mSizeInBytes) {
81 if (mStorage.empty()) {
82 return {};
83 }
84 mUsedInBytes -= static_cast<size_t>(mStorage.front().ByteSize());
85 replacedEntries.emplace_back(mStorage.front());
Vishnu Nair0cc69e12021-11-18 09:05:49 -080086 mStorage.pop_front();
Vishnu Nair00b90132021-11-05 14:03:40 -070087 }
88 mUsedInBytes += protoSize;
Vishnu Nair0cc69e12021-11-18 09:05:49 -080089 mStorage.emplace_back();
Vishnu Nair00b90132021-11-05 14:03:40 -070090 mStorage.back().Swap(&proto);
91 return replacedEntries;
92 }
93
94 void dump(std::string& result) const {
95 std::chrono::milliseconds duration(0);
96 if (frameCount() > 0) {
97 duration = std::chrono::duration_cast<std::chrono::milliseconds>(
98 std::chrono::nanoseconds(systemTime() - front().elapsed_realtime_nanos()));
99 }
100 const int64_t durationCount = duration.count();
101 base::StringAppendF(&result,
102 " number of entries: %zu (%.2fMB / %.2fMB) duration: %" PRIi64 "ms\n",
Vishnu Nair7891e962021-11-11 12:07:21 -0800103 frameCount(), float(used()) / (1024.f * 1024.f),
104 float(size()) / (1024.f * 1024.f), durationCount);
Vishnu Nair00b90132021-11-05 14:03:40 -0700105 }
106
107private:
108 size_t mUsedInBytes = 0U;
109 size_t mSizeInBytes = 0U;
Vishnu Nair0cc69e12021-11-18 09:05:49 -0800110 std::deque<EntryProto> mStorage;
Vishnu Nair00b90132021-11-05 14:03:40 -0700111};
112
113} // namespace android