blob: 2b66391853b085dc855e26f3213e5f8292509336 [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
Vishnu Nairbe0ad902024-06-27 23:38:43 +000022#include <common/trace.h>
Vishnu Nair00b90132021-11-05 14:03:40 -070023#include <log/log.h>
24#include <utils/Errors.h>
Vishnu Nair7891e962021-11-11 12:07:21 -080025#include <utils/Timers.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
29namespace android {
30
31class SurfaceFlinger;
32
33template <typename FileProto, typename EntryProto>
John Reck2a3d29d2023-08-17 17:45:01 -040034class TransactionRingBuffer {
Vishnu Nair00b90132021-11-05 14:03:40 -070035public:
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; }
Vishnu Nair62863552021-12-10 13:34:48 -080040 const std::string& front() const { return mStorage.front(); }
41 const std::string& back() const { return mStorage.back(); }
Vishnu Nair00b90132021-11-05 14:03:40 -070042
Vishnu Nair0cc69e12021-11-18 09:05:49 -080043 void reset() {
Vishnu Nair00b90132021-11-05 14:03:40 -070044 // use the swap trick to make sure memory is released
Vishnu Nair62863552021-12-10 13:34:48 -080045 std::deque<std::string>().swap(mStorage);
Vishnu Nair00b90132021-11-05 14:03:40 -070046 mUsedInBytes = 0U;
47 }
Vishnu Nair0cc69e12021-11-18 09:05:49 -080048
Kean Mariotti3e68a202023-04-19 13:41:55 +000049 void writeToProto(FileProto& fileProto) const {
Vishnu Nair0cc69e12021-11-18 09:05:49 -080050 fileProto.mutable_entry()->Reserve(static_cast<int>(mStorage.size()) +
51 fileProto.entry().size());
Vishnu Nair62863552021-12-10 13:34:48 -080052 for (const std::string& entry : mStorage) {
Vishnu Nair0cc69e12021-11-18 09:05:49 -080053 EntryProto* entryProto = fileProto.add_entry();
Vishnu Nair62863552021-12-10 13:34:48 -080054 entryProto->ParseFromString(entry);
Vishnu Nair00b90132021-11-05 14:03:40 -070055 }
56 }
57
Vishnu Naircb565332023-03-14 21:10:55 -070058 status_t appendToStream(FileProto& fileProto, std::ofstream& out) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +000059 SFTRACE_CALL();
Vishnu Naircb565332023-03-14 21:10:55 -070060 writeToProto(fileProto);
61 std::string output;
62 if (!fileProto.SerializeToString(&output)) {
63 ALOGE("Could not serialize proto.");
64 return UNKNOWN_ERROR;
65 }
66
67 out << output;
68 return NO_ERROR;
69 }
70
Vishnu Nair62863552021-12-10 13:34:48 -080071 std::vector<std::string> emplace(std::string&& serializedProto) {
72 std::vector<std::string> replacedEntries;
73 size_t protoSize = static_cast<size_t>(serializedProto.size());
Vishnu Nair00b90132021-11-05 14:03:40 -070074 while (mUsedInBytes + protoSize > mSizeInBytes) {
75 if (mStorage.empty()) {
76 return {};
77 }
Vishnu Nair62863552021-12-10 13:34:48 -080078 mUsedInBytes -= static_cast<size_t>(mStorage.front().size());
Vishnu Nair00b90132021-11-05 14:03:40 -070079 replacedEntries.emplace_back(mStorage.front());
Vishnu Nair0cc69e12021-11-18 09:05:49 -080080 mStorage.pop_front();
Vishnu Nair00b90132021-11-05 14:03:40 -070081 }
82 mUsedInBytes += protoSize;
Vishnu Nair62863552021-12-10 13:34:48 -080083 mStorage.emplace_back(serializedProto);
Vishnu Nair00b90132021-11-05 14:03:40 -070084 return replacedEntries;
85 }
86
Vishnu Nair62863552021-12-10 13:34:48 -080087 std::vector<std::string> emplace(EntryProto&& proto) {
88 std::string serializedProto;
89 proto.SerializeToString(&serializedProto);
90 return emplace(std::move(serializedProto));
91 }
92
Vishnu Nair00b90132021-11-05 14:03:40 -070093 void dump(std::string& result) const {
94 std::chrono::milliseconds duration(0);
95 if (frameCount() > 0) {
Vishnu Nair62863552021-12-10 13:34:48 -080096 EntryProto entry;
97 entry.ParseFromString(mStorage.front());
Vishnu Nair00b90132021-11-05 14:03:40 -070098 duration = std::chrono::duration_cast<std::chrono::milliseconds>(
Vishnu Nair62863552021-12-10 13:34:48 -080099 std::chrono::nanoseconds(systemTime() - entry.elapsed_realtime_nanos()));
Vishnu Nair00b90132021-11-05 14:03:40 -0700100 }
101 const int64_t durationCount = duration.count();
102 base::StringAppendF(&result,
103 " number of entries: %zu (%.2fMB / %.2fMB) duration: %" PRIi64 "ms\n",
Vishnu Nair7891e962021-11-11 12:07:21 -0800104 frameCount(), float(used()) / (1024.f * 1024.f),
105 float(size()) / (1024.f * 1024.f), durationCount);
Vishnu Nair00b90132021-11-05 14:03:40 -0700106 }
107
108private:
109 size_t mUsedInBytes = 0U;
110 size_t mSizeInBytes = 0U;
Vishnu Nair62863552021-12-10 13:34:48 -0800111 std::deque<std::string> mStorage;
Vishnu Nair00b90132021-11-05 14:03:40 -0700112};
113
114} // namespace android