blob: b41c65b10ed4bf07f06aae3b4c5a85a71e8a8446 [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 Naircb565332023-03-14 21:10:55 -070027#include <fstream>
Vishnu Nair00b90132021-11-05 14:03:40 -070028#include <queue>
29
30namespace android {
31
32class SurfaceFlinger;
33
34template <typename FileProto, typename EntryProto>
35class RingBuffer {
36public:
37 size_t size() const { return mSizeInBytes; }
38 size_t used() const { return mUsedInBytes; }
39 size_t frameCount() const { return mStorage.size(); }
40 void setSize(size_t newSize) { mSizeInBytes = newSize; }
Vishnu Nair62863552021-12-10 13:34:48 -080041 const std::string& front() const { return mStorage.front(); }
42 const std::string& 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 Nair62863552021-12-10 13:34:48 -080046 std::deque<std::string>().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());
Vishnu Nair62863552021-12-10 13:34:48 -080053 for (const std::string& entry : mStorage) {
Vishnu Nair0cc69e12021-11-18 09:05:49 -080054 EntryProto* entryProto = fileProto.add_entry();
Vishnu Nair62863552021-12-10 13:34:48 -080055 entryProto->ParseFromString(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)) {
Vishnu Naird8f5e9f2022-02-03 10:23:28 -080071 ALOGE("Could not save the proto file %s", filename.c_str());
Vishnu Nair00b90132021-11-05 14:03:40 -070072 return PERMISSION_DENIED;
73 }
74 return NO_ERROR;
75 }
76
Vishnu Naircb565332023-03-14 21:10:55 -070077 status_t appendToStream(FileProto& fileProto, std::ofstream& out) {
78 ATRACE_CALL();
79 writeToProto(fileProto);
80 std::string output;
81 if (!fileProto.SerializeToString(&output)) {
82 ALOGE("Could not serialize proto.");
83 return UNKNOWN_ERROR;
84 }
85
86 out << output;
87 return NO_ERROR;
88 }
89
Vishnu Nair62863552021-12-10 13:34:48 -080090 std::vector<std::string> emplace(std::string&& serializedProto) {
91 std::vector<std::string> replacedEntries;
92 size_t protoSize = static_cast<size_t>(serializedProto.size());
Vishnu Nair00b90132021-11-05 14:03:40 -070093 while (mUsedInBytes + protoSize > mSizeInBytes) {
94 if (mStorage.empty()) {
95 return {};
96 }
Vishnu Nair62863552021-12-10 13:34:48 -080097 mUsedInBytes -= static_cast<size_t>(mStorage.front().size());
Vishnu Nair00b90132021-11-05 14:03:40 -070098 replacedEntries.emplace_back(mStorage.front());
Vishnu Nair0cc69e12021-11-18 09:05:49 -080099 mStorage.pop_front();
Vishnu Nair00b90132021-11-05 14:03:40 -0700100 }
101 mUsedInBytes += protoSize;
Vishnu Nair62863552021-12-10 13:34:48 -0800102 mStorage.emplace_back(serializedProto);
Vishnu Nair00b90132021-11-05 14:03:40 -0700103 return replacedEntries;
104 }
105
Vishnu Nair62863552021-12-10 13:34:48 -0800106 std::vector<std::string> emplace(EntryProto&& proto) {
107 std::string serializedProto;
108 proto.SerializeToString(&serializedProto);
109 return emplace(std::move(serializedProto));
110 }
111
Vishnu Nair00b90132021-11-05 14:03:40 -0700112 void dump(std::string& result) const {
113 std::chrono::milliseconds duration(0);
114 if (frameCount() > 0) {
Vishnu Nair62863552021-12-10 13:34:48 -0800115 EntryProto entry;
116 entry.ParseFromString(mStorage.front());
Vishnu Nair00b90132021-11-05 14:03:40 -0700117 duration = std::chrono::duration_cast<std::chrono::milliseconds>(
Vishnu Nair62863552021-12-10 13:34:48 -0800118 std::chrono::nanoseconds(systemTime() - entry.elapsed_realtime_nanos()));
Vishnu Nair00b90132021-11-05 14:03:40 -0700119 }
120 const int64_t durationCount = duration.count();
121 base::StringAppendF(&result,
122 " number of entries: %zu (%.2fMB / %.2fMB) duration: %" PRIi64 "ms\n",
Vishnu Nair7891e962021-11-11 12:07:21 -0800123 frameCount(), float(used()) / (1024.f * 1024.f),
124 float(size()) / (1024.f * 1024.f), durationCount);
Vishnu Nair00b90132021-11-05 14:03:40 -0700125 }
126
127private:
128 size_t mUsedInBytes = 0U;
129 size_t mSizeInBytes = 0U;
Vishnu Nair62863552021-12-10 13:34:48 -0800130 std::deque<std::string> mStorage;
Vishnu Nair00b90132021-11-05 14:03:40 -0700131};
132
133} // namespace android