LCOV - code coverage report
Current view: top level - cpu_detect - cpu_detect_core.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 87.1 % 248 216
Test Date: 2026-08-31 10:07:06 Functions: 96.2 % 26 25

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include "cpu_detect_core.h"
      12              : #include <stdlib.h>
      13              : #define __USE_GNU
      14              : #define _GNU_SOURCE
      15              : #include <sched.h>
      16              : #include <stdio.h>
      17              : #include <stdbool.h>
      18              : #include <errno.h>
      19              : #include <pthread.h>
      20              : #include <limits.h>
      21              : #include <sys/time.h>
      22              : #include <sys/prctl.h>
      23              : #include "cpu_detect_print.h"
      24              : #include "cpu_detect_test.h"
      25              : #include "ascend_hal.h"
      26              : 
      27              : #define DEVICE_MAX_CPU_NUM 64U
      28              : #define THREAD_NAME_LENGTH 16U
      29              : 
      30              : typedef struct {
      31              :     int32_t cpuId;
      32              :     int32_t moduleType;
      33              : } CpuInfo;
      34              : 
      35              : typedef struct {
      36              :     CpuInfo cpu;
      37              :     int32_t tid;
      38              :     pthread_t task;
      39              :     int32_t result;
      40              : } CpuDetectThreadMgr;
      41              : 
      42              : typedef struct {
      43              :     bool stop;
      44              :     uint32_t devNum;
      45              :     uint32_t cpuNum;
      46              :     uint32_t timeout;
      47              :     struct timeval startTime;
      48              :     CpuDetectThreadMgr thread[DEVICE_MAX_CPU_NUM];
      49              : } CpuDetectMgr;
      50              : 
      51              : STATIC CpuDetectMgr g_cpuDetectMgr = {0};
      52              : 
      53            8 : STATIC INLINE bool CpuDetectIsStop(void) { return g_cpuDetectMgr.stop; }
      54              : 
      55            0 : STATIC INLINE void CpuDetectSetStop(void)
      56              : {
      57            0 :     if (!g_cpuDetectMgr.stop) {
      58            0 :         g_cpuDetectMgr.stop = true;
      59              :     }
      60            0 : }
      61              : 
      62           16 : STATIC INLINE struct timeval* CpuDetectGetStartTime(void) { return &g_cpuDetectMgr.startTime; }
      63              : 
      64            8 : STATIC INLINE uint32_t CpuDetectGetTimeout(void) { return g_cpuDetectMgr.timeout; }
      65              : 
      66            1 : STATIC INLINE CpudStatus CpuDetectGetResult(void)
      67              : {
      68            1 :     CpudStatus ret = CPUD_SUCCESS;
      69            9 :     for (uint32_t i = 0; i < g_cpuDetectMgr.cpuNum && i < DEVICE_MAX_CPU_NUM; i++) {
      70            8 :         if (g_cpuDetectMgr.thread[i].result != CPUD_SUCCESS) {
      71            0 :             ret = (g_cpuDetectMgr.thread[i].result > ret) ? g_cpuDetectMgr.thread[i].result : ret;
      72              :         }
      73              :     }
      74            1 :     return ret;
      75              : }
      76              : 
      77            8 : STATIC INLINE int32_t CpuDetectGetTid(void) { return (int32_t)syscall(SYS_gettid); }
      78              : 
      79            8 : STATIC CpudStatus CpuDetectBindCgroup(int32_t moduleType)
      80              : {
      81            8 :     if (moduleType == MODULE_TYPE_AICPU) {
      82            6 :         drvError_t drvRet = halBindCgroup(BIND_AICPU_CGROUP);
      83            6 :         if (drvRet != DRV_ERROR_NONE) {
      84            0 :             ADETECT_ERR("bind aicpu cgroup failed, ret[%d].", drvRet);
      85            0 :             return CPUD_FAILURE;
      86              :         }
      87            6 :         ADETECT_INF("bind aicpu cgroup success.");
      88            6 :         return CPUD_SUCCESS;
      89            2 :     } else if (moduleType == MODULE_TYPE_DCPU) {
      90            1 :         drvError_t drvRet = halBindCgroup(BIND_DATACPU_CGROUP);
      91            1 :         if (drvRet != DRV_ERROR_NONE) {
      92            0 :             ADETECT_ERR("bind datacpu cgroup failed, ret[%d]", drvRet);
      93            0 :             return CPUD_FAILURE;
      94              :         }
      95            1 :         ADETECT_INF("bind datacpu cgroup success.");
      96            1 :         return CPUD_SUCCESS;
      97              :     } else {
      98            1 :         ADETECT_INF("use default cgroup: ccpu_cgroup.");
      99            1 :         return CPUD_SUCCESS;
     100              :     }
     101              : }
     102              : 
     103            8 : STATIC CpudStatus CpuDetectSetAffinity(const CpuInfo* info)
     104              : {
     105            8 :     CpudStatus ret = CpuDetectBindCgroup(info->moduleType);
     106            8 :     if (ret != CPUD_SUCCESS) {
     107            0 :         return CPUD_ERROR_CGROUP_BIND;
     108              :     }
     109              : 
     110              :     cpu_set_t mask;
     111            8 :     CPU_ZERO(&mask);
     112            8 :     CPU_SET(info->cpuId, &mask);
     113            8 :     int32_t err = pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask);
     114            8 :     if (err != 0) {
     115            0 :         ADETECT_ERR(
     116              :             "thread(%d) can not set cpu(%d) setaffinity, errno:%d, err:%s", CpuDetectGetTid(), info->cpuId, errno,
     117              :             strerror(errno));
     118            0 :         return CPUD_ERROR_CPU_AFFINITY;
     119              :     }
     120            8 :     ADETECT_INF("thread(%d) set cpu(%d) setaffinity success", CpuDetectGetTid(), info->cpuId);
     121            8 :     return CPUD_SUCCESS;
     122              : }
     123              : 
     124            8 : STATIC INLINE bool CpuDetectCheckTime(struct timeval* start, uint32_t timeout)
     125              : {
     126            8 :     struct timeval end = {0};
     127            8 :     gettimeofday(&end, NULL);
     128            8 :     if ((end.tv_sec - start->tv_sec) >= timeout) {
     129            8 :         return true;
     130              :     }
     131            0 :     return false;
     132              : }
     133              : 
     134            8 : STATIC INLINE int64_t CpuDetectGetRuntime(struct timeval* start)
     135              : {
     136            8 :     struct timeval end = {0};
     137            8 :     gettimeofday(&end, NULL);
     138            8 :     int64_t time = end.tv_sec - start->tv_sec;
     139            8 :     return (time == 0 ? 1 : time);
     140              : }
     141              : 
     142            8 : STATIC CpudStatus CpuDetectThreadEntry(CpuInfo* info)
     143              : {
     144            8 :     uint32_t* regValues = (uint32_t*)malloc(TEMP_BUF_SIZE);
     145            8 :     if (regValues == NULL) {
     146            0 :         ADETECT_ERR("malloc regValues failed.");
     147            0 :         return CPUD_ERROR_MALLOC;
     148              :     }
     149              : 
     150            8 :     uint32_t* loadStoreBuf = (uint32_t*)malloc(TEMP_BUF_SIZE);
     151            8 :     if (loadStoreBuf == NULL) {
     152            0 :         ADETECT_ERR("malloc loadStoreBuf failed.");
     153            0 :         free(regValues);
     154            0 :         return CPUD_ERROR_MALLOC;
     155              :     }
     156              : 
     157            8 :     int64_t cycle = 0;
     158            8 :     CpudStatus ret = CPUD_SUCCESS;
     159            8 :     while (!CpuDetectIsStop()) {
     160            8 :         cycle++;
     161            8 :         ret = CpuDetectGroup(regValues, loadStoreBuf, info->cpuId);
     162            8 :         if (ret != CPUD_SUCCESS) {
     163            0 :             int64_t run = CpuDetectGetRuntime(CpuDetectGetStartTime());
     164            0 :             ADETECT_ERR(
     165              :                 "cpu(%d) detect failed and exit after running %lld cycles for %lld seconds.", info->cpuId, cycle, run);
     166            0 :             break;
     167              :         }
     168              : 
     169            8 :         bool result = CpuDetectCheckTime(CpuDetectGetStartTime(), CpuDetectGetTimeout());
     170            8 :         if (result || (cycle >= INT_MAX)) {
     171            8 :             int64_t run = CpuDetectGetRuntime(CpuDetectGetStartTime());
     172            8 :             ADETECT_RUN_INF(
     173              :                 "cpu(%d) detect success and exit after running %lld cycles for %lld seconds.", info->cpuId, cycle, run);
     174            8 :             break;
     175              :         }
     176              :     }
     177            8 :     free(regValues);
     178            8 :     free(loadStoreBuf);
     179            8 :     return ret;
     180              : }
     181              : 
     182           15 : STATIC INLINE void CpuDetectSetName(const char* name)
     183              : {
     184           15 :     int32_t ret = prctl(PR_SET_NAME, (unsigned long)(uintptr_t)name);
     185           15 :     ADETECT_CHK_EXPR(ret != EOK, "cpu detect set name(%s) failed with %d.", name, ret);
     186           15 : }
     187              : 
     188            8 : STATIC INLINE void CpuDetectSetThreadName(int32_t cpuId)
     189              : {
     190            8 :     char threadName[THREAD_NAME_LENGTH] = {0};
     191            8 :     int32_t ret = snprintf_s(threadName, THREAD_NAME_LENGTH, THREAD_NAME_LENGTH - 1U, "cpu_detect_%d", cpuId);
     192            8 :     ADETECT_CHK_EXPR(ret == -1, "cpu detect set thread name(%s) failed, snprintf_s err=%d.", threadName, ret);
     193              : 
     194            8 :     CpuDetectSetName((const char*)threadName);
     195            8 : }
     196              : 
     197            8 : STATIC void* CpuDetectThread(void* arg)
     198              : {
     199            8 :     ADETECT_CHK_NULL_PTR(arg, return NULL);
     200              : 
     201            8 :     CpuDetectThreadMgr* thread = (CpuDetectThreadMgr*)arg;
     202            8 :     ADETECT_CHK_EXPR_ACTION(
     203              :         thread->cpu.cpuId < 0, return NULL, "cpu detect thread(%d), cpuId(%d) is invalid.", thread->tid,
     204              :         thread->cpu.cpuId);
     205            8 :     thread->tid = CpuDetectGetTid();
     206            8 :     ADETECT_INF("cpu(%d) detect thread(%d) create success.", thread->cpu.cpuId, thread->tid);
     207            8 :     CpuDetectSetThreadName(thread->cpu.cpuId);
     208              : 
     209            8 :     CpudStatus ret = CpuDetectSetAffinity(&thread->cpu);
     210            8 :     if (ret != CPUD_SUCCESS) {
     211            0 :         thread->result = ret;
     212            0 :         CpuDetectSetStop();
     213            0 :         return NULL;
     214              :     }
     215              : 
     216            8 :     ret = CpuDetectThreadEntry(&thread->cpu);
     217            8 :     if (ret != CPUD_SUCCESS) {
     218            0 :         thread->result = ret;
     219            0 :         CpuDetectSetStop();
     220            0 :         return NULL;
     221              :     }
     222            8 :     return NULL;
     223              : }
     224              : 
     225            8 : STATIC CpudStatus CpuDetectCreateThread(CpuDetectThreadMgr* thread)
     226              : {
     227            8 :     int32_t ret = pthread_create(&thread->task, NULL, CpuDetectThread, (void*)thread);
     228            8 :     if (ret != 0) {
     229            0 :         CpuDetectSetStop();
     230            0 :         thread->result = CPUD_ERROR_CREATE_THREAD;
     231            0 :         return CPUD_ERROR_CREATE_THREAD;
     232              :     }
     233              : 
     234            8 :     return CPUD_SUCCESS;
     235              : }
     236              : 
     237            8 : STATIC void CpuDetectReleaseThread(CpuDetectThreadMgr* thread)
     238              : {
     239            8 :     if (thread->task != 0) {
     240            8 :         int32_t ret = pthread_join(thread->task, NULL);
     241            8 :         ADETECT_CHK_EXPR_ACTION(
     242              :             ret != 0, return, "cpu(%d) detect can not join thread(%d), error=%d, strerr=%s.", thread->cpu.cpuId,
     243              :             thread->tid, errno, strerror(errno));
     244            8 :         ADETECT_INF("cpu(%d) detect thread(%d) release success.", thread->cpu.cpuId, thread->tid);
     245              :     }
     246              : }
     247              : 
     248            7 : STATIC CpudStatus CpuDetectGetDevNum(uint32_t* deviceNum)
     249              : {
     250            7 :     drvError_t drvRet = drvGetDevNum(deviceNum);
     251            7 :     if (drvRet != DRV_ERROR_NONE) {
     252            1 :         ADETECT_ERR("call drvGetDevNum failed with %d.", (int32_t)drvRet);
     253            1 :         return CPUD_FAILURE;
     254              :     }
     255            6 :     return CPUD_SUCCESS;
     256              : }
     257              : 
     258            1 : STATIC CpudStatus CpuDetectGetAicpuInfo(uint32_t deviceId, uint32_t cpuNum, CpuDetectThreadMgr* thread, uint32_t size)
     259              : {
     260            1 :     int64_t aicpuNum = 0;
     261            1 :     int64_t cpuBitMap = 0;
     262            1 :     drvError_t ret = halGetDeviceInfo(deviceId, MODULE_TYPE_AICPU, INFO_TYPE_PF_CORE_NUM, &aicpuNum);
     263            1 :     ADETECT_CHK_EXPR_ACTION(
     264              :         (ret != DRV_ERROR_NONE) || (aicpuNum <= 0) || (aicpuNum >= cpuNum), return CPUD_FAILURE,
     265              :         "device[%u] aicpu detect call halGetDeviceInfo failed with %d, cpu num(%lld).", deviceId, (int32_t)ret,
     266              :         aicpuNum);
     267              : 
     268            1 :     ret = halGetDeviceInfo(deviceId, MODULE_TYPE_AICPU, INFO_TYPE_PF_OCCUPY, &cpuBitMap);
     269            1 :     ADETECT_CHK_EXPR_ACTION(
     270              :         ret != DRV_ERROR_NONE, return CPUD_FAILURE, "device[%u] aicpu detect call halGetDeviceInfo failed with %d.",
     271              :         deviceId, (int32_t)ret);
     272              : 
     273            1 :     ADETECT_INF("device[%u] aicpu detect get info: cpuBitMap[%lld], cpuNum[%lld].", deviceId, cpuBitMap, aicpuNum);
     274           65 :     for (uint32_t i = 0; i < size; i++) {
     275           64 :         if ((cpuBitMap & 0x1LL) != 0LL) {
     276            6 :             uint32_t cpuId = (uint32_t)(deviceId * cpuNum) + i;
     277            6 :             ADETECT_CHK_EXPR_ACTION(
     278              :                 cpuId >= size, return CPUD_FAILURE,
     279              :                 "device[%u] aicpu detect get info failed, cpu num(%lld), cpu id(%u).", deviceId, aicpuNum, cpuId);
     280            6 :             thread[cpuId].cpu.moduleType = MODULE_TYPE_AICPU;
     281            6 :             thread[cpuId].cpu.cpuId = (int32_t)cpuId;
     282            6 :             ADETECT_INF("device[%u] cpu detect get info: aicpu id(%u).", deviceId, cpuId);
     283              :         }
     284           64 :         cpuBitMap = cpuBitMap >> 1;
     285              :     }
     286            1 :     return CPUD_SUCCESS;
     287              : }
     288              : 
     289            3 : STATIC CpudStatus CpuDetectGetCcpuInfo(uint32_t deviceId, uint32_t cpuNum, CpuDetectThreadMgr* thread, uint32_t size)
     290              : {
     291            3 :     int64_t ccpuNum = 0;
     292            3 :     drvError_t ret = halGetDeviceInfo(deviceId, MODULE_TYPE_CCPU, INFO_TYPE_CORE_NUM, &ccpuNum);
     293            3 :     ADETECT_CHK_EXPR_ACTION(
     294              :         (ret != DRV_ERROR_NONE) || (ccpuNum == 0), return CPUD_FAILURE,
     295              :         "device[%u] ccpu detect call halGetDeviceInfo failed with %d.", deviceId, (int32_t)ret);
     296              : 
     297            6 :     for (int32_t i = 0; i < ccpuNum; i++) {
     298            3 :         uint32_t cpuId = (uint32_t)(deviceId * cpuNum) + i;
     299            3 :         ADETECT_CHK_EXPR_ACTION(
     300              :             cpuId >= size, return CPUD_FAILURE, "device[%u] ccpu detect get info failed, cpu num(%lld), cpu id(%u).",
     301              :             deviceId, ccpuNum, cpuId);
     302            3 :         thread[cpuId].cpu.moduleType = MODULE_TYPE_CCPU;
     303            3 :         thread[cpuId].cpu.cpuId = (int32_t)cpuId;
     304            3 :         ADETECT_INF("device[%u] cpu detect get info: ccpu id(%u).", deviceId, cpuId);
     305              :     }
     306            3 :     return CPUD_SUCCESS;
     307              : }
     308              : 
     309            2 : STATIC CpudStatus CpuDetectGetDcpuInfo(uint32_t deviceId, uint32_t cpuNum, CpuDetectThreadMgr* thread, uint32_t size)
     310              : {
     311            2 :     int64_t ccpuNum = 0;
     312            2 :     int64_t dcpuNum = 0;
     313              : 
     314            2 :     drvError_t ret = halGetDeviceInfo(deviceId, MODULE_TYPE_CCPU, INFO_TYPE_CORE_NUM, &ccpuNum);
     315            2 :     ADETECT_CHK_EXPR_ACTION(
     316              :         ret != DRV_ERROR_NONE, return CPUD_FAILURE, "device[%u] ccpu detect call halGetDeviceInfo failed with %d.",
     317              :         deviceId, (int32_t)ret);
     318              : 
     319            2 :     ret = halGetDeviceInfo(deviceId, MODULE_TYPE_DCPU, INFO_TYPE_CORE_NUM, &dcpuNum);
     320            2 :     ADETECT_CHK_EXPR_ACTION(
     321              :         ret != DRV_ERROR_NONE, return CPUD_FAILURE, "device[%u] dcpu detect call halGetDeviceInfo failed with %d.",
     322              :         deviceId, (int32_t)ret);
     323              : 
     324            4 :     for (uint32_t i = 0; i < dcpuNum; i++) {
     325            2 :         uint32_t cpuId = (uint32_t)(deviceId * cpuNum) + (uint32_t)ccpuNum + i;
     326            2 :         ADETECT_CHK_EXPR_ACTION(
     327              :             cpuId >= size, return CPUD_FAILURE, "device[%u] dcpu detect get info failed, cpu num(%lld), cpu id(%u).",
     328              :             deviceId, dcpuNum, cpuId);
     329              : 
     330            2 :         thread[cpuId].cpu.moduleType = MODULE_TYPE_DCPU;
     331            2 :         thread[cpuId].cpu.cpuId = (int32_t)cpuId;
     332            2 :         ADETECT_INF("device[%u] cpu detect get info: dcpu id(%u).", deviceId, cpuId);
     333              :     }
     334            2 :     return CPUD_SUCCESS;
     335              : }
     336              : 
     337            6 : STATIC int32_t CpuDetectGetCpuNum(uint32_t deviceId)
     338              : {
     339            6 :     int64_t cpuNum = 0;
     340            6 :     int32_t totalCpuNum = 0;
     341              : 
     342            6 :     drvError_t ret = halGetDeviceInfo(deviceId, MODULE_TYPE_AICPU, INFO_TYPE_PF_CORE_NUM, &cpuNum);
     343            6 :     ADETECT_CHK_EXPR_ACTION(
     344              :         (ret != DRV_ERROR_NONE) || (cpuNum == 0), return CPUD_FAILURE, "device[%u] get aicpu num failed with %d.",
     345              :         deviceId, (int32_t)ret);
     346            4 :     totalCpuNum += (int32_t)cpuNum;
     347            4 :     ADETECT_INF("device[%u] cpu detect get info: aicpu cpu num(%lld).", deviceId, cpuNum);
     348              : 
     349            4 :     ret = halGetDeviceInfo(deviceId, MODULE_TYPE_CCPU, INFO_TYPE_CORE_NUM, &cpuNum);
     350            4 :     ADETECT_CHK_EXPR_ACTION(
     351              :         (ret != DRV_ERROR_NONE) || (cpuNum == 0), return CPUD_FAILURE, "device[%u] get ccpu num failed with %d.",
     352              :         deviceId, (int32_t)ret);
     353            4 :     totalCpuNum += (int32_t)cpuNum;
     354            4 :     ADETECT_INF("device[%u] cpu detect get info: ccpu cpu num(%lld).", deviceId, cpuNum);
     355              : 
     356            4 :     ret = halGetDeviceInfo(deviceId, MODULE_TYPE_DCPU, INFO_TYPE_CORE_NUM, &cpuNum);
     357            4 :     ADETECT_CHK_EXPR_ACTION(
     358              :         ret != DRV_ERROR_NONE, return CPUD_FAILURE, "device[%u] get dcpu num failed with %d.", deviceId, (int32_t)ret);
     359            4 :     totalCpuNum += (int32_t)cpuNum;
     360            4 :     ADETECT_INF("device[%u] cpu detect get info: dcpu cpu num(%lld).", deviceId, cpuNum);
     361            4 :     return totalCpuNum;
     362              : }
     363              : 
     364            6 : STATIC int32_t CpuDetectGetDeviceCpuInfo(uint32_t deviceId, CpuDetectThreadMgr* thread, uint32_t size)
     365              : {
     366            6 :     int32_t num = CpuDetectGetCpuNum(deviceId);
     367            6 :     if (num <= 0) {
     368            2 :         ADETECT_ERR("device[%u] cpu detect get info failed with %d.", deviceId, num);
     369            2 :         return CPUD_FAILURE;
     370              :     }
     371            4 :     ADETECT_INF("device[%u] cpu detect get info: total cpu num(%d).", deviceId, num);
     372            4 :     uint32_t cpuNum = (uint32_t)num;
     373              :     // cpu分段为:ccpu - dcpu - aicpu
     374            4 :     int32_t ret = CpuDetectGetCcpuInfo(deviceId, cpuNum, thread, size);
     375            4 :     if (ret == CPUD_FAILURE) {
     376            1 :         return CPUD_FAILURE;
     377              :     }
     378              : 
     379            3 :     ret = CpuDetectGetDcpuInfo(deviceId, cpuNum, thread, size);
     380            3 :     if (ret == CPUD_FAILURE) {
     381            1 :         return CPUD_FAILURE;
     382              :     }
     383              : 
     384            2 :     ret = CpuDetectGetAicpuInfo(deviceId, cpuNum, thread, size);
     385            2 :     if (ret == CPUD_FAILURE) {
     386            1 :         return CPUD_FAILURE;
     387              :     }
     388            1 :     return num;
     389              : }
     390              : 
     391            6 : STATIC CpudStatus CpuDetectGetCpuInfo(uint32_t deviceNum, uint32_t* cpuNum, CpuDetectThreadMgr* thread, uint32_t size)
     392              : {
     393            6 :     uint32_t num = 0;
     394            7 :     for (uint32_t i = 0; i < deviceNum; i++) {
     395            6 :         int32_t ret = CpuDetectGetDeviceCpuInfo(i, thread, size);
     396            6 :         if (ret == CPUD_FAILURE) {
     397            5 :             return CPUD_FAILURE;
     398              :         }
     399            1 :         num += (uint32_t)ret;
     400              :     }
     401            1 :     *cpuNum = num;
     402            1 :     return CPUD_SUCCESS;
     403              : }
     404              : 
     405            7 : STATIC CpudStatus CpuDetectInit(uint32_t timeout)
     406              : {
     407              :     // timeout校验
     408            7 :     g_cpuDetectMgr.stop = false;
     409            7 :     g_cpuDetectMgr.timeout = timeout;
     410            7 :     gettimeofday(&g_cpuDetectMgr.startTime, NULL);
     411              : 
     412              :     // 获取dev个数
     413            7 :     CpudStatus ret = CpuDetectGetDevNum(&g_cpuDetectMgr.devNum);
     414            7 :     ADETECT_CHK_EXPR_ACTION(
     415              :         (ret != CPUD_SUCCESS) || (g_cpuDetectMgr.devNum == 0), return CPUD_FAILURE, "get dev num(%u) failed with %d.",
     416              :         g_cpuDetectMgr.devNum, ret);
     417              : 
     418              :     // 获取cpu个数和类型
     419            6 :     g_cpuDetectMgr.cpuNum = 0;
     420          390 :     for (uint32_t i = 0; i < DEVICE_MAX_CPU_NUM; i++) {
     421          384 :         g_cpuDetectMgr.thread[i].cpu.cpuId = -1;
     422          384 :         g_cpuDetectMgr.thread[i].cpu.moduleType = -1;
     423          384 :         g_cpuDetectMgr.thread[i].tid = 0;
     424          384 :         g_cpuDetectMgr.thread[i].task = 0;
     425          384 :         g_cpuDetectMgr.thread[i].result = 0;
     426              :     }
     427            6 :     ret = CpuDetectGetCpuInfo(g_cpuDetectMgr.devNum, &g_cpuDetectMgr.cpuNum, g_cpuDetectMgr.thread, DEVICE_MAX_CPU_NUM);
     428            6 :     ADETECT_CHK_EXPR_ACTION(
     429              :         (ret != CPUD_SUCCESS) || (g_cpuDetectMgr.cpuNum == 0), return CPUD_FAILURE,
     430              :         "get cpu info failed with %d, num(%u).", ret, g_cpuDetectMgr.cpuNum);
     431              : 
     432            1 :     ADETECT_INF("cpu detect get info: devNum(%u), cpuNum(%u).", g_cpuDetectMgr.devNum, g_cpuDetectMgr.cpuNum);
     433            1 :     return CPUD_SUCCESS;
     434              : }
     435              : 
     436            1 : STATIC void CpuDetectExit(void)
     437              : {
     438            1 :     size_t size = sizeof(CpuDetectMgr);
     439            1 :     (void)memset_s(&g_cpuDetectMgr, size, 0, size);
     440            1 :     return;
     441              : }
     442              : 
     443            7 : CpudStatus CpuDetectProcess(uint32_t timeout)
     444              : {
     445            7 :     CpuDetectSetName("cpu_detect_process.");
     446            7 :     CpudStatus ret = CpuDetectInit(timeout);
     447            7 :     if (ret != CPUD_SUCCESS) {
     448            6 :         ADETECT_ERR("cpu detect init failed with %d.", ret);
     449            6 :         return CPUD_ERROR_INIT;
     450              :     }
     451              : 
     452              :     // 启动线程
     453            9 :     for (uint32_t i = 0; i < g_cpuDetectMgr.cpuNum && i < DEVICE_MAX_CPU_NUM; i++) {
     454            8 :         ret = CpuDetectCreateThread(&g_cpuDetectMgr.thread[i]);
     455            8 :         if (ret != CPUD_SUCCESS) {
     456            0 :             ADETECT_ERR(
     457              :                 "cpu(%d) create thread failed, err: %d(%s)", g_cpuDetectMgr.thread[i].cpu.cpuId, errno,
     458              :                 strerror(errno));
     459            0 :             break;
     460              :         }
     461              :     }
     462              : 
     463              :     // 回收线程
     464            9 :     for (uint32_t j = 0; j < g_cpuDetectMgr.cpuNum && j < DEVICE_MAX_CPU_NUM; j++) {
     465            8 :         CpuDetectReleaseThread(&g_cpuDetectMgr.thread[j]);
     466              :     }
     467            1 :     ret = CpuDetectGetResult();
     468            1 :     CpuDetectExit();
     469            1 :     return ret;
     470              : }
        

Generated by: LCOV version 2.0-1