xref: /aosp_15_r20/system/chre/apps/debug_dump_world/debug_dump_world.cc (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2020 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 <cinttypes>
18 
19 #include "chre/util/macros.h"
20 #include "chre/util/nanoapp/log.h"
21 #include "chre/util/time.h"
22 #include "chre_api/chre.h"
23 
24 #define LOG_TAG "[DebugDumpWorld]"
25 
26 /**
27  * A nanoapp that log debug data on receiving CHRE_EVENT_DEBUG_DUMP.
28  */
29 
30 #ifdef CHRE_NANOAPP_INTERNAL
31 namespace chre {
32 namespace {
33 #endif  // CHRE_NANOAPP_INTERNAL
34 
35 namespace {
36 
37 uint32_t gEventCount = 0;
38 uint64_t gDwellTimeNs = 0;
39 
40 }  // namespace
41 
nanoappStart()42 bool nanoappStart() {
43   LOGI("Debug dump world start");
44   chreConfigureDebugDumpEvent(true /* enable */);
45   return true;
46 }
47 
nanoappEnd()48 void nanoappEnd() {
49   LOGI("Debug dump world end");
50 
51   // No need to disable debug dump event delivery since nanoapps can't receive
52   // events after nanoappEnd anyway.
53 }
54 
handleDebugDumpEvent()55 void handleDebugDumpEvent() {
56   // CHRE adds the nanoapp name / ID to the debug dump automatically.
57   chreDebugDumpLog("  Debug event count: %" PRIu32 "\n", ++gEventCount);
58   chreDebugDumpLog("  Total dwell time: %" PRIu64 " us\n",
59                    gDwellTimeNs / chre::kOneMicrosecondInNanoseconds);
60 
61   // Prefer the utility macro if you'll log a float, to suppress double
62   // promotion warnings arising from varargs
63   float floatVal = 1.23f;
64   CHRE_DEBUG_DUMP_LOG("  This is a float: %f", floatVal);
65 }
66 
nanoappHandleEvent(uint32_t senderInstanceId,uint16_t eventType,const void * eventData)67 void nanoappHandleEvent(uint32_t senderInstanceId, uint16_t eventType,
68                         const void *eventData) {
69   UNUSED_VAR(eventData);
70 
71   uint64_t tic = chreGetTime();
72   switch (eventType) {
73     case CHRE_EVENT_DEBUG_DUMP:
74       LOGI("Receiving debug dump event");
75       handleDebugDumpEvent();
76       break;
77     default:
78       LOGW("Unknown event type %" PRIu16 " received from sender %" PRIu32,
79            eventType, senderInstanceId);
80       break;
81   }
82   gDwellTimeNs += chreGetTime() - tic;
83 }
84 
85 #ifdef CHRE_NANOAPP_INTERNAL
86 }  // anonymous namespace
87 }  // namespace chre
88 
89 #include "chre/platform/static_nanoapp_init.h"
90 #include "chre/util/nanoapp/app_id.h"
91 #include "chre/util/system/napp_permissions.h"
92 
93 CHRE_STATIC_NANOAPP_INIT(DebugDumpWorld, chre::kDebugDumpWorldAppId, 0,
94                          chre::NanoappPermissions::CHRE_PERMS_NONE);
95 #endif  // CHRE_NANOAPP_INTERNAL
96