• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2023 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_info_pri.h"
37 #include "los_swtmr.h"
38 
39 
40 #ifdef LOSCFG_KERNEL_CPUP
41 
42 LITE_OS_SEC_BSS STATIC UINT16 cpupSwtmrID;
43 LITE_OS_SEC_BSS STATIC UINT16 cpupInitFlg = 0;
44 LITE_OS_SEC_BSS OsIrqCpupCB *g_irqCpup = NULL;
45 LITE_OS_SEC_BSS STATIC UINT32 cpupMaxNum;
46 LITE_OS_SEC_BSS STATIC UINT16 cpupHisPos = 0; /* current Sampling point of historyTime */
47 LITE_OS_SEC_BSS STATIC UINT64 cpuHistoryTime[OS_CPUP_HISTORY_RECORD_NUM + 1];
48 LITE_OS_SEC_BSS STATIC LosTaskCB *runningTasks[LOSCFG_KERNEL_CORE_NUM];
49 LITE_OS_SEC_BSS STATIC UINT64 cpupStartCycles = 0;
50 #ifdef LOSCFG_CPUP_INCLUDE_IRQ
51 LITE_OS_SEC_BSS UINT64 timeInIrqSwitch[LOSCFG_KERNEL_CORE_NUM];
52 LITE_OS_SEC_BSS STATIC UINT64 cpupIntTimeStart[LOSCFG_KERNEL_CORE_NUM];
53 #endif
54 
55 #define INVALID_ID ((UINT32)-1)
56 
57 #define OS_CPUP_UNUSED 0x0U
58 #define OS_CPUP_USED   0x1U
59 #define HIGH_BITS 32
60 
61 #define CPUP_PRE_POS(pos) (((pos) == 0) ? (OS_CPUP_HISTORY_RECORD_NUM - 1) : ((pos) - 1))
62 #define CPUP_POST_POS(pos) (((pos) == (OS_CPUP_HISTORY_RECORD_NUM - 1)) ? 0 : ((pos) + 1))
63 
OsGetCpuCycle(VOID)64 STATIC UINT64 OsGetCpuCycle(VOID)
65 {
66     UINT32 high;
67     UINT32 low;
68     UINT64 cycles;
69 
70     LOS_GetCpuCycle(&high, &low);
71     cycles = ((UINT64)high << HIGH_BITS) + low;
72     if (cpupStartCycles == 0) {
73         cpupStartCycles = cycles;
74     }
75 
76     /*
77      * The cycles should keep growing, if the checking failed,
78      * it mean LOS_GetCpuCycle has the problem which should be fixed.
79      */
80     LOS_ASSERT(cycles >= cpupStartCycles);
81 
82     return (cycles - cpupStartCycles);
83 }
84 
OsCpupGuard(VOID)85 LITE_OS_SEC_TEXT_INIT VOID OsCpupGuard(VOID)
86 {
87     UINT16 prevPos;
88     UINT32 loop;
89     UINT32 intSave;
90     UINT64 cycle, cycleIncrement;
91     LosProcessCB *processCB = NULL;
92 
93     SCHEDULER_LOCK(intSave);
94 
95     cycle = OsGetCpuCycle();
96     prevPos = cpupHisPos;
97     cpupHisPos = CPUP_POST_POS(cpupHisPos);
98     cpuHistoryTime[prevPos] = cycle;
99 
100 #ifdef LOSCFG_CPUP_INCLUDE_IRQ
101     for (loop = 0; loop < cpupMaxNum; loop++) {
102         if (g_irqCpup[loop].status == OS_CPUP_UNUSED) {
103             continue;
104         }
105         g_irqCpup[loop].cpup.historyTime[prevPos] = g_irqCpup[loop].cpup.allTime;
106     }
107 #endif
108 
109     for (loop = 0; loop < g_processMaxNum; loop++) {
110         processCB = OS_PCB_FROM_RPID(loop);
111         if (processCB->processCpup == NULL) {
112             continue;
113         }
114         processCB->processCpup->historyTime[prevPos] = processCB->processCpup->allTime;
115     }
116 
117     for (loop = 0; loop < g_taskMaxNum; loop++) {
118         LosTaskCB *taskCB = OS_TCB_FROM_RTID(loop);
119         taskCB->taskCpup.historyTime[prevPos] = taskCB->taskCpup.allTime;
120     }
121 
122     for (loop = 0; loop < LOSCFG_KERNEL_CORE_NUM; loop++) {
123         LosTaskCB *runTask = runningTasks[loop];
124         if (runTask == NULL) {
125             continue;
126         }
127 
128         /* reacquire the cycle to prevent flip */
129         cycle = OsGetCpuCycle();
130         cycleIncrement = cycle - runTask->taskCpup.startTime;
131 #ifdef LOSCFG_CPUP_INCLUDE_IRQ
132         cycleIncrement -= timeInIrqSwitch[loop];
133 #endif
134         runTask->taskCpup.historyTime[prevPos] += cycleIncrement;
135         processCB = OS_PCB_FROM_TCB(runTask);
136         if (processCB->processCpup != NULL) {
137             processCB->processCpup->historyTime[prevPos] += cycleIncrement;
138         }
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] = NULL;
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(LosTaskCB * runTask,LosTaskCB * newTask)249 VOID OsCpupCycleEndStart(LosTaskCB *runTask, LosTaskCB *newTask)
250 {
251     /* OsCurrTaskGet and OsCurrProcessGet are not allowed to be called. */
252     OsCpupBase *runTaskCpup = &runTask->taskCpup;
253     OsCpupBase *newTaskCpup = &newTask->taskCpup;
254     OsCpupBase *processCpup = OS_PCB_FROM_TCB(runTask)->processCpup;
255     UINT64 cpuCycle, cycleIncrement;
256     UINT16 cpuid = ArchCurrCpuid();
257 
258     if (cpupInitFlg == 0) {
259         return;
260     }
261 
262     cpuCycle = OsGetCpuCycle();
263     if (runTaskCpup->startTime != 0) {
264         cycleIncrement = cpuCycle - runTaskCpup->startTime;
265 #ifdef LOSCFG_CPUP_INCLUDE_IRQ
266         cycleIncrement -= timeInIrqSwitch[cpuid];
267         timeInIrqSwitch[cpuid] = 0;
268 #endif
269         runTaskCpup->allTime += cycleIncrement;
270         if (processCpup != NULL) {
271             processCpup->allTime += cycleIncrement;
272         }
273         runTaskCpup->startTime = 0;
274     }
275 
276     newTaskCpup->startTime = cpuCycle;
277     runningTasks[cpuid] = newTask;
278 }
279 
OsCpupGetPos(UINT16 mode,UINT16 * curPosPointer,UINT16 * prePosPointer)280 LITE_OS_SEC_TEXT_MINOR STATIC VOID OsCpupGetPos(UINT16 mode, UINT16 *curPosPointer, UINT16 *prePosPointer)
281 {
282     UINT16 curPos;
283     UINT16 tmpPos;
284     UINT16 prePos;
285 
286     tmpPos = cpupHisPos;
287     curPos = CPUP_PRE_POS(tmpPos);
288 
289     /*
290      * The current position has nothing to do with the CPUP modes,
291      * however, the previous position differs.
292      */
293     switch (mode) {
294         case CPUP_LAST_ONE_SECONDS:
295             prePos = CPUP_PRE_POS(curPos);
296             break;
297         case CPUP_LAST_TEN_SECONDS:
298             prePos = tmpPos;
299             break;
300         case CPUP_ALL_TIME:
301             /* fall-through */
302         default:
303             prePos = OS_CPUP_HISTORY_RECORD_NUM;
304             break;
305     }
306 
307     *curPosPointer = curPos;
308     *prePosPointer = prePos;
309 
310     return;
311 }
312 
OsCalculateCpupUsage(const OsCpupBase * cpup,UINT16 pos,UINT16 prePos,UINT64 allCycle)313 STATIC INLINE UINT32 OsCalculateCpupUsage(const OsCpupBase *cpup, UINT16 pos, UINT16 prePos, UINT64 allCycle)
314 {
315     UINT32 usage = 0;
316     UINT64 cpuCycle = cpup->historyTime[pos] - cpup->historyTime[prePos];
317 
318     if (allCycle) {
319         usage = (UINT32)((LOS_CPUP_SINGLE_CORE_PRECISION * cpuCycle) / allCycle);
320     }
321     return usage;
322 }
323 
OsHistorySysCpuUsageUnsafe(UINT16 mode)324 STATIC UINT32 OsHistorySysCpuUsageUnsafe(UINT16 mode)
325 {
326     UINT64 cpuAllCycle;
327     UINT16 pos;
328     UINT16 prePos;
329     OsCpupBase *processCpup = NULL;
330 
331     if (cpupInitFlg == 0) {
332         return LOS_ERRNO_CPUP_NO_INIT;
333     }
334 
335     OsCpupGetPos(mode, &pos, &prePos);
336     cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
337 
338     processCpup = OS_PCB_FROM_PID(OS_KERNEL_IDLE_PROCESS_ID)->processCpup;
339     return (LOS_CPUP_PRECISION - OsCalculateCpupUsage(processCpup, pos, prePos, cpuAllCycle));
340 }
341 
LOS_HistorySysCpuUsage(UINT16 mode)342 LITE_OS_SEC_TEXT_MINOR UINT32 LOS_HistorySysCpuUsage(UINT16 mode)
343 {
344     UINT32 cpup;
345     UINT32 intSave;
346 
347     /* get end time of current process */
348     SCHEDULER_LOCK(intSave);
349     cpup = OsHistorySysCpuUsageUnsafe(mode);
350     SCHEDULER_UNLOCK(intSave);
351     return cpup;
352 }
353 
OsHistoryProcessCpuUsageUnsafe(UINT32 pid,UINT16 mode)354 STATIC UINT32 OsHistoryProcessCpuUsageUnsafe(UINT32 pid, UINT16 mode)
355 {
356     LosProcessCB *processCB = NULL;
357     UINT64 cpuAllCycle;
358     UINT16 pos, prePos;
359 
360     if (cpupInitFlg == 0) {
361         return LOS_ERRNO_CPUP_NO_INIT;
362     }
363 
364     if (OS_PID_CHECK_INVALID(pid)) {
365         return LOS_ERRNO_CPUP_ID_INVALID;
366     }
367 
368     processCB = OS_PCB_FROM_PID(pid);
369     if (OsProcessIsUnused(processCB)) {
370         return LOS_ERRNO_CPUP_NO_CREATED;
371     }
372 
373     if (processCB->processCpup == NULL) {
374         return LOS_ERRNO_CPUP_ID_INVALID;
375     }
376 
377     OsCpupGetPos(mode, &pos, &prePos);
378     cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
379 
380     return OsCalculateCpupUsage(processCB->processCpup, pos, prePos, cpuAllCycle);
381 }
382 
LOS_HistoryProcessCpuUsage(UINT32 pid,UINT16 mode)383 LITE_OS_SEC_TEXT_MINOR UINT32 LOS_HistoryProcessCpuUsage(UINT32 pid, UINT16 mode)
384 {
385     UINT32 cpup;
386     UINT32 intSave;
387 
388     SCHEDULER_LOCK(intSave);
389     cpup = OsHistoryProcessCpuUsageUnsafe(pid, mode);
390     SCHEDULER_UNLOCK(intSave);
391     return cpup;
392 }
393 
OsHistoryTaskCpuUsageUnsafe(UINT32 tid,UINT16 mode)394 STATIC UINT32 OsHistoryTaskCpuUsageUnsafe(UINT32 tid, UINT16 mode)
395 {
396     LosTaskCB *taskCB = NULL;
397     UINT64 cpuAllCycle;
398     UINT16 pos, prePos;
399 
400     if (cpupInitFlg == 0) {
401         return LOS_ERRNO_CPUP_NO_INIT;
402     }
403 
404     if (OS_TID_CHECK_INVALID(tid)) {
405         return LOS_ERRNO_CPUP_ID_INVALID;
406     }
407 
408     taskCB = OS_TCB_FROM_TID(tid);
409     if (OsTaskIsUnused(taskCB)) {
410         return LOS_ERRNO_CPUP_NO_CREATED;
411     }
412 
413     OsCpupGetPos(mode, &pos, &prePos);
414     cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
415 
416     return OsCalculateCpupUsage(&taskCB->taskCpup, pos, prePos, cpuAllCycle);
417 }
418 
LOS_HistoryTaskCpuUsage(UINT32 tid,UINT16 mode)419 LITE_OS_SEC_TEXT_MINOR UINT32 LOS_HistoryTaskCpuUsage(UINT32 tid, UINT16 mode)
420 {
421     UINT32 intSave;
422     UINT32 cpup;
423 
424     SCHEDULER_LOCK(intSave);
425     cpup = OsHistoryTaskCpuUsageUnsafe(tid, mode);
426     SCHEDULER_UNLOCK(intSave);
427     return cpup;
428 }
429 
OsCpupUsageParamCheckAndReset(CPUP_INFO_S * cpupInfo,UINT32 len,UINT32 number)430 STATIC UINT32 OsCpupUsageParamCheckAndReset(CPUP_INFO_S *cpupInfo, UINT32 len, UINT32 number)
431 {
432     if (cpupInitFlg == 0) {
433         return LOS_ERRNO_CPUP_NO_INIT;
434     }
435 
436     if ((cpupInfo == NULL) || (len < (sizeof(CPUP_INFO_S) * number))) {
437         return LOS_ERRNO_CPUP_PTR_ERR;
438     }
439 
440     (VOID)memset_s(cpupInfo, len, 0, len);
441     return LOS_OK;
442 }
443 
GetAllProcessCpuUsageUnsafe(UINT16 mode,CPUP_INFO_S * cpupInfo,UINT32 len)444 STATIC UINT32 GetAllProcessCpuUsageUnsafe(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
445 {
446     LosProcessCB *processCB = NULL;
447     UINT64 cpuAllCycle;
448     UINT16 pos, prePos;
449     UINT32 processID;
450     UINT32 ret;
451 
452     ret = OsCpupUsageParamCheckAndReset(cpupInfo, len, g_processMaxNum);
453     if (ret != LOS_OK) {
454         return ret;
455     }
456 
457     OsCpupGetPos(mode, &pos, &prePos);
458     cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
459 
460     for (processID = 0; processID < g_processMaxNum; processID++) {
461         processCB = OS_PCB_FROM_PID(processID);
462         if (OsProcessIsUnused(processCB) || (processCB->processCpup == NULL)) {
463             continue;
464         }
465 
466         cpupInfo[processID].usage = OsCalculateCpupUsage(processCB->processCpup, pos, prePos, cpuAllCycle);
467         cpupInfo[processID].status = OS_CPUP_USED;
468     }
469 
470     return LOS_OK;
471 }
472 
LOS_GetAllProcessCpuUsage(UINT16 mode,CPUP_INFO_S * cpupInfo,UINT32 len)473 LITE_OS_SEC_TEXT_MINOR UINT32 LOS_GetAllProcessCpuUsage(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
474 {
475     UINT32 intSave;
476     UINT32 ret;
477 
478     SCHEDULER_LOCK(intSave);
479     ret = GetAllProcessCpuUsageUnsafe(mode, cpupInfo, len);
480     SCHEDULER_UNLOCK(intSave);
481     return ret;
482 }
483 
OsGetProcessAllCpuUsageUnsafe(OsCpupBase * processCpup,ProcessInfo * processInfo)484 UINT32 OsGetProcessAllCpuUsageUnsafe(OsCpupBase *processCpup, ProcessInfo *processInfo)
485 {
486     UINT64 cpuAllCycle;
487     UINT16 pos, prePos;
488     if ((processCpup == NULL) || (processInfo == NULL)) {
489         return LOS_ERRNO_CPUP_PTR_ERR;
490     }
491 
492     OsCpupGetPos(CPUP_LAST_ONE_SECONDS, &pos, &prePos);
493     cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
494     processInfo->cpup1sUsage = OsCalculateCpupUsage(processCpup, pos, prePos, cpuAllCycle);
495 
496     OsCpupGetPos(CPUP_LAST_TEN_SECONDS, &pos, &prePos);
497     cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
498     processInfo->cpup10sUsage = OsCalculateCpupUsage(processCpup, pos, prePos, cpuAllCycle);
499 
500     OsCpupGetPos(CPUP_ALL_TIME, &pos, &prePos);
501     cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
502     processInfo->cpupAllsUsage = OsCalculateCpupUsage(processCpup, pos, prePos, cpuAllCycle);
503     return LOS_OK;
504 }
505 
OsGetTaskAllCpuUsageUnsafe(OsCpupBase * taskCpup,TaskInfo * taskInfo)506 UINT32 OsGetTaskAllCpuUsageUnsafe(OsCpupBase *taskCpup, TaskInfo *taskInfo)
507 {
508     UINT64 cpuAllCycle;
509     UINT16 pos, prePos;
510     if ((taskCpup == NULL) || (taskInfo == NULL)) {
511         return LOS_ERRNO_CPUP_PTR_ERR;
512     }
513 
514     OsCpupGetPos(CPUP_LAST_ONE_SECONDS, &pos, &prePos);
515     cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
516     taskInfo->cpup1sUsage = OsCalculateCpupUsage(taskCpup, pos, prePos, cpuAllCycle);
517 
518     OsCpupGetPos(CPUP_LAST_TEN_SECONDS, &pos, &prePos);
519     cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
520     taskInfo->cpup10sUsage = OsCalculateCpupUsage(taskCpup, pos, prePos, cpuAllCycle);
521 
522     OsCpupGetPos(CPUP_ALL_TIME, &pos, &prePos);
523     cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
524     taskInfo->cpupAllsUsage = OsCalculateCpupUsage(taskCpup, pos, prePos, cpuAllCycle);
525     return LOS_OK;
526 }
527 
528 #ifdef LOSCFG_CPUP_INCLUDE_IRQ
OsCpupIrqStart(UINT16 cpuid)529 LITE_OS_SEC_TEXT_MINOR VOID OsCpupIrqStart(UINT16 cpuid)
530 {
531     UINT32 high;
532     UINT32 low;
533 
534     LOS_GetCpuCycle(&high, &low);
535     cpupIntTimeStart[cpuid] = ((UINT64)high << HIGH_BITS) + low;
536     return;
537 }
538 
OsCpupIrqEnd(UINT16 cpuid,UINT32 intNum)539 LITE_OS_SEC_TEXT_MINOR VOID OsCpupIrqEnd(UINT16 cpuid, UINT32 intNum)
540 {
541     UINT32 high;
542     UINT32 low;
543     UINT64 intTimeEnd;
544     UINT64 usedTime;
545 
546     LOS_GetCpuCycle(&high, &low);
547     intTimeEnd = ((UINT64)high << HIGH_BITS) + low;
548     OsIrqCpupCB *irqCb = &g_irqCpup[(intNum * LOSCFG_KERNEL_CORE_NUM) + cpuid];
549     irqCb->id = intNum;
550     irqCb->status = OS_CPUP_USED;
551     usedTime = intTimeEnd - cpupIntTimeStart[cpuid];
552     timeInIrqSwitch[cpuid] += usedTime;
553     irqCb->cpup.allTime += usedTime;
554     if (irqCb->count <= 100) { /* Take 100 samples */
555         irqCb->allTime += usedTime;
556         irqCb->count++;
557     } else {
558         irqCb->allTime = 0;
559         irqCb->count = 0;
560     }
561     if (usedTime > irqCb->timeMax) {
562         irqCb->timeMax = usedTime;
563     }
564     return;
565 }
566 
OsGetIrqCpupArrayBase(VOID)567 LITE_OS_SEC_TEXT_MINOR OsIrqCpupCB *OsGetIrqCpupArrayBase(VOID)
568 {
569     return g_irqCpup;
570 }
571 
OsGetAllIrqCpuUsageUnsafe(UINT16 mode,CPUP_INFO_S * cpupInfo,UINT32 len)572 LITE_OS_SEC_TEXT_MINOR UINT32 OsGetAllIrqCpuUsageUnsafe(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
573 {
574     UINT16 pos, prePos;
575     UINT64 cpuAllCycle;
576     UINT32 loop;
577     UINT32 ret;
578 
579     ret = OsCpupUsageParamCheckAndReset(cpupInfo, len, cpupMaxNum);
580     if (ret != LOS_OK) {
581         return ret;
582     }
583 
584     OsCpupGetPos(mode, &pos, &prePos);
585     cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
586 
587     for (loop = 0; loop < cpupMaxNum; loop++) {
588         if (g_irqCpup[loop].status == OS_CPUP_UNUSED) {
589             continue;
590         }
591 
592         cpupInfo[loop].usage = OsCalculateCpupUsage(&g_irqCpup[loop].cpup, pos, prePos, cpuAllCycle);
593         cpupInfo[loop].status = g_irqCpup[loop].status;
594     }
595 
596     return LOS_OK;
597 }
598 
LOS_GetAllIrqCpuUsage(UINT16 mode,CPUP_INFO_S * cpupInfo,UINT32 len)599 LITE_OS_SEC_TEXT_MINOR UINT32 LOS_GetAllIrqCpuUsage(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
600 {
601     UINT32 intSave;
602     UINT32 ret;
603 
604     SCHEDULER_LOCK(intSave);
605     ret = OsGetAllIrqCpuUsageUnsafe(mode, cpupInfo, len);
606     SCHEDULER_UNLOCK(intSave);
607     return ret;
608 }
609 #endif
610 
611 #endif /* LOSCFG_KERNEL_CPUP */
612