diff --git a/common/inc/pwrdata.h b/common/inc/pwrdata.h index 108f88f23655daaa39859bdbdc57a7a6a6309ac3..057607e5cb75212f6136f0239c06252773e5d9d1 100644 --- a/common/inc/pwrdata.h +++ b/common/inc/pwrdata.h @@ -15,19 +15,20 @@ #ifndef __POWERAPI_DATA_H__ #define __POWERAPI_DATA_H__ +#define MAX_ARCH_NAME_LEN 32 #define MAX_NAME_LEN 128 -#define MAX_CPU_LIST_LEN 256 +#define MAX_CPU_LIST_LEN 248 #define MAX_NUMA_NODE_NUM 16 -typedef struct PWR_CPU_NumaInfo{ +typedef struct PWR_CPU_NumaInfo { int nodeNo; char cpuList[MAX_CPU_LIST_LEN]; } PWR_CPU_NumaInfo; -typedef struct PWR_CPU_Info{ - int arch; - int byteOrder; +typedef struct PWR_CPU_Info { + char arch[MAX_ARCH_NAME_LEN]; char modelName[MAX_NAME_LEN]; + int byteOrder; int coreNum; char onlineList[MAX_CPU_LIST_LEN]; int threadsPerCore; @@ -38,10 +39,15 @@ typedef struct PWR_CPU_Info{ PWR_CPU_NumaInfo numa[MAX_NUMA_NODE_NUM]; } PWR_CPU_Info; +typedef struct PWR_CPU_CoreUsage { + int coreNo; + double usage; +} PWR_CPU_CoreUsage; -typedef struct CPUUsage { - // todo complete CPUUsage definition - int usage; -} CPUUsage; +typedef struct PWR_CPU_Usage { + double avgUsage; + int coreNum; + PWR_CPU_CoreUsage coreUsage[0]; +} PWR_CPU_Usage; #endif diff --git a/common/inc/pwrmsg.h b/common/inc/pwrmsg.h index 3fde0589ed9e74fb0c3a84025cebd5fa1400ad54..ae06170f8bf7920a39a14998d0e309005d5898cb 100644 --- a/common/inc/pwrmsg.h +++ b/common/inc/pwrmsg.h @@ -11,7 +11,7 @@ * Author: queyanwen * Create: 2022-06-23 * Description: Message struct and operations definition. These messages used for communication between - * PowerAPI.so and PowerAPI service. + * PowerAPI.so and PowerAPI service. * **************************************************************************** */ #ifndef __POWERAPI_PROTOCOL_H__ #define __POWERAPI_PROTOCOL_H__ @@ -47,8 +47,8 @@ typedef struct PwrMsg { } PwrMsg; enum OperationType { - CPU_GET_USAGE = 0, - CPU_GET_INFO, + CPU_GET_INFO = 1, + CPU_GET_USAGE, CPU_GET_CACHE_MISS, DISK_GET_IO_RATE = 30, // todo @@ -57,7 +57,7 @@ enum DataFormat { FMT_BIN = 0, }; enum MsgType { - MT_REQ = 0, // Request message + MT_REQ = 1, // Request message MT_RSP, // Response message MT_EVT, // event notification MT_MDT // metadata @@ -75,7 +75,7 @@ static inline void ReleasePwrMsg(PwrMsg **msg) } PwrMsg *ClonePwrMsg(PwrMsg *msg); -PwrMsg * CreateReqMsg(enum OperationType optType, uint32_t taskNo, uint32_t dataLen, char *data); +PwrMsg *CreateReqMsg(enum OperationType optType, uint32_t taskNo, uint32_t dataLen, char *data); int InitMsgFactory(); void DestroyMsgFactory(); int GenerateRspMsg(PwrMsg *req, PwrMsg *rsp, int rspCode, char *data, int dataLen); diff --git a/common/src/pwrmsg.c b/common/src/pwrmsg.c index b1ac1b4c92a0c97bf6dca64b10d93ef92dda6ce3..550201c07e5af200261d0b17d6c4f67f88ef87df 100644 --- a/common/src/pwrmsg.c +++ b/common/src/pwrmsg.c @@ -11,7 +11,7 @@ * Author: queyanwen * Create: 2022-06-23 * Description: Message struct and operations implementation. These messages used for communication between - * PowerAPI.so and PowerAPI service. + * PowerAPI.so and PowerAPI service. * **************************************************************************** */ #include "pwrmsg.h" #include @@ -58,10 +58,10 @@ static int GenerateReqMsgHead(MsgHead *head, enum OperationType optType, uint32_ return SUCCESS; } -PwrMsg * CreateReqMsg(enum OperationType optType, uint32_t taskNo, uint32_t dataLen, char *data) +PwrMsg *CreateReqMsg(enum OperationType optType, uint32_t taskNo, uint32_t dataLen, char *data) { - PwrMsg *req = (PwrMsg*) malloc(sizeof(PwrMsg)); - if(!req) { + PwrMsg *req = (PwrMsg *)malloc(sizeof(PwrMsg)); + if (!req) { return NULL; } @@ -122,7 +122,6 @@ int GenerateRspMsg(PwrMsg *req, PwrMsg *rsp, int rspCode, char *data, int dataLe rsp->head.seqId = req->head.seqId; rsp->head.taskNo = req->head.taskNo; rsp->head.crcMagic = GenerateCrcMagic(); - ; rsp->head.dataLen = dataLen; rsp->head.sysId = req->head.sysId; rsp->data = data; diff --git a/pwrapic/inc/powerapi.h b/pwrapic/inc/powerapi.h index 69243297da103c0954a8cf22938aae4e42a7990b..07fba96540f46aba5c8f15fc1fc1e437d3941e8e 100644 --- a/pwrapic/inc/powerapi.h +++ b/pwrapic/inc/powerapi.h @@ -16,6 +16,7 @@ #define __POWERAPI_H__ #include +#include #include "pwrerr.h" #include "pwrdata.h" @@ -33,7 +34,7 @@ PWR_API int PWR_UnRegister(); // CPU PWR_API int PWR_CPU_GetInfo(PWR_CPU_Info *cpuInfo); -PWR_API int PWR_CPU_GetUsage(CPUUsage *usage); +PWR_API int PWR_CPU_GetUsage(PWR_CPU_Usage *usage, uint32_t bufferSize); #ifdef __cplusplus } diff --git a/pwrapic/inc/pwrcpu.h b/pwrapic/inc/pwrcpu.h index 5c37c50ecd73d3a74f4abd206a5a220a34ade237..94fcded40a83273223a2ae4f592d948c7e2d8653 100644 --- a/pwrapic/inc/pwrcpu.h +++ b/pwrapic/inc/pwrcpu.h @@ -17,7 +17,7 @@ #include "powerapi.h" int GetCpuInfo(PWR_CPU_Info *cpuInfo); -int GetUsage(CPUUsage *usage); +int GetCpuUsage(PWR_CPU_Usage *usage, uint32_t bufferSize); #endif diff --git a/pwrapic/src/powerapi.c b/pwrapic/src/powerapi.c index 8481821ec32b5c7f15ec30ca0cf56d90583fa4df..1d99b03815222fbb01a85201d90ed9de1e445bfa 100644 --- a/pwrapic/src/powerapi.c +++ b/pwrapic/src/powerapi.c @@ -70,12 +70,16 @@ int PWR_CPU_GetInfo(PWR_CPU_Info *cpuInfo) return GetCpuInfo(cpuInfo); } -int PWR_CPU_GetUsage(CPUUsage *usage) +int PWR_CPU_GetUsage(PWR_CPU_Usage *usage, uint32_t bufferSize) { CHECK_STATUS if (!usage) { return ERR_NULL_POINTER; } - int ret = GetUsage(usage); + if (bufferSize < sizeof(PWR_CPU_Usage)) { + return ERR_INVALIDE_PARAM; + } + + int ret = GetCpuUsage(usage, bufferSize); return ret; } diff --git a/pwrapic/src/pwrcpu.c b/pwrapic/src/pwrcpu.c index cd6314ef9471799c43985b272b007b0f221125a8..83884429208366ed04b447a477b9cc8210586aed 100644 --- a/pwrapic/src/pwrcpu.c +++ b/pwrapic/src/pwrcpu.c @@ -44,7 +44,7 @@ int GetCpuInfo(PWR_CPU_Info *cpuInfo) return SUCCESS; } -int GetUsage(CPUUsage *usage) +int GetCpuUsage(PWR_CPU_Usage *usage, uint32_t bufferSize) { // 组装消息 PwrMsg *req = CreateReqMsg(CPU_GET_USAGE, 0, 0, NULL); @@ -56,7 +56,7 @@ int GetUsage(CPUUsage *usage) // 发送消息 PwrMsg *rsp = NULL; int ret = SendReqAndWaitForRsp(req, &rsp); - if (ret != SUCCESS || !(rsp->data) || rsp->head.dataLen != sizeof(CPUUsage)) { + if (ret != SUCCESS || !(rsp->data) || rsp->head.dataLen < sizeof(PWR_CPU_Usage)) { PwrLog(ERROR, "CPU_GET_USAGE req failed. ret:%d", ret); ReleasePwrMsg(&req); ReleasePwrMsg(&rsp); @@ -64,7 +64,9 @@ int GetUsage(CPUUsage *usage) } // 填充结果 - usage->usage = ((CPUUsage *)(rsp->data))->usage; + int dlen = rsp->head.dataLen < bufferSize ? rsp->head.dataLen : bufferSize; + memcpy(usage, rsp->data, dlen); + usage->coreNum = (dlen - sizeof(PWR_CPU_Usage)) / sizeof(PWR_CPU_CoreUsage); PwrLog(DEBUG, "GET_CPU_USAGE succeed."); ReleasePwrMsg(&req); diff --git a/pwrapic/src/sockclient.c b/pwrapic/src/sockclient.c index 85c7f868d6cd8714cdec9d9260a948e5986f3c5b..3c0906807ac8d1e10ff61ac68607d0024020bc95 100644 --- a/pwrapic/src/sockclient.c +++ b/pwrapic/src/sockclient.c @@ -121,8 +121,8 @@ static void RecvMsgFromSocket() return; } - char *msgcontent = malloc(sizeof(msg->head.dataLen)); - if (!msgcontent || ReadMsg(msgcontent, sizeof(msg->head.dataLen)) != SUCCESS) { + char *msgcontent = malloc(msg->head.dataLen); + if (!msgcontent || ReadMsg(msgcontent, msg->head.dataLen) != SUCCESS) { ReleasePwrMsg(&msg); return; } @@ -324,12 +324,12 @@ int SendReqAndWaitForRsp(PwrMsg *req, PwrMsg **rsp) CHECK_SOCKET_STATUS(); if (SendMsgSyn(req, rsp) != SUCCESS) { - PwrLog(ERROR, "send msg to server failed. optType: %d, seqId:%d", req->head.optType, req->head.seqId); + PwrLog(ERROR, "send msg to server failed. optType: %d, seqId:%u", req->head.optType, req->head.seqId); return ERR_SYS_EXCEPTION; } if (*rsp == NULL || (*rsp)->head.rspCode != SUCCESS) { - PwrLog(ERROR, "rsp error. optType: %d, seqId:%d", req->head.optType, req->head.seqId); + PwrLog(ERROR, "rsp error. optType: %d, seqId:%u", req->head.optType, req->head.seqId); return *rsp == NULL ? ERR_COMMON : (*rsp)->head.rspCode; } return SUCCESS; diff --git a/pwrapic/test/demo_main.c b/pwrapic/test/demo_main.c index 39aafde4cc2fe285bebfec3879e4d8180d6f3183..431d5dc1cddb826be72ac96f41c8cd3a7b061545 100644 --- a/pwrapic/test/demo_main.c +++ b/pwrapic/test/demo_main.c @@ -77,10 +77,21 @@ int main(int argc, const char *args[]) continue; } printf("main regist succeed.\n"); - CPUUsage u; - bzero(&u, sizeof(CPUUsage)); - PWR_CPU_GetUsage(&u); - printf("CPU usage:%d\n", u.usage); + + int ret = 0; + + // PWR_CPU_GetUsage + int buffSize = sizeof(PWR_CPU_Usage) + 128 * sizeof(PWR_CPU_CoreUsage); + PWR_CPU_Usage *u = (PWR_CPU_Usage *)malloc(buffSize); + bzero(u, buffSize); + ret = PWR_CPU_GetUsage(u, buffSize); + printf("ret: %d, CPU avgUsage:%f, coreNum: %d \n", ret, u->avgUsage, u->coreNum); + for (int i = 0; i < u->coreNum; i++) { + printf("core%d usage: %f\n", u->coreUsage[i].coreNo, u->coreUsage[i].usage); + } + free(u); + + // todo: 其他接口测试 while (g_run) { diff --git a/pwrapis/src/cpuservice.c b/pwrapis/src/cpuservice.c index 1b02bd64861899a4aa9f1ea2874135fb34f0e987..ce55eb9488a20296ac54d15fadaa5fbd1aea5571 100644 --- a/pwrapis/src/cpuservice.c +++ b/pwrapis/src/cpuservice.c @@ -26,13 +26,14 @@ void GetCpuUsage(PwrMsg *req) if (!req) { return; } - Logger(DEBUG, MD_NM_SVR_CPU, "Get GetCpuUsage Req. seqId:%d, sysId:%d", req->head.seqId, req->head.sysId); + Logger(DEBUG, MD_NM_SVR_CPU, "Get GetCpuUsage Req. seqId:%u, sysId:%d", req->head.seqId, req->head.sysId); int rspCode = SUCCESS; - CPUUsage *rstData = malloc(sizeof(CPUUsage)); + PWR_CPU_Usage *rstData = malloc(sizeof(PWR_CPU_Usage)); if (!rstData) { return; } - rstData->usage = 40; // todo 调用适配层能力获取CPU使用率 + rstData->avgUsage = 40; // todo 调用适配层能力获取CPU使用率 + rstData->coreNum = 0; PwrMsg *rsp = (PwrMsg *)malloc(sizeof(PwrMsg)); if (!rsp) { @@ -41,7 +42,7 @@ void GetCpuUsage(PwrMsg *req) return; } bzero(rsp, sizeof(PwrMsg)); - GenerateRspMsg(req, rsp, rspCode, (char *)rstData, sizeof(CPUUsage)); + GenerateRspMsg(req, rsp, rspCode, (char *)rstData, sizeof(PWR_CPU_Usage)); if (SendRspMsg(rsp) != SUCCESS) { ReleasePwrMsg(&rsp); }