• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023, 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 #define STATSD_DEBUG true
17 #include "Log.h"
18 
19 #include "RestrictedEventMetricProducer.h"
20 
21 #include "stats_annotations.h"
22 #include "stats_log_util.h"
23 #include "utils/DbUtils.h"
24 
25 using std::lock_guard;
26 using std::vector;
27 
28 namespace android {
29 namespace os {
30 namespace statsd {
31 
32 #define NS_PER_DAY (24 * 3600 * NS_PER_SEC)
33 
RestrictedEventMetricProducer(const ConfigKey & key,const EventMetric & metric,const int conditionIndex,const vector<ConditionState> & initialConditionCache,const sp<ConditionWizard> & wizard,const uint64_t protoHash,const int64_t startTimeNs,const unordered_map<int,shared_ptr<Activation>> & eventActivationMap,const unordered_map<int,vector<shared_ptr<Activation>>> & eventDeactivationMap,const vector<int> & slicedStateAtoms,const unordered_map<int,unordered_map<int,int64_t>> & stateGroupMap)34 RestrictedEventMetricProducer::RestrictedEventMetricProducer(
35         const ConfigKey& key, const EventMetric& metric, const int conditionIndex,
36         const vector<ConditionState>& initialConditionCache, const sp<ConditionWizard>& wizard,
37         const uint64_t protoHash, const int64_t startTimeNs,
38         const unordered_map<int, shared_ptr<Activation>>& eventActivationMap,
39         const unordered_map<int, vector<shared_ptr<Activation>>>& eventDeactivationMap,
40         const vector<int>& slicedStateAtoms,
41         const unordered_map<int, unordered_map<int, int64_t>>& stateGroupMap)
42     : EventMetricProducer(key, metric, conditionIndex, initialConditionCache, wizard, protoHash,
43                           startTimeNs, eventActivationMap, eventDeactivationMap, slicedStateAtoms,
44                           stateGroupMap),
45       mRestrictedDataCategory(CATEGORY_UNKNOWN) {
46 }
47 
onMatchedLogEventInternalLocked(const size_t matcherIndex,const MetricDimensionKey & eventKey,const ConditionKey & conditionKey,bool condition,const LogEvent & event,const std::map<int,HashableDimensionKey> & statePrimaryKeys)48 void RestrictedEventMetricProducer::onMatchedLogEventInternalLocked(
49         const size_t matcherIndex, const MetricDimensionKey& eventKey,
50         const ConditionKey& conditionKey, bool condition, const LogEvent& event,
51         const std::map<int, HashableDimensionKey>& statePrimaryKeys) {
52     if (!condition) {
53         return;
54     }
55     if (mRestrictedDataCategory != CATEGORY_UNKNOWN &&
56         mRestrictedDataCategory != event.getRestrictionCategory()) {
57         StatsdStats::getInstance().noteRestrictedMetricCategoryChanged(mConfigKey, mMetricId);
58         deleteMetricTable();
59         mLogEvents.clear();
60         mTotalSize = 0;
61     }
62     mRestrictedDataCategory = event.getRestrictionCategory();
63     mLogEvents.push_back(event);
64     mTotalSize += getSize(event.getValues()) + sizeof(event);
65 }
66 
onDumpReportLocked(const int64_t dumpTimeNs,const bool include_current_partial_bucket,const bool erase_data,const DumpLatency dumpLatency,std::set<string> * str_set,android::util::ProtoOutputStream * protoOutput)67 void RestrictedEventMetricProducer::onDumpReportLocked(
68         const int64_t dumpTimeNs, const bool include_current_partial_bucket, const bool erase_data,
69         const DumpLatency dumpLatency, std::set<string>* str_set,
70         android::util::ProtoOutputStream* protoOutput) {
71     VLOG("Unexpected call to onDumpReportLocked() in RestrictedEventMetricProducer");
72 }
73 
onMetricRemove()74 void RestrictedEventMetricProducer::onMetricRemove() {
75     std::lock_guard<std::mutex> lock(mMutex);
76     if (!mIsMetricTableCreated) {
77         return;
78     }
79     deleteMetricTable();
80 }
81 
enforceRestrictedDataTtl(sqlite3 * db,const int64_t wallClockNs)82 void RestrictedEventMetricProducer::enforceRestrictedDataTtl(sqlite3* db,
83                                                              const int64_t wallClockNs) {
84     int32_t ttlInDays = RestrictedPolicyManager::getInstance().getRestrictedCategoryTtl(
85             mRestrictedDataCategory);
86     int64_t ttlTime = wallClockNs - ttlInDays * NS_PER_DAY;
87     dbutils::flushTtl(db, mMetricId, ttlTime);
88 }
89 
clearPastBucketsLocked(const int64_t dumpTimeNs)90 void RestrictedEventMetricProducer::clearPastBucketsLocked(const int64_t dumpTimeNs) {
91     VLOG("Unexpected call to clearPastBucketsLocked in RestrictedEventMetricProducer");
92 }
93 
dropDataLocked(const int64_t dropTimeNs)94 void RestrictedEventMetricProducer::dropDataLocked(const int64_t dropTimeNs) {
95     mLogEvents.clear();
96     mTotalSize = 0;
97     StatsdStats::getInstance().noteBucketDropped(mMetricId);
98 }
99 
flushRestrictedData()100 void RestrictedEventMetricProducer::flushRestrictedData() {
101     std::lock_guard<std::mutex> lock(mMutex);
102     if (mLogEvents.empty()) {
103         return;
104     }
105     int64_t flushStartNs = getElapsedRealtimeNs();
106     if (!mIsMetricTableCreated) {
107         if (!dbutils::isEventCompatible(mConfigKey, mMetricId, mLogEvents[0])) {
108             // Delete old data if schema changes
109             // TODO(b/268150038): report error to statsdstats
110             ALOGD("Detected schema change for metric %lld", (long long)mMetricId);
111             deleteMetricTable();
112         }
113         // TODO(b/271481944): add retry.
114         if (!dbutils::createTableIfNeeded(mConfigKey, mMetricId, mLogEvents[0])) {
115             ALOGE("Failed to create table for metric %lld", (long long)mMetricId);
116             StatsdStats::getInstance().noteRestrictedMetricTableCreationError(mConfigKey,
117                                                                               mMetricId);
118             return;
119         }
120         mIsMetricTableCreated = true;
121     }
122     string err;
123     if (!dbutils::insert(mConfigKey, mMetricId, mLogEvents, err)) {
124         ALOGE("Failed to insert logEvent to table for metric %lld. err=%s", (long long)mMetricId,
125               err.c_str());
126         StatsdStats::getInstance().noteRestrictedMetricInsertError(mConfigKey, mMetricId);
127     } else {
128         StatsdStats::getInstance().noteRestrictedMetricFlushLatency(
129                 mConfigKey, mMetricId, getElapsedRealtimeNs() - flushStartNs);
130     }
131     mLogEvents.clear();
132     mTotalSize = 0;
133 }
134 
writeMetricMetadataToProto(metadata::MetricMetadata * metricMetadata)135 bool RestrictedEventMetricProducer::writeMetricMetadataToProto(
136         metadata::MetricMetadata* metricMetadata) {
137     metricMetadata->set_metric_id(mMetricId);
138     metricMetadata->set_restricted_category(mRestrictedDataCategory);
139     return true;
140 }
141 
loadMetricMetadataFromProto(const metadata::MetricMetadata & metricMetadata)142 void RestrictedEventMetricProducer::loadMetricMetadataFromProto(
143         const metadata::MetricMetadata& metricMetadata) {
144     mRestrictedDataCategory =
145             static_cast<StatsdRestrictionCategory>(metricMetadata.restricted_category());
146 }
147 
deleteMetricTable()148 void RestrictedEventMetricProducer::deleteMetricTable() {
149     if (!dbutils::deleteTable(mConfigKey, mMetricId)) {
150         StatsdStats::getInstance().noteRestrictedMetricTableDeletionError(mConfigKey, mMetricId);
151         VLOG("Failed to delete table for metric %lld", (long long)mMetricId);
152     }
153     mIsMetricTableCreated = false;
154 }
155 
156 }  // namespace statsd
157 }  // namespace os
158 }  // namespace android
159