From 62b67cb0904dc12346b684e66214486ac3c6ccc3 Mon Sep 17 00:00:00 2001 From: Chen Gong Date: Fri, 26 Aug 2022 08:34:06 +0800 Subject: [PATCH] Add diskservice for powerapi server side Initial support for disk service on the server side. Just a few interfaces are implemented and some helper functions. Signed-off-by: Chen Gong --- .gitignore | 4 +- common/inc/list.h | 231 +++++++++++++++++++++++++++++++++++++++++++ common/inc/pwrdata.h | 5 + pwrapis/inc/common.h | 1 + pwrapis/inc/config.h | 7 ++ pwrapis/inc/gather.h | 165 +++++++++++++++++++++++++++++++ pwrapis/inc/utils.h | 19 ++-- pwrapis/src/server.c | 15 ++- pwrapis/src/utils.c | 47 +++++++-- 9 files changed, 477 insertions(+), 17 deletions(-) create mode 100644 common/inc/list.h create mode 100644 pwrapis/inc/gather.h diff --git a/.gitignore b/.gitignore index 8660c18..4c0711e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ install build release -debug \ No newline at end of file +debug +cscope.* +tags diff --git a/common/inc/list.h b/common/inc/list.h new file mode 100644 index 0000000..73dce1e --- /dev/null +++ b/common/inc/list.h @@ -0,0 +1,231 @@ +/****************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved. + * iSulad 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: luocaimin + * Create: 2022-08-26 + * Description: provide list interface + ******************************************************************************/ +#ifndef __LIST_H__ +#define __LIST_H__ + +#include + +struct ListHead { + struct ListHead* next; + struct ListHead* prev; +}; + +static inline void ListInit(struct ListHead* list) +{ + list->next = list; + list->prev = list; +}; + +/* + * Add item before next + */ +static inline void ListAdd(struct ListHead* item, struct ListHead* prev, struct ListHead* next) +{ + next->prev = item; + item->next = next; + item->prev = prev; + prev->next = item; +} + +/** + * Add item before head + */ +static inline void ListAddHead(struct ListHead* item, struct ListHead* head) +{ + ListAdd(item, head, head->next); +} + +/** + * Add item after tail + */ +static inline void ListAddTail(struct ListHead* item, struct ListHead* head) +{ + ListAdd(item, head->prev, head); +} + +/* + * Connect two node + */ +static inline void ConnectItem(struct ListHead* prev, struct ListHead* next) +{ + next->prev = prev; + prev->next = next; +} + +/** + * Delete item from list + */ +static inline void DelItem(struct ListHead* item) +{ + ConnectItem(item->prev, item->next); +} + +static inline void DelAndClearItem(struct ListHead* item) +{ + DelItem(item); + item->next = NULL; + item->prev = NULL; +} + +/** + * Replace item + */ +static inline void ReplaceItem(struct ListHead* oldItem, struct ListHead* newItem) +{ + newItem->next = oldItem->next; + newItem->next->prev = newItem; + newItem->prev = oldItem->prev; + newItem->prev->next = newItem; +} + +/** + * IsLastItem - tests whether item is the last item in list head + */ +static inline int IsLastItem(const struct ListHead* item, const struct ListHead* head) +{ + return item->next == head; +} + +/** + * IsFirstItem - tests whether item is the first item in list head + */ +static inline int IsFirstItem(const struct ListHead* item, const struct ListHead* head) +{ + return item->prev == head; +} + +/** + * IsEmptyList - tests whether a list is empty + */ +static inline int IsEmptyList(const struct ListHead* head) +{ + return head->next == head; +} + +static inline void JoinList(const struct ListHead* list, struct ListHead* prev, struct ListHead* next) +{ + struct ListHead* first = list->next; + struct ListHead* last = list->prev; + + first->prev = prev; + prev->next = first; + + last->next = next; + next->prev = last; +} + +/** + * SpliceList - join two lists + */ +static inline void SpliceList(const struct ListHead* list, struct ListHead* head) +{ + if (!IsEmptyList(list)) { + JoinList(list, head, head->next); + } +} + +/** + * OFFSETOF - calculate member offset + */ +#define OFFSETOF(type, member) ((size_t) &((type*)0)->member) + +/** + * CONTAINER_OF - cast a member of a structure out to the containing structure + * @ptr: the pointer to the member. + * @type: the type of the container struct this is embedded in. + * @member: the name of the member within the struct. + * + */ + +#define CONTAINER_OF(ptr, type, member) ({ (type*)((char*)(ptr) - OFFSETOF(type, member));}) + +/** + * GET_LIST_ITEM - get the struct address by it's' member + * @ptr: the ListHead pointer. + * @type: the type of the struct . + * @member: the name of the ListHead within the struct. + */ +#define GET_LIST_ITEM(ptr, type, member) \ + CONTAINER_OF(ptr, type, member) + +/** + * GET_FIRST_LIST_ITEM - get the first element from a list + */ +#define GET_FIRST_LIST_ITEM(ptr, type, member) \ + GET_LIST_ITEM((ptr)->next, type, member) + +/** + * GET_LAST_ITEM - get the last element from a list + */ +#define GET_LAST_ITEM(ptr, type, member) \ + GET_LIST_ITEM((ptr)->prev, type, member) + +/** + * LIST_NEXT_ITEM - get the next element in list + * @pos: the type * to cursor + * @member: the name of the ListHead within the struct. + */ +#define LIST_NEXT_ITEM(pos, member) \ + GET_LIST_ITEM((pos)->member.next, typeof(* (pos)), member) + +/** + * LIST_PREV_ITEM - get the prev element in list + * @pos: the type * to cursor + * @member: the name of the ListHead within the struct. + */ +#define LIST_PREV_ITEM(pos, member) \ + GET_LIST_ITEM((pos)->member.prev, typeof(* (pos)), member) + +/** + * LIST_FOR_EACH - iterate over a list + * @pos: the &struct ListHead to use as a loop cursor. + * @head: the head for your list. + */ +#define LIST_FOR_EACH(pos, head) \ + for ((pos) = (head)->next; (pos) != (head); (pos) = (pos)->next) + +/** + * LIST_FOR_EACH_PREV - iterate over a list backwards + * @pos: the &struct ListHead to use as a loop cursor. + * @head: the head for your list. + */ +#define LIST_FOR_EACH_PREV(pos, head) \ + for ((pos) = (head)->prev; (pos) != (head); (pos) = (pos)->prev) + +/** + * LIST_FOR_EACH_SAFE - remove safe loop + * @pos: the list_head to use as a loop cursor. + * @n: the list_head to use as temporary storage + * @head: the list head + */ +#define LIST_FOR_EACH_SAFE(pos, n, head) \ + for ((pos) = (head)->next, (n) = (pos)->next; (pos) != (head); \ + (pos) = (n), (n) = (pos)->next) + +/** + * FREE_LIST_ITEM - Release the space of linked list elements + * @pos: the list_head to use as a loop cursor. + * @n: the list_head to use as temporary storage + * @head: the list head + * @type: the type of the container struct this is embedded in. + * @member: the name of the member within the struct. + */ +#define FREE_LIST_ITEM(pos, n, head, type, member) do { \ + LIST_FOR_EACH_SAFE(pos, n, head) { \ + DelAndClearItem(pos); \ + free((type *)GET_LIST_ITEM(pos, type, member)); \ + } \ +}while (0) +#endif diff --git a/common/inc/pwrdata.h b/common/inc/pwrdata.h index 75f3113..dd7e0bf 100644 --- a/common/inc/pwrdata.h +++ b/common/inc/pwrdata.h @@ -22,6 +22,7 @@ #define MAX_GOV_NUM 16 #define MAX_STRING_LEN 1000 #define MAX_CPU_DMA_LATENCY 2000000000 +#define MAX_DISK_LIST_LEN 128 enum Arch { AARCH_64 = 0, @@ -109,6 +110,10 @@ typedef struct PWR_NET_Through { // DISK +typedef struct PWR_DISK_Info { + char diskId[MAX_ELEMENT_NAME_LEN]; +} PWR_DISK_Info; + typedef struct PWR_DISK_Load { char diskId[MAX_ELEMENT_NAME_LEN]; uint32_t rLoad; diff --git a/pwrapis/inc/common.h b/pwrapis/inc/common.h index 79dcbe9..ff142bb 100644 --- a/pwrapis/inc/common.h +++ b/pwrapis/inc/common.h @@ -56,6 +56,7 @@ #define MD_NM_NET "NET" #define MD_NM_MAN "MAIN" #define MD_NM_SVR_CPU "CPU_SERVICE" +#define MD_NM_SVR_DISK "DISK_SERVICE" // Define configuration section name #define CFG_NM_PST "persist" diff --git a/pwrapis/inc/config.h b/pwrapis/inc/config.h index a1b1ac6..d970e70 100644 --- a/pwrapis/inc/config.h +++ b/pwrapis/inc/config.h @@ -16,6 +16,7 @@ #define __PAPIS_CONFIG_H__ #include #include "common.h" +#include "list.h" #define DEFAULT_SERVER_ADDR "pwrserver.sock" #define DEFAULT_LOG_PATH "/opt/os_data/log" @@ -42,6 +43,12 @@ typedef struct LogCfg { char logPfx[MAX_PATH_NAME]; } LogCfg; +// Config Item +struct CnfItem { + char name[MAX_NAME]; + char value[MAX_VALUE]; + struct ListHead node; +}; // ServCfg typedef struct ServCfg { diff --git a/pwrapis/inc/gather.h b/pwrapis/inc/gather.h new file mode 100644 index 0000000..314a190 --- /dev/null +++ b/pwrapis/inc/gather.h @@ -0,0 +1,165 @@ +/****************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved. + * iSulad 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: luocaimin + * Create: 2022-08-26 + * Description: provide cpu collect methods + ******************************************************************************/ +#ifndef __GATHER_H__ +#define __GATHER_H__ + +#include +#include +#include +#include +#include + +#define BASE_PAY_LOAD 10000 +#define NORMAL_PAY_LOAD 100 +typedef struct CollVal { + time_t collSec; + const char* pVal; +} CollVal; + +// Collect method type +typedef CollVal (* CollHandler)(void*); + +// Collect config item +struct CollCfg { + uint16_t interSec; // The number of seconds between the collection of the collector + char mntPoint[MAX_FILE_NAME]; +}; + +// Time keeper +struct TimeKeeper { + int16_t leftSecs; // Seconds to next acquisition + int16_t period; // Data Sampling period + CollHandler handler; // Collection function + char dataName[MAX_NAME]; // Collection item name + struct ListHead node; + long reserv; // Reserved field + unsigned long long reserv1; // Reserved field +}; + +// Acquisition timer +struct TypeKeepers { + struct ListHead node; + struct ListHead tmKprLst; + char typeName[MAX_SEC_NAME]; +}; + +// Collection item configuration +struct CollDataCfg { + uint16_t period; + CollHandler handler; + char dataName[MAX_NAME]; + char typeName[MAX_SEC_NAME]; +}; +// Payload struction define +typedef struct TypePayload { + char* typeName; + long payload; + struct ListHead node; +} TypePayload; + +typedef struct PayloadTab { + uint16_t basePeriod; + long totalPayload; + struct ListHead typePayloadLst; +} PayloadTab; + +typedef struct PriodCnt { + uint16_t period; + uint16_t cnt; + struct ListHead node; +} PriodCnt; + +typedef struct Tid { + pthread_t tid; + struct ListHead node; +} Tid; + +typedef enum ExistSt (*ExistFuncType)(const char*); + +typedef struct ExistFuncMap { + const char* pMapNm; + ExistFuncType handler; +} ExistFuncMap; +/** + * InitCollector - According to the collection configuration, + * initialize the collector + * + * Note: return 0 if success; -1 if failed + */ +int InitCollector(const struct CollCfg cfg); + +/** + * UpdColCfg - Update the acquisition configuration and make it effective + */ +int UpdColCfg(const struct CollCfg cfg); +/** + * UpdPeriod - Update data collection cycle and make it effective + * + * @pTpNm: Pointer to the name of the collection type + * @dNm: Pointer to the name of the collected data + * @period: New acquisition cycle + * + * Note: return 0 if success; - 1 if failed + */ +int UpdPeriod(const char* pTpNm, const char* dNm, uint16_t period); +/** + * DtCollReg - Data collection registration + * + * Note: return 0 if success; - 1 if failed + */ +int DtCollReg(const struct CollDataCfg* pItem); +/* + * MapItmNm: Construct the collection item name according to + * the mapping rulonstruct the collection item name + * according to the mapping rule + * + * IN: + * @name Mapping item name + * @val Mapped name + * + * RETURNS: 0 on success, -1 on other + * + */ +char* MapItmNm(const char *oldName, char *newName, int nameLen); +/** + * DoCollect - Collect each type of data according to the collection interval, + * summarize the collection result set, and entrust the persistence + * module for persistence + */ +void DoCollect(void); + +int StartCollector(void); + +int StopCollector(void); + +int RestartCollector(void); +/* + * ClearCollector - Clean up the collector + * + * Note: return 0 if success; - 1 if failed + **/ +int ClearCollector(void); +/* + * IsObjExisted - Determine whether the collection object exists + * + * IN: + * @mapNm Collection object type identification string(eg: io.disk0 net.eth1...) + * @itmNm Collection object name(eg: vda eth0 ...) + * + * RETURNS: EXIST or NOT_EXIST + * + */ +enum ExistSt IsObjExisted(const char* mapNm, const char* itemNm); +#endif diff --git a/pwrapis/inc/utils.h b/pwrapis/inc/utils.h index 3885b9c..1c00700 100644 --- a/pwrapis/inc/utils.h +++ b/pwrapis/inc/utils.h @@ -12,8 +12,8 @@ * Create: 2022-06-23 * Description: provide common methods * **************************************************************************** */ -#ifndef LCM_UTILS_H -#define LCM_UTILS_H +#ifndef __UTILS_H__ +#define __UTILS_H__ #include #include #include @@ -56,6 +56,13 @@ const char *GetCurFullTime(char *fullTime, int bufLen); */ size_t GetFileSize(const char *fileName); +/** + * Return file lines + * + * Note: Return zero if it successes + */ +int GetFileLines(const char *file, int *num); + /** * StrTime2Sec - Convert strTime with format(20201206134723) to seconds */ @@ -71,7 +78,7 @@ time_t GetLastDaySec(const char *day); */ time_t GetLastCurDaySec(void); /** - * LcmGetNthField - Find the @nth field separated by @sep in the string + * GetNthField - Find the @nth field separated by @sep in the string * * @src : Source string * @sep : Separating substrings, where each character @@ -81,14 +88,14 @@ time_t GetLastCurDaySec(void); * Note : return the start position in the @src if success * return NULL if others */ -const char *LcmGetNthField(const char *src, const char *sep, int nth, char *pField, size_t bufLen); +const char *GetNthField(const char *src, const char *sep, int nth, char *pField, size_t bufLen); /** - * LcmGetNthLine - Get the @nth line string of the file + * GetNthLine - Get the @nth line string of the file * * Note : return line if success; NULL if failed; */ -const char *LcmGetNthLine(const char *fileName, int nth, char *lineBuf, size_t bufLen); +const char *GetNthLine(const char *fileName, int nth, char *lineBuf, size_t bufLen); /** * GetVal - Get the value of the @fieldNum'th field in the @lineNum'th line of the file */ diff --git a/pwrapis/src/server.c b/pwrapis/src/server.c index 5b62252..910e4da 100644 --- a/pwrapis/src/server.c +++ b/pwrapis/src/server.c @@ -340,7 +340,18 @@ static void ProcessReqMsg(PwrMsg *req) GetCpuFreq(req); break; case DISK_GET_IO_RATE: - // todo + break; + case DISK_GET_LIST: + break; + case DISK_GET_LOAD: + break; + case DISK_GET_POWER_LEVEL: + break; + case DISK_SET_POWER_LEVEL: + break; + case DISK_GET_SCSI_POLICY: + break; + case DISK_SET_SCSI_POLICY: break; default: break; @@ -412,4 +423,4 @@ void StopServer(void) int SendRspMsg(PwrMsg *rsp) { return AddToBufferTail(&g_sendBuff, rsp); -} \ No newline at end of file +} diff --git a/pwrapis/src/utils.c b/pwrapis/src/utils.c index b961c72..d653d47 100644 --- a/pwrapis/src/utils.c +++ b/pwrapis/src/utils.c @@ -13,6 +13,7 @@ * Description: provide common methods * **************************************************************************** */ #include "utils.h" +#include "pwrerr.h" #include #include @@ -156,7 +157,37 @@ size_t GetFileSize(const char *fileName) return fileStat.st_size; } -static int LcmStrptime(const char *strTime, struct tm *pTm) +int GetFileLines(const char *file, int *num) +{ + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t read; + int *p = num; + + if (file == NULL) { + return ERR_INVALIDE_PARAM; + } + fp = fopen(file, "r"); + if (fp == NULL) { + return ERR_NULL_POINTER; + } + + *p = 0; + while ((read = getline(&line, &len, fp)) != -1) { + *p++; + } + + if (line) { + free(line); + } + if (fclose(fp) < 0) { + return ERR_COMMON; + } + return SUCCESS; +} + +static int Strptime(const char *strTime, struct tm *pTm) { int inNum; if (pTm == NULL || strTime == NULL || strlen(strTime) < MIN_TM_LEN || !IsNumStr(strTime)) { @@ -182,7 +213,7 @@ time_t StrTime2Sec(const char *strTime) return 0; } bzero(&tmpTm, sizeof(struct tm)); - if (LcmStrptime(strTime, &tmpTm) != SUCCESS) { + if (Strptime(strTime, &tmpTm) != SUCCESS) { return 0; } return mktime(&tmpTm); @@ -206,7 +237,7 @@ time_t GetLastCurDaySec(void) return GetLastDaySec(pDay); } /** - * LcmGetNthField - Find the @nth field separated by @sep in the string + * GetNthField - Find the @nth field separated by @sep in the string * * @src : Source string * @sep : Separating substrings, where each character @@ -216,7 +247,7 @@ time_t GetLastCurDaySec(void) * Note : return the start position in the @src if success * return NULL if others */ -const char *LcmGetNthField(const char *src, const char *sep, int nth, char *pField, size_t bufLen) +const char *GetNthField(const char *src, const char *sep, int nth, char *pField, size_t bufLen) { size_t tmpLen = 0; const char *ps = src; @@ -250,11 +281,11 @@ const char *LcmGetNthField(const char *src, const char *sep, int nth, char *pFie } /** - * LcmGetNthLine - Get the @nth line string of the file + * GetNthLine - Get the @nth line string of the file * * Note : return line if success; NULL if failed; */ -const char *LcmGetNthLine(const char *fileName, int nth, char *lineBuf, size_t bufLen) +const char *GetNthLine(const char *fileName, int nth, char *lineBuf, size_t bufLen) { const char *pRes = NULL; char line[MAX_LINE_NUM] = {0}; @@ -294,11 +325,11 @@ const char *GetVal(struct FieldLocation fdLt, char *valBuf, size_t bufLen) if (bufLen < 0) { return NULL; } - pRes = LcmGetNthLine(fdLt.fileName, fdLt.lineNum, lineBuf, sizeof(lineBuf) - 1); + pRes = GetNthLine(fdLt.fileName, fdLt.lineNum, lineBuf, sizeof(lineBuf) - 1); if (pRes == NULL) { return NULL; } - pRes = LcmGetNthField(lineBuf, fdLt.sep, fdLt.fieldNum, valBuf, bufLen); + pRes = GetNthField(lineBuf, fdLt.sep, fdLt.fieldNum, valBuf, bufLen); return pRes; } -- Gitee