1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "perf_output_pri.h"
33
34 STATIC PERF_BUF_NOTIFY_HOOK g_perfBufNotifyHook = NULL;
35 STATIC PERF_BUF_FLUSH_HOOK g_perfBufFlushHook = NULL;
36 STATIC PerfOutputCB g_perfOutputCb;
37
OsPerfDefaultNotify(VOID)38 STATIC VOID OsPerfDefaultNotify(VOID)
39 {
40 PRINT_INFO("perf buf waterline notify!\n");
41 }
42
OsPerfOutputInit(VOID * buf,UINT32 size)43 UINT32 OsPerfOutputInit(VOID *buf, UINT32 size)
44 {
45 UINT32 ret;
46 BOOL releaseFlag = FALSE;
47 if (buf == NULL) {
48 buf = LOS_MemAlloc(m_aucSysMem1, size);
49 if (buf == NULL) {
50 return LOS_NOK;
51 } else {
52 releaseFlag = TRUE;
53 }
54 }
55 ret = LOS_CirBufInit(&g_perfOutputCb.ringbuf, buf, size);
56 if (ret != LOS_OK) {
57 goto RELEASE;
58 }
59 g_perfOutputCb.waterMark = size / PERF_BUFFER_WATERMARK_ONE_N;
60 g_perfBufNotifyHook = OsPerfDefaultNotify;
61 return ret;
62 RELEASE:
63 if (releaseFlag) {
64 (VOID)LOS_MemFree(m_aucSysMem1, buf);
65 }
66 return ret;
67 }
68
OsPerfOutputFlush(VOID)69 VOID OsPerfOutputFlush(VOID)
70 {
71 if (g_perfBufFlushHook != NULL) {
72 g_perfBufFlushHook(g_perfOutputCb.ringbuf.fifo, g_perfOutputCb.ringbuf.size);
73 }
74 }
75
OsPerfOutputRead(CHAR * dest,UINT32 size)76 UINT32 OsPerfOutputRead(CHAR *dest, UINT32 size)
77 {
78 OsPerfOutputFlush();
79 return LOS_CirBufRead(&g_perfOutputCb.ringbuf, dest, size);
80 }
81
OsPerfOutputBegin(UINT32 size)82 STATIC BOOL OsPerfOutputBegin(UINT32 size)
83 {
84 if (g_perfOutputCb.ringbuf.remain < size) {
85 PRINT_INFO("perf buf has no enough space for 0x%x\n", size);
86 return FALSE;
87 }
88 return TRUE;
89 }
90
OsPerfOutputEnd(VOID)91 STATIC VOID OsPerfOutputEnd(VOID)
92 {
93 OsPerfOutputFlush();
94 if (LOS_CirBufUsedSize(&g_perfOutputCb.ringbuf) >= g_perfOutputCb.waterMark) {
95 if (g_perfBufNotifyHook != NULL) {
96 g_perfBufNotifyHook();
97 }
98 }
99 }
100
OsPerfOutputWrite(CHAR * data,UINT32 size)101 UINT32 OsPerfOutputWrite(CHAR *data, UINT32 size)
102 {
103 if (!OsPerfOutputBegin(size)) {
104 return LOS_NOK;
105 }
106
107 LOS_CirBufWrite(&g_perfOutputCb.ringbuf, data, size);
108
109 OsPerfOutputEnd();
110 return LOS_OK;
111 }
112
OsPerfOutputInfo(VOID)113 VOID OsPerfOutputInfo(VOID)
114 {
115 PRINT_EMG("dump perf data, addr: %p length: %#x\n", g_perfOutputCb.ringbuf.fifo, g_perfOutputCb.ringbuf.size);
116 }
117
OsPerfNotifyHookReg(const PERF_BUF_NOTIFY_HOOK func)118 VOID OsPerfNotifyHookReg(const PERF_BUF_NOTIFY_HOOK func)
119 {
120 g_perfBufNotifyHook = func;
121 }
122
OsPerfFlushHookReg(const PERF_BUF_FLUSH_HOOK func)123 VOID OsPerfFlushHookReg(const PERF_BUF_FLUSH_HOOK func)
124 {
125 g_perfBufFlushHook = func;
126 }
127