From 7954fcf8f3fa2567ae9e0958a02bbc788da16163 Mon Sep 17 00:00:00 2001 From: queyanwen Date: Thu, 19 Oct 2023 16:43:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A2=E6=88=B7=E7=AB=AF=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E7=93=A6=E7=89=B9=E8=B0=83=E5=BA=A6=E5=92=8Csmart=5Fg?= =?UTF-8?q?rid=E7=AE=A1=E7=90=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/inc/pwrdata.h | 21 ++++ common/inc/pwrmsg.h | 12 +- pwrapic/inc/powerapi.h | 15 +++ pwrapic/inc/pwrproc.h | 32 ++++++ pwrapic/src/powerapi.c | 104 +++++++++++++++++ pwrapic/src/pwrproc.c | 248 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 431 insertions(+), 1 deletion(-) create mode 100644 pwrapic/inc/pwrproc.h create mode 100644 pwrapic/src/pwrproc.c diff --git a/common/inc/pwrdata.h b/common/inc/pwrdata.h index be3d12f..67b40f2 100644 --- a/common/inc/pwrdata.h +++ b/common/inc/pwrdata.h @@ -16,6 +16,7 @@ #define POWERAPI_DATA_H__ #include +#include #define PWR_MAX_ELEMENT_NAME_LEN 32 #define PWR_MAX_ARRRIBUTES 11 #define PWR_MAX_NAME_LEN 128 @@ -39,6 +40,10 @@ #define PWR_MAX_CPU_DMA_LATENCY 2000000000 #define PWR_MAX_DISK_LIST_LEN 128 +#define PWR_INIT_RESERVED_LEN 32 +#define PWR_ONE_HUNDRED 100 +#define PWR_ONE_THOUSAND 1000 +#define PWR_MAX_WATT_SCALE_INTERVAL 3600000 enum PWR_Arch { PWR_AARCH_64 = 0, @@ -63,6 +68,10 @@ enum PWR_CpuAttType { PWR_MAX_MHZ, PWR_MIN_MHZ, }; +typedef enum PWR_PROC_SMART_GRID_LEVEL { + PWR_PROC_SG_LEVEL_0 = 0, + PWR_PROC_SG_LEVEL_1 = 1, +} PWR_PROC_SMART_GRID_LEVEL; typedef enum PWR_COM_COL_DATATYPE { PWR_COM_DATATYPE_CPU_PERF = 1, @@ -245,4 +254,16 @@ typedef struct PWR_USB_AutoSuspend { int wakeup; } PWR_USB_AutoSuspend; +typedef struct PWR_PROC_WattAttrs { + int scaleThreshold; + int domainMask; + int scaleInterval; + char reserved[PWR_INIT_RESERVED_LEN]; +} PWR_PROC_WattAttrs; + +typedef struct PWR_PROC_SmartGridProcs { + PWR_PROC_SMART_GRID_LEVEL level; + int procNum; + pid_t procs[0]; +} PWR_PROC_SmartGridProcs; #endif \ No newline at end of file diff --git a/common/inc/pwrmsg.h b/common/inc/pwrmsg.h index 9508ef4..36e0930 100644 --- a/common/inc/pwrmsg.h +++ b/common/inc/pwrmsg.h @@ -86,7 +86,17 @@ enum OperationType { NET_SET_SPEED_MOD, USB_GET_AUTO_SUSPEND = 500, USB_SET_AUTO_SUSPEND, - // todo + PROC_GET_WATT_STATE = 600, + PROC_SET_WATT_STATE, + PROC_GET_WATT_ARRTS, + PROC_SET_WATT_ARRTS, + PROC_GET_WATT_PROCS, + PROC_ADD_WATT_PROCS, + PROC_DEL_WATT_PROCS, + PROC_GET_SMART_GRID_STATE, + PROC_SET_SMART_GRID_STATE, + PROC_GET_SMART_GRID_PROCS, + PROC_SET_SMART_GRID_PROCS_LEVEL, }; enum DataFormat { FMT_BIN = 0, diff --git a/pwrapic/inc/powerapi.h b/pwrapic/inc/powerapi.h index a42d652..b5af516 100644 --- a/pwrapic/inc/powerapi.h +++ b/pwrapic/inc/powerapi.h @@ -17,6 +17,7 @@ #include #include +#include #include "pwrerr.h" #include "pwrdata.h" @@ -80,6 +81,20 @@ PWR_API int PWR_NET_SetSpeedMod(char ethName[], uint32_t speedMod); PWR_API int PWR_USB_GetAutoSuspend(PWR_USB_AutoSuspend usbAts[], uint32_t *len); PWR_API int PWR_USB_SetAutoSuspend(PWR_USB_AutoSuspend usbAts[], uint32_t len); + +// PROC +PWR_API int PWR_PROC_GetWattState(int *state); +PWR_API int PWR_PROC_SetWattState(int state); +PWR_API int PWR_PROC_GetWattAttrs(PWR_PROC_WattAttrs *wattAttrs); +PWR_API int PWR_PROC_SetWattAttrs(const PWR_PROC_WattAttrs *wattAttrs); +PWR_API int PWR_PROC_GetWattProcs(pid_t wattProcs[], uint32_t *num); +PWR_API int PWR_PROC_AddWattProcs(const pid_t wattProcs[], uint32_t num); +PWR_API int PWR_PROC_DelWattProcs(const pid_t wattProcs[], uint32_t num); +PWR_API int PWR_PROC_GetSmartGridState(int *state); +PWR_API int PWR_PROC_SetSmartGridState(int state); +PWR_API int PWR_PROC_GetSmartGridProcs(PWR_PROC_SMART_GRID_LEVEL level, PWR_PROC_SmartGridProcs *sgProcs); +PWR_API int PWR_PROC_SetSmartGridLevel(const PWR_PROC_SmartGridProcs *sgProcs); + #ifdef __cplusplus } #endif diff --git a/pwrapic/inc/pwrproc.h b/pwrapic/inc/pwrproc.h new file mode 100644 index 0000000..ff1c4ba --- /dev/null +++ b/pwrapic/inc/pwrproc.h @@ -0,0 +1,32 @@ +/* ***************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. 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: queyanwen + * Create: 2023-10-19 + * Description: Provide service for PowerAPI refer to process function. + * **************************************************************************** */ +#ifndef POWERAPI_PROC_H__ +#define POWERAPI_PROC_H__ +#include +#include "pwrdata.h" + +int GetProcWattState(int *state); +int SetProcWattState(int state); +int GetProcWattAttrs(PWR_PROC_WattAttrs *wattAttrs); +int SetProcWattAttrs(const PWR_PROC_WattAttrs *wattAttrs); +int GetWattProcs(pid_t wattProcs[], uint32_t *num); +int AddWattProcs(const pid_t wattProcs[], uint32_t num); +int DelWattProcs(const pid_t wattProcs[], uint32_t num); +int GetSmartGridState(int *state); +int SetSmartGridState(int state); +int GetSmartGridProcs(PWR_PROC_SMART_GRID_LEVEL level, PWR_PROC_SmartGridProcs *sgProcs); +int SetSmartGridLevel(const PWR_PROC_SmartGridProcs *sgProcs); + +#endif diff --git a/pwrapic/src/powerapi.c b/pwrapic/src/powerapi.c index c163cf7..434fb92 100644 --- a/pwrapic/src/powerapi.c +++ b/pwrapic/src/powerapi.c @@ -26,6 +26,7 @@ #include "pwrdisk.h" #include "pwrnet.h" #include "pwrusb.h" +#include "pwrproc.h" typedef enum PwrApiStatus { STATUS_UNREGISTERED = 0, @@ -482,3 +483,106 @@ int PWR_USB_SetAutoSuspend(PWR_USB_AutoSuspend usbAts[], uint32_t len) return SetUsbAutoSuspend(usbAts, len); } + +// PROC +int PWR_PROC_GetWattState(int *state) +{ + CHECK_STATUS(STATUS_REGISTERTED); + CHECK_NULL_POINTER(state); + return GetProcWattState(state); +} + +int PWR_PROC_SetWattState(int state) +{ + CHECK_STATUS(STATUS_AUTHED); + if (state != PWR_TRUE || state != PWR_FALSE) { + return PWR_ERR_INVALIDE_PARAM; + } + return SetProcWattState(state); +} + +int PWR_PROC_GetWattAttrs(PWR_PROC_WattAttrs *wattAttrs) +{ + CHECK_STATUS(STATUS_REGISTERTED); + CHECK_NULL_POINTER(wattAttrs); + return GetProcWattAttrs(wattAttrs); +} + +int PWR_PROC_SetWattAttrs(const PWR_PROC_WattAttrs *wattAttrs) +{ + CHECK_STATUS(STATUS_AUTHED); + CHECK_NULL_POINTER(wattAttrs); + if (wattAttrs->scaleThreshold < 0 || wattAttrs->scaleThreshold > PWR_ONE_HUNDRED || + wattAttrs->scaleInterval < 0 || wattAttrs->scaleInterval > PWR_MAX_WATT_SCALE_INTERVAL || + wattAttrs->domainMask < 0) { + return PWR_ERR_INVALIDE_PARAM; + } + return SetProcWattAttrs(wattAttrs); +} + +int PWR_PROC_GetWattProcs(pid_t wattProcs[], uint32_t *num) +{ + CHECK_STATUS(STATUS_REGISTERTED); + CHECK_NULL_POINTER(wattProcs); + CHECK_NULL_POINTER(num); + if (*num == 0) { + return PWR_ERR_INVALIDE_PARAM; + } + return GetWattProcs(wattProcs, num); +} + +int PWR_PROC_AddWattProcs(const pid_t wattProcs[], uint32_t num) +{ + CHECK_STATUS(STATUS_AUTHED); + CHECK_NULL_POINTER(wattProcs); + if (num == 0) { + return PWR_ERR_INVALIDE_PARAM; + } + return AddWattProcs(wattProcs, num); +} + +int PWR_PROC_DelWattProcs(const pid_t wattProcs[], uint32_t num) +{ + CHECK_STATUS(STATUS_AUTHED); + CHECK_NULL_POINTER(wattProcs); + if (num == 0) { + return PWR_ERR_INVALIDE_PARAM; + } + return DelWattProcs(wattProcs, num); +} + +int PWR_PROC_GetSmartGridState(int *state) +{ + CHECK_STATUS(STATUS_REGISTERTED); + CHECK_NULL_POINTER(state); + return GetSmartGridState(state); +} + +int PWR_PROC_SetSmartGridState(int state) +{ + CHECK_STATUS(STATUS_AUTHED); + if (state != PWR_TRUE || state != PWR_FALSE) { + return PWR_ERR_INVALIDE_PARAM; + } + return SetSmartGridState(state); +} + +int PWR_PROC_GetSmartGridProcs(PWR_PROC_SMART_GRID_LEVEL level, PWR_PROC_SmartGridProcs *sgProcs) +{ + CHECK_STATUS(STATUS_REGISTERTED); + CHECK_NULL_POINTER(sgProcs); + if (sgProcs->procNum == 0) { + return PWR_ERR_INVALIDE_PARAM; + } + return GetSmartGridProcs(level, sgProcs); +} + +int PWR_PROC_SetSmartGridLevel(const PWR_PROC_SmartGridProcs *sgProcs) +{ + CHECK_STATUS(STATUS_AUTHED); + CHECK_NULL_POINTER(sgProcs); + if (sgProcs->procNum == 0) { + return PWR_ERR_INVALIDE_PARAM; + } + return SetSmartGridLevel(sgProcs); +} \ No newline at end of file diff --git a/pwrapic/src/pwrproc.c b/pwrapic/src/pwrproc.c new file mode 100644 index 0000000..196746c --- /dev/null +++ b/pwrapic/src/pwrproc.c @@ -0,0 +1,248 @@ +/* ***************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023 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: queyanwen + * Create: 2023-10-19 + * Description: Provide service for PowerAPI refer to process function. + * **************************************************************************** */ + +#include "pwrproc.h" +#include "pwrlog.h" +#include "pwrerr.h" +#include "sockclient.h" + +int GetProcWattState(int *state) +{ + ReqInputParam input; + input.optType = PROC_GET_WATT_STATE; + input.dataLen = 0; + input.data = NULL; + + RspOutputParam output; + uint32_t size = (uint32_t)sizeof(int); + output.rspBuffSize = &size; + output.rspData = (char *)state; + + int ret = SendReqAndWaitForRsp(input, output); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "GetProcWattState failed. ret:%d", ret); + } else { + PwrLog(DEBUG, "GetProcWattState succeed."); + } + return ret; +} + +int SetProcWattState(int state) +{ + ReqInputParam input; + input.optType = PROC_SET_WATT_STATE; + input.dataLen = (uint32_t)sizeof(state); + input.data = (char *)&state; + + RspOutputParam output; + output.rspBuffSize = NULL; + output.rspData = NULL; + + int ret = SendReqAndWaitForRsp(input, output); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "SetProcWattState failed. ret:%d", ret); + } else { + PwrLog(DEBUG, "SetProcWattState succeed."); + } + return ret; +} + +int GetProcWattAttrs(PWR_PROC_WattAttrs *wattAttrs) +{ + ReqInputParam input; + input.optType = PROC_GET_WATT_ARRTS; + input.dataLen = 0; + input.data = NULL; + + RspOutputParam output; + uint32_t size = (uint32_t)sizeof(PWR_PROC_WattAttrs); + output.rspBuffSize = &size; + output.rspData = (char *)wattAttrs; + + int ret = SendReqAndWaitForRsp(input, output); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "GetProcWattState failed. ret:%d", ret); + } else { + PwrLog(DEBUG, "GetProcWattState succeed."); + } + return ret; +} + +int SetProcWattAttrs(const PWR_PROC_WattAttrs *wattAttrs) +{ + ReqInputParam input; + input.optType = PROC_SET_WATT_ARRTS; + uint32_t size = (uint32_t)sizeof(PWR_PROC_WattAttrs); + input.dataLen = size; + input.data = (char *)wattAttrs; + + RspOutputParam output; + output.rspBuffSize = NULL; + output.rspData = NULL; + + int ret = SendReqAndWaitForRsp(input, output); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "SetProcWattAttrs failed. ret:%d", ret); + } else { + PwrLog(DEBUG, "SetProcWattAttrs succeed."); + } + return ret; +} + +int GetWattProcs(pid_t wattProcs[], uint32_t *num) +{ + ReqInputParam input; + input.optType = PROC_GET_WATT_PROCS; + input.dataLen = 0; + input.data = NULL; + + RspOutputParam output; + uint32_t size = (uint32_t)sizeof(pid_t) * (*num); + output.rspBuffSize = &size; + output.rspData = (char *)wattProcs; + + int ret = SendReqAndWaitForRsp(input, output); + *num = (uint32_t)(size / sizeof(pid_t)); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "GetWattProcs failed. ret:%d", ret); + } else { + PwrLog(DEBUG, "GetWattProcs succeed."); + } + return ret; +} + +int AddWattProcs(const pid_t wattProcs[], uint32_t num) +{ + ReqInputParam input; + input.optType = PROC_ADD_WATT_PROCS; + input.dataLen = (uint32_t)sizeof(pid_t) * num; + input.data = (char *)&wattProcs; + + RspOutputParam output; + output.rspBuffSize = NULL; + output.rspData = NULL; + + int ret = SendReqAndWaitForRsp(input, output); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "AddWattProcs failed. ret:%d", ret); + } else { + PwrLog(DEBUG, "AddWattProcs succeed."); + } + return ret; +} + +int DelWattProcs(const pid_t wattProcs[], uint32_t num) +{ + ReqInputParam input; + input.optType = PROC_DEL_WATT_PROCS; + input.dataLen = (uint32_t)sizeof(pid_t) * num; + input.data = (char *)&wattProcs; + + RspOutputParam output; + output.rspBuffSize = NULL; + output.rspData = NULL; + + int ret = SendReqAndWaitForRsp(input, output); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "DelWattProcs failed. ret:%d", ret); + } else { + PwrLog(DEBUG, "DelWattProcs succeed."); + } + return ret; +} + + +int GetSmartGridState(int *state) +{ + ReqInputParam input; + input.optType = PROC_GET_SMART_GRID_STATE; + input.dataLen = 0; + input.data = NULL; + + RspOutputParam output; + uint32_t size = (uint32_t)sizeof(int); + output.rspBuffSize = &size; + output.rspData = (char *)state; + + int ret = SendReqAndWaitForRsp(input, output); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "GetSmartGridState failed. ret:%d", ret); + } else { + PwrLog(DEBUG, "GetSmartGridState succeed."); + } + return ret; +} + +int SetSmartGridState(int state) +{ + ReqInputParam input; + input.optType = PROC_SET_SMART_GRID_STATE; + input.dataLen = (uint32_t)sizeof(state); + input.data = (char *)&state; + + RspOutputParam output; + output.rspBuffSize = NULL; + output.rspData = NULL; + + int ret = SendReqAndWaitForRsp(input, output); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "SetSmartGridState failed. ret:%d", ret); + } else { + PwrLog(DEBUG, "SetSmartGridState succeed."); + } + return ret; +} + +int GetSmartGridProcs(PWR_PROC_SMART_GRID_LEVEL level, PWR_PROC_SmartGridProcs *sgProcs) +{ + ReqInputParam input; + input.optType = PROC_GET_SMART_GRID_PROCS; + input.dataLen = (uint32_t)sizeof(level); + input.data = (char *)&level; + + RspOutputParam output; + uint32_t size = (uint32_t)(sizeof(PWR_PROC_SmartGridProcs) + sizeof(pid_t) * (sgProcs->procNum)); + output.rspBuffSize = &size; + output.rspData = (char *)sgProcs; + + int ret = SendReqAndWaitForRsp(input, output); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "GetWattProcs failed. ret:%d", ret); + } else { + PwrLog(DEBUG, "GetWattProcs succeed."); + } + return ret; +} + +int SetSmartGridLevel(const PWR_PROC_SmartGridProcs *sgProcs) +{ + ReqInputParam input; + input.optType = PROC_SET_SMART_GRID_PROCS_LEVEL; + input.dataLen = (uint32_t)(sizeof(PWR_PROC_SmartGridProcs) + sizeof(pid_t) * (sgProcs->procNum)); + input.data = (char *)&sgProcs; + + RspOutputParam output; + output.rspBuffSize = NULL; + output.rspData = NULL; + + int ret = SendReqAndWaitForRsp(input, output); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "SetSmartGridState failed. ret:%d", ret); + } else { + PwrLog(DEBUG, "SetSmartGridState succeed."); + } + return ret; +} + -- Gitee