1 /*
2 *
3 * Copyright 2013 Rockchip Electronics Co., LTD.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /*
19 * @file Rockchip_OSAL_ETC.c
20 * @brief
21 * @author csy(csy@rock-chips.com)
22 * @version 1.0.0
23 * @history
24 * 2013.11.26 : Create
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <securec.h>
31 #include <sys/time.h>
32
33 #include "Rockchip_OSAL_Memory.h"
34 #include "Rockchip_OSAL_Log.h"
35 #include "Rockchip_OSAL_ETC.h"
36
37 static struct timeval perfStart[PERF_ID_MAX + 1], perfStop[PERF_ID_MAX + 1];
38 static unsigned long perfTime[PERF_ID_MAX + 1], totalPerfTime[PERF_ID_MAX + 1];
39 static unsigned int perfFrameCount[PERF_ID_MAX + 1], perfOver30ms[PERF_ID_MAX + 1];
40
41 #ifndef HAVE_GETLINE
getline(char ** ppLine,size_t * pLen,FILE * pStream)42 ssize_t getline(char **ppLine, size_t *pLen, FILE *pStream)
43 {
44 char *pCurrentPointer = NULL;
45 size_t const chunk = 512;
46
47 size_t defaultBufferSize = chunk + 1;
48 size_t retSize = 0;
49
50 if (*ppLine == NULL) {
51 *ppLine = (char *)malloc(defaultBufferSize);
52 if (*ppLine == NULL) {
53 retSize = -1;
54 goto EXIT;
55 }
56 *pLen = defaultBufferSize;
57 } else {
58 if (*pLen < defaultBufferSize) {
59 *ppLine = (char *)realloc(*ppLine, defaultBufferSize);
60 if (*ppLine == NULL) {
61 retSize = -1;
62 goto EXIT;
63 }
64 *pLen = defaultBufferSize;
65 }
66 }
67
68 while (1) {
69 size_t i;
70 size_t j = 0;
71 size_t readByte = 0;
72
73 pCurrentPointer = *ppLine + readByte;
74
75 i = fread(pCurrentPointer, 1, chunk, pStream);
76 if (i < chunk && ferror(pStream)) {
77 retSize = -1;
78 goto EXIT;
79 }
80 while (j < i) {
81 ++j;
82 if (*pCurrentPointer++ == (char)'\n') {
83 *pCurrentPointer = '\0';
84 if (j != i) {
85 if (fseek(pStream, j - i, SEEK_CUR)) {
86 retSize = -1;
87 goto EXIT;
88 }
89 if (feof(pStream))
90 clearerr(pStream);
91 }
92 readByte += j;
93 retSize = readByte;
94 goto EXIT;
95 }
96 }
97
98 readByte += j;
99 if (feof(pStream)) {
100 if (readByte) {
101 retSize = readByte;
102 goto EXIT;
103 }
104 if (!i) {
105 retSize = -1;
106 goto EXIT;
107 }
108 }
109
110 i = ((readByte + (chunk * 2)) / chunk) * chunk; // 2:byte alignment
111 if (i != *pLen) {
112 *ppLine = (char *)realloc(*ppLine, i);
113 if (*ppLine == NULL) {
114 retSize = -1;
115 goto EXIT;
116 }
117 *pLen = i;
118 }
119 }
120
121 EXIT:
122 return retSize;
123 }
124 #endif /* HAVE_GETLINE */
125
Rockchip_OSAL_Strcpy(OMX_PTR dest,OMX_PTR src)126 OMX_S32 Rockchip_OSAL_Strcpy(OMX_PTR dest, OMX_PTR src)
127 {
128 return strcpy_s(dest, strlen(src)+1, src);
129 }
130
Rockchip_OSAL_Strncpy(OMX_PTR dest,OMX_PTR src,size_t num)131 OMX_S32 Rockchip_OSAL_Strncpy(OMX_PTR dest, OMX_PTR src, size_t num)
132 {
133 return strncpy_s(dest, num, src, strlen(src)+1);
134 }
135
Rockchip_OSAL_Strcmp(OMX_PTR str1,OMX_PTR str2)136 OMX_S32 Rockchip_OSAL_Strcmp(OMX_PTR str1, OMX_PTR str2)
137 {
138 return strcmp(str1, str2);
139 }
140
Rockchip_OSAL_Strncmp(OMX_PTR str1,OMX_PTR str2,size_t num)141 OMX_S32 Rockchip_OSAL_Strncmp(OMX_PTR str1, OMX_PTR str2, size_t num)
142 {
143 return strncmp(str1, str2, num);
144 }
145
Rockchip_OSAL_Strcat(OMX_PTR dest,OMX_PTR src)146 OMX_S32 Rockchip_OSAL_Strcat(OMX_PTR dest, OMX_PTR src)
147 {
148 return strcat_s(dest, strlen(src)+1, src);
149 }
150
Rockchip_OSAL_Strncat(OMX_PTR dest,OMX_PTR src,size_t num)151 OMX_S32 Rockchip_OSAL_Strncat(OMX_PTR dest, OMX_PTR src, size_t num)
152 {
153 return strncat_s(dest, num, src, strlen(src)+1);
154 }
155
Rockchip_OSAL_Strlen(const char * str)156 size_t Rockchip_OSAL_Strlen(const char *str)
157 {
158 return strlen(str);
159 }
160
MeasureTime(struct timeval * start,struct timeval * stop)161 static OMX_U32 MeasureTime(struct timeval *start, struct timeval *stop)
162 {
163 unsigned long sec, usec, time;
164
165 sec = stop->tv_sec - start->tv_sec;
166 if (stop->tv_usec >= start->tv_usec) {
167 usec = stop->tv_usec - start->tv_usec;
168 } else {
169 usec = stop->tv_usec + 1000000 - start->tv_usec; // 1000000:time shift
170 sec--;
171 }
172
173 time = sec * 1000000 + (usec); // 1000000:time shift
174
175 return time;
176 }
177
Rockchip_OSAL_PerfInit(PERF_ID_TYPE id)178 void Rockchip_OSAL_PerfInit(PERF_ID_TYPE id)
179 {
180 memset_s(&perfStart[id], sizeof(perfStart[id]), 0, sizeof(perfStart[id]));
181 memset_s(&perfStop[id], sizeof(perfStart[id]), 0, sizeof(perfStop[id]));
182 perfTime[id] = 0;
183 totalPerfTime[id] = 0;
184 perfFrameCount[id] = 0;
185 perfOver30ms[id] = 0;
186 }
187
Rockchip_OSAL_PerfStart(PERF_ID_TYPE id)188 void Rockchip_OSAL_PerfStart(PERF_ID_TYPE id)
189 {
190 gettimeofday(&perfStart[id], NULL);
191 }
192
Rockchip_OSAL_PerfStop(PERF_ID_TYPE id)193 void Rockchip_OSAL_PerfStop(PERF_ID_TYPE id)
194 {
195 gettimeofday(&perfStop[id], NULL);
196
197 perfTime[id] = MeasureTime(&perfStart[id], &perfStop[id]);
198 totalPerfTime[id] += perfTime[id];
199 perfFrameCount[id]++;
200
201 if (perfTime[id] > 30000) { // 30000:time
202 perfOver30ms[id]++;
203 }
204 }
205
Rockchip_OSAL_PerfFrame(PERF_ID_TYPE id)206 OMX_U32 Rockchip_OSAL_PerfFrame(PERF_ID_TYPE id)
207 {
208 return perfTime[id];
209 }
210
Rockchip_OSAL_PerfTotal(PERF_ID_TYPE id)211 OMX_U32 Rockchip_OSAL_PerfTotal(PERF_ID_TYPE id)
212 {
213 return totalPerfTime[id];
214 }
215
Rockchip_OSAL_PerfFrameCount(PERF_ID_TYPE id)216 OMX_U32 Rockchip_OSAL_PerfFrameCount(PERF_ID_TYPE id)
217 {
218 return perfFrameCount[id];
219 }
220
Rockchip_OSAL_PerfOver30ms(PERF_ID_TYPE id)221 int Rockchip_OSAL_PerfOver30ms(PERF_ID_TYPE id)
222 {
223 return perfOver30ms[id];
224 }
225
Rockchip_OSAL_PerfPrint(OMX_STRING prefix,PERF_ID_TYPE id)226 void Rockchip_OSAL_PerfPrint(OMX_STRING prefix, PERF_ID_TYPE id)
227 {
228 OMX_U32 perfTotal;
229 int frameCount;
230
231 frameCount = Rockchip_OSAL_PerfFrameCount(id);
232 perfTotal = Rockchip_OSAL_PerfTotal(id);
233
234 omx_info("%s Frame Count: %d", prefix, frameCount);
235 omx_info("%s Avg Time: %.2f ms, Over 30ms: %d",
236 prefix, (float)perfTotal / (float)(frameCount * 1000), Rockchip_OSAL_PerfOver30ms(id)); // 1000:time shift
237 }