feat: 进程cpup占用率结构优化为动态分配

Close #I4GLNT
Signed-off-by: zhushengle <zhushengle@huawei.com>
Change-Id: I7f3dd61bbf014fa157b1f9a0a089fd985fe188c2
This commit is contained in:
zhushengle 2021-11-02 19:15:04 +08:00
parent 38163de8f3
commit f06e090a10
4 changed files with 50 additions and 14 deletions

View File

@ -509,7 +509,11 @@ static int ProcessGetCputime(clockid_t clockID, struct timespec *ats)
}
SCHEDULER_LOCK(intSave);
runtime = spcb->processCpup.allTime;
if (spcb->processCpup == NULL) {
SCHEDULER_UNLOCK(intSave);
return -EINVAL;
}
runtime = spcb->processCpup->allTime;
SCHEDULER_UNLOCK(intSave);
ats->tv_sec = runtime / OS_SYS_NS_PER_SECOND;

View File

@ -376,6 +376,15 @@ LITE_OS_SEC_TEXT VOID OsProcessResourcesToFree(LosProcessCB *processCB)
(VOID)LiteIpcPoolDestroy(processCB->processID);
#endif
#ifdef LOSCFG_KERNEL_CPUP
UINT32 intSave;
OsCpupBase *processCpup = processCB->processCpup;
SCHEDULER_LOCK(intSave);
processCB->processCpup = NULL;
SCHEDULER_UNLOCK(intSave);
(VOID)LOS_MemFree(m_aucSysMem1, processCpup);
#endif
if (processCB->resourceLimit != NULL) {
(VOID)LOS_MemFree((VOID *)m_aucSysMem0, processCB->resourceLimit);
processCB->resourceLimit = NULL;
@ -654,12 +663,21 @@ STATIC UINT32 OsInitPCB(LosProcessCB *processCB, UINT32 mode, UINT16 priority, c
}
#endif
#ifdef LOSCFG_KERNEL_CPUP
processCB->processCpup = (OsCpupBase *)LOS_MemAlloc(m_aucSysMem1, sizeof(OsCpupBase));
if (processCB->processCpup == NULL) {
return LOS_ENOMEM;
}
(VOID)memset_s(processCB->processCpup, sizeof(OsCpupBase), 0, sizeof(OsCpupBase));
#endif
#ifdef LOSCFG_SECURITY_VID
status_t status = VidMapListInit(processCB);
if (status != LOS_OK) {
return LOS_ENOMEM;
}
#endif
#ifdef LOSCFG_SECURITY_CAPABILITY
OsInitCapability(processCB);
#endif

View File

@ -125,7 +125,7 @@ typedef struct ProcessCB {
#endif
mode_t umask;
#ifdef LOSCFG_KERNEL_CPUP
OsCpupBase processCpup; /**< Process cpu usage */
OsCpupBase *processCpup; /**< Process cpu usage */
#endif
struct rlimit *resourceLimit;
} LosProcessCB;

View File

@ -106,7 +106,10 @@ LITE_OS_SEC_TEXT_INIT VOID OsCpupGuard(VOID)
for (loop = 0; loop < g_processMaxNum; loop++) {
processCB = OS_PCB_FROM_PID(loop);
processCB->processCpup.historyTime[prevPos] = processCB->processCpup.allTime;
if (processCB->processCpup == NULL) {
continue;
}
processCB->processCpup->historyTime[prevPos] = processCB->processCpup->allTime;
}
for (loop = 0; loop < g_taskMaxNum; loop++) {
@ -129,7 +132,7 @@ LITE_OS_SEC_TEXT_INIT VOID OsCpupGuard(VOID)
#endif
taskCB->taskCpup.historyTime[prevPos] += cycleIncrement;
processCB = OS_PCB_FROM_PID(taskCB->processID);
processCB->processCpup.historyTime[prevPos] += cycleIncrement;
processCB->processCpup->historyTime[prevPos] += cycleIncrement;
}
SCHEDULER_UNLOCK(intSave);
@ -209,7 +212,10 @@ LITE_OS_SEC_TEXT_INIT VOID LOS_CpupReset(VOID)
for (index = 0; index < g_processMaxNum; index++) {
processCB = OS_PCB_FROM_PID(index);
OsResetCpup(&processCB->processCpup, cycle);
if (processCB->processCpup == NULL) {
continue;
}
OsResetCpup(processCB->processCpup, cycle);
}
for (index = 0; index < g_taskMaxNum; index++) {
@ -242,7 +248,7 @@ VOID OsCpupCycleEndStart(UINT32 runTaskID, UINT32 newTaskID)
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);
OsCpupBase *processCpup = OS_PCB_FROM_PID(runTask->processID)->processCpup;
UINT64 cpuCycle, cycleIncrement;
UINT16 cpuID = ArchCurrCpuid();
@ -258,7 +264,9 @@ VOID OsCpupCycleEndStart(UINT32 runTaskID, UINT32 newTaskID)
timeInIrqSwitch[cpuID] = 0;
#endif
runTaskCpup->allTime += cycleIncrement;
processCpup->allTime += cycleIncrement;
if (processCpup != NULL) {
processCpup->allTime += cycleIncrement;
}
runTaskCpup->startTime = 0;
}
@ -326,7 +334,7 @@ STATIC UINT32 OsHistorySysCpuUsageUnsafe(UINT16 mode)
cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
idleProcessID = OsGetIdleProcessID();
processCpup = (OsCpupBase *)&(OS_PCB_FROM_PID(idleProcessID)->processCpup);
processCpup = OS_PCB_FROM_PID(idleProcessID)->processCpup;
return (LOS_CPUP_PRECISION - OsCalculateCpupUsage(processCpup, pos, prePos, cpuAllCycle));
}
@ -361,10 +369,14 @@ STATIC UINT32 OsHistoryProcessCpuUsageUnsafe(UINT32 pid, UINT16 mode)
return LOS_ERRNO_CPUP_NO_CREATED;
}
if (processCB->processCpup == NULL) {
return LOS_ERRNO_CPUP_ID_INVALID;
}
OsCpupGetPos(mode, &pos, &prePos);
cpuAllCycle = cpuHistoryTime[pos] - cpuHistoryTime[prePos];
return OsCalculateCpupUsage(&processCB->processCpup, pos, prePos, cpuAllCycle);
return OsCalculateCpupUsage(processCB->processCpup, pos, prePos, cpuAllCycle);
}
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_HistoryProcessCpuUsage(UINT32 pid, UINT16 mode)
@ -446,11 +458,11 @@ LITE_OS_SEC_TEXT_MINOR UINT32 OsGetAllProcessCpuUsageUnsafe(UINT16 mode, CPUP_IN
for (processID = 0; processID < g_processMaxNum; processID++) {
processCB = OS_PCB_FROM_PID(processID);
if (OsProcessIsUnused(processCB)) {
if (OsProcessIsUnused(processCB) || (processCB->processCpup == NULL)) {
continue;
}
cpupInfo[processID].usage = OsCalculateCpupUsage(&processCB->processCpup, pos, prePos, cpuAllCycle);
cpupInfo[processID].usage = OsCalculateCpupUsage(processCB->processCpup, pos, prePos, cpuAllCycle);
cpupInfo[processID].status = OS_CPUP_USED;
}
@ -496,9 +508,11 @@ LITE_OS_SEC_TEXT_MINOR UINT32 OsGetAllProcessAndTaskCpuUsageUnsafe(UINT16 mode,
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;
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;
}
}
}