• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
17 
18 #include <aidl/android/hardware/contexthub/ContextHubMessage.h>
19 #include <aidl/android/hardware/contexthub/NanoappBinary.h>
20 #include <optional>
21 #include <string>
22 
23 #include "chre/util/array_queue.h"
24 #include "chre/util/non_copyable.h"
25 #include "chre_host/generated/host_messages_generated.h"
26 
27 namespace aidl::android::hardware::contexthub {
28 
29 /**
30  * Logs HAL events.
31  *
32  * The logged events are store in a fixed size queue. When the number of logged
33  * events exceed the size of the queue, older events are deleted.
34  */
35 class EventLogger {
36  public:
37   /** Maximum number of load and unload events to store. */
38   static constexpr int kMaxNanoappEvents = 20;
39   /** Maximum number of Context Hub restart events to store. */
40   static constexpr int kMaxRestartEvents = 20;
41   /** Maximum number of message events to store. */
42   static constexpr int kMaxMessageEvents = 20;
43 
44   void logNanoappLoad(const NanoappBinary &app, bool success);
45 
46   void logNanoappUnload(int64_t appId, bool success);
47 
48   void logContextHubRestart();
49 
50   void logMessageToNanoapp(const ContextHubMessage &message, bool success);
51 
52   void logMessageFromNanoapp(const ::chre::fbs::NanoappMessageT &message);
53 
54   void logMessageFromNanoapp(const ContextHubMessage &message);
55 
56   /** Returns a textual representation of the logged events. */
57   std::string dump() const;
58 
59  protected:
60   struct NanoappLoad {
61     int64_t timestampMs;
62     int64_t id;
63     int32_t version;
64     size_t sizeBytes;
65     bool success;
66   };
67 
68   struct NanoappUnload {
69     int64_t timestampMs;
70     int64_t id;
71     bool success;
72   };
73 
74   struct NanoappMessage {
75     int64_t timestampMs;
76     int64_t id;
77     size_t sizeBytes;
78     bool success;
79   };
80 
81   ::chre::ArrayQueue<NanoappLoad, kMaxNanoappEvents> mNanoappLoads;
82   ::chre::ArrayQueue<NanoappUnload, kMaxNanoappEvents> mNanoappUnloads;
83   ::chre::ArrayQueue<int64_t, kMaxRestartEvents> mContextHubRestarts;
84   ::chre::ArrayQueue<NanoappMessage, kMaxMessageEvents> mMsgToNanoapp;
85   ::chre::ArrayQueue<NanoappMessage, kMaxMessageEvents> mMsgFromNanoapp;
86 
87   /**
88    * Current time in milliseconds.
89    * Override the current time when a value is provided.
90    * Used for tests.
91    */
92   std::optional<int64_t> mNowMs;
93 
94  private:
95   /** Protects concurrent reads and writes to the queue. */
96   mutable std::mutex mQueuesMutex;
97 
98   /** Returns the current time in milliseconds */
99   int64_t getTimeMs() const;
100 };
101 
102 }  // namespace aidl::android::hardware::contexthub
103