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