Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 1 | /* |
| 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 Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 24 | #include <utils/Timers.h> |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 25 | #include <utils/Trace.h> |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 26 | #include <chrono> |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame] | 27 | #include <fstream> |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 28 | #include <queue> |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | class SurfaceFlinger; |
| 33 | |
| 34 | template <typename FileProto, typename EntryProto> |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 35 | class TransactionRingBuffer { |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 36 | public: |
| 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 Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 41 | const std::string& front() const { return mStorage.front(); } |
| 42 | const std::string& back() const { return mStorage.back(); } |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 43 | |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 44 | void reset() { |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 45 | // use the swap trick to make sure memory is released |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 46 | std::deque<std::string>().swap(mStorage); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 47 | mUsedInBytes = 0U; |
| 48 | } |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 49 | |
Kean Mariotti | 3e68a20 | 2023-04-19 13:41:55 +0000 | [diff] [blame] | 50 | void writeToProto(FileProto& fileProto) const { |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 51 | fileProto.mutable_entry()->Reserve(static_cast<int>(mStorage.size()) + |
| 52 | fileProto.entry().size()); |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 53 | for (const std::string& entry : mStorage) { |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 54 | EntryProto* entryProto = fileProto.add_entry(); |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 55 | entryProto->ParseFromString(entry); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Vishnu Nair | cb56533 | 2023-03-14 21:10:55 -0700 | [diff] [blame] | 59 | status_t appendToStream(FileProto& fileProto, std::ofstream& out) { |
| 60 | ATRACE_CALL(); |
| 61 | writeToProto(fileProto); |
| 62 | std::string output; |
| 63 | if (!fileProto.SerializeToString(&output)) { |
| 64 | ALOGE("Could not serialize proto."); |
| 65 | return UNKNOWN_ERROR; |
| 66 | } |
| 67 | |
| 68 | out << output; |
| 69 | return NO_ERROR; |
| 70 | } |
| 71 | |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 72 | std::vector<std::string> emplace(std::string&& serializedProto) { |
| 73 | std::vector<std::string> replacedEntries; |
| 74 | size_t protoSize = static_cast<size_t>(serializedProto.size()); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 75 | while (mUsedInBytes + protoSize > mSizeInBytes) { |
| 76 | if (mStorage.empty()) { |
| 77 | return {}; |
| 78 | } |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 79 | mUsedInBytes -= static_cast<size_t>(mStorage.front().size()); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 80 | replacedEntries.emplace_back(mStorage.front()); |
Vishnu Nair | 0cc69e1 | 2021-11-18 09:05:49 -0800 | [diff] [blame] | 81 | mStorage.pop_front(); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 82 | } |
| 83 | mUsedInBytes += protoSize; |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 84 | mStorage.emplace_back(serializedProto); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 85 | return replacedEntries; |
| 86 | } |
| 87 | |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 88 | std::vector<std::string> emplace(EntryProto&& proto) { |
| 89 | std::string serializedProto; |
| 90 | proto.SerializeToString(&serializedProto); |
| 91 | return emplace(std::move(serializedProto)); |
| 92 | } |
| 93 | |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 94 | void dump(std::string& result) const { |
| 95 | std::chrono::milliseconds duration(0); |
| 96 | if (frameCount() > 0) { |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 97 | EntryProto entry; |
| 98 | entry.ParseFromString(mStorage.front()); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 99 | duration = std::chrono::duration_cast<std::chrono::milliseconds>( |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 100 | std::chrono::nanoseconds(systemTime() - entry.elapsed_realtime_nanos())); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 101 | } |
| 102 | const int64_t durationCount = duration.count(); |
| 103 | base::StringAppendF(&result, |
| 104 | " number of entries: %zu (%.2fMB / %.2fMB) duration: %" PRIi64 "ms\n", |
Vishnu Nair | 7891e96 | 2021-11-11 12:07:21 -0800 | [diff] [blame] | 105 | frameCount(), float(used()) / (1024.f * 1024.f), |
| 106 | float(size()) / (1024.f * 1024.f), durationCount); |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | private: |
| 110 | size_t mUsedInBytes = 0U; |
| 111 | size_t mSizeInBytes = 0U; |
Vishnu Nair | 6286355 | 2021-12-10 13:34:48 -0800 | [diff] [blame] | 112 | std::deque<std::string> mStorage; |
Vishnu Nair | 00b9013 | 2021-11-05 14:03:40 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | } // namespace android |