1 /*
2 * Percepio 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 interface for the multi-core event buffer.
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 
xTraceMultiCoreEventBufferInitialize(TraceMultiCoreEventBuffer_t * pxTraceMultiCoreEventBuffer,uint32_t uiOptions,uint8_t * puiBuffer,uint32_t uiSize)17 traceResult xTraceMultiCoreEventBufferInitialize(TraceMultiCoreEventBuffer_t* pxTraceMultiCoreEventBuffer, uint32_t uiOptions,
18 	uint8_t* puiBuffer, uint32_t uiSize)
19 {
20 	uint32_t i;
21 	uint32_t uiBufferSizePerCore;
22 
23 	/* This should never fail */
24 	TRC_ASSERT(pxTraceMultiCoreEventBuffer != 0);
25 
26 	/* This should never fail */
27 	TRC_ASSERT(puiBuffer != 0);
28 
29 	uiBufferSizePerCore = uiSize / TRC_CFG_CORE_COUNT;
30 
31 	/* This should never fail */
32 	TRC_ASSERT(uiBufferSizePerCore != 0);
33 
34 	for (i = 0; i < TRC_CFG_CORE_COUNT; i++)
35 	{
36 		/* Set the event buffer pointers to point into the allocated space we have been given, this ensures
37 		 * a flat memory layout necessary for usage in streaming snaphot. */
38 		pxTraceMultiCoreEventBuffer->xEventBuffer[i] = (TraceEventBuffer_t*)(&puiBuffer[i * uiBufferSizePerCore]);
39 
40 		/* Initialize the event buffer structure with its memory buffer placed following its own structure data. */
41 		/* We need to check this */
42 		if (xTraceEventBufferInitialize(pxTraceMultiCoreEventBuffer->xEventBuffer[i], uiOptions,
43 			&puiBuffer[(i * uiBufferSizePerCore) + sizeof(TraceEventBuffer_t)],
44 			uiBufferSizePerCore - sizeof(TraceEventBuffer_t)) == TRC_FAIL)
45 		{
46 			return TRC_FAIL;
47 		}
48 	}
49 
50 	return TRC_SUCCESS;
51 }
52 
53 #if ((TRC_CFG_USE_TRACE_ASSERT) == 1)
54 
xTraceMultiCoreEventBufferPush(TraceMultiCoreEventBuffer_t * pxTraceMultiCoreEventBuffer,void * pvData,uint32_t uiSize,int32_t * piBytesWritten)55 traceResult xTraceMultiCoreEventBufferPush(TraceMultiCoreEventBuffer_t* pxTraceMultiCoreEventBuffer,
56 	void* pvData, uint32_t uiSize, int32_t* piBytesWritten)
57 {
58 	/* This should never fail */
59 	TRC_ASSERT(pxTraceMultiCoreEventBuffer != 0);
60 
61 	TRC_ASSERT((TRC_CFG_GET_CURRENT_CORE()) < (TRC_CFG_CORE_COUNT));
62 
63 	return xTraceEventBufferPush(pxTraceMultiCoreEventBuffer->xEventBuffer[TRC_CFG_GET_CURRENT_CORE()], pvData, uiSize, piBytesWritten);
64 }
65 
66 #endif
67 
xTraceMultiCoreEventBufferTransfer(TraceMultiCoreEventBuffer_t * pxTraceMultiCoreEventBuffer,int32_t * piBytesWritten)68 traceResult xTraceMultiCoreEventBufferTransfer(TraceMultiCoreEventBuffer_t* pxTraceMultiCoreEventBuffer, int32_t* piBytesWritten)
69 {
70 	int32_t iBytesWritten = 0;
71 	uint32_t coreId;
72 
73 	/* This should never fail */
74 	TRC_ASSERT(pxTraceMultiCoreEventBuffer != 0);
75 
76 	/* This should never fail */
77 	TRC_ASSERT(piBytesWritten != 0);
78 
79 	*piBytesWritten = 0;
80 
81 	for (coreId = 0; coreId < TRC_CFG_CORE_COUNT; coreId++)
82 	{
83 		/* We need to check this */
84 		if (xTraceEventBufferTransfer(pxTraceMultiCoreEventBuffer->xEventBuffer[coreId], &iBytesWritten) == TRC_FAIL)
85 		{
86 			return TRC_FAIL;
87 		}
88 
89 		*piBytesWritten += iBytesWritten;
90 	}
91 
92 	return TRC_SUCCESS;
93 }
94 
xTraceMultiCoreEventBufferClear(TraceMultiCoreEventBuffer_t * pxTraceMultiCoreEventBuffer)95 traceResult xTraceMultiCoreEventBufferClear(TraceMultiCoreEventBuffer_t* pxTraceMultiCoreEventBuffer)
96 {
97 	uint32_t coreId;
98 
99 	/* This should never fail */
100 	TRC_ASSERT(pxTraceMultiCoreEventBuffer != 0);
101 
102 	for (coreId = 0; coreId < TRC_CFG_CORE_COUNT; coreId++)
103 	{
104 		/* This should never fail */
105 		TRC_ASSERT_ALWAYS_EVALUATE(xTraceEventBufferClear(pxTraceMultiCoreEventBuffer->xEventBuffer[coreId]) == TRC_SUCCESS);
106 	}
107 
108 	return TRC_SUCCESS;
109 }
110 
111 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
112 
113 #endif /* (TRC_USE_TRACEALYZER_RECORDER == 1) */
114