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 gfxstream {
30 namespace guest {
31 
32 // Struct for hanging events
33 struct EventHangMetadata {
34     const char* file;
35     const char* function;
36     const char* msg;
37     const int line;
38     const unsigned long threadId;
39     // Field for adding custom key value annotations
40     using HangAnnotations = std::unordered_map<std::string, std::string>;
41     // Field for adding custom key value annotations
42     std::unique_ptr<HangAnnotations> data;
43 
44     // TODO: willho@ replace this enum with a generic string field embedded in the
45     // proto and replace the individual event codes with a general hang event
46     // Requires a new callback to be passed from the vm to gfxstream_backend_init
47     enum class HangType { kRenderThread, kSyncThread, kOther };
48     HangType hangType;
49 
EventHangMetadataEventHangMetadata50     EventHangMetadata(const char* file, const char* function, const char* msg, int line,
51                       HangType hangType, std::unique_ptr<HangAnnotations> data)
52         : file(file),
53           function(function),
54           msg(msg),
55           line(line),
56           threadId(getCurrentThreadId()),
57           data(std::move(data)),
58           hangType(hangType) {}
59 
EventHangMetadataEventHangMetadata60     EventHangMetadata()
61         : EventHangMetadata(nullptr, nullptr, nullptr, 0, HangType::kRenderThread, nullptr) {}
62 
mergeAnnotationsEventHangMetadata63     void mergeAnnotations(std::unique_ptr<HangAnnotations> annotations) {
64         if (!data) {
65             data = std::make_unique<HangAnnotations>();
66         }
67         data->merge(*annotations);
68     }
69 };
70 
71 class HealthMonitorConsumer {
72 public:
73     virtual void consumeHangEvent(uint64_t taskId, const EventHangMetadata* metadata,
74                                   int64_t otherHungTasks) = 0;
75     virtual void consumeUnHangEvent(uint64_t taskId, const EventHangMetadata* metadata,
76                                     int64_t hungMs) = 0;
~HealthMonitorConsumer()77     virtual ~HealthMonitorConsumer() {}
78 };
79 
80 } // namespace guest
81 } // namespace gfxstream
82