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 "los_cpup_pri.h"
33 #include "los_base.h"
34 #include "los_init.h"
35 #include "los_process_pri.h"
36 #include "los_swtmr.h"
37
38
39 #ifdef LOSCFG_KERNEL_CPUP
40
41 LITE_OS_SEC_BSS STATIC UINT16 cpupSwtmrID;
42 LITE_OS_SEC_BSS STATIC UINT16 cpupInitFlg = 0;
43 LITE_OS_SEC_BSS OsIrqCpupCB *g_irqCpup = NULL;
44 LITE_OS_SEC_BSS STATIC UINT32 cpupMaxNum;
45 LITE_OS_SEC_BSS STATIC UINT16 cpupHisPos = 0; /* current Sampling point of historyTime */
46 LITE_OS_SEC_BSS STATIC UINT64 cpuHistoryTime[OS_CPUP_HISTORY_RECORD_NUM + 1];
47 LITE_OS_SEC_BSS STATIC UINT32 runningTasks[LOSCFG_KERNEL_CORE_NUM];
48 LITE_OS_SEC_BSS STATIC UINT64 cpupStartCycles = 0;
49 #ifdef LOSCFG_CPUP_INCLUDE_IRQ
50 LITE_OS_SEC_BSS UINT64 timeInIrqSwitch[LOSCFG_KERNEL_CORE_NUM];
51 LITE_OS_SEC_BSS STATIC UINT64 cpupIntTimeStart[LOSCFG_KERNEL_CORE_NUM];
52 #endif
53
54 #define INVALID_ID ((UINT32)-1)
55
56 #define OS_CPUP_UNUSED 0x0U
57 #define OS_CPUP_USED 0x1U
58 #define HIGH_BITS 32
59
60 #define CPUP_PRE_POS(pos) (((pos) == 0) ? (OS_CPUP_HISTORY_RECORD_NUM - 1) : ((pos) - 1))
61 #define CPUP_POST_POS(pos) (((pos) == (OS_CPUP_HISTORY_RECORD_NUM - 1)) ? 0 : ((pos) + 1))
62
OsGetCpuCycle(VOID)63 STATIC UINT64 OsGetCpuCycle(VOID)
64 {
65 UINT32 high;
66 UINT32 low;
67 UINT64 cycles;
68
69 LOS_GetCpuCycle(&high, &low);
70 cycles = ((UINT64)high << HIGH_BITS) + low;
71 if (cpupStartCycles == 0) {
72 cpupStartCycles = cycles;
73 }
74
75 /*
76 * The cycles should keep growing, if the checking failed,
77 * it mean LOS_GetCpuCycle has the problem which should be fixed.
78 */
79 LOS_ASSERT(cycles >= cpupStartCycles);
80
81 return (cycles - cpupStartCycles);
82 }
83
OsCpupGuard(VOID)84 LITE_OS_SEC_TEXT_INIT VOID OsCpupGuard(VOID)
85 {
86 UINT16 prevPos;
87 UINT32 loop;
88 UINT32 runTaskID;
89 UINT32 intSave;
90 UINT64 cycle, cycleIncrement;
91 LosTaskCB *taskCB = NULL;
92 LosProcessCB *processCB = NULL;
93
94 SCHEDULER_LOCK(intSave);
95
96 cycle = OsGetCpuCycle();
97 prevPos = cpupHisPos;
98 cpupHisPos = CPUP_POST_POS(cpupHisPos);
99 cpuHistoryTime[prevPos] = cycle;
100
101 #ifdef LOSCFG_CPUP_INCLUDE_IRQ
102 for (loop = 0; loop < cpupMaxNum; loop++) {
103 if (g_irqCpup[loop].status == OS_CPUP_UNUSED) {
104 continue;
105 }
106 g_irqCpup[loop].cpup.historyTime[prevPos] = g_irqCpup[loop].cpup.allTime;
107 }
108 #endif
109
110 for (loop = 0; loop < g_processMaxNum; loop++) {
111 processCB = OS_PCB_FROM_PID(loop);
112 if (processCB->processCpup == NULL) {
113 continue;
114 }
115 processCB->processCpup->historyTime[prevPos] = processCB->processCpup->allTime;
116 }
117
118 for (loop = 0; loop < g_taskMaxNum; loop++) {
119 taskCB = OS_TCB_FROM_TID(loop);
120 taskCB->taskCpup.historyTime[prevPos] = taskCB->taskCpup.allTime;
121 }
122
123 for (loop = 0; loop < LOSCFG_KERNEL_CORE_NUM; loop++) {
124 runTaskID = runningTasks[loop];
125 if (runTaskID == INVALID_ID) {
126 continue;
127 }
128 taskCB = OS_TCB_FROM_TID(runTaskID);
129
130 /* reacquire the cycle to prevent flip */
131 cycle = OsGetCpuCycle();
132 cycleIncrement = cycle - taskCB->taskCpup.startTime;
133 #ifdef LOSCFG_CPUP_INCLUDE_IRQ
134 cycleIncrement -= timeInIrqSwitch[loop];
135 #endif
136 taskCB->taskCpup.historyTime[prevPos] += cycleIncrement;
137 processCB = OS_PCB_FROM_PID(taskCB->processID);
138 processCB->processCpup->historyTime[prevPos] += cycleIncrement;
139 }
140
141 SCHEDULER_UNLOCK(intSave);
142 }
143
OsCpupGuardCreator(VOID)144 LITE_OS_SEC_TEXT_INIT UINT32 OsCpupGuardCreator(VOID)
145 {
146 (VOID)LOS_SwtmrCreate(LOSCFG_BASE_CORE_TICK_PER_SECOND, LOS_SWTMR_MODE_PERIOD,
147 (SWTMR_PROC_FUNC)OsCpupGuard, &cpupSwtmrID, 0);
148
149 (VOID)LOS_SwtmrStart(cpupSwtmrID);
150
151 return LOS_OK;
152 }
153
154 LOS_MODULE_INIT(OsCpupGuardCreator, LOS_INIT_LEVEL_KMOD_TASK);
155
156 /*
157 * Description: initialization of CPUP
158 * Return : LOS_OK or Error Information
159 */
OsCpupInit(VOID)160 LITE_OS_SEC_TEXT_INIT UINT32 OsCpupInit(VOID)
161 {
162 UINT16 loop;
163 #ifdef LOSCFG_CPUP_INCLUDE_IRQ
164 UINT32 size;
165
166 cpupMaxNum = OS_HWI_MAX_NUM * LOSCFG_KERNEL_CORE_NUM;
167
168 /* every process has only one record, and it won't operated at the same time */
169 size = cpupMaxNum * sizeof(OsIrqCpupCB);
170 g_irqCpup = (OsIrqCpupCB *)LOS_MemAlloc(m_aucSysMem0, size);
171 if (g_irqCpup == NULL) {
172 PRINT_ERR("OsCpupInit error\n");
173 return LOS_ERRNO_CPUP_NO_MEMORY;
174 }
175
176 (VOID)memset_s(g_irqCpup, size, 0, size);
177 #endif
178
179 for (loop = 0; loop < LOSCFG_KERNEL_CORE_NUM; loop++) {
180 runningTasks[loop] = INVALID_ID;
181 }
182 cpupInitFlg = 1;
183 return LOS_OK;
184 }
185
186 LOS_MODULE_INIT(OsCpupInit, LOS_INIT_LEVEL_KMOD_EXTENDED);
187
OsResetCpup(OsCpupBase * cpup,UINT64 cycle)188 STATIC VOID OsResetCpup(OsCpupBase *cpup, UINT64 cycle)
189 {
190 UINT16 loop;
191
192 cpup->startTime = cycle;
193 cpup->allTime = cycle;
194 for (loop = 0; loop < (OS_CPUP_HISTORY_RECORD_NUM + 1); loop++) {
195 cpup->historyTime[loop] = cycle;
196 }
197 }
198
LOS_CpupReset(VOID)199 LITE_OS_SEC_TEXT_INIT VOID LOS_CpupReset(VOID)
200 {
201 LosProcessCB *processCB = NULL;
202 LosTaskCB *taskCB = NULL;
203 UINT32 index;
204 UINT64 cycle;
205 UINT32 intSave;
206
207 cpupInitFlg = 0;
208 intSave = LOS_IntLock();
209 (VOID)LOS_SwtmrStop(cpupSwtmrID);
210 cycle = OsGetCpuCycle();
211
212 for (index = 0; index < (OS_CPUP_HISTORY_RECORD_NUM + 1); index++) {
213 cpuHistoryTime[index] = cycle;
214 }
215
216 for (index = 0; index < g_processMaxNum; index++) {
217 processCB = OS_PCB_FROM_PID(index);
218 if (processCB->processCpup == NULL) {
219 continue;
220 }
221 OsResetCpup(processCB->processCpup, cycle);
222 }
223
224 for (index = 0; index < g_taskMaxNum; index++) {
225 taskCB = OS_TCB_FROM_TID(index);
226 OsResetCpup(&taskCB->taskCpup, cycle);
227 }
228
229 #ifdef LOSCFG_CPUP_INCLUDE_IRQ
230 if (g_irqCpup != NULL) {
231 for (index = 0; index < cpupMaxNum; index++) {
232 OsResetCpup(&g_irqCpup[index].cpup, cycle);
233 g_irqCpup[index].timeMax = 0;
234 }
235
236 for (index = 0; index < LOSCFG_KERNEL_CORE_NUM; index++) {
237 timeInIrqSwitch[index] = 0;
238 }
239 }
240 #endif
241
242 (VOID)LOS_SwtmrStart(cpupSwtmrID);
243 LOS_IntRestore(intSave);
244 cpupInitFlg = 1;
245
246 return;
247 }
248
OsCpupCycleEndStart(UINT32 runTaskID,UINT32 newTaskID)249 VOID OsCpupCycleEndStart(UINT32 runTaskID, UINT32 newTaskID)
250 {
251 /* OsCurrTaskGet and OsCurrProcessGet are not allowed to be called. */
252 LosTaskCB *runTask = OS_TCB_FROM_TID(runTaskID);
253 OsCpupBase *runTaskCpup = &runTask->taskCpup;
254 OsCpupBase *newTaskCpup = (OsCpupBase *)&(OS_TCB_FROM_TID(newTaskID)->taskCpup);
255 OsCpupBase *processCpup = OS_PCB_FROM_PID(runTask->processID)->processCpup;
256 UINT64 cpuCycle, cycleIncrement;
257 UINT16 cpuid = ArchCurrCpuid();
258
259 if (cpupInitFlg == 0) {
260 return;
261 }
262
263 cpuCycle = OsGetCpuCycle();
264 if (runTaskCpup->startTime != 0) {
265 cycleIncrement = cpuCycle - runTaskCpup->startTime;
266 #ifdef LOSCFG_CPUP_INCLUDE_IRQ
267 cycleIncrement -= timeInIrqSwitch[cpuid];
268 timeInIrqSwitch[cpuid] = 0;
269 #endif
270 runTaskCpup->allTime += cycleIncrement;
271 if (processCpup != NULL) {
272 processCpup->allTime += cycleIncrement;
273 }
274 runTaskCpup->startTime = 0;
275 }
276
277 newTaskCpup->startTime = cpuCycle;
278 runningTasks[cpuid] = newTaskID;
279 }
280
OsCpupGetPos(UINT16 mode,UINT16 * curPosPointer,UINT16 * prePosPointer)281 LITE_OS_SEC_TEXT_MINOR STATIC VOID OsCpupGetPos(UINT16 mode, UINT16 *curPosPointer, UINT16 *prePosPointer)
282 {
283 UINT16 curPos;
284 UINT16 tmpPos;
285 UINT16 prePos;
286
287 tmpPos = cpupHisPos;
288 curPos = CPUP_PRE_POS(tmpPos);
289
290 /*
291 * The current position has nothing to do with the CPUP modes,
292 * however, the previous position differs.
293 */
294 switch (mode) {
295 case CPUP_LAST_ONE_SECONDS:
296 prePos = CPUP_PRE_POS(curPos);
297 break;
298 case CPUP_LAST_TEN_SECONDS:
299 prePos = tmpPos;
300 break;
301 case CPUP_ALL_TIME:
302 /* fall-through */
303 default:
304 prePos = OS_CPUP_HISTORY_RECORD_NUM;
305 break;
306 }
307
308 *curPosPointer = curPos;
309 *prePosPointer = prePos;
310
311 return;
312 }
313
OsCalculateCpupUsage(const OsCpupBase * cpup,UINT16 pos,UINT16 prePos,UINT64 allCycle)314 STATIC INLINE UINT32 OsCalculateCpupUsage(const OsCpupBase *cpup, UINT16 pos, UINT16 prePos, UINT64 allCycle)
315 {
316 UINT32 usage = 0;
317 UINT64 cpuCycle = cpup->historyTime[pos] - cpup->historyTime[prePos];
318
319 if (allCycle) {
320 usage = (UINT32)((LOS_CPUP_SINGLE_CORE_PRECISION * cpuCycle) / allCycle);
321 }
322 return usage;
323 }
324
OsHistorySysCpuUsageUnsafe(UINT16 mode)325 STATIC UINT32 OsHistorySysCpuUsageUnsafe(UINT16 mode)
326 {
327 UINT64 cpuAllCycle;
328 UINT16 pos;
329 UINT16 prePos;
330 UINT32 idleProcessID;
331 OsCpupBase *processCpup = NULL;
332
333 if (cpupInitFlg == 0) {
334 return LOS_ERRNO_CPUP_NO_INIT;
335 }
336
337 OsCpupGetPos(mode, &pos, &prePos);
338 cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
339
340 idleProcessID = OsGetIdleProcessID();
341 processCpup = OS_PCB_FROM_PID(idleProcessID)->processCpup;
342 return (LOS_CPUP_PRECISION - OsCalculateCpupUsage(processCpup, pos, prePos, cpuAllCycle));
343 }
344
LOS_HistorySysCpuUsage(UINT16 mode)345 LITE_OS_SEC_TEXT_MINOR UINT32 LOS_HistorySysCpuUsage(UINT16 mode)
346 {
347 UINT32 cpup;
348 UINT32 intSave;
349
350 /* get end time of current process */
351 SCHEDULER_LOCK(intSave);
352 cpup = OsHistorySysCpuUsageUnsafe(mode);
353 SCHEDULER_UNLOCK(intSave);
354 return cpup;
355 }
356
OsHistoryProcessCpuUsageUnsafe(UINT32 pid,UINT16 mode)357 STATIC UINT32 OsHistoryProcessCpuUsageUnsafe(UINT32 pid, UINT16 mode)
358 {
359 LosProcessCB *processCB = NULL;
360 UINT64 cpuAllCycle;
361 UINT16 pos, prePos;
362
363 if (cpupInitFlg == 0) {
364 return LOS_ERRNO_CPUP_NO_INIT;
365 }
366
367 if (OS_PID_CHECK_INVALID(pid)) {
368 return LOS_ERRNO_CPUP_ID_INVALID;
369 }
370
371 processCB = OS_PCB_FROM_PID(pid);
372 if (OsProcessIsUnused(processCB)) {
373 return LOS_ERRNO_CPUP_NO_CREATED;
374 }
375
376 if (processCB->processCpup == NULL) {
377 return LOS_ERRNO_CPUP_ID_INVALID;
378 }
379
380 OsCpupGetPos(mode, &pos, &prePos);
381 cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
382
383 return OsCalculateCpupUsage(processCB->processCpup, pos, prePos, cpuAllCycle);
384 }
385
LOS_HistoryProcessCpuUsage(UINT32 pid,UINT16 mode)386 LITE_OS_SEC_TEXT_MINOR UINT32 LOS_HistoryProcessCpuUsage(UINT32 pid, UINT16 mode)
387 {
388 UINT32 cpup;
389 UINT32 intSave;
390
391 SCHEDULER_LOCK(intSave);
392 cpup = OsHistoryProcessCpuUsageUnsafe(pid, mode);
393 SCHEDULER_UNLOCK(intSave);
394 return cpup;
395 }
396
OsHistoryTaskCpuUsageUnsafe(UINT32 tid,UINT16 mode)397 STATIC UINT32 OsHistoryTaskCpuUsageUnsafe(UINT32 tid, UINT16 mode)
398 {
399 LosTaskCB *taskCB = NULL;
400 UINT64 cpuAllCycle;
401 UINT16 pos, prePos;
402
403 if (cpupInitFlg == 0) {
404 return LOS_ERRNO_CPUP_NO_INIT;
405 }
406
407 if (OS_TID_CHECK_INVALID(tid)) {
408 return LOS_ERRNO_CPUP_ID_INVALID;
409 }
410
411 taskCB = OS_TCB_FROM_TID(tid);
412 if (OsTaskIsUnused(taskCB)) {
413 return LOS_ERRNO_CPUP_NO_CREATED;
414 }
415
416 OsCpupGetPos(mode, &pos, &prePos);
417 cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
418
419 return OsCalculateCpupUsage(&taskCB->taskCpup, pos, prePos, cpuAllCycle);
420 }
421
LOS_HistoryTaskCpuUsage(UINT32 tid,UINT16 mode)422 LITE_OS_SEC_TEXT_MINOR UINT32 LOS_HistoryTaskCpuUsage(UINT32 tid, UINT16 mode)
423 {
424 UINT32 intSave;
425 UINT32 cpup;
426
427 SCHEDULER_LOCK(intSave);
428 cpup = OsHistoryTaskCpuUsageUnsafe(tid, mode);
429 SCHEDULER_UNLOCK(intSave);
430 return cpup;
431 }
432
OsCpupUsageParamCheckAndReset(CPUP_INFO_S * cpupInfo,UINT32 len,UINT32 number)433 STATIC UINT32 OsCpupUsageParamCheckAndReset(CPUP_INFO_S *cpupInfo, UINT32 len, UINT32 number)
434 {
435 if (cpupInitFlg == 0) {
436 return LOS_ERRNO_CPUP_NO_INIT;
437 }
438
439 if ((cpupInfo == NULL) || (len < (sizeof(CPUP_INFO_S) * number))) {
440 return LOS_ERRNO_CPUP_PTR_ERR;
441 }
442
443 (VOID)memset_s(cpupInfo, len, 0, len);
444 return LOS_OK;
445 }
446
OsGetAllProcessCpuUsageUnsafe(UINT16 mode,CPUP_INFO_S * cpupInfo,UINT32 len)447 LITE_OS_SEC_TEXT_MINOR UINT32 OsGetAllProcessCpuUsageUnsafe(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
448 {
449 LosProcessCB *processCB = NULL;
450 UINT64 cpuAllCycle;
451 UINT16 pos, prePos;
452 UINT32 processID;
453 UINT32 ret;
454
455 ret = OsCpupUsageParamCheckAndReset(cpupInfo, len, g_processMaxNum);
456 if (ret != LOS_OK) {
457 return ret;
458 }
459
460 OsCpupGetPos(mode, &pos, &prePos);
461 cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
462
463 for (processID = 0; processID < g_processMaxNum; processID++) {
464 processCB = OS_PCB_FROM_PID(processID);
465 if (OsProcessIsUnused(processCB) || (processCB->processCpup == NULL)) {
466 continue;
467 }
468
469 cpupInfo[processID].usage = OsCalculateCpupUsage(processCB->processCpup, pos, prePos, cpuAllCycle);
470 cpupInfo[processID].status = OS_CPUP_USED;
471 }
472
473 return LOS_OK;
474 }
475
LOS_GetAllProcessCpuUsage(UINT16 mode,CPUP_INFO_S * cpupInfo,UINT32 len)476 LITE_OS_SEC_TEXT_MINOR UINT32 LOS_GetAllProcessCpuUsage(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
477 {
478 UINT32 intSave;
479 UINT32 ret;
480
481 SCHEDULER_LOCK(intSave);
482 ret = OsGetAllProcessCpuUsageUnsafe(mode, cpupInfo, len);
483 SCHEDULER_UNLOCK(intSave);
484 return ret;
485 }
486
OsGetAllProcessAndTaskCpuUsageUnsafe(UINT16 mode,CPUP_INFO_S * cpupInfo,UINT32 len)487 LITE_OS_SEC_TEXT_MINOR UINT32 OsGetAllProcessAndTaskCpuUsageUnsafe(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
488 {
489 UINT64 cpuAllCycle;
490 UINT16 pos, prePos;
491 UINT32 taskID;
492 UINT32 ret;
493 LosTaskCB *taskCB = NULL;
494 OsCpupBase *processCpupBase = NULL;
495 CPUP_INFO_S *processCpup = cpupInfo;
496 CPUP_INFO_S *taskCpup = (CPUP_INFO_S *)((UINTPTR)cpupInfo + sizeof(CPUP_INFO_S) * g_processMaxNum);
497
498 ret = OsCpupUsageParamCheckAndReset(cpupInfo, len, g_taskMaxNum + g_processMaxNum);
499 if (ret != LOS_OK) {
500 return ret;
501 }
502
503 OsCpupGetPos(mode, &pos, &prePos);
504 cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
505
506 for (taskID = 0; taskID < g_taskMaxNum; taskID++) {
507 taskCB = OS_TCB_FROM_TID(taskID);
508 if (OsTaskIsUnused(taskCB)) {
509 continue;
510 }
511
512 taskCpup[taskID].usage = OsCalculateCpupUsage(&taskCB->taskCpup, pos, prePos, cpuAllCycle);
513 taskCpup[taskID].status = OS_CPUP_USED;
514 if (processCpup[taskCB->processID].status == OS_CPUP_UNUSED) {
515 processCpupBase = OS_PCB_FROM_PID(taskCB->processID)->processCpup;
516 if (processCpupBase != NULL) {
517 processCpup[taskCB->processID].usage = OsCalculateCpupUsage(processCpupBase, pos, prePos, cpuAllCycle);
518 processCpup[taskCB->processID].status = OS_CPUP_USED;
519 }
520 }
521 }
522
523 return LOS_OK;
524 }
525
526 #ifdef LOSCFG_CPUP_INCLUDE_IRQ
OsCpupIrqStart(UINT16 cpuid)527 LITE_OS_SEC_TEXT_MINOR VOID OsCpupIrqStart(UINT16 cpuid)
528 {
529 UINT32 high;
530 UINT32 low;
531
532 LOS_GetCpuCycle(&high, &low);
533 cpupIntTimeStart[cpuid] = ((UINT64)high << HIGH_BITS) + low;
534 return;
535 }
536
OsCpupIrqEnd(UINT16 cpuid,UINT32 intNum)537 LITE_OS_SEC_TEXT_MINOR VOID OsCpupIrqEnd(UINT16 cpuid, UINT32 intNum)
538 {
539 UINT32 high;
540 UINT32 low;
541 UINT64 intTimeEnd;
542 UINT64 usedTime;
543
544 LOS_GetCpuCycle(&high, &low);
545 intTimeEnd = ((UINT64)high << HIGH_BITS) + low;
546 OsIrqCpupCB *irqCb = &g_irqCpup[(intNum * LOSCFG_KERNEL_CORE_NUM) + cpuid];
547 irqCb->id = intNum;
548 irqCb->status = OS_CPUP_USED;
549 usedTime = intTimeEnd - cpupIntTimeStart[cpuid];
550 timeInIrqSwitch[cpuid] += usedTime;
551 irqCb->cpup.allTime += usedTime;
552 if (irqCb->count <= 100) { /* Take 100 samples */
553 irqCb->allTime += usedTime;
554 irqCb->count++;
555 } else {
556 irqCb->allTime = 0;
557 irqCb->count = 0;
558 }
559 if (usedTime > irqCb->timeMax) {
560 irqCb->timeMax = usedTime;
561 }
562 return;
563 }
564
OsGetIrqCpupArrayBase(VOID)565 LITE_OS_SEC_TEXT_MINOR OsIrqCpupCB *OsGetIrqCpupArrayBase(VOID)
566 {
567 return g_irqCpup;
568 }
569
OsGetAllIrqCpuUsageUnsafe(UINT16 mode,CPUP_INFO_S * cpupInfo,UINT32 len)570 LITE_OS_SEC_TEXT_MINOR UINT32 OsGetAllIrqCpuUsageUnsafe(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
571 {
572 UINT16 pos, prePos;
573 UINT64 cpuAllCycle;
574 UINT32 loop;
575 UINT32 ret;
576
577 ret = OsCpupUsageParamCheckAndReset(cpupInfo, len, cpupMaxNum);
578 if (ret != LOS_OK) {
579 return ret;
580 }
581
582 OsCpupGetPos(mode, &pos, &prePos);
583 cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
584
585 for (loop = 0; loop < cpupMaxNum; loop++) {
586 if (g_irqCpup[loop].status == OS_CPUP_UNUSED) {
587 continue;
588 }
589
590 cpupInfo[loop].usage = OsCalculateCpupUsage(&g_irqCpup[loop].cpup, pos, prePos, cpuAllCycle);
591 cpupInfo[loop].status = g_irqCpup[loop].status;
592 }
593
594 return LOS_OK;
595 }
596
LOS_GetAllIrqCpuUsage(UINT16 mode,CPUP_INFO_S * cpupInfo,UINT32 len)597 LITE_OS_SEC_TEXT_MINOR UINT32 LOS_GetAllIrqCpuUsage(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
598 {
599 UINT32 intSave;
600 UINT32 ret;
601
602 SCHEDULER_LOCK(intSave);
603 ret = OsGetAllIrqCpuUsageUnsafe(mode, cpupInfo, len);
604 SCHEDULER_UNLOCK(intSave);
605 return ret;
606 }
607 #endif
608
609 #endif /* LOSCFG_KERNEL_CPUP */
610