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