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