blob: b955f1cdaf5a4f070c2069a201978ae049fd4a53 [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
Chenjie Yub3dda412017-10-24 13:41:59 -070017#define DEBUG true
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 Yu5305e1d2017-10-31 13:49:36 -070023#include "StatsCompanionServicePuller.h"
Chenjie Yu1a317ba2017-10-05 16:05:32 -070024#include "StatsService.h"
Chenjie Yub038b702017-12-18 15:15:34 -080025#include "guardrail/StatsdStats.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
46bool StatsCompanionServicePuller::PullInternal(vector<shared_ptr<LogEvent> >* data) {
Chenjie Yu1a317ba2017-10-05 16:05:32 -070047 sp<IStatsCompanionService> statsCompanion = StatsService::getStatsCompanionService();
David Chen1481fe12017-10-16 13:16:34 -070048 vector<StatsLogEventWrapper> returned_value;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070049 if (statsCompanion != NULL) {
Chenjie Yub038b702017-12-18 15:15:34 -080050 Status status = statsCompanion->pullData(mTagId, &returned_value);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070051 if (!status.isOk()) {
Chenjie Yub038b702017-12-18 15:15:34 -080052 ALOGW("error pulling for %d", mTagId);
Chenjie Yu5305e1d2017-10-31 13:49:36 -070053 return false;
54 }
55 data->clear();
Chenjie Yu6842a8c2017-12-05 22:34:34 -080056 int timestamp = time(nullptr);
Chenjie Yu5305e1d2017-10-31 13:49:36 -070057 for (const StatsLogEventWrapper& it : returned_value) {
58 log_msg tmp;
59 tmp.entry_v1.len = it.bytes.size();
60 // Manually set the header size to 28 bytes to match the pushed log events.
61 tmp.entry.hdr_size = kLogMsgHeaderSize;
Chenjie Yu6842a8c2017-12-05 22:34:34 -080062 tmp.entry_v1.sec = timestamp;
Chenjie Yu5305e1d2017-10-31 13:49:36 -070063 // And set the received bytes starting after the 28 bytes reserved for header.
64 std::copy(it.bytes.begin(), it.bytes.end(), tmp.buf + kLogMsgHeaderSize);
65 data->push_back(make_shared<LogEvent>(tmp));
Joe Onorato9fc9edf2017-10-15 20:08:52 -070066 }
Chenjie Yub038b702017-12-18 15:15:34 -080067 ALOGD("StatsCompanionServicePuller::pull succeeded for %d", mTagId);
Chenjie Yu5305e1d2017-10-31 13:49:36 -070068 return true;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070069 } else {
70 ALOGW("statsCompanion not found!");
Chenjie Yu5305e1d2017-10-31 13:49:36 -070071 return false;
Chenjie Yu1a317ba2017-10-05 16:05:32 -070072 }
73}
74
75} // namespace statsd
76} // namespace os
77} // namespace android