1 /*
2 * Copyright (C) 2022 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 #ifdef CHRE_TELEMETRY_SUPPORT_ENABLED
18
19 #include "chre/core/telemetry_manager.h"
20
21 #include <pb_encode.h>
22
23 #include "chre/core/event_loop_manager.h"
24 #include "chre/platform/fatal_error.h"
25 #include "chre/util/macros.h"
26 #include "chre/util/nested_data_ptr.h"
27 #include "chre/util/time.h"
28 #include "core/chre_metrics.nanopb.h"
29
30 namespace chre {
31
32 namespace {
33
34 // !!!!!!!!!!!!!!!!!!!!!!!!!!!!! DISCLAIMER !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
35 // The metrics implemented in this class makes use of open-sourced PixelAtoms,
36 // but they are not Pixel-specific, and can be extended to OEM use. If you
37 // would like to use this code for telemetry purposes, please contact us for
38 // details.
39
40 //! Helper define macros for nanopb types.
41 #define CHREATOMS_GET(x) android_chre_metrics_##x
42 #define CHREATOMS_GET_PAL_TYPE(x) \
43 _android_chre_metrics_ChrePalType:: \
44 android_chre_metrics_ChrePalType_CHRE_PAL_TYPE_##x
45
46 // These IDs must be kept in sync with
47 // hardware/google/pixel/pixelstats/pixelatoms.proto.
48 constexpr uint32_t kEventQueueSnapshotReportedId = 105035;
49 constexpr uint32_t kPalOpenedFailedId = 105032;
50
sendMetricToHost(uint32_t atomId,const pb_field_t fields[],const void * data)51 void sendMetricToHost(uint32_t atomId, const pb_field_t fields[],
52 const void *data) {
53 size_t size;
54 if (!pb_get_encoded_size(&size, fields, data)) {
55 LOGE("Failed to get message size");
56 } else {
57 pb_byte_t *bytes = static_cast<pb_byte_t *>(memoryAlloc(size));
58 if (bytes == nullptr) {
59 LOG_OOM();
60 } else {
61 pb_ostream_t stream = pb_ostream_from_buffer(bytes, size);
62 if (!pb_encode(&stream, fields, data)) {
63 LOGE("Failed to metric error %s", PB_GET_ERROR(&stream));
64 } else {
65 HostCommsManager &manager =
66 EventLoopManagerSingleton::get()->getHostCommsManager();
67 if (!manager.sendMetricLog(atomId, bytes, size)) {
68 LOGE("Failed to send metric message");
69 }
70 }
71 memoryFree(bytes);
72 }
73 }
74 }
75
sendPalOpenFailedMetric(_android_chre_metrics_ChrePalType pal)76 void sendPalOpenFailedMetric(_android_chre_metrics_ChrePalType pal) {
77 _android_chre_metrics_ChrePalOpenFailed result =
78 CHREATOMS_GET(ChrePalOpenFailed_init_default);
79 result.has_pal = true;
80 result.pal = pal;
81 result.has_type = true;
82 result.type = _android_chre_metrics_ChrePalOpenFailed_Type::
83 android_chre_metrics_ChrePalOpenFailed_Type_INITIAL_OPEN;
84
85 sendMetricToHost(kPalOpenedFailedId, CHREATOMS_GET(ChrePalOpenFailed_fields),
86 &result);
87 }
88
sendEventLoopStats(uint32_t maxQueueSize,uint32_t numDroppedEvents)89 void sendEventLoopStats(uint32_t maxQueueSize, uint32_t numDroppedEvents) {
90 _android_chre_metrics_ChreEventQueueSnapshotReported result =
91 CHREATOMS_GET(ChreEventQueueSnapshotReported_init_default);
92 result.has_snapshot_chre_get_time_ms = true;
93 result.snapshot_chre_get_time_ms =
94 SystemTime::getMonotonicTime().toRawNanoseconds() /
95 kOneMillisecondInNanoseconds;
96 result.has_max_event_queue_size = true;
97 result.max_event_queue_size = maxQueueSize;
98 result.has_num_dropped_events = true;
99 result.num_dropped_events = numDroppedEvents;
100
101 sendMetricToHost(kEventQueueSnapshotReportedId,
102 CHREATOMS_GET(ChreEventQueueSnapshotReported_fields),
103 &result);
104 }
105
toAtomPalType(TelemetryManager::PalType type)106 _android_chre_metrics_ChrePalType toAtomPalType(
107 TelemetryManager::PalType type) {
108 switch (type) {
109 case TelemetryManager::PalType::SENSOR:
110 return CHREATOMS_GET_PAL_TYPE(SENSOR);
111 case TelemetryManager::PalType::WIFI:
112 return CHREATOMS_GET_PAL_TYPE(WIFI);
113 case TelemetryManager::PalType::GNSS:
114 return CHREATOMS_GET_PAL_TYPE(GNSS);
115 case TelemetryManager::PalType::WWAN:
116 return CHREATOMS_GET_PAL_TYPE(WWAN);
117 case TelemetryManager::PalType::AUDIO:
118 return CHREATOMS_GET_PAL_TYPE(AUDIO);
119 case TelemetryManager::PalType::BLE:
120 return CHREATOMS_GET_PAL_TYPE(BLE);
121 case TelemetryManager::PalType::UNKNOWN:
122 default:
123 LOGW("Unknown PAL type %" PRIu8, type);
124 return CHREATOMS_GET_PAL_TYPE(UNKNOWN);
125 }
126 }
127
128 } // anonymous namespace
129
TelemetryManager()130 TelemetryManager::TelemetryManager() {
131 scheduleMetricTimer();
132 }
133
onPalOpenFailure(PalType type)134 void TelemetryManager::onPalOpenFailure(PalType type) {
135 auto callback = [](uint16_t /*type*/, void *data, void * /*extraData*/) {
136 _android_chre_metrics_ChrePalType palType =
137 toAtomPalType(NestedDataPtr<PalType>(data));
138
139 if (palType != CHREATOMS_GET_PAL_TYPE(UNKNOWN)) {
140 sendPalOpenFailedMetric(palType);
141 }
142 };
143
144 // Defer the metric sending callback to better ensure that the host can
145 // receive this message, as this method may be called prior to chre::init()
146 // completion.
147 EventLoopManagerSingleton::get()->deferCallback(
148 SystemCallbackType::DeferredMetricPostEvent, NestedDataPtr<PalType>(type),
149 callback);
150 }
151
collectSystemMetrics()152 void TelemetryManager::collectSystemMetrics() {
153 EventLoop &eventLoop = EventLoopManagerSingleton::get()->getEventLoop();
154 sendEventLoopStats(eventLoop.getMaxEventQueueSize(),
155 eventLoop.getNumEventsDropped());
156
157 scheduleMetricTimer();
158 }
159
scheduleMetricTimer()160 void TelemetryManager::scheduleMetricTimer() {
161 constexpr Seconds kDelay = Seconds(kOneDayInSeconds);
162 auto callback = [](uint16_t /* eventType */, void * /* data */,
163 void * /* extraData */) {
164 EventLoopManagerSingleton::get()
165 ->getTelemetryManager()
166 .collectSystemMetrics();
167 };
168 TimerHandle handle = EventLoopManagerSingleton::get()->setDelayedCallback(
169 SystemCallbackType::DeferredMetricPostEvent, nullptr /* data */, callback,
170 kDelay);
171 if (handle == CHRE_TIMER_INVALID) {
172 LOGE("Failed to set daily metric timer");
173 }
174 }
175
176 } // namespace chre
177
178 #endif // CHRE_TELEMETRY_SUPPORT_ENABLED
179