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 #define DEBUG false
18 #include "Log.h"
19 
20 #include <android/os/IStatsCompanionService.h>
21 #include <binder/IPCThreadState.h>
22 #include <private/android_filesystem_config.h>
23 #include "../stats_log_util.h"
24 #include "../statscompanion_util.h"
25 #include "StatsCompanionServicePuller.h"
26 
27 using namespace android;
28 using namespace android::base;
29 using namespace android::binder;
30 using namespace android::os;
31 using std::make_shared;
32 using std::shared_ptr;
33 using std::vector;
34 
35 namespace android {
36 namespace os {
37 namespace statsd {
38 
39 const int kLogMsgHeaderSize = 28;
40 
41 // The reading and parsing are implemented in Java. It is not difficult to port over. But for now
42 // let StatsCompanionService handle that and send the data back.
StatsCompanionServicePuller(int tagId)43 StatsCompanionServicePuller::StatsCompanionServicePuller(int tagId) : StatsPuller(tagId) {
44 }
45 
SetStatsCompanionService(sp<IStatsCompanionService> statsCompanionService)46 void StatsCompanionServicePuller::SetStatsCompanionService(
47         sp<IStatsCompanionService> statsCompanionService) {
48     AutoMutex _l(mStatsCompanionServiceLock);
49     sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
50     mStatsCompanionService = statsCompanionService;
51 }
52 
PullInternal(vector<shared_ptr<LogEvent>> * data)53 bool StatsCompanionServicePuller::PullInternal(vector<shared_ptr<LogEvent> >* data) {
54     sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
55     if (statsCompanionServiceCopy != nullptr) {
56         vector<StatsLogEventWrapper> returned_value;
57         Status status = statsCompanionServiceCopy->pullData(mTagId, &returned_value);
58         if (!status.isOk()) {
59             ALOGW("error pulling for %d", mTagId);
60             return false;
61         }
62         data->clear();
63         int32_t timestampSec = getWallClockSec();
64         for (const StatsLogEventWrapper& it : returned_value) {
65             log_msg tmp;
66             tmp.entry_v1.len = it.bytes.size();
67             // Manually set the header size to 28 bytes to match the pushed log events.
68             tmp.entry.hdr_size = kLogMsgHeaderSize;
69             tmp.entry_v1.sec = timestampSec;
70             // And set the received bytes starting after the 28 bytes reserved for header.
71             std::copy(it.bytes.begin(), it.bytes.end(), tmp.buf + kLogMsgHeaderSize);
72             data->push_back(make_shared<LogEvent>(tmp));
73         }
74         VLOG("StatsCompanionServicePuller::pull succeeded for %d", mTagId);
75         return true;
76     } else {
77         ALOGW("statsCompanion not found!");
78         return false;
79     }
80 }
81 
82 }  // namespace statsd
83 }  // namespace os
84 }  // namespace android
85