blob: 1fd0d757cca884e3183ca0e6487e9378585dd949 [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;
Bookatz906a35c2017-09-20 15:26:44 -070023
24namespace android {
25namespace os {
26namespace statsd {
Yao Chenab273e22017-09-06 12:53:50 -070027
28class DropboxWriter {
29public:
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
yro00698da2017-09-15 10:06:40 -070035 void addStatsLogReport(const StatsLogReport& log);
Yao Chenab273e22017-09-06 12:53:50 -070036
37 /* Request a flush to dropbox. */
38 void flush();
39
40private:
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
yro00698da2017-09-15 10:06:40 -070051 /* Data that was captured for a single metric over a given interval of time. */
52 StatsLogReport mLogReport;
Yao Chenab273e22017-09-06 12:53:50 -070053
54 /* Current *serialized* size of the logs kept in memory.
Yao Chenef99c4f2017-09-22 16:26:54 -070055 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 Chenab273e22017-09-06 12:53:50 -070059 */
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. */
yro00698da2017-09-15 10:06:40 -070064 void flushIfNecessary(const StatsLogReport& log);
Yao Chenab273e22017-09-06 12:53:50 -070065};
66
Yao Chenef99c4f2017-09-22 16:26:54 -070067} // namespace statsd
68} // namespace os
69} // namespace android
Bookatz906a35c2017-09-20 15:26:44 -070070
Yao Chenef99c4f2017-09-22 16:26:54 -070071#endif // DROPBOX_WRITER_H