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