1 /*
2 * Trace Recorder for Tracealyzer v4.8.1
3 * Copyright 2023 Percepio AB
4 * www.percepio.com
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * The implementation of the diagnostics.
9 */
10 
11 #include <trcRecorder.h>
12 
13 #if (TRC_USE_TRACEALYZER_RECORDER == 1)
14 
15 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
16 
17 static TraceDiagnosticsData_t *pxDiagnostics TRC_CFG_RECORDER_DATA_ATTRIBUTE;
18 
xTraceDiagnosticsInitialize(TraceDiagnosticsData_t * pxBuffer)19 traceResult xTraceDiagnosticsInitialize(TraceDiagnosticsData_t *pxBuffer)
20 {
21 	uint32_t i;
22 
23 	/* This should never fail */
24 	TRC_ASSERT(pxBuffer != (void*)0);
25 
26 	pxDiagnostics = pxBuffer;
27 
28 	for (i = 0u; i < (TRC_DIAGNOSTICS_COUNT); i++)
29 	{
30 		pxDiagnostics->metrics[i] = 0;
31 	}
32 
33 	xTraceSetComponentInitialized(TRC_RECORDER_COMPONENT_DIAGNOSTICS);
34 
35 	return TRC_SUCCESS;
36 }
37 
xTraceDiagnosticsGet(TraceDiagnosticsType_t xType,TraceBaseType_t * pxValue)38 traceResult xTraceDiagnosticsGet(TraceDiagnosticsType_t xType, TraceBaseType_t* pxValue)
39 {
40 	/* This should never fail */
41 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_DIAGNOSTICS));
42 
43 	/* This should never fail */
44 	TRC_ASSERT((TraceUnsignedBaseType_t)xType < TRC_DIAGNOSTICS_COUNT);
45 
46 	/* This should never fail */
47 	TRC_ASSERT(pxValue != (void*)0);
48 
49 	*pxValue = pxDiagnostics->metrics[(TraceUnsignedBaseType_t)xType];
50 
51 	return TRC_SUCCESS;
52 }
53 
xTraceDiagnosticsSet(TraceDiagnosticsType_t xType,TraceBaseType_t xValue)54 traceResult xTraceDiagnosticsSet(TraceDiagnosticsType_t xType, TraceBaseType_t xValue)
55 {
56 	/* This should never fail */
57 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_DIAGNOSTICS));
58 
59 	/* This should never fail */
60 	TRC_ASSERT((TraceUnsignedBaseType_t)xType < TRC_DIAGNOSTICS_COUNT);
61 
62 	pxDiagnostics->metrics[(TraceUnsignedBaseType_t)xType] = xValue;
63 
64 	return TRC_SUCCESS;
65 }
66 
xTraceDiagnosticsAdd(TraceDiagnosticsType_t xType,TraceBaseType_t xValue)67 traceResult xTraceDiagnosticsAdd(TraceDiagnosticsType_t xType, TraceBaseType_t xValue)
68 {
69 	/* This should never fail */
70 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_DIAGNOSTICS));
71 
72 	/* This should never fail */
73 	TRC_ASSERT((TraceUnsignedBaseType_t)xType < TRC_DIAGNOSTICS_COUNT);
74 
75 	pxDiagnostics->metrics[(TraceUnsignedBaseType_t)xType] += xValue;
76 
77 	return TRC_SUCCESS;
78 }
79 
xTraceDiagnosticsIncrease(TraceDiagnosticsType_t xType)80 traceResult xTraceDiagnosticsIncrease(TraceDiagnosticsType_t xType)
81 {
82 	return xTraceDiagnosticsAdd(xType, 1);
83 }
84 
xTraceDiagnosticsDecrease(TraceDiagnosticsType_t xType)85 traceResult xTraceDiagnosticsDecrease(TraceDiagnosticsType_t xType)
86 {
87 	return xTraceDiagnosticsAdd(xType, -1);
88 }
89 
xTraceDiagnosticsSetIfHigher(TraceDiagnosticsType_t xType,TraceBaseType_t xValue)90 traceResult xTraceDiagnosticsSetIfHigher(TraceDiagnosticsType_t xType, TraceBaseType_t xValue)
91 {
92 	/* This should never fail */
93 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_DIAGNOSTICS));
94 
95 	/* This should never fail */
96 	TRC_ASSERT((TraceUnsignedBaseType_t)xType < TRC_DIAGNOSTICS_COUNT);
97 
98 	if (xValue > pxDiagnostics->metrics[xType])
99 	{
100 		pxDiagnostics->metrics[(TraceUnsignedBaseType_t)xType] = xValue;
101 	}
102 
103 	return TRC_SUCCESS;
104 }
105 
xTraceDiagnosticsSetIfLower(TraceDiagnosticsType_t xType,TraceBaseType_t xValue)106 traceResult xTraceDiagnosticsSetIfLower(TraceDiagnosticsType_t xType, TraceBaseType_t xValue)
107 {
108 	/* This should never fail */
109 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_DIAGNOSTICS));
110 
111 	/* This should never fail */
112 	TRC_ASSERT((TraceUnsignedBaseType_t)xType < TRC_DIAGNOSTICS_COUNT);
113 
114 	if (xValue < pxDiagnostics->metrics[(TraceUnsignedBaseType_t)xType])
115 	{
116 		pxDiagnostics->metrics[(TraceUnsignedBaseType_t)xType] = xValue;
117 	}
118 
119 	return TRC_SUCCESS;
120 }
121 
xTraceDiagnosticsCheckStatus(void)122 traceResult xTraceDiagnosticsCheckStatus(void)
123 {
124 	/* It is probably good if we always check this */
125 	if (xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_DIAGNOSTICS) == 0U)
126 	{
127 		return TRC_FAIL;
128 	}
129 
130 	if (pxDiagnostics->metrics[TRC_DIAGNOSTICS_ENTRY_SLOTS_NO_ROOM] > 0)
131 	{
132 		(void)xTraceWarning(TRC_WARNING_ENTRY_TABLE_SLOTS);
133 		pxDiagnostics->metrics[TRC_DIAGNOSTICS_ENTRY_SLOTS_NO_ROOM] = 0;
134 	}
135 
136 	if (pxDiagnostics->metrics[TRC_DIAGNOSTICS_ENTRY_SYMBOL_LONGEST_LENGTH] > (TRC_CFG_ENTRY_SYMBOL_MAX_LENGTH))
137 	{
138 		(void)xTraceWarning(TRC_WARNING_ENTRY_SYMBOL_MAX_LENGTH);
139 		pxDiagnostics->metrics[TRC_DIAGNOSTICS_ENTRY_SYMBOL_LONGEST_LENGTH] = 0;
140 	}
141 
142 	if (pxDiagnostics->metrics[TRC_DIAGNOSTICS_BLOB_MAX_BYTES_TRUNCATED] > 0)
143 	{
144 		(void)xTraceWarning(TRC_WARNING_EVENT_SIZE_TRUNCATED);
145 		pxDiagnostics->metrics[TRC_DIAGNOSTICS_BLOB_MAX_BYTES_TRUNCATED] = 0;
146 	}
147 
148 	if (pxDiagnostics->metrics[TRC_DIAGNOSTICS_STACK_MONITOR_NO_SLOTS] > 0)
149 	{
150 		(void)xTraceWarning(TRC_WARNING_STACKMON_NO_SLOTS);
151 		pxDiagnostics->metrics[TRC_DIAGNOSTICS_STACK_MONITOR_NO_SLOTS] = 0;
152 	}
153 
154 	return TRC_SUCCESS;
155 }
156 
157 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
158 
159 #endif /* (TRC_USE_TRACEALYZER_RECORDER == 1) */
160