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