From c292609a1e0188aaf50900a243b381b740f5abfc Mon Sep 17 00:00:00 2001 From: heppen Date: Mon, 29 Apr 2024 15:57:18 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(hbm):=20=E6=96=B0=E5=A2=9EHBM?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=20=E6=96=B0=E5=A2=9EHBM=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=9A=E6=9F=A5=E8=AF=A2HBM=E7=9A=84=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E5=92=8CHBM=E4=B8=8A=E4=B8=8B=E7=94=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.sh | 0 clean.sh | 0 common/inc/pwrdata.h | 8 +++ common/inc/pwrerr.h | 1 + common/inc/pwrmsg.h | 2 + pwrapic/inc/powerapi.h | 4 ++ pwrapic/inc/pwrhbm.h | 24 +++++++ pwrapic/src/powerapi.c | 17 +++++ pwrapic/src/pwrhbm.c | 60 ++++++++++++++++++ pwrapic/test/demo_main.c | 31 +++++++++ pwrapis/inc/common.h | 1 + pwrapis/inc/hbmservice.h | 25 ++++++++ pwrapis/src/cpuservice.c | 1 + pwrapis/src/hbmservice.c | 133 +++++++++++++++++++++++++++++++++++++++ pwrapis/src/server.c | 3 + 15 files changed, 310 insertions(+) mode change 100644 => 100755 build.sh mode change 100644 => 100755 clean.sh create mode 100644 pwrapic/inc/pwrhbm.h create mode 100644 pwrapic/src/pwrhbm.c create mode 100644 pwrapis/inc/hbmservice.h create mode 100644 pwrapis/src/hbmservice.c diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 diff --git a/clean.sh b/clean.sh old mode 100644 new mode 100755 diff --git a/common/inc/pwrdata.h b/common/inc/pwrdata.h index e8bcb6e..9f95c30 100644 --- a/common/inc/pwrdata.h +++ b/common/inc/pwrdata.h @@ -298,4 +298,12 @@ typedef struct PWR_PROC_SmartGridGov { char sgLevel1Gov[PWR_MAX_ELEMENT_NAME_LEN]; } PWR_PROC_SmartGridGov; +// HBM +typedef enum PWR_HBM_SysState { + PWR_HBM_NOT_SUPPORT = 0, + PWR_HBM_FLAT_MOD, + PWR_HBM_CACHE_MOD, + PWR_HBM_HYBRID_MOD, +} PWR_HBM_SysState; + #endif \ No newline at end of file diff --git a/common/inc/pwrerr.h b/common/inc/pwrerr.h index c4431e6..4de333e 100644 --- a/common/inc/pwrerr.h +++ b/common/inc/pwrerr.h @@ -55,5 +55,6 @@ enum PWR_RtnCode { PWR_ERR_FILE_FFLUSH_FAILED, PWR_ERR_FILE_OPEN_FAILED, PWR_ERR_FILE_SPRINTF_FAILED, + PWR_ERR_HBM_NOT_SUPPORTED = 600, }; #endif \ No newline at end of file diff --git a/common/inc/pwrmsg.h b/common/inc/pwrmsg.h index dc27c08..19a2e37 100644 --- a/common/inc/pwrmsg.h +++ b/common/inc/pwrmsg.h @@ -103,6 +103,8 @@ enum OperationType { PROC_SET_SMART_GRID_PROCS_LEVEL, PROC_GET_SMART_GRID_GOV, PROC_SET_SMART_GRID_GOV, + HBM_GET_SYS_STATE = 700, + HBM_SET_ALL_POWER_STATE, }; enum DataFormat { FMT_BIN = 0, diff --git a/pwrapic/inc/powerapi.h b/pwrapic/inc/powerapi.h index bef75f4..6eacb40 100644 --- a/pwrapic/inc/powerapi.h +++ b/pwrapic/inc/powerapi.h @@ -103,6 +103,10 @@ PWR_API int PWR_PROC_SetSmartGridLevel(const PWR_PROC_SmartGridProcs *sgProcs); PWR_API int PWR_PROC_GetSmartGridGov(PWR_PROC_SmartGridGov *sgGov); PWR_API int PWR_PROC_SetSmartGridGov(const PWR_PROC_SmartGridGov *sgGov); +// HBM +PWR_API int PWR_HBM_GetSysState(PWR_HBM_SysState *hbmState); +PWR_API int PWR_HBM_SetAllPwrState(int state); + #endif // #ifndef RELEASE_MODE #ifdef __cplusplus diff --git a/pwrapic/inc/pwrhbm.h b/pwrapic/inc/pwrhbm.h new file mode 100644 index 0000000..c757beb --- /dev/null +++ b/pwrapic/inc/pwrhbm.h @@ -0,0 +1,24 @@ +/****************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024 All rights reserved. + * PowerAPI licensed under the Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR + * PURPOSE. + * See the Mulan PSL v2 for more details. + * Author: heppen + * Create: 2024-04-30 + * Description: provide hbm operation interface + * **************************************************************************** */ + +#ifndef POWERAPI_HBM__H__ +#define POWERAPI_HBM__H__ + +#include "pwrdata.h" + +int GetHbmSysState(PWR_HBM_SysState *hmbState); +int SetAllHbmPowerState(int state); + +#endif //!POWERAPI_HBM__H__ \ No newline at end of file diff --git a/pwrapic/src/powerapi.c b/pwrapic/src/powerapi.c index 3724178..e5b2d13 100644 --- a/pwrapic/src/powerapi.c +++ b/pwrapic/src/powerapi.c @@ -27,6 +27,7 @@ #include "pwrnet.h" #include "pwrusb.h" #include "pwrproc.h" +#include "pwrhbm.h" #define CHECK_STATUS(s) \ { \ @@ -290,6 +291,22 @@ int PWR_CPU_DmaSetLatency(int latency) return SetCpuDmaLatency(latency); } +// HBM +int PWR_HBM_GetSysState(PWR_HBM_SysState *hbmState) +{ + CHECK_STATUS(STATUS_REGISTERTED); + + return GetHbmSysState(hbmState); +} + +int PWR_HBM_SetAllPwrState(int state) +{ + CHECK_STATUS(STATUS_AUTHED); + if (state < 0 || state > 1) { + return PWR_ERR_INVALIDE_PARAM; + } + return SetAllHbmPowerState(state); +} #ifndef RELEASE_MODE int PWR_SetMetaDataCallback(void(MetaDataCallback)(const PWR_COM_CallbackData *)) diff --git a/pwrapic/src/pwrhbm.c b/pwrapic/src/pwrhbm.c new file mode 100644 index 0000000..9066808 --- /dev/null +++ b/pwrapic/src/pwrhbm.c @@ -0,0 +1,60 @@ +/****************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024 All rights reserved. + * PowerAPI licensed under the Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR + * PURPOSE. + * See the Mulan PSL v2 for more details. + * Author: heppen + * Create: 2024-04-30 + * Description: provide hbm operation interface + * **************************************************************************** */ + +#include "pwrhbm.h" +#include "pwrlog.h" +#include "pwrerr.h" +#include "sockclient.h" + +int GetHbmSysState(PWR_HBM_SysState *hmbState) +{ + ReqInputParam input; + input.optType = HBM_GET_SYS_STATE; + input.dataLen = 0; + input.data = NULL; + + uint32_t size = sizeof(PWR_HBM_SysState); + RspOutputParam output; + output.rspBuffSize = &size; + output.rspData = (void *)hmbState; + + int ret = SendReqAndWaitForRsp(input, output); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "GetHbmSysState failed. ret: %d", ret); + } else { + PwrLog(DEBUG, "GetHbmSysState succeed."); + } + return ret; +} + +int SetAllHbmPowerState(int state) +{ + ReqInputParam input; + input.optType = HBM_SET_ALL_POWER_STATE; + input.dataLen = sizeof(int); + input.data = (char *)&state; + + RspOutputParam output; + output.rspBuffSize = NULL; + output.rspData = NULL; + + int ret = SendReqAndWaitForRsp(input, output); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "SetAllHbmPowerState failed. ret: %d", ret); + } else { + PwrLog(DEBUG, "SetAllHbmPowerState succeed."); + } + return ret; +} \ No newline at end of file diff --git a/pwrapic/test/demo_main.c b/pwrapic/test/demo_main.c index 663e230..b711cca 100644 --- a/pwrapic/test/demo_main.c +++ b/pwrapic/test/demo_main.c @@ -581,6 +581,32 @@ static void TEST_PWR_DISK_GetList(void) } /*************************** DISK END ************************/ +/***************************** HBM **************************/ +static void TEST_PWR_HBM_GetSysState(void) +{ + int ret = -1; + PWR_HBM_SysState state = PWR_HBM_HYBRID_MOD; + ret = PWR_HBM_GetSysState(&state); + PrintResult("TEST_PWR_HBM_GetSysState", ret); + printf("hbm state is %d.\n", (int)state); +} + +static void TEST_PWR_HBM_SetAllPwrState(void) +{ + int ret = -1; + ret = PWR_HBM_SetAllPwrState(0); + PrintResult("TEST_PWR_HBM_SetAllPwrState", ret); + + ret = PWR_HBM_SetAllPwrState(1); + PrintResult("TEST_PWR_HBM_SetAllPwrState", ret); + + ret = PWR_HBM_SetAllPwrState(2); + PrintResult("TEST_PWR_HBM_SetAllPwrState", ret); +} + + +/*************************** HBM END ************************/ + int main(int argc, const char *args[]) { /********** Common **********/ @@ -619,6 +645,11 @@ int main(int argc, const char *args[]) // TEST_PWR_COM_DcTaskMgr(); /************ PROC ***********/ TEST_PROC_AllFunc(); + + /************ HBM ***********/ + TEST_PWR_HBM_GetSysState(); + TEST_PWR_HBM_SetAllPwrState(); + // todo: 其他接口测试 while (g_run) { sleep(MAIN_LOOP_INTERVAL); diff --git a/pwrapis/inc/common.h b/pwrapis/inc/common.h index 8530d7e..f97c3ba 100644 --- a/pwrapis/inc/common.h +++ b/pwrapis/inc/common.h @@ -58,6 +58,7 @@ #define MD_NM_SVR_TASK "TASK_SERVICE" #define MD_NM_SVR_PROC "PROC_SERVICE" #define MD_NM_SVR_COM "COM_SERVICE" +#define MD_NM_SVR_HBM "HBM_SERVICE" #define MD_NM_CRED "CREDENTIALS" #define MD_NM_OTHS "OTHERS" diff --git a/pwrapis/inc/hbmservice.h b/pwrapis/inc/hbmservice.h new file mode 100644 index 0000000..9157520 --- /dev/null +++ b/pwrapis/inc/hbmservice.h @@ -0,0 +1,25 @@ +/****************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024 All rights reserved. + * PowerAPI licensed under the Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR + * PURPOSE. + * See the Mulan PSL v2 for more details. + * Author: heppen + * Create: 2024-04-30 + * Description: provide hbm get and set interface + * **************************************************************************** */ + +#ifndef PAPIS_HBM_SERVICE_H__ +#define PAPIS_HBM_SERVICE_H__ + +#include "pwrmsg.h" +#include "pwrdata.h" + +void GetHbmSysState(PwrMsg *req); +void SetHbmAllPowerState(PwrMsg *req); + +#endif //!PAPIS_HBM_SERVICE_H__ \ No newline at end of file diff --git a/pwrapis/src/cpuservice.c b/pwrapis/src/cpuservice.c index 9e4dd2d..1e220c5 100644 --- a/pwrapis/src/cpuservice.c +++ b/pwrapis/src/cpuservice.c @@ -294,6 +294,7 @@ int PerfDataRead(PWR_CPU_PerfData *perfData) perfData->llcMiss = (double)cacheMiss / ins; perfData->ipc = (double)ins / cycles; pclose(fp); + remove("perf.txt"); return PWR_SUCCESS; } diff --git a/pwrapis/src/hbmservice.c b/pwrapis/src/hbmservice.c new file mode 100644 index 0000000..443e159 --- /dev/null +++ b/pwrapis/src/hbmservice.c @@ -0,0 +1,133 @@ +/****************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024 All rights reserved. + * PowerAPI licensed under the Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR + * PURPOSE. + * See the Mulan PSL v2 for more details. + * Author: heppen + * Create: 2024-04-30 + * Description: provide hbm service + * **************************************************************************** */ + +#include "string.h" +#include "pwrerr.h" +#include "server.h" +#include "log.h" +#include "unistd.h" +#include "utils.h" +#include "hbmservice.h" + +#define EXEC_COMMAND(cmd) \ + do { \ + FILE *fp = popen(cmd, "r"); \ + if (fp == NULL) { \ + return PWR_ERR_COMMON; \ + } \ + pclose(fp); \ + } while (0) + +static int GetHbmMode(PWR_HBM_SysState *state) +{ + char cache_mod_cmd[] = "find /sys/devices/LNXSYSTM* -name 'HISI04A1*'"; + char flat_mod_cmd[] = "find /sys/devices/LNXSYSTM* -name 'PNP0C80*'"; + *state = PWR_HBM_NOT_SUPPORT; + + FILE *cache_mod_fp = popen(cache_mod_cmd, "r"); + if (cache_mod_fp == NULL) { + return PWR_ERR_COMMON; + } + char cache_buf[PWR_MAX_STRING_LEN] = {0}; + if (fgets(cache_buf, PWR_MAX_STRING_LEN, cache_mod_fp) != NULL) { + *state |= PWR_HBM_FLAT_MOD; + } + + FILE *flat_mod_fp = popen(flat_mod_cmd, "r"); + if (flat_mod_fp == NULL) { + pclose(cache_mod_fp); + return PWR_ERR_COMMON; + } + char flat_buf[PWR_MAX_STRING_LEN] = {0}; + if (fgets(cache_buf, PWR_MAX_STRING_LEN, cache_mod_fp) != NULL) { + *state |= PWR_HBM_CACHE_MOD; + } + pclose(cache_mod_fp); + pclose(flat_mod_fp); + return PWR_SUCCESS; +} + +void GetHbmSysState(PwrMsg *req) +{ + PWR_HBM_SysState *state = (PWR_HBM_SysState *)malloc(sizeof(PWR_HBM_SysState)); + if (!state) { + SendRspToClient(req, PWR_ERR_SYS_EXCEPTION, NULL, 0); + return; + } + *state = PWR_HBM_NOT_SUPPORT; + int rspCode = GetHbmMode(state); + if (rspCode != PWR_SUCCESS) { + free(state); + SendRspToClient(req, rspCode, NULL, 0); + } else { + SendRspToClient(req, rspCode, (char *)state, sizeof(PWR_HBM_SysState)); + } +} + +static int SetPowerState(int powerState) +{ + PWR_HBM_SysState hbmState = PWR_HBM_NOT_SUPPORT; + if (GetHbmMode(&hbmState) != PWR_SUCCESS) { + Logger(ERROR, MD_NM_SVR_HBM, "GetHbmMode failed"); + return PWR_ERR_COMMON; + } + if (hbmState == PWR_HBM_NOT_SUPPORT) { + Logger(ERROR, MD_NM_SVR_HBM, "SetHbmAllPowerState: HBM is not support"); + return PWR_ERR_HBM_NOT_SUPPORTED; + } + + const char *state_str = (powerState == 0) ? "offline" : "online"; + if (hbmState == PWR_HBM_CACHE_MOD || hbmState == PWR_HBM_HYBRID_MOD) { + char cmd[PWR_MAX_STRING_LEN] = {0}; + snprintf( + cmd, sizeof(cmd), + "find /sys/kernel/hbm_cache/*/state -type f | xargs -I {} sh -c 'echo \"%s\" > {}'", + state_str); + EXEC_COMMAND(cmd); + } + + if (hbmState == PWR_HBM_FLAT_MOD || hbmState == PWR_HBM_HYBRID_MOD) { + char cmd[PWR_MAX_STRING_LEN] = {0}; + snprintf(cmd, sizeof(cmd), + "find /sys/devices/system/container/PNP0A06*/state -type f | xargs -I {} sh -c 'echo " + "\"%s\" > {}'", + state_str); + EXEC_COMMAND(cmd); + } +} + +void SetHbmAllPowerState(PwrMsg *req) +{ + int rspCode = PWR_SUCCESS; + do { + if (!req || req->head.dataLen != sizeof(int)) { + Logger(ERROR, MD_NM_SVR_HBM, "SetHbmAllPowerState: wrong req msg.dataLen:%d", + req->head.dataLen); + rspCode = PWR_ERR_INVALIDE_PARAM; + break; + } + + int state = *(int *)req->data; + if (state != PWR_ENABLE && state != PWR_DISABLE) { + Logger(ERROR, MD_NM_SVR_HBM, "SetHbmAllPowerState: wrong state:%d", state); + rspCode = PWR_ERR_INVALIDE_PARAM; + break; + } + + rspCode = SetPowerState(state); + } while (PWR_FALSE); + + SendRspToClient(req, rspCode, NULL, 0); +} \ No newline at end of file diff --git a/pwrapis/src/server.c b/pwrapis/src/server.c index 1e1ce0b..95decc1 100644 --- a/pwrapis/src/server.c +++ b/pwrapis/src/server.c @@ -36,6 +36,7 @@ #include "procservice.h" #include "pwrerr.h" #include "utils.h" +#include "hbmservice.h" #define COUNT_MAX 5 #define THREAD_LOOP_INTERVAL 2000 // us @@ -520,6 +521,8 @@ static OptToFunct g_optToFunct[] = { {PROC_SET_SMART_GRID_PROCS_LEVEL, ProcSetSmartGridProcsLevel}, {PROC_GET_SMART_GRID_GOV, ProcGetSmartGridGov}, {PROC_SET_SMART_GRID_GOV, ProcSetSmartGridGov}, + {HBM_GET_SYS_STATE, GetHbmSysState}, + {HBM_SET_ALL_POWER_STATE, SetHbmAllPowerState}, }; static void ProcessReqMsg(PwrMsg *req) -- Gitee