From a319d661a162a96b92291207b4a4c57a20d09f35 Mon Sep 17 00:00:00 2001 From: Yipeng Zou Date: Tue, 17 Oct 2023 11:02:31 +0800 Subject: [PATCH] smart_grid: init commit to smart_grid hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/powerapi/issues/I88NO1 -------------------------------- Introduce libsmart_grid.so, Some APIs, including enabling basic smart_gird functions, can be used by callers to integrate smart_grid functions. Signed-off-by: Yipeng Zou --- CMakeLists.txt | 2 + build.sh | 14 +++ smart_grid/CMakeLists.txt | 6 ++ smart_grid/demo/CMakeLists.txt | 19 ++++ smart_grid/demo/smart_grid_demo.c | 123 ++++++++++++++++++++++++ smart_grid/inc/smart_grid.h | 92 ++++++++++++++++++ smart_grid/src/CMakeLists.txt | 24 +++++ smart_grid/src/libsmart_grid.c | 153 ++++++++++++++++++++++++++++++ uninstall.sh | 1 + 9 files changed, 434 insertions(+) create mode 100755 smart_grid/CMakeLists.txt create mode 100755 smart_grid/demo/CMakeLists.txt create mode 100755 smart_grid/demo/smart_grid_demo.c create mode 100644 smart_grid/inc/smart_grid.h create mode 100755 smart_grid/src/CMakeLists.txt create mode 100644 smart_grid/src/libsmart_grid.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f8ae171..8f6ef43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,3 +6,5 @@ add_subdirectory(pwrapic/src) add_subdirectory(pwrapic/test) add_subdirectory(pwrapic/gtest) add_subdirectory(pwrapis/src) +add_subdirectory(smart_grid/src) +add_subdirectory(smart_grid/demo) diff --git a/build.sh b/build.sh index 9a0e486..de97bab 100644 --- a/build.sh +++ b/build.sh @@ -17,6 +17,10 @@ then elif [[ "$1" == "$PWRAPIS" ]] then make pwrapis +elif [[ "$1" == "$SMART_GRID" ]] +then + make smart_grid + make smart_grid_demo else make all fi @@ -48,6 +52,16 @@ mkdir ./release/pwrapis cp ./build/pwrapis/src/pwrapis ./release/pwrapis/ cp -r ./pwrapis/conf ./release/pwrapis/ +mkdir ./release/smart_grid +mkdir ./release/smart_grid/lib +mkdir ./release/smart_grid/inc +cp ./build/smart_grid/src/libsmart_grid.so ./release/smart_grid/lib +cp ./smart_grid/inc/*.h ./release/smart_grid/inc + +mkdir ./release/smart_grid_demo +cp ./build/smart_grid/demo/smart_grid_demo ./release/smart_grid_demo/ +cp ./build/smart_grid/src/libsmart_grid.so ./release/smart_grid_demo/ + #make clean #cd .. #rm -rf build diff --git a/smart_grid/CMakeLists.txt b/smart_grid/CMakeLists.txt new file mode 100755 index 0000000..1528350 --- /dev/null +++ b/smart_grid/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required (VERSION 3.16) +project (smart_grid_entrance C) +set(CMAKE_VERBOSE_MAKEFILE on) + +add_subdirectory(src) +add_subdirectory(demo) diff --git a/smart_grid/demo/CMakeLists.txt b/smart_grid/demo/CMakeLists.txt new file mode 100755 index 0000000..d4e04f1 --- /dev/null +++ b/smart_grid/demo/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required (VERSION 3.16) +project (smart_grid_demo C) +set ( CMAKE_INCLUDE_CURRENT_DIR ON) + +# Add head file directory +include_directories ("${PROJECT_SOURCE_DIR}/../inc") + +# Add dependent lib directory +link_directories( ${PROJECT_SOURCE_DIR}/../../build/smart_grid/src ) + +# Set compile policy +set (PG_NAME ${PROJECT_NAME}) +add_executable (${PG_NAME} smart_grid_demo.c) +target_link_libraries(${PG_NAME} -lsmart_grid -lpthread) +set (CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# Release compile mode +set(CMAKE_BUILD_TYPE "Release") +set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall") diff --git a/smart_grid/demo/smart_grid_demo.c b/smart_grid/demo/smart_grid_demo.c new file mode 100755 index 0000000..e5e277a --- /dev/null +++ b/smart_grid/demo/smart_grid_demo.c @@ -0,0 +1,123 @@ +/* ***************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved. + * Author: zouyipeng + * Create: 2023-10-17 + * Description: smart_grid API Demo. + * **************************************************************************** */ +#include "smart_grid.h" + +int main(int argc, const char *args[]) +{ + pid_t *pid_list = NULL; + pid_t *pid_ptr = NULL; + int pid_nr; + pid_t first_pid; + int ret = -1; + int pre_state = -1; + int index; + + if (argc != 3) { + printf("Invaild input arg count %d != 3.\n", argc); + printf("Help:\n"); + printf("smart_grid_demo pid pid_nr\n"); + return -1; + } + + first_pid = atoi(args[1]); + pid_nr = atoi(args[2]); + printf("PID: %d ~ %d NR: %d\n", first_pid, first_pid + pid_nr, pid_nr); + + pid_list = malloc(sizeof(pid_t) * pid_nr); + if (pid_list == NULL) { + printf("Malloc %d PID list fail.\n", pid_nr); + return -1; + } + + ret = smart_grid_is_enable(); + printf("Current smart_grid status: %d.\n", ret); + + /* Set Disable */ + ret = smart_grid_sysctl(SMART_GRID_DISABLE); + if (ret < 0) { + printf("Set smart_grid to disable fail: %d.\n", ret); + return -1; + } + ret = smart_grid_is_enable(); + if (ret != SMART_GRID_DISABLE) { + printf("Current smart_grid status should be %d but now is %d.\n", SMART_GRID_DISABLE, ret); + return -1; + } + + /* Set Enable */ + ret = smart_grid_sysctl(SMART_GRID_ENABLE); + if (ret < 0) { + printf("Set smart_grid to enable fail: %d.\n", ret); + return -1; + } + ret = smart_grid_is_enable(); + if (ret != SMART_GRID_ENABLE) { + printf("Current smart_grid status should be %d but now is %d.\n", SMART_GRID_ENABLE, ret); + return -1; + } + + /* Is VIP task */ + ret = smart_grid_is_vip(first_pid); + if (ret < 0) { + printf("Current PID %d Get VIP State Fail %d.\n", first_pid, ret); + return -1; + } + pre_state = ret; + + if (ret == 0) + ret = smart_grid_set_vip(first_pid, 1); + else + ret = smart_grid_set_vip(first_pid, 0); + if (ret < 0) { + printf("Current PID %d Get VIP State Fail %d.\n", first_pid, ret); + return -1; + } + ret = smart_grid_is_vip(first_pid); + if (ret < 0) { + printf("Current PID %d Get VIP State Fail %d.\n", first_pid, ret); + return -1; + } + if (ret == pre_state) { + printf("Get PID %d Get VIP State Fail pre %d current %d.\n", first_pid, pre_state, ret); + return -1; + } + + /* PID VIP List */ + printf("Before set VIP list\n"); + for (index =0; index < pid_nr; index++) { + pid_ptr = pid_list + index; + *pid_ptr = first_pid + index; + ret = smart_grid_is_vip(*pid_ptr); + if (ret < 0) { + printf("Get PID %d VIP fail ret %d.\n", *pid_ptr, ret); + return -1; + } + printf("PID %d: VIP State %d.\n", *pid_ptr, ret); + } + ret = smart_grid_set_vip_list(pid_list, pid_nr); + if (ret != pid_nr) { + printf("smart_grid_set_vip_list fail ret %d.\n", ret); + return -1; + } + printf("After set VIP list\n"); + for (index =0; index < pid_nr; index++) { + pid_ptr = pid_list + index; + *pid_ptr = first_pid + index; + ret = smart_grid_is_vip(*pid_ptr); + if (ret < 0) { + printf("Get PID %d VIP fail ret %d.\n", *pid_ptr, ret); + return -1; + } + if (ret != 1) { + printf("Get PID %d VIP State should 1 but now id %d.\n", *pid_ptr, ret); + return -1; + } + printf("PID %d: VIP State %d.\n", *pid_ptr, ret); + } + + return 0; +} \ No newline at end of file diff --git a/smart_grid/inc/smart_grid.h b/smart_grid/inc/smart_grid.h new file mode 100644 index 0000000..f780f12 --- /dev/null +++ b/smart_grid/inc/smart_grid.h @@ -0,0 +1,92 @@ +/* ***************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved. + * Author: zouyipeng + * Create: 2023-10-17 + * Description: Provide smart_grid API. + * **************************************************************************** */ +#include +#include + +#ifndef __SMART_GRID_H__ +#define __SMART_GRID_H__ + +enum smart_grid_status { + SMART_GRID_DISABLE = 0, + SMART_GRID_ENABLE = 1, + SMART_GRID_NONE, +}; + +/* + * smart_grid_sysctl + * + * Update smart_grid status in *sysctl_flag* which can be: + * *SMART_GRID_ENABLE*: + * when smart_grid strategy was enabled, we use the task's actually tag. + * *SMART_GRID_DISABLE*: Smart_grid strategy was disable by default. + * When smart_grid strategy was disabled, We treat all task as the highest qos_level. + * + * Note: + * The status change of the SMART_GRID policy does not change the VIP status of all tasks. + * For example, the VIP status of a task that is set to VIP in the SMART_GRID_ENABLE state does + * not change after switched to the SMART_GRID_DISABLE state. + * The real VIP status of the task to be used until the next time SMART_GRID_ENABLE is used. + * Returns + * 0 on success, or a negative error in case of failure. + */ +int smart_grid_sysctl(int sysctl_flag); + +/* + * smart_grid_sysctl + * + * Check whether smart_grid was enabled. + * Returns + * 1 : The smart_grid was enabled, in SMART_GRID_ENABLE. + * 0 : The smart_grid was disabled, in SMART_GRID_DISABLE. + * or a negative error in case of failure. + */ +int smart_grid_is_enable(void); + +/* + * smart_grid_set_vip_list + * + * Set a list of pid as VIP task. + * *pid_ptr* : the source pid list. + * *pid_nr* : the number of source pid list. + * + * Note: + * This interface does not maintain the status of all current VIP tasks. + * Therefore, this interface only modifies the VIP status of tasks in the current PID list. + * Returns + * >= 0 : The number of PID successfully set. + * or a negative error in case of failure. + */ +int smart_grid_set_vip_list(pid_t* pid_ptr, int pid_nr); + +/* + * smart_grid_set_vip + * + * Set individual pid as VIP task. + * *pid* : the pid going to set. + * *is_vip* : 1 set pid VIP flag, 0 clear PID VIP flag + * + * Note: + * This interface does not maintain the status of all current VIP tasks. + * Therefore, this interface only modifies the VIP status of tasks in the current PID. + * Returns + * 0 on success, or a negative error in case of failure. + */ +int smart_grid_set_vip(pid_t pid, int is_vip); + +/* + * smart_grid_is_vip + * + * Check whether task is vip. + * *pid* : the pid need to check. + * + * Returns + * 1 : The pid was VIP task. + * 0 : The pid not VIP task. + * or a negative error in case of failure. + */ +int smart_grid_is_vip(pid_t pid); +#endif \ No newline at end of file diff --git a/smart_grid/src/CMakeLists.txt b/smart_grid/src/CMakeLists.txt new file mode 100755 index 0000000..6bbee66 --- /dev/null +++ b/smart_grid/src/CMakeLists.txt @@ -0,0 +1,24 @@ +cmake_minimum_required (VERSION 3.16) +project (smart_grid C) +set ( CMAKE_INCLUDE_CURRENT_DIR ON) + +# Add head directory +include_directories ("${PROJECT_SOURCE_DIR}/../inc") + +# Load source file +aux_source_directory(${PROJECT_SOURCE_DIR} SMART_GRID_SRC_DIR) +set(SMART_GRID_SRC ${SMART_GRID_SRC_DIR}) + +# Set compile policy +set (PG_NAME ${PROJECT_NAME}) +add_library(${PG_NAME} SHARED ${SMART_GRID_SRC}) +set_target_properties(${PG_NAME} PROPERTIES LINKER_LANGUAGE C) +set (CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# set installation path +set ( CMAKE_INSTALL_PREFIX "/usr/local" CACHE PATH "Install path prefix" FORCE) +install (TARGETS ${PG_NAME} DESTINATION lib) + +# Release compile mode +set(CMAKE_BUILD_TYPE "Release") +set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall") diff --git a/smart_grid/src/libsmart_grid.c b/smart_grid/src/libsmart_grid.c new file mode 100644 index 0000000..6eefadb --- /dev/null +++ b/smart_grid/src/libsmart_grid.c @@ -0,0 +1,153 @@ +/* ***************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved. + * Author: zouyipeng + * Create: 2023-10-17 + * Description: Provide smart_grid API. + * **************************************************************************** */ +#include "smart_grid.h" +#include +#include +#include +#include + +#define SG_PID_PATH "/proc/%d/smart_grid_level" +#define SG_TASK_VIP "0" +#define SG_TASK_ENERGY "1" +#define SG_TASK_LEN 2 + +#define SG_ENABLE_PATH "/proc/sys/kernel/smart_grid_strategy_ctrl" +#define SG_STATUS_ENABLE "1" +#define SG_STATUS_DISABLE "0" +#define SG_STATUS_LEN 2 + +#define MAX_PID_LEVEL_LEN 255 + +int smart_grid_sysctl(int sysctl_flag) +{ + int fd; + int len; + + if (sysctl_flag >= SMART_GRID_NONE || sysctl_flag < 0) + return -EINVAL; + + fd = open(SG_ENABLE_PATH, O_RDWR); + if (fd < 0) + return fd; + + if (sysctl_flag == SMART_GRID_ENABLE) + len = write(fd, SG_STATUS_ENABLE, strlen(SG_STATUS_ENABLE)); + else + len = write(fd, SG_STATUS_DISABLE, strlen(SG_STATUS_DISABLE)); + + close(fd); + + if (len < 0) + return len; + + return 0; +} + +int smart_grid_is_enable(void) +{ + int fd; + int len; + char read_buff[SG_STATUS_LEN]; + + fd = open(SG_ENABLE_PATH, O_RDONLY); + if (fd < 0) + return fd; + + len = read(fd, read_buff, SG_STATUS_LEN); + + close(fd); + + if (len < 0) + return len; + + read_buff[SG_STATUS_LEN - 1] = '\0'; + + if (!strcmp(read_buff, SG_STATUS_ENABLE)) + return 1; + + return 0; +} + +int smart_grid_set_vip(pid_t pid, int is_vip) +{ + int fd; + int len; + char str_buff[MAX_PID_LEVEL_LEN]; + + if (is_vip < 0 || is_vip > 1) + return -EINVAL; + + if (snprintf(str_buff, sizeof(str_buff), SG_PID_PATH, pid) < 0) + return -EINVAL; + + fd = open(str_buff, O_RDWR); + if (fd < 0) + return fd; + + if (is_vip == 1) + len = write(fd, SG_TASK_VIP, strlen(SG_TASK_VIP)); + else + len = write(fd, SG_TASK_ENERGY, strlen(SG_TASK_ENERGY)); + + close(fd); + + if (len < 0) + return len; + + return 0; +} + +int smart_grid_set_vip_list(pid_t* pid_ptr, int pid_nr) +{ + int index; + int ret = 0; + pid_t *current_pid = NULL; + + if (pid_ptr == NULL || pid_nr <= 0) + return -EINVAL; + + for (index = 0; index < pid_nr; index++) { + current_pid = pid_ptr + index; + if (current_pid == NULL) + continue; + + if (smart_grid_set_vip(*current_pid, 1)) + continue; + + ret++; + } + return ret; +} + +int smart_grid_is_vip(pid_t pid) +{ + int fd; + int len; + char str_buff[MAX_PID_LEVEL_LEN]; + char read_buff[SG_TASK_LEN]; + + if (snprintf(str_buff, sizeof(str_buff), SG_PID_PATH, pid) < 0) + return -EINVAL; + + fd = open(str_buff, O_RDONLY); + if (fd < 0) + return fd; + + len = read(fd, read_buff, SG_TASK_LEN); + + close(fd); + + if (len < 0) + return len; + + read_buff[SG_TASK_LEN - 1] = '\0'; + + if (!strcmp(read_buff, SG_TASK_VIP)) + return 1; + + return 0; +} diff --git a/uninstall.sh b/uninstall.sh index 29ab1d3..a524a3f 100644 --- a/uninstall.sh +++ b/uninstall.sh @@ -8,6 +8,7 @@ if [ -f "./build/install_manifest.txt" ];then xargs rm < install_manifest.txt else rm /usr/lib/libpwrapi.so + rm /usr/lib/libsmart_grid.so rm /usr/sbin/pwrapis rm /etc/sysconfig/pwrapis/pwrapis_config.ini rm /usr/lib/systemd/system/pwrapis.service -- Gitee