1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - OS - include
3   File:     functionCost.h
4 
5   Copyright 2003-2008 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 #ifndef NITRO_OS_FUNCTIONCOST_H_
18 #define NITRO_OS_FUNCTIONCOST_H_
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #include <nitro/os/common/profile.h>
25 
26 //================================================================================
27 //---- define if consider thread interval
28 
29 #if defined(OS_PROFILE_AVAILABLE) && defined(OS_PROFILE_FUNCTION_COST)
30 #ifndef OS_NO_FUNCTIONCOST
31 #define OSi_FUNCTIONCOST_THREAD_INTERVAL
32 #endif
33 #endif
34 
35 //================================================================================
36 //---------------- functionCost structure
37 
38 #define OSi_FUNCTIONCOST_EXIT_TAG   0xffffffff
39 
40 typedef union
41 {
42     struct
43     {
44         u32     name;                  // pointer to function name
45         u32     time;                  // time for __PROFILE_ENTRY
46 #ifdef OSi_FUNCTIONCOST_THREAD_INTERVAL
47         u32     interval;
48 #endif
49     }
50     entry;
51     struct
52     {
53         u32     tag;                   // tag(0xffffffff)
54         u32     time;                  // time for __PROFILE_EXIT
55 #ifdef OSi_FUNCTIONCOST_THREAD_INTERVAL
56         u32     interval;
57 #endif
58     }
59     exit;
60 }
61 OSFunctionCost;
62 
63 #define OSi_SIZEOF_FUNCTIONCOST      sizeof(OSFunctionCost)
64 
65 //---------------- functionCostInfo structure
66 typedef struct
67 {
68     OSFunctionCost *current;           // current pointer
69     OSFunctionCost *limit;             // buffer limit as hi-address
70     u16     enable;
71     u16     padding;                   // padding
72 #ifdef OSi_FUNCTIONCOST_THREAD_INTERVAL
73     u32     breakTime;
74 #endif
75     OSFunctionCost array[1];           // valiable length
76 
77 }
78 OSFunctionCostInfo;
79 
80 #define OSi_COSTINFO_SIZEOF_HEADERPART (sizeof(OSFunctionCostInfo) - sizeof(OSFunctionCost))
81 
82 //---------------- statstics
83 typedef struct
84 {
85     u32     name;
86     u32     count;
87     u64     time;
88 }
89 OSFunctionCostStatistics;
90 
91 typedef struct
92 {
93     u32     size;
94     OSFunctionCostStatistics *limit;
95     OSFunctionCostStatistics array[1]; // valiable length
96 }
97 OSFunctionCostStatisticsInfo;
98 
99 
100 #define OSi_STATISTICS_SIZEOF_HEADERPART (sizeof(OSFunctionCostStatisticsInfo) - sizeof(OSFunctionCostStatistics))
101 #define OSi_STATISTICS_LEAST_SIZE        (OSi_STATISTICS_SIZEOF_HEADERPART + sizeof(OSFunctionCostStatistics))
102 
103 //================================================================================
104 /*---------------------------------------------------------------------------*
105   Name:         OS_InitFunctionCost
106 
107   Description:  Initialize functionCost buffer,
108                 and start recording.
109 
110   Arguments:    buf    address to buffer for recording entering and exitting function.
111                 size   size of buffer (by byte)
112 
113   Returns:      None
114  *---------------------------------------------------------------------------*/
115 #ifndef OS_NO_FUNCTIONCOST
116 void    OS_InitFunctionCost(void *buf, u32 size);
117 #else
OS_InitFunctionCost(void *,u32)118 static inline void OS_InitFunctionCost(void *, u32)
119 {                                      /* do nothing */
120 }
121 #endif
122 
123 /*---------------------------------------------------------------------------*
124   Name:         OS_CalcFunctionCostLines
125 
126   Description:  calculate number of lines to be able to be allocated
127 
128   Arguments:    size :    FunctionCost buffer size
129                           same as argument of OS_InitFunctionCost()'s size
130 
131   Returns:      number of lines to be able to allocate in 'size' byte
132  *---------------------------------------------------------------------------*/
133 #ifndef OS_NO_FUNCTIONCOST
134 int     OS_CalcFunctionCostLines(u32 size);
135 #else
OS_CalcFunctionCostLines(u32)136 static inline int OS_CalcFunctionCostLines(u32)
137 {
138     return 0;
139 }
140 #endif
141 
142 /*---------------------------------------------------------------------------*
143   Name:         OS_CalcFunctionCostBufferSize
144 
145   Description:  calculate buffer size to allocate specified lines
146 
147   Arguments:    lines :   lines to want to allocate
148 
149   Returns:      buffer size to need
150  *---------------------------------------------------------------------------*/
151 #ifndef OS_NO_FUNCTIONCOST
152 u32     OS_CalcFunctionCostBufferSize(int lines);
153 #else
OS_CalcFunctionCostBufferSize(int)154 static inline u32 OS_CalcFunctionCostBufferSize(int)
155 {
156     return 0;
157 }
158 #endif
159 
160 
161 /*---------------------------------------------------------------------------*
162   Name:         OS_CheckFunctionCostBuffer
163 
164   Description:  check if function cost buffer overflows the limit.
165 
166   Arguments:    buf    address to buffer for recording entering and exitting function.
167 
168   Returns:      FALSE if overflowed. TRUE if not
169  *---------------------------------------------------------------------------*/
170 #ifndef OS_NO_FUNCTIONCOST
171 BOOL    OS_CheckFunctionCostBuffer(void *buf);
172 #else
OS_CheckFunctionCostBuffer(void * buf)173 static inline BOOL OS_CheckFunctionCostBuffer(void *buf)
174 {
175 #pragma unused(buf)
176     return FALSE;
177 }
178 #endif
179 
180 /*---------------------------------------------------------------------------*
181   Name:         OS_ClearFunctionCostBuffer
182 
183   Description:  clear buffer
184 
185   Arguments:    None
186 
187   Returns:      None
188  *---------------------------------------------------------------------------*/
189 #ifndef OS_NO_FUNCTIONCOST
190 extern void OSi_ClearThreadFunctionCostBuffer(OSThread *thread);
OS_ClearFunctionCostBuffer(void)191 static inline void OS_ClearFunctionCostBuffer(void)
192 {
193     OSi_ClearThreadFunctionCostBuffer(NULL);
194 }
195 #else
OS_ClearFunctionCostBuffer(void)196 static inline void OS_ClearFunctionCostBuffer(void)
197 {                                      /* do nothing */
198 }
199 #endif
200 
201 /*---------------------------------------------------------------------------*
202   Name:         OS_EnableFunctionCost
203 
204   Description:  enable functionCost
205 
206   Arguments:    None
207 
208   Returns:      previous status. TRUE if enable
209  *---------------------------------------------------------------------------*/
210 #ifndef OS_NO_FUNCTIONCOST
211 BOOL    OS_EnableFunctionCost();
212 #else
OS_EnableFunctionCost()213 static inline BOOL OS_EnableFunctionCost()
214 {
215     return FALSE;
216 }
217 #endif
218 
219 /*---------------------------------------------------------------------------*
220   Name:         OS_DisableFunctionCost
221 
222   Description:  disble functionCost
223 
224   Arguments:    None
225 
226   Returns:      previous status. TRUE if enable
227  *---------------------------------------------------------------------------*/
228 #ifndef OS_NO_FUNCTIONCOST
229 BOOL    OS_DisableFunctionCost();
230 #else
OS_DisableFunctionCost()231 static inline BOOL OS_DisableFunctionCost()
232 {
233     return FALSE;
234 }
235 #endif
236 
237 /*---------------------------------------------------------------------------*
238   Name:         OS_RestoreFunctionCost
239 
240   Description:  set status of functionCost
241 
242   Arguments:    enable :  TRUE if set to be enable, FALSE if not
243 
244   Returns:      previous status. TRUE if enable
245  *---------------------------------------------------------------------------*/
246 #ifndef OS_NO_FUNCTIONCOST
247 BOOL    OS_RestoreFunctionCost(BOOL enable);
248 #else
OS_RestoreFunctionCost(BOOL)249 static inline BOOL OS_RestoreFunctionCost(BOOL)
250 {
251     return FALSE;
252 }
253 #endif
254 
255 /*---------------------------------------------------------------------------*
256   Name:         OS_InitStatistics
257 
258   Description:  initialize statistics buffer
259 
260   Arguments:    statBuf  pointer to statistics buffer
261                 size     size of statBuf
262 
263   Returns:      None
264  *---------------------------------------------------------------------------*/
265 #ifndef OS_NO_FUNCTIONCOST
266 void    OS_InitStatistics(void *statBuf, u32 size);
267 #else
OS_InitStatistics(void *,u32)268 static inline void OS_InitStatistics(void *, u32)
269 {                                      /* do nothing */
270 }
271 #endif
272 
273 /*---------------------------------------------------------------------------*
274   Name:         OS_CalcThreadStatistics
275 
276   Description:  calculate statistics to statBuf of thread
277 
278   Arguments:    statBuf    pointer to statistics buffer
279                 thread     thread to calculate.
280                            if current or no thread then specify NULL
281 
282   Returns:      None
283  *---------------------------------------------------------------------------*/
284 #ifndef OS_NO_FUNCTIONCOST
285 void    OS_CalcThreadStatistics(void *statBuf, OSThread *thread);
286 #else
OS_CalcThreadStatistics(void *,OSThread *)287 static inline void OS_CalcThreadStatistics(void *, OSThread *)
288 {                                      /* do nothing */
289 }
290 #endif
291 
292 /*---------------------------------------------------------------------------*
293   Name:         OS_CalcStatistics
294 
295   Description:  calculate statistics to statBuf
296 
297   Arguments:    statBuf    pointer to statistics buffer
298 
299   Returns:      None
300  *---------------------------------------------------------------------------*/
OS_CalcStatistics(void * statBuf)301 static inline void OS_CalcStatistics(void *statBuf)
302 {
303     OS_CalcThreadStatistics(statBuf, NULL);
304 }
305 
306 
307 /*---------------------------------------------------------------------------*
308   Name:         OS_CheckStatisticsBuffer
309 
310   Description:  check if statistics buffer overflows the limit.
311 
312   Arguments:    statBuf    pointer to statistics buffer
313 
314   Returns:      FALSE if overflowed. TRUE if not
315  *---------------------------------------------------------------------------*/
316 #ifndef OS_NO_FUNCTIONCOST
317 BOOL    OS_CheckStatisticsBuffer(void *statBuf);
318 #else
OS_CheckStatisticsBuffer(void * statBuf)319 static inline BOOL OS_CheckStatisticsBuffer(void *statBuf)
320 {
321 #pragma unused(statBuf)
322     return FALSE;
323 }
324 #endif
325 
326 /*---------------------------------------------------------------------------*
327   Name:         OS_DumpStatistics
328 
329   Description:  dump statistics of functionCost
330 
331   Arguments:    statBuf  buffer for statistics
332 
333   Returns:      None
334  *---------------------------------------------------------------------------*/
335 #ifndef OS_NO_FUNCTIONCOST
336 void    OS_DumpStatistics(void *statBuf);
337 #else
OS_DumpStatistics(void *)338 static inline void OS_DumpStatistics(void *)
339 {                                      /* do nothing */
340 }
341 #endif
342 
343 
344 //--------------------------------------------------------------------------------
345 #ifdef __cplusplus
346 } /* extern "C" */
347 #endif
348 
349 /* NITRO_OS_FUNCTIONCOST_H_ */
350 #endif
351