los_cpup.c 16.8 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2 3
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
W
wenjun 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "los_cpup_pri.h"
#include "los_base.h"
34 35
#include "los_init.h"
#include "los_process_pri.h"
W
wenjun 已提交
36 37 38 39 40
#include "los_swtmr.h"


#ifdef LOSCFG_KERNEL_CPUP

M
mamingshuai 已提交
41
LITE_OS_SEC_BSS STATIC UINT16 cpupSwtmrID;
W
wenjun 已提交
42
LITE_OS_SEC_BSS STATIC UINT16 cpupInitFlg = 0;
M
mamingshuai 已提交
43
LITE_OS_SEC_BSS OsIrqCpupCB *g_irqCpup = NULL;
W
wenjun 已提交
44
LITE_OS_SEC_BSS STATIC UINT16 cpupMaxNum;
M
mamingshuai 已提交
45
LITE_OS_SEC_BSS STATIC UINT16 cpupHisPos = 0; /* current Sampling point of historyTime */
W
wenjun 已提交
46
LITE_OS_SEC_BSS STATIC UINT64 cpuHistoryTime[OS_CPUP_HISTORY_RECORD_NUM + 1];
M
mamingshuai 已提交
47 48
LITE_OS_SEC_BSS STATIC UINT32 runningTasks[LOSCFG_KERNEL_CORE_NUM];
LITE_OS_SEC_BSS STATIC UINT64 cpupStartCycles = 0;
W
wenjun 已提交
49
#ifdef LOSCFG_CPUP_INCLUDE_IRQ
M
mamingshuai 已提交
50 51
LITE_OS_SEC_BSS UINT64 timeInIrqSwitch[LOSCFG_KERNEL_CORE_NUM];
LITE_OS_SEC_BSS STATIC UINT64 cpupIntTimeStart[LOSCFG_KERNEL_CORE_NUM];
W
wenjun 已提交
52 53
#endif

M
mamingshuai 已提交
54 55
#define INVALID_ID ((UINT32)-1)

W
wenjun 已提交
56 57 58 59 60 61 62
#define OS_CPUP_UNUSED 0x0U
#define OS_CPUP_USED   0x1U
#define HIGH_BITS 32

#define CPUP_PRE_POS(pos) (((pos) == 0) ? (OS_CPUP_HISTORY_RECORD_NUM - 1) : ((pos) - 1))
#define CPUP_POST_POS(pos) (((pos) == (OS_CPUP_HISTORY_RECORD_NUM - 1)) ? 0 : ((pos) + 1))

M
mamingshuai 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
STATIC UINT64 OsGetCpuCycle(VOID)
{
    UINT32 high;
    UINT32 low;
    UINT64 cycles;

    LOS_GetCpuCycle(&high, &low);
    cycles = ((UINT64)high << HIGH_BITS) + low;
    if (cpupStartCycles == 0) {
        cpupStartCycles = cycles;
    }

    /*
     * The cycles should keep growing, if the checking failed,
     * it mean LOS_GetCpuCycle has the problem which should be fixed.
     */
    LOS_ASSERT(cycles >= cpupStartCycles);

    return (cycles - cpupStartCycles);
}

W
wenjun 已提交
84 85 86
LITE_OS_SEC_TEXT_INIT VOID OsCpupGuard(VOID)
{
    UINT16 prevPos;
M
mamingshuai 已提交
87 88
    UINT32 loop;
    UINT32 runTaskID;
W
wenjun 已提交
89
    UINT32 intSave;
M
mamingshuai 已提交
90 91 92
    UINT64 cycle, cycleIncrement;
    LosTaskCB *taskCB = NULL;
    LosProcessCB *processCB = NULL;
W
wenjun 已提交
93 94 95

    SCHEDULER_LOCK(intSave);

M
mamingshuai 已提交
96 97 98 99
    cycle = OsGetCpuCycle();
    prevPos = cpupHisPos;
    cpupHisPos = CPUP_POST_POS(cpupHisPos);
    cpuHistoryTime[prevPos] = cycle;
W
wenjun 已提交
100

M
mamingshuai 已提交
101
#ifdef LOSCFG_CPUP_INCLUDE_IRQ
W
wenjun 已提交
102
    for (loop = 0; loop < cpupMaxNum; loop++) {
M
mamingshuai 已提交
103 104 105 106 107 108
        g_irqCpup[loop].cpup.historyTime[prevPos] = g_irqCpup[loop].cpup.allTime;
    }
#endif

    for (loop = 0; loop < g_processMaxNum; loop++) {
        processCB = OS_PCB_FROM_PID(loop);
109 110 111 112
        if (processCB->processCpup == NULL) {
            continue;
        }
        processCB->processCpup->historyTime[prevPos] = processCB->processCpup->allTime;
M
mamingshuai 已提交
113 114 115 116 117
    }

    for (loop = 0; loop < g_taskMaxNum; loop++) {
        taskCB = OS_TCB_FROM_TID(loop);
        taskCB->taskCpup.historyTime[prevPos] = taskCB->taskCpup.allTime;
W
wenjun 已提交
118 119 120
    }

    for (loop = 0; loop < LOSCFG_KERNEL_CORE_NUM; loop++) {
M
mamingshuai 已提交
121 122 123 124 125 126
        runTaskID = runningTasks[loop];
        if (runTaskID == INVALID_ID) {
            continue;
        }
        taskCB = OS_TCB_FROM_TID(runTaskID);

W
wenjun 已提交
127
        /* reacquire the cycle to prevent flip */
M
mamingshuai 已提交
128 129
        cycle = OsGetCpuCycle();
        cycleIncrement = cycle - taskCB->taskCpup.startTime;
W
wenjun 已提交
130
#ifdef LOSCFG_CPUP_INCLUDE_IRQ
M
mamingshuai 已提交
131
        cycleIncrement -= timeInIrqSwitch[loop];
W
wenjun 已提交
132
#endif
M
mamingshuai 已提交
133 134
        taskCB->taskCpup.historyTime[prevPos] += cycleIncrement;
        processCB = OS_PCB_FROM_PID(taskCB->processID);
135
        processCB->processCpup->historyTime[prevPos] += cycleIncrement;
W
wenjun 已提交
136 137 138 139 140
    }

    SCHEDULER_UNLOCK(intSave);
}

141
LITE_OS_SEC_TEXT_INIT UINT32 OsCpupGuardCreator(VOID)
W
wenjun 已提交
142 143
{
    (VOID)LOS_SwtmrCreate(LOSCFG_BASE_CORE_TICK_PER_SECOND, LOS_SWTMR_MODE_PERIOD,
M
mamingshuai 已提交
144
                          (SWTMR_PROC_FUNC)OsCpupGuard, &cpupSwtmrID, 0);
W
wenjun 已提交
145

M
mamingshuai 已提交
146
    (VOID)LOS_SwtmrStart(cpupSwtmrID);
147 148

    return LOS_OK;
W
wenjun 已提交
149 150
}

151 152
LOS_MODULE_INIT(OsCpupGuardCreator, LOS_INIT_LEVEL_KMOD_TASK);

W
wenjun 已提交
153 154 155 156 157 158
/*
 * Description: initialization of CPUP
 * Return     : LOS_OK or Error Information
 */
LITE_OS_SEC_TEXT_INIT UINT32 OsCpupInit(VOID)
{
M
mamingshuai 已提交
159 160
    UINT16 loop;
#ifdef LOSCFG_CPUP_INCLUDE_IRQ
W
wenjun 已提交
161 162
    UINT32 size;

M
mamingshuai 已提交
163
    cpupMaxNum = OS_HWI_MAX_NUM;
W
wenjun 已提交
164 165

    /* every process has only one record, and it won't operated at the same time */
M
mamingshuai 已提交
166 167 168
    size = cpupMaxNum * sizeof(OsIrqCpupCB);
    g_irqCpup = (OsIrqCpupCB *)LOS_MemAlloc(m_aucSysMem0, size);
    if (g_irqCpup == NULL) {
169
        PRINT_ERR("OsCpupInit error\n");
W
wenjun 已提交
170 171 172
        return LOS_ERRNO_CPUP_NO_MEMORY;
    }

M
mamingshuai 已提交
173 174 175 176 177 178
    (VOID)memset_s(g_irqCpup, size, 0, size);
#endif

    for (loop = 0; loop < LOSCFG_KERNEL_CORE_NUM; loop++) {
        runningTasks[loop] = INVALID_ID;
    }
W
wenjun 已提交
179 180 181 182
    cpupInitFlg = 1;
    return LOS_OK;
}

183 184
LOS_MODULE_INIT(OsCpupInit, LOS_INIT_LEVEL_KMOD_EXTENDED);

M
mamingshuai 已提交
185
STATIC VOID OsResetCpup(OsCpupBase *cpup, UINT64 cycle)
W
wenjun 已提交
186
{
M
mamingshuai 已提交
187
    UINT16 loop;
W
wenjun 已提交
188

M
mamingshuai 已提交
189 190 191 192 193
    cpup->startTime = cycle;
    cpup->allTime = cycle;
    for (loop = 0; loop < (OS_CPUP_HISTORY_RECORD_NUM + 1); loop++) {
        cpup->historyTime[loop] = cycle;
    }
W
wenjun 已提交
194 195 196 197
}

LITE_OS_SEC_TEXT_INIT VOID LOS_CpupReset(VOID)
{
M
mamingshuai 已提交
198 199 200 201
    LosProcessCB *processCB = NULL;
    LosTaskCB *taskCB = NULL;
    UINT32 index;
    UINT64 cycle;
W
wenjun 已提交
202 203 204 205
    UINT32 intSave;

    cpupInitFlg = 0;
    intSave = LOS_IntLock();
M
mamingshuai 已提交
206 207
    (VOID)LOS_SwtmrStop(cpupSwtmrID);
    cycle = OsGetCpuCycle();
W
wenjun 已提交
208

M
mamingshuai 已提交
209 210
    for (index = 0; index < (OS_CPUP_HISTORY_RECORD_NUM + 1); index++) {
        cpuHistoryTime[index] = cycle;
W
wenjun 已提交
211 212
    }

M
mamingshuai 已提交
213 214
    for (index = 0; index < g_processMaxNum; index++) {
        processCB = OS_PCB_FROM_PID(index);
215 216 217 218
        if (processCB->processCpup == NULL) {
            continue;
        }
        OsResetCpup(processCB->processCpup, cycle);
W
wenjun 已提交
219 220
    }

M
mamingshuai 已提交
221 222 223
    for (index = 0; index < g_taskMaxNum; index++) {
        taskCB = OS_TCB_FROM_TID(index);
        OsResetCpup(&taskCB->taskCpup, cycle);
W
wenjun 已提交
224 225 226
    }

#ifdef LOSCFG_CPUP_INCLUDE_IRQ
M
mamingshuai 已提交
227 228 229
    if (g_irqCpup != NULL) {
        for (index = 0; index < cpupMaxNum; index++) {
            OsResetCpup(&g_irqCpup[index].cpup, cycle);
W
wenjun 已提交
230 231
        }

M
mamingshuai 已提交
232 233 234
        for (index = 0; index < LOSCFG_KERNEL_CORE_NUM; index++) {
            timeInIrqSwitch[index] = 0;
        }
W
wenjun 已提交
235 236 237
    }
#endif

M
mamingshuai 已提交
238 239 240
    (VOID)LOS_SwtmrStart(cpupSwtmrID);
    LOS_IntRestore(intSave);
    cpupInitFlg = 1;
W
wenjun 已提交
241 242 243 244

    return;
}

M
mamingshuai 已提交
245
VOID OsCpupCycleEndStart(UINT32 runTaskID, UINT32 newTaskID)
W
wenjun 已提交
246
{
M
mamingshuai 已提交
247 248 249 250
    /* OsCurrTaskGet and OsCurrProcessGet are not allowed to be called. */
    LosTaskCB *runTask = OS_TCB_FROM_TID(runTaskID);
    OsCpupBase *runTaskCpup = &runTask->taskCpup;
    OsCpupBase *newTaskCpup = (OsCpupBase *)&(OS_TCB_FROM_TID(newTaskID)->taskCpup);
251
    OsCpupBase *processCpup = OS_PCB_FROM_PID(runTask->processID)->processCpup;
M
mamingshuai 已提交
252 253
    UINT64 cpuCycle, cycleIncrement;
    UINT16 cpuID = ArchCurrCpuid();
W
wenjun 已提交
254 255 256 257 258 259

    if (cpupInitFlg == 0) {
        return;
    }

    cpuCycle = OsGetCpuCycle();
M
mamingshuai 已提交
260 261
    if (runTaskCpup->startTime != 0) {
        cycleIncrement = cpuCycle - runTaskCpup->startTime;
W
wenjun 已提交
262
#ifdef LOSCFG_CPUP_INCLUDE_IRQ
M
mamingshuai 已提交
263 264
        cycleIncrement -= timeInIrqSwitch[cpuID];
        timeInIrqSwitch[cpuID] = 0;
W
wenjun 已提交
265
#endif
M
mamingshuai 已提交
266
        runTaskCpup->allTime += cycleIncrement;
267 268 269
        if (processCpup != NULL) {
            processCpup->allTime += cycleIncrement;
        }
M
mamingshuai 已提交
270
        runTaskCpup->startTime = 0;
W
wenjun 已提交
271 272
    }

M
mamingshuai 已提交
273 274
    newTaskCpup->startTime = cpuCycle;
    runningTasks[cpuID] = newTaskID;
W
wenjun 已提交
275 276 277 278 279 280 281 282
}

LITE_OS_SEC_TEXT_MINOR STATIC VOID OsCpupGetPos(UINT16 mode, UINT16 *curPosPointer, UINT16 *prePosPointer)
{
    UINT16 curPos;
    UINT16 tmpPos;
    UINT16 prePos;

M
mamingshuai 已提交
283
    tmpPos = cpupHisPos;
W
wenjun 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    curPos = CPUP_PRE_POS(tmpPos);

    /*
     * The current postion has nothing to do with the CPUP modes,
     * however, the previous postion differs.
     */
    switch (mode) {
        case CPUP_LAST_ONE_SECONDS:
            prePos = CPUP_PRE_POS(curPos);
            break;
        case CPUP_LAST_TEN_SECONDS:
            prePos = tmpPos;
            break;
        case CPUP_ALL_TIME:
            /* fall-through */
        default:
            prePos = OS_CPUP_HISTORY_RECORD_NUM;
            break;
    }

    *curPosPointer = curPos;
    *prePosPointer = prePos;

    return;
}

M
mamingshuai 已提交
310
STATIC INLINE UINT32 OsCalculateCpupUsage(const OsCpupBase *cpup, UINT16 pos, UINT16 prePos, UINT64 allCycle)
W
wenjun 已提交
311
{
M
mamingshuai 已提交
312 313
    UINT32 usage = 0;
    UINT64 cpuCycle = cpup->historyTime[pos] - cpup->historyTime[prePos];
W
wenjun 已提交
314

M
mamingshuai 已提交
315 316
    if (allCycle) {
        usage = (UINT32)((LOS_CPUP_SINGLE_CORE_PRECISION * cpuCycle) / allCycle);
W
wenjun 已提交
317
    }
M
mamingshuai 已提交
318
    return usage;
W
wenjun 已提交
319 320
}

M
mamingshuai 已提交
321
STATIC UINT32 OsHistorySysCpuUsageUnsafe(UINT16 mode)
W
wenjun 已提交
322
{
M
mamingshuai 已提交
323
    UINT64 cpuAllCycle;
W
wenjun 已提交
324 325
    UINT16 pos;
    UINT16 prePos;
M
mamingshuai 已提交
326 327
    UINT32 idleProcessID;
    OsCpupBase *processCpup = NULL;
W
wenjun 已提交
328 329 330 331 332 333

    if (cpupInitFlg == 0) {
        return LOS_ERRNO_CPUP_NO_INIT;
    }

    OsCpupGetPos(mode, &pos, &prePos);
M
mamingshuai 已提交
334
    cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
W
wenjun 已提交
335

M
mamingshuai 已提交
336
    idleProcessID = OsGetIdleProcessID();
337
    processCpup = OS_PCB_FROM_PID(idleProcessID)->processCpup;
M
mamingshuai 已提交
338
    return (LOS_CPUP_PRECISION - OsCalculateCpupUsage(processCpup, pos, prePos, cpuAllCycle));
W
wenjun 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352
}

LITE_OS_SEC_TEXT_MINOR UINT32 LOS_HistorySysCpuUsage(UINT16 mode)
{
    UINT32 cpup;
    UINT32 intSave;

    /* get end time of current process */
    SCHEDULER_LOCK(intSave);
    cpup = OsHistorySysCpuUsageUnsafe(mode);
    SCHEDULER_UNLOCK(intSave);
    return cpup;
}

M
mamingshuai 已提交
353
STATIC UINT32 OsHistoryProcessCpuUsageUnsafe(UINT32 pid, UINT16 mode)
W
wenjun 已提交
354
{
M
mamingshuai 已提交
355 356 357
    LosProcessCB *processCB = NULL;
    UINT64 cpuAllCycle;
    UINT16 pos, prePos;
W
wenjun 已提交
358

M
mamingshuai 已提交
359 360
    if (cpupInitFlg == 0) {
        return LOS_ERRNO_CPUP_NO_INIT;
W
wenjun 已提交
361 362
    }

M
mamingshuai 已提交
363 364 365
    if (OS_PID_CHECK_INVALID(pid)) {
        return LOS_ERRNO_CPUP_ID_INVALID;
    }
W
wenjun 已提交
366

M
mamingshuai 已提交
367 368 369
    processCB = OS_PCB_FROM_PID(pid);
    if (OsProcessIsUnused(processCB)) {
        return LOS_ERRNO_CPUP_NO_CREATED;
W
wenjun 已提交
370 371
    }

372 373 374 375
    if (processCB->processCpup == NULL) {
        return LOS_ERRNO_CPUP_ID_INVALID;
    }

M
mamingshuai 已提交
376 377
    OsCpupGetPos(mode, &pos, &prePos);
    cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
W
wenjun 已提交
378

379
    return OsCalculateCpupUsage(processCB->processCpup, pos, prePos, cpuAllCycle);
W
wenjun 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392
}

LITE_OS_SEC_TEXT_MINOR UINT32 LOS_HistoryProcessCpuUsage(UINT32 pid, UINT16 mode)
{
    UINT32 cpup;
    UINT32 intSave;

    SCHEDULER_LOCK(intSave);
    cpup = OsHistoryProcessCpuUsageUnsafe(pid, mode);
    SCHEDULER_UNLOCK(intSave);
    return cpup;
}

M
mamingshuai 已提交
393
STATIC UINT32 OsHistoryTaskCpuUsageUnsafe(UINT32 tid, UINT16 mode)
W
wenjun 已提交
394
{
M
mamingshuai 已提交
395 396 397
    LosTaskCB *taskCB = NULL;
    UINT64 cpuAllCycle;
    UINT16 pos, prePos;
W
wenjun 已提交
398 399

    if (cpupInitFlg == 0) {
M
mamingshuai 已提交
400
        return LOS_ERRNO_CPUP_NO_INIT;
W
wenjun 已提交
401 402
    }

M
mamingshuai 已提交
403 404
    if (OS_TID_CHECK_INVALID(tid)) {
        return LOS_ERRNO_CPUP_ID_INVALID;
W
wenjun 已提交
405 406
    }

M
mamingshuai 已提交
407 408 409
    taskCB = OS_TCB_FROM_TID(tid);
    if (OsTaskIsUnused(taskCB)) {
        return LOS_ERRNO_CPUP_NO_CREATED;
W
wenjun 已提交
410 411
    }

M
mamingshuai 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    OsCpupGetPos(mode, &pos, &prePos);
    cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];

    return OsCalculateCpupUsage(&taskCB->taskCpup, pos, prePos, cpuAllCycle);
}

LITE_OS_SEC_TEXT_MINOR UINT32 LOS_HistoryTaskCpuUsage(UINT32 tid, UINT16 mode)
{
    UINT32 intSave;
    UINT32 cpup;

    SCHEDULER_LOCK(intSave);
    cpup = OsHistoryTaskCpuUsageUnsafe(tid, mode);
    SCHEDULER_UNLOCK(intSave);
    return cpup;
}

STATIC UINT32 OsCpupUsageParamCheckAndReset(CPUP_INFO_S *cpupInfo, UINT32 len, UINT32 number)
{
    if (cpupInitFlg == 0) {
        return LOS_ERRNO_CPUP_NO_INIT;
W
wenjun 已提交
433 434
    }

M
mamingshuai 已提交
435 436
    if ((cpupInfo == NULL) || (len < (sizeof(CPUP_INFO_S) * number))) {
        return LOS_ERRNO_CPUP_PTR_ERR;
W
wenjun 已提交
437 438
    }

M
mamingshuai 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
    (VOID)memset_s(cpupInfo, len, 0, len);
    return LOS_OK;
}

LITE_OS_SEC_TEXT_MINOR UINT32 OsGetAllProcessCpuUsageUnsafe(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
{
    LosProcessCB *processCB = NULL;
    UINT64 cpuAllCycle;
    UINT16 pos, prePos;
    UINT32 processID;
    UINT32 ret;

    ret = OsCpupUsageParamCheckAndReset(cpupInfo, len, g_processMaxNum);
    if (ret != LOS_OK) {
        return ret;
    }
W
wenjun 已提交
455 456

    OsCpupGetPos(mode, &pos, &prePos);
M
mamingshuai 已提交
457
    cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
W
wenjun 已提交
458

M
mamingshuai 已提交
459 460
    for (processID = 0; processID < g_processMaxNum; processID++) {
        processCB = OS_PCB_FROM_PID(processID);
461
        if (OsProcessIsUnused(processCB) || (processCB->processCpup == NULL)) {
W
wenjun 已提交
462 463 464
            continue;
        }

465
        cpupInfo[processID].usage = OsCalculateCpupUsage(processCB->processCpup, pos, prePos, cpuAllCycle);
M
mamingshuai 已提交
466
        cpupInfo[processID].status = OS_CPUP_USED;
W
wenjun 已提交
467 468 469 470 471
    }

    return LOS_OK;
}

M
mamingshuai 已提交
472
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_GetAllProcessCpuUsage(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
W
wenjun 已提交
473 474 475 476 477
{
    UINT32 intSave;
    UINT32 ret;

    SCHEDULER_LOCK(intSave);
M
mamingshuai 已提交
478
    ret = OsGetAllProcessCpuUsageUnsafe(mode, cpupInfo, len);
W
wenjun 已提交
479 480 481 482
    SCHEDULER_UNLOCK(intSave);
    return ret;
}

M
mamingshuai 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
LITE_OS_SEC_TEXT_MINOR UINT32 OsGetAllProcessAndTaskCpuUsageUnsafe(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
{
    UINT64 cpuAllCycle;
    UINT16 pos, prePos;
    UINT32 taskID;
    UINT32 ret;
    LosTaskCB *taskCB = NULL;
    OsCpupBase *processCpupBase = NULL;
    CPUP_INFO_S *processCpup = cpupInfo;
    CPUP_INFO_S *taskCpup = (CPUP_INFO_S *)((UINTPTR)cpupInfo + sizeof(CPUP_INFO_S) * g_processMaxNum);

    ret = OsCpupUsageParamCheckAndReset(cpupInfo, len, g_taskMaxNum + g_processMaxNum);
    if (ret != LOS_OK) {
        return ret;
    }

    OsCpupGetPos(mode, &pos, &prePos);
    cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];

    for (taskID = 0; taskID < g_taskMaxNum; taskID++) {
        taskCB = OS_TCB_FROM_TID(taskID);
        if (OsTaskIsUnused(taskCB)) {
            continue;
        }

        taskCpup[taskID].usage = OsCalculateCpupUsage(&taskCB->taskCpup, pos, prePos, cpuAllCycle);
        taskCpup[taskID].status = OS_CPUP_USED;
        if (processCpup[taskCB->processID].status == OS_CPUP_UNUSED) {
511 512 513 514 515
            processCpupBase = OS_PCB_FROM_PID(taskCB->processID)->processCpup;
            if (processCpupBase != NULL) {
                processCpup[taskCB->processID].usage = OsCalculateCpupUsage(processCpupBase, pos, prePos, cpuAllCycle);
                processCpup[taskCB->processID].status = OS_CPUP_USED;
            }
M
mamingshuai 已提交
516 517 518 519 520 521
        }
    }

    return LOS_OK;
}

W
wenjun 已提交
522 523 524 525 526 527 528
#ifdef LOSCFG_CPUP_INCLUDE_IRQ
LITE_OS_SEC_TEXT_MINOR VOID OsCpupIrqStart(VOID)
{
    UINT32 high;
    UINT32 low;

    LOS_GetCpuCycle(&high, &low);
M
mamingshuai 已提交
529
    cpupIntTimeStart[ArchCurrCpuid()] = ((UINT64)high << HIGH_BITS) + low;
W
wenjun 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542
    return;
}

LITE_OS_SEC_TEXT_MINOR VOID OsCpupIrqEnd(UINT32 intNum)
{
    UINT32 high;
    UINT32 low;
    UINT64 intTimeEnd;
    UINT32 cpuID = ArchCurrCpuid();

    LOS_GetCpuCycle(&high, &low);
    intTimeEnd = ((UINT64)high << HIGH_BITS) + low;

M
mamingshuai 已提交
543 544 545 546
    g_irqCpup[intNum].id = intNum;
    g_irqCpup[intNum].status = OS_CPUP_USED;
    timeInIrqSwitch[cpuID] += (intTimeEnd - cpupIntTimeStart[cpuID]);
    g_irqCpup[intNum].cpup.allTime += (intTimeEnd - cpupIntTimeStart[cpuID]);
W
wenjun 已提交
547 548 549

    return;
}
M
mamingshuai 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592

LITE_OS_SEC_TEXT_MINOR OsIrqCpupCB *OsGetIrqCpupArrayBase(VOID)
{
    return g_irqCpup;
}

LITE_OS_SEC_TEXT_MINOR UINT32 OsGetAllIrqCpuUsageUnsafe(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
{
    UINT16 pos, prePos;
    UINT64 cpuAllCycle;
    UINT32 loop;
    UINT32 ret;

    ret = OsCpupUsageParamCheckAndReset(cpupInfo, len, cpupMaxNum);
    if (ret != LOS_OK) {
        return ret;
    }

    OsCpupGetPos(mode, &pos, &prePos);
    cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];

    for (loop = 0; loop < cpupMaxNum; loop++) {
        if (g_irqCpup[loop].status == OS_CPUP_UNUSED) {
            continue;
        }

        cpupInfo[loop].usage = OsCalculateCpupUsage(&g_irqCpup[loop].cpup, pos, prePos, cpuAllCycle);
        cpupInfo[loop].status = g_irqCpup[loop].status;
    }

    return LOS_OK;
}

LITE_OS_SEC_TEXT_MINOR UINT32 LOS_GetAllIrqCpuUsage(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
{
    UINT32 intSave;
    UINT32 ret;

    SCHEDULER_LOCK(intSave);
    ret = OsGetAllIrqCpuUsageUnsafe(mode, cpupInfo, len);
    SCHEDULER_UNLOCK(intSave);
    return ret;
}
W
wenjun 已提交
593 594 595
#endif

#endif /* LOSCFG_KERNEL_CPUP */