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; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace os { |
| 26 | namespace statsd { |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 27 | |
| 28 | class DropboxWriter { |
| 29 | public: |
| 30 | /* tag will be part of the file name, and used as the key to build the file index inside |
| 31 | DropBoxManagerService. |
| 32 | */ |
| 33 | DropboxWriter(const string& tag); |
| 34 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 35 | void addStatsLogReport(const StatsLogReport& log); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 36 | |
| 37 | /* Request a flush to dropbox. */ |
| 38 | void flush(); |
| 39 | |
| 40 | private: |
| 41 | /* Max *serialized* size of the logs kept in memory before flushing to dropbox. |
| 42 | Proto lite does not implement the SpaceUsed() function which gives the in memory byte size. |
| 43 | So we cap memory usage by limiting the serialized size. Note that protobuf's in memory size |
| 44 | is higher than its serialized size. DropboxManager will compress the file when the data is |
| 45 | larger than 4KB. So the final file size is less than this number. |
| 46 | */ |
| 47 | static const size_t kMaxSerializedBytes = 16 * 1024; |
| 48 | |
| 49 | const string mTag; |
| 50 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 51 | /* Data that was captured for a single metric over a given interval of time. */ |
| 52 | StatsLogReport mLogReport; |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 53 | |
| 54 | /* Current *serialized* size of the logs kept in memory. |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 55 | To save computation, we will not calculate the size of the StatsLogReport every time when a |
| 56 | new entry is added, which would recursively call ByteSize() on every log entry. Instead, we |
| 57 | keep the sum of all individual stats log entry sizes. The size of a proto is approximately |
| 58 | the sum of the size of all member protos. |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 59 | */ |
| 60 | size_t mBufferSize = 0; |
| 61 | |
| 62 | /* Check if the buffer size exceeds the max buffer size when the new entry is added, and flush |
| 63 | the logs to dropbox if true. */ |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 64 | void flushIfNecessary(const StatsLogReport& log); |
Yao Chen | ab273e2 | 2017-09-06 12:53:50 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 67 | } // namespace statsd |
| 68 | } // namespace os |
| 69 | } // namespace android |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 70 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 71 | #endif // DROPBOX_WRITER_H |