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