los_cpup.c 16.4 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 109 110 111 112 113 114
        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);
        processCB->processCpup.historyTime[prevPos] = processCB->processCpup.allTime;
    }

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

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

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

    SCHEDULER_UNLOCK(intSave);
}

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

M
mamingshuai 已提交
143
    (VOID)LOS_SwtmrStart(cpupSwtmrID);
144 145

    return LOS_OK;
W
wenjun 已提交
146 147
}

148 149
LOS_MODULE_INIT(OsCpupGuardCreator, LOS_INIT_LEVEL_KMOD_TASK);

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

M
mamingshuai 已提交
160
    cpupMaxNum = OS_HWI_MAX_NUM;
W
wenjun 已提交
161 162

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

M
mamingshuai 已提交
170 171 172 173 174 175
    (VOID)memset_s(g_irqCpup, size, 0, size);
#endif

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

180 181
LOS_MODULE_INIT(OsCpupInit, LOS_INIT_LEVEL_KMOD_EXTENDED);

M
mamingshuai 已提交
182
STATIC VOID OsResetCpup(OsCpupBase *cpup, UINT64 cycle)
W
wenjun 已提交
183
{
M
mamingshuai 已提交
184
    UINT16 loop;
W
wenjun 已提交
185

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

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

    cpupInitFlg = 0;
    intSave = LOS_IntLock();
M
mamingshuai 已提交
203 204
    (VOID)LOS_SwtmrStop(cpupSwtmrID);
    cycle = OsGetCpuCycle();
W
wenjun 已提交
205

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

M
mamingshuai 已提交
210 211 212
    for (index = 0; index < g_processMaxNum; index++) {
        processCB = OS_PCB_FROM_PID(index);
        OsResetCpup(&processCB->processCpup, cycle);
W
wenjun 已提交
213 214
    }

M
mamingshuai 已提交
215 216 217
    for (index = 0; index < g_taskMaxNum; index++) {
        taskCB = OS_TCB_FROM_TID(index);
        OsResetCpup(&taskCB->taskCpup, cycle);
W
wenjun 已提交
218 219 220
    }

#ifdef LOSCFG_CPUP_INCLUDE_IRQ
M
mamingshuai 已提交
221 222 223
    if (g_irqCpup != NULL) {
        for (index = 0; index < cpupMaxNum; index++) {
            OsResetCpup(&g_irqCpup[index].cpup, cycle);
W
wenjun 已提交
224 225
        }

M
mamingshuai 已提交
226 227 228
        for (index = 0; index < LOSCFG_KERNEL_CORE_NUM; index++) {
            timeInIrqSwitch[index] = 0;
        }
W
wenjun 已提交
229 230 231
    }
#endif

M
mamingshuai 已提交
232 233 234
    (VOID)LOS_SwtmrStart(cpupSwtmrID);
    LOS_IntRestore(intSave);
    cpupInitFlg = 1;
W
wenjun 已提交
235 236 237 238

    return;
}

M
mamingshuai 已提交
239
VOID OsCpupCycleEndStart(UINT32 runTaskID, UINT32 newTaskID)
W
wenjun 已提交
240
{
M
mamingshuai 已提交
241 242 243 244 245 246 247
    /* 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);
    OsCpupBase *processCpup = (OsCpupBase *)&(OS_PCB_FROM_PID(runTask->processID)->processCpup);
    UINT64 cpuCycle, cycleIncrement;
    UINT16 cpuID = ArchCurrCpuid();
W
wenjun 已提交
248 249 250 251 252 253

    if (cpupInitFlg == 0) {
        return;
    }

    cpuCycle = OsGetCpuCycle();
M
mamingshuai 已提交
254 255
    if (runTaskCpup->startTime != 0) {
        cycleIncrement = cpuCycle - runTaskCpup->startTime;
W
wenjun 已提交
256
#ifdef LOSCFG_CPUP_INCLUDE_IRQ
M
mamingshuai 已提交
257 258
        cycleIncrement -= timeInIrqSwitch[cpuID];
        timeInIrqSwitch[cpuID] = 0;
W
wenjun 已提交
259
#endif
M
mamingshuai 已提交
260 261 262
        runTaskCpup->allTime += cycleIncrement;
        processCpup->allTime += cycleIncrement;
        runTaskCpup->startTime = 0;
W
wenjun 已提交
263 264
    }

M
mamingshuai 已提交
265 266
    newTaskCpup->startTime = cpuCycle;
    runningTasks[cpuID] = newTaskID;
W
wenjun 已提交
267 268 269 270 271 272 273 274
}

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

M
mamingshuai 已提交
275
    tmpPos = cpupHisPos;
W
wenjun 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    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 已提交
302
STATIC INLINE UINT32 OsCalculateCpupUsage(const OsCpupBase *cpup, UINT16 pos, UINT16 prePos, UINT64 allCycle)
W
wenjun 已提交
303
{
M
mamingshuai 已提交
304 305
    UINT32 usage = 0;
    UINT64 cpuCycle = cpup->historyTime[pos] - cpup->historyTime[prePos];
W
wenjun 已提交
306

M
mamingshuai 已提交
307 308
    if (allCycle) {
        usage = (UINT32)((LOS_CPUP_SINGLE_CORE_PRECISION * cpuCycle) / allCycle);
W
wenjun 已提交
309
    }
M
mamingshuai 已提交
310
    return usage;
W
wenjun 已提交
311 312
}

M
mamingshuai 已提交
313
STATIC UINT32 OsHistorySysCpuUsageUnsafe(UINT16 mode)
W
wenjun 已提交
314
{
M
mamingshuai 已提交
315
    UINT64 cpuAllCycle;
W
wenjun 已提交
316 317
    UINT16 pos;
    UINT16 prePos;
M
mamingshuai 已提交
318 319
    UINT32 idleProcessID;
    OsCpupBase *processCpup = NULL;
W
wenjun 已提交
320 321 322 323 324 325

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

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

M
mamingshuai 已提交
328 329 330
    idleProcessID = OsGetIdleProcessID();
    processCpup = (OsCpupBase *)&(OS_PCB_FROM_PID(idleProcessID)->processCpup);
    return (LOS_CPUP_PRECISION - OsCalculateCpupUsage(processCpup, pos, prePos, cpuAllCycle));
W
wenjun 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344
}

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 已提交
345
STATIC UINT32 OsHistoryProcessCpuUsageUnsafe(UINT32 pid, UINT16 mode)
W
wenjun 已提交
346
{
M
mamingshuai 已提交
347 348 349
    LosProcessCB *processCB = NULL;
    UINT64 cpuAllCycle;
    UINT16 pos, prePos;
W
wenjun 已提交
350

M
mamingshuai 已提交
351 352
    if (cpupInitFlg == 0) {
        return LOS_ERRNO_CPUP_NO_INIT;
W
wenjun 已提交
353 354
    }

M
mamingshuai 已提交
355 356 357
    if (OS_PID_CHECK_INVALID(pid)) {
        return LOS_ERRNO_CPUP_ID_INVALID;
    }
W
wenjun 已提交
358

M
mamingshuai 已提交
359 360 361
    processCB = OS_PCB_FROM_PID(pid);
    if (OsProcessIsUnused(processCB)) {
        return LOS_ERRNO_CPUP_NO_CREATED;
W
wenjun 已提交
362 363
    }

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

M
mamingshuai 已提交
367
    return OsCalculateCpupUsage(&processCB->processCpup, pos, prePos, cpuAllCycle);
W
wenjun 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380
}

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 已提交
381
STATIC UINT32 OsHistoryTaskCpuUsageUnsafe(UINT32 tid, UINT16 mode)
W
wenjun 已提交
382
{
M
mamingshuai 已提交
383 384 385
    LosTaskCB *taskCB = NULL;
    UINT64 cpuAllCycle;
    UINT16 pos, prePos;
W
wenjun 已提交
386 387

    if (cpupInitFlg == 0) {
M
mamingshuai 已提交
388
        return LOS_ERRNO_CPUP_NO_INIT;
W
wenjun 已提交
389 390
    }

M
mamingshuai 已提交
391 392
    if (OS_TID_CHECK_INVALID(tid)) {
        return LOS_ERRNO_CPUP_ID_INVALID;
W
wenjun 已提交
393 394
    }

M
mamingshuai 已提交
395 396 397
    taskCB = OS_TCB_FROM_TID(tid);
    if (OsTaskIsUnused(taskCB)) {
        return LOS_ERRNO_CPUP_NO_CREATED;
W
wenjun 已提交
398 399
    }

M
mamingshuai 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    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 已提交
421 422
    }

M
mamingshuai 已提交
423 424
    if ((cpupInfo == NULL) || (len < (sizeof(CPUP_INFO_S) * number))) {
        return LOS_ERRNO_CPUP_PTR_ERR;
W
wenjun 已提交
425 426
    }

M
mamingshuai 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
    (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 已提交
443 444

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

M
mamingshuai 已提交
447 448 449
    for (processID = 0; processID < g_processMaxNum; processID++) {
        processCB = OS_PCB_FROM_PID(processID);
        if (OsProcessIsUnused(processCB)) {
W
wenjun 已提交
450 451 452
            continue;
        }

M
mamingshuai 已提交
453 454
        cpupInfo[processID].usage = OsCalculateCpupUsage(&processCB->processCpup, pos, prePos, cpuAllCycle);
        cpupInfo[processID].status = OS_CPUP_USED;
W
wenjun 已提交
455 456 457 458 459
    }

    return LOS_OK;
}

M
mamingshuai 已提交
460
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_GetAllProcessCpuUsage(UINT16 mode, CPUP_INFO_S *cpupInfo, UINT32 len)
W
wenjun 已提交
461 462 463 464 465
{
    UINT32 intSave;
    UINT32 ret;

    SCHEDULER_LOCK(intSave);
M
mamingshuai 已提交
466
    ret = OsGetAllProcessCpuUsageUnsafe(mode, cpupInfo, len);
W
wenjun 已提交
467 468 469 470
    SCHEDULER_UNLOCK(intSave);
    return ret;
}

M
mamingshuai 已提交
471 472 473 474 475 476 477 478 479 480 481 482 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
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) {
            processCpupBase = &(OS_PCB_FROM_PID(taskCB->processID)->processCpup);
            processCpup[taskCB->processID].usage = OsCalculateCpupUsage(processCpupBase, pos, prePos, cpuAllCycle);
            processCpup[taskCB->processID].status = OS_CPUP_USED;
        }
    }

    return LOS_OK;
}

W
wenjun 已提交
508 509 510 511 512 513 514
#ifdef LOSCFG_CPUP_INCLUDE_IRQ
LITE_OS_SEC_TEXT_MINOR VOID OsCpupIrqStart(VOID)
{
    UINT32 high;
    UINT32 low;

    LOS_GetCpuCycle(&high, &low);
M
mamingshuai 已提交
515
    cpupIntTimeStart[ArchCurrCpuid()] = ((UINT64)high << HIGH_BITS) + low;
W
wenjun 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528
    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 已提交
529 530 531 532
    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 已提交
533 534 535

    return;
}
M
mamingshuai 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 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

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 已提交
579 580 581
#endif

#endif /* LOSCFG_KERNEL_CPUP */