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