1 /*
2  * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "platform_trace_test.h"
10 #include "hdf_log.h"
11 #include "platform_trace.h"
12 
13 #define HDF_LOG_TAG platform_trace_test
14 
15 #define TRACE_UNIT_INFO_TEST_SIZE 6
16 static unsigned int g_infos[TRACE_UNIT_INFO_TEST_SIZE] = {100, 200, 300, 400, 500, 600};
PlatformTraceSetUintptrInfoTest(void)17 static int32_t PlatformTraceSetUintptrInfoTest(void)
18 {
19     int i = 0;
20     int32_t ret = PlatformTraceStart();
21     if (ret != HDF_SUCCESS) {
22         HDF_LOGE("PlatformTraceSetUintptrInfoTest: PlatformTraceStart fail, ret: %d!", ret);
23         return ret;
24     }
25 
26     for (i = 1; i <= TRACE_UNIT_INFO_TEST_SIZE; i++) {
27         PlatformTraceAddUintMsg(PLATFORM_TRACE_MODULE_UNITTEST, PLATFORM_TRACE_MODULE_UNITTEST_FUN_TEST,
28             g_infos, i);
29     }
30     PlatformTraceStop();
31     PlatformTraceInfoDump();
32     return HDF_SUCCESS;
33 }
34 
PlatformTraceFmtInfoTest(void)35 static int32_t PlatformTraceFmtInfoTest(void)
36 {
37     int32_t ret = PlatformTraceStart();
38     if (ret != HDF_SUCCESS) {
39         HDF_LOGE("PlatformTraceFmtInfoTest: PlatformTraceStart fail, ret: %d!", ret);
40         return ret;
41     }
42     PlatformTraceAddMsg(
43         "TraceTestModule", "TraceTestModuleFun", ":%d-%d-%s-%s", g_infos[0], g_infos[1], "testparam1", "testparam2");
44     PlatformTraceStop();
45     PlatformTraceInfoDump();
46     return HDF_SUCCESS;
47 }
48 
PlatformTraceTestReliability(void)49 static int32_t PlatformTraceTestReliability(void)
50 {
51     PlatformTraceAddUintMsg(PLATFORM_TRACE_MODULE_MAX, PLATFORM_TRACE_MODULE_MAX_FUN, NULL, 0);
52     PlatformTraceAddMsg(NULL, "TraceTestModuleFun", ":%d-%d-%s-%s",
53         g_infos[0], g_infos[1], "testparam1", "testparam2");
54 
55     return HDF_SUCCESS;
56 }
57 
58 struct PlatformTraceTestEntry {
59     int cmd;
60     int32_t (*func)(void);
61 };
62 
63 static struct PlatformTraceTestEntry g_entry[] = {
64     {PLATFORM_TRACE_TEST_SET_UINT,    PlatformTraceSetUintptrInfoTest},
65     {PLATFORM_TRACE_TEST_SET_FMT,     PlatformTraceFmtInfoTest       },
66     {PLATFORM_TRACE_TEST_RELIABILITY, PlatformTraceTestReliability   },
67 };
68 
PlatformTraceTestExecute(int cmd)69 int PlatformTraceTestExecute(int cmd)
70 {
71 #ifdef __LITEOS__
72 #ifndef LOSCFG_KERNEL_TRACE
73     HDF_LOGE("PlatformTraceTestExecute: please open trace info");
74     return HDF_SUCCESS;
75 #endif
76 #else
77 #ifndef CONFIG_EVENT_TRACING
78     HDF_LOGE("PlatformTraceTestExecute: please open trace info");
79     return HDF_SUCCESS;
80 #endif
81 #endif
82 
83     uint32_t i;
84     int32_t ret = HDF_ERR_NOT_SUPPORT;
85     struct PlatformTraceTestEntry *entry = NULL;
86 
87     if (cmd > PLATFORM_TRACE_TEST_CMD_MAX) {
88         HDF_LOGE("PlatformTraceTestExecute: invalid cmd:%d", cmd);
89         return HDF_ERR_NOT_SUPPORT;
90     }
91 
92     for (i = 0; i < sizeof(g_entry) / sizeof(g_entry[0]); i++) {
93         if (g_entry[i].cmd != cmd || g_entry[i].func == NULL) {
94             continue;
95         }
96         entry = &g_entry[i];
97         break;
98     }
99 
100     if (entry == NULL) {
101         HDF_LOGE("PlatformTraceTestExecute: no entry matched, cmd = %d!", cmd);
102         return HDF_ERR_NOT_SUPPORT;
103     }
104 
105     ret = entry->func();
106     HDF_LOGI("[PlatformTraceTestExecute][======cmd:%d====ret:%d======]", cmd, ret);
107     return ret;
108 }
109