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 STATSD_DEBUG false // STOPSHIP if true
18 #include "Log.h"
19
20 #include "SimpleAtomMatchingTracker.h"
21
22 namespace android {
23 namespace os {
24 namespace statsd {
25
26 using std::unordered_map;
27 using std::vector;
28
SimpleAtomMatchingTracker(const int64_t & id,const int index,const uint64_t protoHash,const SimpleAtomMatcher & matcher,const sp<UidMap> & uidMap)29 SimpleAtomMatchingTracker::SimpleAtomMatchingTracker(const int64_t& id, const int index,
30 const uint64_t protoHash,
31 const SimpleAtomMatcher& matcher,
32 const sp<UidMap>& uidMap)
33 : AtomMatchingTracker(id, index, protoHash), mMatcher(matcher), mUidMap(uidMap) {
34 if (!matcher.has_atom_id()) {
35 mInitialized = false;
36 } else {
37 mAtomIds.insert(matcher.atom_id());
38 mInitialized = true;
39 }
40 }
41
~SimpleAtomMatchingTracker()42 SimpleAtomMatchingTracker::~SimpleAtomMatchingTracker() {
43 }
44
init(const vector<AtomMatcher> & allAtomMatchers,const vector<sp<AtomMatchingTracker>> & allAtomMatchingTrackers,const unordered_map<int64_t,int> & matcherMap,vector<bool> & stack)45 optional<InvalidConfigReason> SimpleAtomMatchingTracker::init(
46 const vector<AtomMatcher>& allAtomMatchers,
47 const vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
48 const unordered_map<int64_t, int>& matcherMap, vector<bool>& stack) {
49 // no need to do anything.
50 if (!mInitialized) {
51 return createInvalidConfigReasonWithMatcher(
52 INVALID_CONFIG_REASON_MATCHER_TRACKER_NOT_INITIALIZED, mId);
53 }
54 return nullopt;
55 }
56
onConfigUpdated(const AtomMatcher & matcher,const int index,const unordered_map<int64_t,int> & atomMatchingTrackerMap)57 optional<InvalidConfigReason> SimpleAtomMatchingTracker::onConfigUpdated(
58 const AtomMatcher& matcher, const int index,
59 const unordered_map<int64_t, int>& atomMatchingTrackerMap) {
60 mIndex = index;
61 // Do not need to update mMatcher since the matcher must be identical across the update.
62 if (!mInitialized) {
63 return createInvalidConfigReasonWithMatcher(
64 INVALID_CONFIG_REASON_MATCHER_TRACKER_NOT_INITIALIZED, mId);
65 }
66 return nullopt;
67 }
68
onLogEvent(const LogEvent & event,const vector<sp<AtomMatchingTracker>> & allAtomMatchingTrackers,vector<MatchingState> & matcherResults)69 void SimpleAtomMatchingTracker::onLogEvent(
70 const LogEvent& event, const vector<sp<AtomMatchingTracker>>& allAtomMatchingTrackers,
71 vector<MatchingState>& matcherResults) {
72 if (matcherResults[mIndex] != MatchingState::kNotComputed) {
73 VLOG("Matcher %lld already evaluated ", (long long)mId);
74 return;
75 }
76
77 if (mAtomIds.find(event.GetTagId()) == mAtomIds.end()) {
78 matcherResults[mIndex] = MatchingState::kNotMatched;
79 return;
80 }
81
82 bool matched = matchesSimple(mUidMap, mMatcher, event);
83 matcherResults[mIndex] = matched ? MatchingState::kMatched : MatchingState::kNotMatched;
84 VLOG("Stats SimpleAtomMatcher %lld matched? %d", (long long)mId, matched);
85 }
86
87 } // namespace statsd
88 } // namespace os
89 } // namespace android
90