1 // Copyright (C) 2022 The Android Open Source Project 2 // Copyright (C) 2022 Google Inc. 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 #pragma once 16 17 #include <inttypes.h> 18 19 #include <memory> 20 #include <string> 21 #include <thread> 22 #include <unordered_map> 23 #include <variant> 24 25 #include "aemu/base/threads/AndroidThread.h" 26 27 // Interface for consuming events from HealthMonitor 28 29 namespace android { 30 namespace base { 31 namespace guest { 32 33 // Struct for hanging events 34 struct EventHangMetadata { 35 const char* file; 36 const char* function; 37 const char* msg; 38 const int line; 39 const unsigned long threadId; 40 // Field for adding custom key value annotations 41 using HangAnnotations = std::unordered_map<std::string, std::string>; 42 // Field for adding custom key value annotations 43 std::unique_ptr<HangAnnotations> data; 44 45 // TODO: willho@ replace this enum with a generic string field embedded in the 46 // proto and replace the individual event codes with a general hang event 47 // Requires a new callback to be passed from the vm to gfxstream_backend_init 48 enum class HangType { kRenderThread, kSyncThread, kOther }; 49 HangType hangType; 50 EventHangMetadataEventHangMetadata51 EventHangMetadata(const char* file, const char* function, const char* msg, int line, 52 HangType hangType, std::unique_ptr<HangAnnotations> data) 53 : file(file), 54 function(function), 55 msg(msg), 56 line(line), 57 threadId(getCurrentThreadId()), 58 data(std::move(data)), 59 hangType(hangType) {} 60 EventHangMetadataEventHangMetadata61 EventHangMetadata() 62 : EventHangMetadata(nullptr, nullptr, nullptr, 0, HangType::kRenderThread, nullptr) {} 63 mergeAnnotationsEventHangMetadata64 void mergeAnnotations(std::unique_ptr<HangAnnotations> annotations) { 65 if (!data) { 66 data = std::make_unique<HangAnnotations>(); 67 } 68 data->merge(*annotations); 69 } 70 }; 71 72 class HealthMonitorConsumer { 73 public: 74 virtual void consumeHangEvent(uint64_t taskId, const EventHangMetadata* metadata, 75 int64_t otherHungTasks) = 0; 76 virtual void consumeUnHangEvent(uint64_t taskId, const EventHangMetadata* metadata, 77 int64_t hungMs) = 0; ~HealthMonitorConsumer()78 virtual ~HealthMonitorConsumer() {} 79 }; 80 81 } // namespace guest 82 } // namespace base 83 } // namespace android 84