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