1 /*
2  * Copyright (C) 2018 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 "subscriber_util.h"
21 
22 #include "external/Perfetto.h"
23 #include "external/Uprobestats.h"
24 #include "subscriber/IncidentdReporter.h"
25 #include "subscriber/SubscriberReporter.h"
26 
27 namespace android {
28 namespace os {
29 namespace statsd {
30 
triggerSubscribers(const int64_t ruleId,const int64_t metricId,const MetricDimensionKey & dimensionKey,int64_t metricValue,const ConfigKey & configKey,const std::vector<Subscription> & subscriptions)31 void triggerSubscribers(const int64_t ruleId, const int64_t metricId,
32                         const MetricDimensionKey& dimensionKey, int64_t metricValue,
33                         const ConfigKey& configKey,
34                         const std::vector<Subscription>& subscriptions) {
35     VLOG("informSubscribers called.");
36     if (subscriptions.empty()) {
37         VLOG("No Subscriptions were associated.");
38         return;
39     }
40 
41     for (const Subscription& subscription : subscriptions) {
42         if (subscription.probability_of_informing() < 1
43                 && ((float)rand() / (float)RAND_MAX) >= subscription.probability_of_informing()) {
44             // Note that due to float imprecision, 0.0 and 1.0 might not truly mean never/always.
45             // The config writer was advised to use -0.1 and 1.1 for never/always.
46             ALOGI("Fate decided that a subscriber would not be informed.");
47             continue;
48         }
49         switch (subscription.subscriber_information_case()) {
50             case Subscription::SubscriberInformationCase::kIncidentdDetails:
51                 if (!GenerateIncidentReport(subscription.incidentd_details(), ruleId, metricId,
52                                             dimensionKey, metricValue, configKey)) {
53                     ALOGW("Failed to generate incident report.");
54                 }
55                 break;
56             case Subscription::SubscriberInformationCase::kPerfettoDetails:
57                 if (!CollectPerfettoTraceAndUploadToDropbox(subscription.perfetto_details(),
58                                                             subscription.id(), ruleId, configKey)) {
59                     ALOGW("Failed to generate perfetto traces.");
60                 }
61                 break;
62             case Subscription::SubscriberInformationCase::kUprobestatsDetails:
63                 if (!StartUprobeStats(subscription.uprobestats_details())) {
64                     ALOGW("Failed to start uprobestats.");
65                 }
66                 break;
67             case Subscription::SubscriberInformationCase::kBroadcastSubscriberDetails:
68                 SubscriberReporter::getInstance().alertBroadcastSubscriber(configKey, subscription,
69                                                                            dimensionKey);
70                 break;
71             default:
72                 break;
73         }
74     }
75 }
76 
77 }  // namespace statsd
78 }  // namespace os
79 }  // namespace android
80