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 "event_logger.h"
18
19 #include <inttypes.h>
20 #include <stdio.h>
21 #include <chrono>
22 #include <cstddef>
23 #include <ctime>
24 #include <string>
25
26 namespace aidl::android::hardware::contexthub {
27
28 namespace {
29
30 /**
31 * Returns the time formatted in the local timezone.
32 * The format is similar to the adb logcat format, i.e. `01-31 18:22:51.275`.
33 */
formatLocalTime(int64_t ms)34 std::string formatLocalTime(int64_t ms) {
35 const std::chrono::milliseconds duration(ms);
36 const std::chrono::time_point<std::chrono::system_clock> timePoint(duration);
37 time_t time = std::chrono::system_clock::to_time_t(timePoint);
38 constexpr int kBufferSize = 50;
39 char buffer[kBufferSize];
40 std::strftime(buffer, kBufferSize, "%m-%d %H:%M:%S.", std::localtime(&time));
41 return std::string(buffer) + std::to_string(1000 + ms % 1000).substr(1);
42 }
43
44 } // namespace
45
logNanoappLoad(const NanoappBinary & app,bool success)46 void EventLogger::logNanoappLoad(const NanoappBinary &app, bool success) {
47 std::lock_guard<std::mutex> lock(mQueuesMutex);
48 mNanoappLoads.kick_push({
49 .timestampMs = getTimeMs(),
50 .id = app.nanoappId,
51 .version = app.nanoappVersion,
52 .sizeBytes = app.customBinary.size(),
53 .success = success,
54 });
55 }
56
logNanoappUnload(int64_t appId,bool success)57 void EventLogger::logNanoappUnload(int64_t appId, bool success) {
58 std::lock_guard<std::mutex> lock(mQueuesMutex);
59 mNanoappUnloads.kick_push({
60 .timestampMs = getTimeMs(),
61 .id = appId,
62 .success = success,
63 });
64 }
65
logContextHubRestart()66 void EventLogger::logContextHubRestart() {
67 std::lock_guard<std::mutex> lock(mQueuesMutex);
68 mContextHubRestarts.kick_push(getTimeMs());
69 }
70
logMessageToNanoapp(const ContextHubMessage & message,bool success)71 void EventLogger::logMessageToNanoapp(const ContextHubMessage &message,
72 bool success) {
73 std::lock_guard<std::mutex> lock(mQueuesMutex);
74 mMsgToNanoapp.kick_push({
75 .timestampMs = getTimeMs(),
76 .id = message.nanoappId,
77 .sizeBytes = message.messageBody.size(),
78 .success = success,
79 });
80 }
81
logMessageFromNanoapp(const::chre::fbs::NanoappMessageT & message)82 void EventLogger::logMessageFromNanoapp(
83 const ::chre::fbs::NanoappMessageT &message) {
84 std::lock_guard<std::mutex> lock(mQueuesMutex);
85 mMsgFromNanoapp.kick_push({
86 .timestampMs = getTimeMs(),
87 .id = static_cast<int64_t>(message.app_id),
88 .sizeBytes = message.message.size(),
89 });
90 }
91
dump() const92 std::string EventLogger::dump() const {
93 constexpr int kBufferSize = 100;
94 char buffer[kBufferSize];
95 std::string logs;
96 std::lock_guard<std::mutex> lock(mQueuesMutex);
97
98 logs.append("\nNanoapp loads:\n");
99 for (const NanoappLoad &load : mNanoappLoads) {
100 // Use snprintf because std::format is not available and fmtlib {:x} format
101 // crashes.
102 if (snprintf(buffer, kBufferSize,
103 " %s id 0x%" PRIx64 " version 0x%" PRIx32 " size %zu"
104 " status %s\n",
105 formatLocalTime(load.timestampMs).c_str(), load.id,
106 load.version, load.sizeBytes,
107 load.success ? "ok" : "fail") > 0) {
108 logs.append(buffer);
109 }
110 }
111
112 logs.append("\nNanoapp unloads:\n");
113 for (const NanoappUnload &unload : mNanoappUnloads) {
114 if (snprintf(buffer, kBufferSize, " %s id 0x%" PRIx64 " status %s\n",
115 formatLocalTime(unload.timestampMs).c_str(), unload.id,
116 unload.success ? "ok" : "fail") > 0) {
117 logs.append(buffer);
118 }
119 }
120
121 logs.append("\nMessages to Nanoapps:\n");
122 for (const NanoappMessage &msg : mMsgToNanoapp) {
123 if (snprintf(buffer, kBufferSize,
124 " %s to id 0x%" PRIx64 " size %zu status %s\n",
125 formatLocalTime(msg.timestampMs).c_str(), msg.id,
126 msg.sizeBytes, msg.success ? "ok" : "fail") > 0) {
127 logs.append(buffer);
128 }
129 }
130
131 logs.append("\nMessages from Nanoapps:\n");
132 for (const NanoappMessage &msg : mMsgFromNanoapp) {
133 if (snprintf(buffer, kBufferSize, " %s from id 0x%" PRIx64 " size %zu\n",
134 formatLocalTime(msg.timestampMs).c_str(), msg.id,
135 msg.sizeBytes) > 0) {
136 logs.append(buffer);
137 }
138 }
139
140 logs.append("\nContext hub restarts:\n");
141 for (const int64_t &ms : mContextHubRestarts) {
142 if (snprintf(buffer, kBufferSize, " %s\n", formatLocalTime(ms).c_str()) >
143 0) {
144 logs.append(buffer);
145 }
146 }
147
148 return logs;
149 }
150
getTimeMs() const151 int64_t EventLogger::getTimeMs() const {
152 if (mNowMs.has_value()) {
153 return mNowMs.value();
154 }
155 struct timespec now;
156 clock_gettime(CLOCK_REALTIME, &now);
157 return 1000 * now.tv_sec + static_cast<int64_t>(now.tv_nsec / 1e6);
158 }
159
160 } // namespace aidl::android::hardware::contexthub