鸿蒙轻内核M核源码分析系列十五 CPU使用率CPUP(2)
3.2.2 LOS_HistorySysCpuUsage
该函数获取系统历史CPU占用率,对于历史CPU占用率,需要传入时间间隔模式参数,支持10秒、1秒、小于1秒三种。
⑴处先判断CPUP
是否已经初始化,如果没有初始化过,返回错误码。⑵处调用函数OsTskCycleEnd()
获取当前任务的结束时间,并计算出运行总时间。⑶处调用函数OsGetPositions()
计算出历史运行时间数组索引位置。⑷处计算出各个任务的周期内运行总时间,如果时间间隔模式为1秒,取值两个历史运行时间之差,即为1秒内任务的运行时间数。对于时间间隔模式为10秒,historyTime[curPos]
表示10秒前的自系统启动以来的任务运行的时间数,计算出来的差值即为10秒内任务的运行时间数。对于时间间隔模式为小于1秒,historyTime[curPos]
表示上一秒前的自系统启动以来的任务运行的时间数,计算出来的差值即为小于1秒内任务的运行时间数。⑸处计算空闲任务周期内运行总时间。⑹处如果总时间不为0,计算出系统的任务历史CPU占用率。最后,调用函数OsTskCycleStart()
设置新任务的CPUP
统计的开始时间。可以参考示意图进行理解:
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_HistorySysCpuUsage(UINT16 mode)
{
UINT64 cpuCycleAll = 0;
UINT64 idleCycleAll = 0;
UINT32 cpupRet = 0;
UINT16 loopNum;
UINT16 curPos;
UINT16 prePos = 0;
UINT32 intSave;
⑴ if (g_cpupInitFlg == 0) {
return LOS_ERRNO_CPUP_NO_INIT;
}
// get end time of current task
intSave = LOS_IntLock();
⑵ OsTskCycleEnd();
⑶ OsGetPositions(mode, &curPos, &prePos);
for (loopNum = 0; loopNum < g_taskMaxNum; loopNum++) {
⑷ if (mode == CPUP_IN_1S) {
cpuCycleAll += g_cpup[loopNum].historyTime[curPos] - g_cpup[loopNum].historyTime[prePos];
} else {
cpuCycleAll += g_cpup[loopNum].allTime - g_cpup[loopNum].historyTime[curPos];
}
}
⑸ if (mode == CPUP_IN_1S) {
idleCycleAll += g_cpup[g_idleTaskID].historyTime[curPos] -
g_cpup[g_idleTaskID].historyTime[prePos];
} else {
idleCycleAll += g_cpup[g_idleTaskID].allTime - g_cpup[g_idleTaskID].historyTime[curPos];
}
⑹ if (cpuCycleAll) {
cpupRet = (LOS_CPUP_PRECISION - (UINT32)((LOS_CPUP_PRECISION * idleCycleAll) / cpuCycleAll));
}
OsTskCycleStart();
LOS_IntRestore(intSave);
return cpupRet;
}
3.2.3 LOS_TaskCpuUsage
该函数会统计指定任务的CPU占用率,和函数LOS_SysCpuUsage()
代码相似度高,可以参考上文对该函数的讲解。
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_TaskCpuUsage(UINT32 taskID)
{
UINT64 cpuCycleAll = 0;
UINT16 loopNum;
UINT32 intSave;
UINT32 cpupRet = 0;
if (g_cpupInitFlg == 0) {
return LOS_ERRNO_CPUP_NO_INIT;
}
if (OS_TSK_GET_INDEX(taskID) >= g_taskMaxNum) {
return LOS_ERRNO_CPUP_TSK_ID_INVALID;
}
if (g_cpup[taskID].cpupID != taskID) {
return LOS_ERRNO_CPUP_THREAD_NO_CREATED;
}
if ((g_cpup[taskID].status & OS_TASK_STATUS_UNUSED) || (g_cpup[taskID].status == 0)) {
return LOS_ERRNO_CPUP_THREAD_NO_CREATED;
}
intSave = LOS_IntLock();
OsTskCycleEnd();
for (loopNum = 0; loopNum < g_taskMaxNum; loopNum++) {
if ((g_cpup[loopNum].status & OS_TASK_STATUS_UNUSED) || (g_cpup[loopNum].status == 0)) {
continue;
}
cpuCycleAll += g_cpup[loopNum].allTime;
}
if (cpuCycleAll) {
cpupRet = (UINT32)((LOS_CPUP_PRECISION * g_cpup[taskID].allTime) / cpuCycleAll);
}
OsTskCycleStart();
LOS_IntRestore(intSave);
return cpupRet;
}
3.2.4 LOS_HistoryTaskCpuUsage
该函数获取指定任务的历史CPU占用率,和函数LOS_HistorySysCpuUsage()
代码相似度高,可以参考上文对该函数的讲解。
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_HistoryTaskCpuUsage(UINT32 taskID, UINT16 mode)
{
UINT64 cpuCycleAll = 0;
UINT64 cpuCycleCurTsk = 0;
UINT16 loopNum, curPos;
UINT16 prePos = 0;
UINT32 intSave;
UINT32 cpupRet = 0;
if (g_cpupInitFlg == 0) {
return LOS_ERRNO_CPUP_NO_INIT;
}
if (OS_TSK_GET_INDEX(taskID) >= g_taskMaxNum) {
return LOS_ERRNO_CPUP_TSK_ID_INVALID;
}
if (g_cpup[taskID].cpupID != taskID) {
return LOS_ERRNO_CPUP_THREAD_NO_CREATED;
}
if ((g_cpup[taskID].status & OS_TASK_STATUS_UNUSED) || (g_cpup[taskID].status == 0)) {
return LOS_ERRNO_CPUP_THREAD_NO_CREATED;
}
intSave = LOS_IntLock();
OsTskCycleEnd();
OsGetPositions(mode, &curPos, &prePos);
for (loopNum = 0; loopNum < g_taskMaxNum; loopNum++) {
if ((g_cpup[loopNum].status & OS_TASK_STATUS_UNUSED) || (g_cpup[loopNum].status == 0)) {
continue;
}
if (mode == CPUP_IN_1S) {
cpuCycleAll += g_cpup[loopNum].historyTime[curPos] - g_cpup[loopNum].historyTime[prePos];
} else {
cpuCycleAll += g_cpup[loopNum].allTime - g_cpup[loopNum].historyTime[curPos];
}
}
if (mode == CPUP_IN_1S) {
cpuCycleCurTsk += g_cpup[taskID].historyTime[curPos] - g_cpup[taskID].historyTime[prePos];
} else {
cpuCycleCurTsk += g_cpup[taskID].allTime - g_cpup[taskID].historyTime[curPos];
}
if (cpuCycleAll) {
cpupRet = (UINT32)((LOS_CPUP_PRECISION * cpuCycleCurTsk) / cpuCycleAll);
}
OsTskCycleStart();
LOS_IntRestore(intSave);
return cpupRet;
}
3.2.5 LOS_AllTaskCpuUsage
该函数获取全部任务的CPU占用率,获取的CPU占用率信息保存在传出参数结构体CPUP_INFO_S *cpupInfo
指向的内存区域里,需要注意这个内存区域的大小需要等于sizeof(CPUP_INFO_S) * g_taskMaxNum
。还需要传入时间间隔模式参数,支持10秒、1秒、小于1秒三种。
⑴处先判断CPUP
是否已经初始化,如果没有初始化过,返回错误码。传出参数cpupInfo
指针不能为空,否则返回错误码。⑵处调用函数OsTskCycleEnd()
获取当前任务的结束时间,并计算出运行总时间。⑶处调用函数OsGetPositions()
计算出历史运行时间数组索引位置。⑷处计算出各个任务的周期内运行总时间,如果时间间隔模式为1秒,取值两个历史运行时间之差,否则取值XX。⑸处设置每一个任务的状态,然后计算出每一个任务的CPU占用率。最后,调用函数OsTskCycleStart()
设置新任务的CPUP
统计的开始时间。
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_AllTaskCpuUsage(CPUP_INFO_S *cpupInfo, UINT16 mode)
{
UINT16 loopNum;
UINT16 curPos;
UINT16 prePos = 0;
UINT32 intSave;
UINT64 cpuCycleAll = 0;
UINT64 cpuCycleCurTsk = 0;
⑴ if (g_cpupInitFlg == 0) {
return LOS_ERRNO_CPUP_NO_INIT;
}
if (cpupInfo == NULL) {
return LOS_ERRNO_CPUP_TASK_PTR_NULL;
}
intSave = LOS_IntLock();
⑵ OsTskCycleEnd();
⑶ OsGetPositions(mode, &curPos, &prePos);
for (loopNum = 0; loopNum < g_taskMaxNum; loopNum++) {
if ((g_cpup[loopNum].status & OS_TASK_STATUS_UNUSED) ||
(g_cpup[loopNum].status == 0)) {
continue;
}
if (mode == CPUP_IN_1S) {
cpuCycleAll += g_cpup[loopNum].historyTime[curPos] - g_cpup[loopNum].historyTime[prePos];
} else {
cpuCycleAll += g_cpup[loopNum].allTime - g_cpup[loopNum].historyTime[curPos];
}
}
⑷ for (loopNum = 0; loopNum < g_taskMaxNum; loopNum++) {
if ((g_cpup[loopNum].status & OS_TASK_STATUS_UNUSED) ||
(g_cpup[loopNum].status == 0)) {
continue;
}
if (mode == CPUP_IN_1S) {
cpuCycleCurTsk += g_cpup[loopNum].historyTime[curPos] - g_cpup[loopNum].historyTime[prePos];
} else {
cpuCycleCurTsk += g_cpup[loopNum].allTime - g_cpup[loopNum].historyTime[curPos];
}
⑸ cpupInfo[loopNum].usStatus = g_cpup[loopNum].status;
if (cpuCycleAll) {
cpupInfo[loopNum].uwUsage = (UINT32)((LOS_CPUP_PRECISION * cpuCycleCurTsk) / cpuCycleAll);
}
cpuCycleCurTsk = 0;
}
OsTskCycleStart();
LOS_IntRestore(intSave);
return LOS_OK;
}
3.2.6 LOS_CpupUsageMonitor
该函数获取历史CPU占用率并打印输出,传入参数有三个:CPU
占用率类型,CPUP
时间周期模式,指定的任务编号。对于任务CPU占用率,才需要指定有效的任务编号。
⑴处处理CPU
占用率类型为系统CPU占用率的情况,⑵处打印使用的CPUP
时间周期模式。⑶处通过调用函数LOS_HistorySysCpuUsage()
获取系统历史CPU占用率,然后执行⑷打印输出CPU占用率结果,输出结果范围为[0,100]。
⑸处处理CPU
占用率类型为指定任务CPU占用率的情况,首先判断下任务编号的有效性,校验任务是否创建等。⑹处打印使用的CPUP
时间周期模式。⑺处通过调用函数LOS_HistoryTaskCpuUsage()
获取指定任务的历史CPU占用率,然后执行⑻打印输出CPU占用率结果,输出结果范围为[0,100]。
LITE_OS_SEC_TEXT_MINOR UINT32 LOS_CpupUsageMonitor(CPUP_TYPE_E type, CPUP_MODE_E mode, UINT32 taskID)
{
UINT32 ret;
LosTaskCB *taskCB = NULL;
switch (type) {
⑴ case SYS_CPU_USAGE:
⑵ if (mode == CPUP_IN_10S) {
PRINTK("\nSysCpuUsage in 10s: ");
} else if (mode == CPUP_IN_1S) {
PRINTK("\nSysCpuUsage in 1s: ");
} else {
PRINTK("\nSysCpuUsage in <1s: ");
}
⑶ ret = LOS_HistorySysCpuUsage(mode);
⑷ PRINTK("%d.%d", ret / LOS_CPUP_PRECISION_MULT, ret % LOS_CPUP_PRECISION_MULT);
break;
⑸ case TASK_CPU_USAGE:
if (taskID > LOSCFG_BASE_CORE_TSK_LIMIT) {
PRINT_ERR("\nThe taskid is invalid.\n");
return OS_ERROR;
}
taskCB = OS_TCB_FROM_TID(taskID);
if ((taskCB->taskStatus & OS_TASK_STATUS_UNUSED)) {
PRINT_ERR("\nThe taskid is invalid.\n");
return OS_ERROR;
}
⑹ if (mode == CPUP_IN_10S) {
PRINTK("\nCPUusage of taskID %d in 10s: ", taskID);
} else if (mode == CPUP_IN_1S) {
PRINTK("\nCPUusage of taskID %d in 1s: ", taskID);
} else {
PRINTK("\nCPUusage of taskID %d in <1s: ", taskID);
}
⑺ ret = LOS_HistoryTaskCpuUsage(taskID, mode);
⑻ PRINTK("%u.%u", ret / LOS_CPUP_PRECISION_MULT, ret % LOS_CPUP_PRECISION_MULT);
break;
default:
PRINT_ERR("\nThe type is invalid.\n");
return OS_ERROR;
}
return LOS_OK;
}
小结
本文带领大家一起剖析了鸿蒙轻内核的CPUP扩展模块的源代码。感谢阅读,如有任何问题、建议,都可以博客下留言给我,谢谢。
- 点赞
- 收藏
- 关注作者
评论(0)