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 internal buffer.
9  */
10 
11 #include <trcRecorder.h>
12 
13 #if (TRC_USE_TRACEALYZER_RECORDER == 1) && (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) && (TRC_USE_INTERNAL_BUFFER == 1)
14 
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdarg.h>
18 
19 static TraceMultiCoreEventBuffer_t *pxInternalEventBuffer TRC_CFG_RECORDER_DATA_ATTRIBUTE;
20 
xTraceInternalEventBufferInitialize(uint8_t * puiBuffer,uint32_t uiSize)21 traceResult xTraceInternalEventBufferInitialize(uint8_t* puiBuffer, uint32_t uiSize)
22 {
23 	/* uiSize must be larger than sizeof(TraceMultiCoreEventBuffer_t) or there will be no room for any data */
24 	/* This should never fail */
25 	TRC_ASSERT(uiSize > sizeof(TraceMultiCoreEventBuffer_t));
26 
27 	/* pxInternalBuffer will be placed at the beginning of the puiBuffer */
28 	pxInternalEventBuffer = (TraceMultiCoreEventBuffer_t*)puiBuffer;
29 
30 	/* Send in a an address pointing after the TraceMultiCoreEventBuffer_t */
31 	/* We need to check this */
32 	if (xTraceMultiCoreEventBufferInitialize(pxInternalEventBuffer, TRC_EVENT_BUFFER_OPTION_SKIP,
33 		&puiBuffer[sizeof(TraceMultiCoreEventBuffer_t)], uiSize - sizeof(TraceMultiCoreEventBuffer_t)) == TRC_FAIL)
34 	{
35 		return TRC_FAIL;
36 	}
37 
38 	xTraceSetComponentInitialized(TRC_RECORDER_COMPONENT_INTERNAL_EVENT_BUFFER);
39 
40 	return TRC_SUCCESS;
41 }
42 
xTraceInternalEventBufferAlloc(uint32_t uiSize,void ** ppvData)43 traceResult xTraceInternalEventBufferAlloc(uint32_t uiSize, void **ppvData)
44 {
45 	/* This should never fail */
46 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_INTERNAL_EVENT_BUFFER));
47 
48 	return xTraceMultiCoreEventBufferAlloc(pxInternalEventBuffer, uiSize, ppvData);
49 }
50 
xTraceInternalEventBufferAllocCommit(void * pvData,uint32_t uiSize,int32_t * piBytesWritten)51 traceResult xTraceInternalEventBufferAllocCommit(void *pvData, uint32_t uiSize, int32_t *piBytesWritten)
52 {
53 	(void)pvData;
54 
55 	/* This should never fail */
56 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_INTERNAL_EVENT_BUFFER));
57 
58 	return xTraceMultiCoreEventBufferAllocCommit(pxInternalEventBuffer, pvData, uiSize, piBytesWritten);
59 }
60 
xTraceInternalEventBufferPush(void * pvData,uint32_t uiSize,int32_t * piBytesWritten)61 traceResult xTraceInternalEventBufferPush(void *pvData, uint32_t uiSize, int32_t *piBytesWritten)
62 {
63 	/* This should never fail */
64 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_INTERNAL_EVENT_BUFFER));
65 
66 	return xTraceMultiCoreEventBufferPush(pxInternalEventBuffer, pvData, uiSize, piBytesWritten);
67 }
68 
xTraceInternalEventBufferTransferAll(void)69 traceResult xTraceInternalEventBufferTransferAll(void)
70 {
71 	int32_t iBytesWritten = 0;
72 
73 	/* This should never fail */
74 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_INTERNAL_EVENT_BUFFER));
75 
76 	return xTraceMultiCoreEventBufferTransferAll(pxInternalEventBuffer, &iBytesWritten);
77 }
78 
xTraceInternalEventBufferTransferChunk(void)79 traceResult xTraceInternalEventBufferTransferChunk(void)
80 {
81 	int32_t iBytesWritten = 0;
82 	int32_t iCounter = 0;
83 
84 	/* This should never fail */
85 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_INTERNAL_EVENT_BUFFER));
86 
87 	do
88 	{
89 		if (xTraceMultiCoreEventBufferTransferChunk(pxInternalEventBuffer, TRC_INTERNAL_BUFFER_CHUNK_SIZE, &iBytesWritten) == TRC_FAIL)
90 		{
91 			return TRC_FAIL;
92 		}
93 
94 		iCounter++;
95 		/* This will do another loop if TRC_INTERNAL_BUFFER_CHUNK_TRANSFER_AGAIN_SIZE_LIMIT of data was transferred and we haven't already looped TRC_INTERNAL_BUFFER_CHUNK_TRANSFER_AGAIN_COUNT_LIMIT number of times */
96 	} while (iBytesWritten >= (int32_t)(TRC_INTERNAL_BUFFER_CHUNK_TRANSFER_AGAIN_SIZE_LIMIT) && iCounter < (int32_t)(TRC_INTERNAL_BUFFER_CHUNK_TRANSFER_AGAIN_COUNT_LIMIT));
97 
98 	return TRC_SUCCESS;
99 }
100 
xTraceInternalEventBufferClear()101 traceResult xTraceInternalEventBufferClear()
102 {
103 	/* This should never fail */
104 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_INTERNAL_EVENT_BUFFER));
105 
106 	return xTraceMultiCoreEventBufferClear(pxInternalEventBuffer);
107 }
108 
109 #endif
110