Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #ifndef DROPBOX_WRITER_H |
| 18 | #define DROPBOX_WRITER_H |
| 19 | |
| 20 | #include <frameworks/base/cmds/statsd/src/stats_log.pb.h> |
| 21 | |
| 22 | using std::string; |
| 23 | using android::os::statsd::StatsLogEntry; |
| 24 | using android::os::statsd::StatsLogList; |
| 25 | |
| 26 | class DropboxWriter { |
| 27 | public: |
| 28 | /* tag will be part of the file name, and used as the key to build the file index inside |
| 29 | DropBoxManagerService. |
| 30 | */ |
| 31 | DropboxWriter(const string& tag); |
| 32 | |
| 33 | void addEntry(const StatsLogEntry& entry); |
| 34 | |
| 35 | /* Request a flush to dropbox. */ |
| 36 | void flush(); |
| 37 | |
| 38 | private: |
| 39 | /* Max *serialized* size of the logs kept in memory before flushing to dropbox. |
| 40 | Proto lite does not implement the SpaceUsed() function which gives the in memory byte size. |
| 41 | So we cap memory usage by limiting the serialized size. Note that protobuf's in memory size |
| 42 | is higher than its serialized size. DropboxManager will compress the file when the data is |
| 43 | larger than 4KB. So the final file size is less than this number. |
| 44 | */ |
| 45 | static const size_t kMaxSerializedBytes = 16 * 1024; |
| 46 | |
| 47 | const string mTag; |
| 48 | |
| 49 | /* StatsLogList is a wrapper for storing a list of StatsLogEntry */ |
| 50 | StatsLogList mLogList; |
| 51 | |
| 52 | /* Current *serialized* size of the logs kept in memory. |
| 53 | To save computation, we will not calculate the size of the StatsLogList every time when a new |
| 54 | entry is added, which would recursively call ByteSize() on every log entry. Instead, we keep |
| 55 | the sum of all individual stats log entry sizes. The size of a proto is approximately the sum |
| 56 | of the size of all member protos. |
| 57 | */ |
| 58 | size_t mBufferSize = 0; |
| 59 | |
| 60 | /* Check if the buffer size exceeds the max buffer size when the new entry is added, and flush |
| 61 | the logs to dropbox if true. */ |
| 62 | void flushIfNecessary(const StatsLogEntry& entry); |
| 63 | |
| 64 | }; |
| 65 | |
| 66 | #endif //DROPBOX_WRITER_H |