blob: d953f50bb5d84208c96bcacc39181a8872a09521 [file] [log] [blame]
Chenjie Yu1a317ba2017-10-05 16:05:32 -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
Tej Singh484524a2018-02-01 15:10:05 -080017#define DEBUG false
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
19
Chenjie Yu1a317ba2017-10-05 16:05:32 -070020#include <android/os/IStatsCompanionService.h>
21#include <binder/IPCThreadState.h>
Chenjie Yu1a317ba2017-10-05 16:05:32 -070022#include <private/android_filesystem_config.h>
Chenjie Yuaa5b2012018-03-21 13:53:15 -070023#include "../stats_log_util.h"
24#include "../statscompanion_util.h"
Chenjie Yu5305e1d2017-10-31 13:49:36 -070025#include "StatsCompanionServicePuller.h"
Chenjie Yu1a317ba2017-10-05 16:05:32 -070026
27using namespace android;
28using namespace android::base;
29using namespace android::binder;
30using namespace android::os;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070031using std::make_shared;
32using std::shared_ptr;
33using std::vector;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070034
35namespace android {
36namespace os {
37namespace statsd {
38
Chenjie Yu5305e1d2017-10-31 13:49:36 -070039const int kLogMsgHeaderSize = 28;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070040
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.
Chenjie Yub038b702017-12-18 15:15:34 -080043StatsCompanionServicePuller::StatsCompanionServicePuller(int tagId) : StatsPuller(tagId) {
44}
45
Chenjie Yuaa5b2012018-03-21 13:53:15 -070046void StatsCompanionServicePuller::SetStatsCompanionService(
47 sp<IStatsCompanionService> statsCompanionService) {
48 AutoMutex _l(mStatsCompanionServiceLock);
49 sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
50 mStatsCompanionService = statsCompanionService;
51}
52
Chenjie Yub038b702017-12-18 15:15:34 -080053bool StatsCompanionServicePuller::PullInternal(vector<shared_ptr<LogEvent> >* data) {
Chenjie Yuaa5b2012018-03-21 13:53:15 -070054 sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
55 if (statsCompanionServiceCopy != nullptr) {
56 vector<StatsLogEventWrapper> returned_value;
57 Status status = statsCompanionServiceCopy->pullData(mTagId, &returned_value);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070058 if (!status.isOk()) {
Chenjie Yub038b702017-12-18 15:15:34 -080059 ALOGW("error pulling for %d", mTagId);
Chenjie Yu5305e1d2017-10-31 13:49:36 -070060 return false;
61 }
62 data->clear();
Yangster-mac330af582018-02-08 15:24:38 -080063 int32_t timestampSec = getWallClockSec();
Chenjie Yu5305e1d2017-10-31 13:49:36 -070064 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;
Yangster-mac330af582018-02-08 15:24:38 -080069 tmp.entry_v1.sec = timestampSec;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070070 // 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));
Joe Onorato9fc9edf2017-10-15 20:08:52 -070073 }
Tej Singh484524a2018-02-01 15:10:05 -080074 VLOG("StatsCompanionServicePuller::pull succeeded for %d", mTagId);
Chenjie Yu5305e1d2017-10-31 13:49:36 -070075 return true;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070076 } else {
77 ALOGW("statsCompanion not found!");
Chenjie Yu5305e1d2017-10-31 13:49:36 -070078 return false;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070079 }
80}
81
82} // namespace statsd
83} // namespace os
84} // namespace android