From 0b4398d6074473b65e779a05feb5911ad42cb2e3 Mon Sep 17 00:00:00 2001 From: JerryWang Date: Mon, 27 May 2024 15:45:22 +0800 Subject: [PATCH 1/6] [Add] Transcend plugin command for PLP health --- .../nvme-transcend-plphealthvalue.txt | 38 ++ plugins/transcend/transcend-nvme.c | 354 +++++++++++++++++- plugins/transcend/transcend-nvme.h | 2 +- 3 files changed, 380 insertions(+), 14 deletions(-) create mode 100644 Documentation/nvme-transcend-plphealthvalue.txt diff --git a/Documentation/nvme-transcend-plphealthvalue.txt b/Documentation/nvme-transcend-plphealthvalue.txt new file mode 100644 index 000000000..930a1e546 --- /dev/null +++ b/Documentation/nvme-transcend-plphealthvalue.txt @@ -0,0 +1,38 @@ +nvme-transcend-plphealthvalue(1) +============================= + +NAME +---- +nvme-transcend-plphealthvalue - Use NVMe SMART table to analyze the plp health value of +Transcend device. + +SYNOPSIS +-------- +[verse] +'nvme transcend plphealthvalue' + +DESCRIPTION +----------- +Retrieves the NVMe Device SMART log page from the Transcend device and evaluate +plp health status of Transcend device. + +The parameter is mandatory and may be either the NVMe character +device (ex: /dev/nvme0), or a namespace block device (ex: /dev/nvme0n1). + +On success, the returned value would print plp health percentage value. + +OPTIONS +------- +none + +EXAMPLES +-------- +* Print the Transcend Device plp health value in a human readable format: ++ +------------ +# nvme transcend plphealthvalue /dev/nvme0 +------------ + +NVME +---- +Part of the nvme-user suite diff --git a/plugins/transcend/transcend-nvme.c b/plugins/transcend/transcend-nvme.c index 547fbf4ea..070159b7e 100644 --- a/plugins/transcend/transcend-nvme.c +++ b/plugins/transcend/transcend-nvme.c @@ -9,39 +9,90 @@ #include "nvme.h" #include "libnvme.h" #include "plugin.h" +#include +#include #define CREATE_CMD + +#define PLPRecordPath "PLPRec.txt" +#define PLPDataExpiredTime 20 + #include "transcend-nvme.h" static const __u32 OP_BAD_BLOCK = 0xc2; static const __u32 DW10_BAD_BLOCK = 0x400; static const __u32 DW12_BAD_BLOCK = 0x5a; +static const __u32 OP_HEALTH = 0xc2; +static const __u32 DW10_HEALTH = 0x01; +static const __u32 DW12_HEALTH = 0xb3; + +static const __u32 OP_ID = 0x06; +static const __u32 DW10_ID = 0x01; + +static const int iDis = 20; +static const double fullValue = 170; + +enum PLPErrorCode +{ + PLP_ERROR_NO_MATCH = -1, + PLP_ERROR_DATA_EXPIRED = -2 +}; + +const char *string_list[] = { + "UTE210T", + "MTE712P", + "MTE560P", + "MTE662P", + "MTE662P-I", + "MTS970P", + "MTS952P", + "MTS952P-I", + "MTS570P", + "MTS400P", + "SSD910T", + "SSD470P", + "SSD470P-I", + "SSD452P", + "SSD452P-I", + "SSD420P", + "MSA470P", + "MSA452P"}; +const int list_size = sizeof(string_list) / sizeof(string_list[0]); + +static int getPLPHealth(int argc, char **argv, struct command *cmd, struct plugin *plugin); +static int readUsefulPLPValue(const char *device); +static void recordPLPValue(const char *device, int value, bool isReplace); + static int getHealthValue(int argc, char **argv, struct command *cmd, struct plugin *plugin) { struct nvme_smart_log smart_log; char *desc = "Get nvme health percentage."; - int percent_used = 0, healthvalue = 0; + int percent_used = 0, healthvalue = 0; struct nvme_dev *dev; int result; OPT_ARGS(opts) = { - OPT_END() - }; + OPT_END()}; result = parse_and_open(&dev, argc, argv, desc, opts); - if (result) { + if (result) + { printf("\nDevice not found\n"); - return -1; + return PLP_ERROR_NO_MATCH; } result = nvme_get_log_smart(dev_fd(dev), 0xffffffff, false, &smart_log); - if (!result) { + if (!result) + { printf("Transcend NVME heath value: "); percent_used = smart_log.percent_used; - if (percent_used > 100 || percent_used < 0) { + if (percent_used > 100 || percent_used < 0) + { printf("0%%\n"); - } else { + } + else + { healthvalue = 100 - percent_used; printf("%d%%\n", healthvalue); } @@ -58,11 +109,11 @@ static int getBadblock(int argc, char **argv, struct command *cmd, struct plugin int result; OPT_ARGS(opts) = { - OPT_END() - }; + OPT_END()}; result = parse_and_open(&dev, argc, argv, desc, opts); - if (result) { + if (result) + { printf("\nDevice not found\n"); return -1; } @@ -76,11 +127,288 @@ static int getBadblock(int argc, char **argv, struct command *cmd, struct plugin nvmecmd.addr = (__u64)(uintptr_t)data; nvmecmd.data_len = 0x1; result = nvme_submit_admin_passthru(dev_fd(dev), &nvmecmd, NULL); - if (!result) { - int badblock = data[0]; + if (!result) + { + int badblock = data[0]; printf("Transcend NVME badblock count: %d\n", badblock); } dev_close(dev); return result; } + +const char *format_char_array(char *str, int strsize, unsigned char *chr, int chrsize) +{ + int b = 0; + while (b < chrsize && chr[b] == ' ') + b++; + int n = 0; + while (b + n < chrsize && chr[b + n]) + n++; + while (n > 0 && chr[b + n - 1] == ' ') + n--; + + if (n >= strsize) + n = strsize - 1; + + for (int i = 0; i < n; i++) + { + char c = chr[b + i]; + str[i] = (' ' <= c && c <= '~' ? c : '?'); + } + str[n] = 0; + return str; +} + +int contains_string(const char *str) +{ + for (int i = 0; i < list_size; i++) + { + if (strstr(str, string_list[i]) != NULL) + { + return 1; + } + } + return 0; +} + +static int getPLPHealth(int argc, char **argv, struct command *cmd, struct plugin *plugin) +{ + char *desc = "Get nvme PLP Health."; + struct nvme_dev *dev; + int result; + int txtPLPHealth = -1; + int plpHealth_percentage = -1; + OPT_ARGS(opts) = { + OPT_END()}; + + result = parse_and_open(&dev, argc, argv, desc, opts); + if (result) + { + printf("\nDevice not found\n"); + return -1; + } + + unsigned char dataID[4096]; + struct nvme_passthru_cmd nvmecmdID; + + memset(&nvmecmdID, 0, sizeof(nvmecmdID)); + nvmecmdID.opcode = OP_ID; + nvmecmdID.cdw10 = DW10_ID; + nvmecmdID.addr = (__u64)(uintptr_t)dataID; + nvmecmdID.data_len = 4096; + result = nvme_submit_admin_passthru(dev_fd(dev), &nvmecmdID, NULL); + if (result) + { + printf("\nThis device is not support.\n"); + return -1; + } + else + { + char modelName[40]; + const char *model_str_byte; + model_str_byte = format_char_array((char *)modelName, sizeof(modelName), dataID, sizeof(dataID)); + if (!contains_string(model_str_byte)) + { + printf("\nThis device is not support.\n"); + return -1; + } + } + + txtPLPHealth = readUsefulPLPValue(dev->name); + if (txtPLPHealth >= 0) + { + plpHealth_percentage = txtPLPHealth; + printf("Capacitor health for PLP: %d%%\n", plpHealth_percentage); + } + else + { + unsigned char data[512]; + struct nvme_passthru_cmd nvmecmd; + + memset(&nvmecmd, 0, sizeof(nvmecmd)); + nvmecmd.opcode = OP_HEALTH; + nvmecmd.cdw10 = DW10_HEALTH; + nvmecmd.cdw12 = DW12_HEALTH; + nvmecmd.addr = (__u64)(uintptr_t)data; + nvmecmd.data_len = 512; + result = nvme_submit_admin_passthru(dev_fd(dev), &nvmecmd, NULL); + if (result) + { + printf("\nGet PLP Health Fail.\n"); + return PLP_ERROR_NO_MATCH; + } + else + { + int tDis = (int)(data[4 + 3] << 24) + (int)(data[4 + 2] << 16) + (int)(data[4 + 1] << 8) + (int)(data[4]); + int vDis1 = (int)(data[0]); + int vDis2 = (int)(data[1]); + if (vDis1 != 0 || vDis2 != 0) + { + int normalHealth = 0; + + if (vDis1 - vDis2 != 0) + { + normalHealth = (iDis * tDis) / (vDis1 - vDis2); + } + else + { + normalHealth = 0; + } + if (normalHealth >= 0) + { + if (fullValue - normalHealth >= 0) + { + plpHealth_percentage = (normalHealth / fullValue) * 100; + } + else + { + plpHealth_percentage = 100; + } + } + else + { + plpHealth_percentage = 0; + } + printf("Capacitor health for PLP: %d%%\n", plpHealth_percentage); + } + } + } + if (plpHealth_percentage >= 0) + { + if (txtPLPHealth == PLP_ERROR_DATA_EXPIRED) + { + recordPLPValue(dev->name, plpHealth_percentage, true); + } + else + { + recordPLPValue(dev->name, plpHealth_percentage, false); + } + } + dev_close(dev); + return result; +} + +int readUsefulPLPValue(const char *device) +{ + FILE *file; + char logFilePath[256]; + char str[256]; + char matchedStr[256] = ""; + int ret = -1; + + snprintf(logFilePath, sizeof(logFilePath), "%s", PLPRecordPath); + file = fopen(logFilePath, "r"); + if (!file) + { + return PLP_ERROR_NO_MATCH; + } + + while (fgets(str, sizeof(str), file)) + { + if (strncmp(str, device, strlen(device)) == 0) + { + strcpy(matchedStr, str); + break; + } + } + + fclose(file); + + if (matchedStr[0] == '\0') + { + return PLP_ERROR_NO_MATCH; + } + + char *token; + token = strtok(matchedStr, "#"); + token = strtok(NULL, "#"); + struct tm tm; + memset(&tm, 0, sizeof(tm)); + strptime(token, "%a %b %d %H:%M:%S %Y", &tm); + time_t t = mktime(&tm); + + time_t t_current = time(NULL); + int timeDiff = difftime(t_current, t); + + if (timeDiff <= PLPDataExpiredTime) + { + token = strtok(NULL, "#"); + ret = atoi(token); + return ret; + } + else + { + return PLP_ERROR_DATA_EXPIRED; + } +} + +void recordPLPValue(const char *device, int value, bool isReplace) +{ + char logFilePath[256]; + strncpy(logFilePath, PLPRecordPath, sizeof(logFilePath) - 1); + logFilePath[sizeof(logFilePath) - 1] = '\0'; + char tempFilePath[] = "temp.txt"; + + time_t ct = time(0); + char *timeStr = ctime(&ct); + if (timeStr == NULL) + { + perror("Error getting current time"); + return; + } + timeStr[strcspn(timeStr, "\n")] = '\0'; + + char line[256]; + sprintf(line, "%s#%s#%d", device, timeStr, value); + + if (isReplace) + { + FILE *filein = fopen(logFilePath, "r"); + FILE *fileout = fopen(tempFilePath, "w"); + if (filein == NULL || fileout == NULL) + { + perror("Error opening file"); + if (filein != NULL) + fclose(filein); + if (fileout != NULL) + fclose(fileout); + return; + } + + char str[256]; + while (fgets(str, sizeof(str), filein)) + { + if (strncmp(str, device, strlen(device)) == 0) + { + fprintf(fileout, "%s\n", line); + } + else + { + fprintf(fileout, "%s", str); + } + } + fclose(filein); + fclose(fileout); + + if (remove(logFilePath) == 0) + { + rename(tempFilePath, logFilePath); + } + else + { + remove(tempFilePath); + } + } + else + { + FILE *out = fopen(logFilePath, "a"); + if (out == NULL) + { + perror("Error opening file"); + return; + } + fprintf(out, "%s\n", line); + fclose(out); + } +} diff --git a/plugins/transcend/transcend-nvme.h b/plugins/transcend/transcend-nvme.h index 9c8988380..c89027731 100644 --- a/plugins/transcend/transcend-nvme.h +++ b/plugins/transcend/transcend-nvme.h @@ -12,7 +12,7 @@ PLUGIN(NAME("transcend", "Transcend vendor specific extensions", NVME_VERSION), COMMAND_LIST( ENTRY("healthvalue", "NVME health percentage", getHealthValue) ENTRY("badblock", "Get NVME bad block number", getBadblock) - + ENTRY("plphealthvalue", "Get NVME PLP Health.", getPLPHealth) ) ); From 2049e2988a17bf27363d36dbb1995c7c44c2bd2b Mon Sep 17 00:00:00 2001 From: JerryWang Date: Mon, 27 May 2024 15:53:44 +0800 Subject: [PATCH 2/6] [Add] Transcend plugin command for PLP health --- meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/meson.build b/meson.build index 11f7cc545..b1cd66997 100644 --- a/meson.build +++ b/meson.build @@ -281,6 +281,7 @@ sources = [ 'nvme-wrap.c', 'plugin.c', 'libnvme-wrap.c', + 'plugins/transcend/transcend-nvme.c' ] if json_c_dep.found() sources += [ From 55133c4d03bdd6e2991c6cb07b16e84b773fcde5 Mon Sep 17 00:00:00 2001 From: JerryWang Date: Mon, 21 Oct 2024 15:20:19 +0800 Subject: [PATCH 3/6] coding sytle fixed --- meson.build | 1 - plugins/transcend/transcend-nvme.c | 139 +++++++++++------------------ 2 files changed, 54 insertions(+), 86 deletions(-) diff --git a/meson.build b/meson.build index b1cd66997..11f7cc545 100644 --- a/meson.build +++ b/meson.build @@ -281,7 +281,6 @@ sources = [ 'nvme-wrap.c', 'plugin.c', 'libnvme-wrap.c', - 'plugins/transcend/transcend-nvme.c' ] if json_c_dep.found() sources += [ diff --git a/plugins/transcend/transcend-nvme.c b/plugins/transcend/transcend-nvme.c index 070159b7e..96d4aa4db 100644 --- a/plugins/transcend/transcend-nvme.c +++ b/plugins/transcend/transcend-nvme.c @@ -58,7 +58,7 @@ const char *string_list[] = { "SSD420P", "MSA470P", "MSA452P"}; -const int list_size = sizeof(string_list) / sizeof(string_list[0]); +const int list_size = ARRAY_SIZE(string_list) / sizeof(string_list[0]); static int getPLPHealth(int argc, char **argv, struct command *cmd, struct plugin *plugin); static int readUsefulPLPValue(const char *device); @@ -68,31 +68,29 @@ static int getHealthValue(int argc, char **argv, struct command *cmd, struct plu { struct nvme_smart_log smart_log; char *desc = "Get nvme health percentage."; - int percent_used = 0, healthvalue = 0; + int percent_used = 0; + int healthvalue = 0; struct nvme_dev *dev; int result; OPT_ARGS(opts) = { - OPT_END()}; + OPT_END() + }; result = parse_and_open(&dev, argc, argv, desc, opts); - if (result) - { + if (result) { printf("\nDevice not found\n"); return PLP_ERROR_NO_MATCH; } result = nvme_get_log_smart(dev_fd(dev), 0xffffffff, false, &smart_log); - if (!result) - { + if (!result) { printf("Transcend NVME heath value: "); percent_used = smart_log.percent_used; - if (percent_used > 100 || percent_used < 0) - { + if (percent_used > 100 || percent_used < 0) { printf("0%%\n"); } - else - { + else { healthvalue = 100 - percent_used; printf("%d%%\n", healthvalue); } @@ -109,11 +107,11 @@ static int getBadblock(int argc, char **argv, struct command *cmd, struct plugin int result; OPT_ARGS(opts) = { - OPT_END()}; + OPT_END() + }; result = parse_and_open(&dev, argc, argv, desc, opts); - if (result) - { + if (result) { printf("\nDevice not found\n"); return -1; } @@ -127,10 +125,8 @@ static int getBadblock(int argc, char **argv, struct command *cmd, struct plugin nvmecmd.addr = (__u64)(uintptr_t)data; nvmecmd.data_len = 0x1; result = nvme_submit_admin_passthru(dev_fd(dev), &nvmecmd, NULL); - if (!result) - { + if (!result) { int badblock = data[0]; - printf("Transcend NVME badblock count: %d\n", badblock); } dev_close(dev); @@ -140,22 +136,26 @@ static int getBadblock(int argc, char **argv, struct command *cmd, struct plugin const char *format_char_array(char *str, int strsize, unsigned char *chr, int chrsize) { int b = 0; + int n = 0; + int i; + while (b < chrsize && chr[b] == ' ') b++; - int n = 0; + while (b + n < chrsize && chr[b + n]) n++; + while (n > 0 && chr[b + n - 1] == ' ') n--; if (n >= strsize) n = strsize - 1; - for (int i = 0; i < n; i++) - { + for (i = 0; i < n; i++) { char c = chr[b + i]; str[i] = (' ' <= c && c <= '~' ? c : '?'); } + str[n] = 0; return str; } @@ -180,11 +180,11 @@ static int getPLPHealth(int argc, char **argv, struct command *cmd, struct plugi int txtPLPHealth = -1; int plpHealth_percentage = -1; OPT_ARGS(opts) = { - OPT_END()}; + OPT_END() + }; result = parse_and_open(&dev, argc, argv, desc, opts); - if (result) - { + if (result) { printf("\nDevice not found\n"); return -1; } @@ -198,31 +198,26 @@ static int getPLPHealth(int argc, char **argv, struct command *cmd, struct plugi nvmecmdID.addr = (__u64)(uintptr_t)dataID; nvmecmdID.data_len = 4096; result = nvme_submit_admin_passthru(dev_fd(dev), &nvmecmdID, NULL); - if (result) - { + if (result) { printf("\nThis device is not support.\n"); return -1; } - else - { + else { char modelName[40]; const char *model_str_byte; model_str_byte = format_char_array((char *)modelName, sizeof(modelName), dataID, sizeof(dataID)); - if (!contains_string(model_str_byte)) - { + if (!contains_string(model_str_byte)) { printf("\nThis device is not support.\n"); return -1; } } txtPLPHealth = readUsefulPLPValue(dev->name); - if (txtPLPHealth >= 0) - { + if (txtPLPHealth >= 0) { plpHealth_percentage = txtPLPHealth; printf("Capacitor health for PLP: %d%%\n", plpHealth_percentage); } - else - { + else { unsigned char data[512]; struct nvme_passthru_cmd nvmecmd; @@ -233,55 +228,43 @@ static int getPLPHealth(int argc, char **argv, struct command *cmd, struct plugi nvmecmd.addr = (__u64)(uintptr_t)data; nvmecmd.data_len = 512; result = nvme_submit_admin_passthru(dev_fd(dev), &nvmecmd, NULL); - if (result) - { + if (result) { printf("\nGet PLP Health Fail.\n"); return PLP_ERROR_NO_MATCH; } - else - { + else { int tDis = (int)(data[4 + 3] << 24) + (int)(data[4 + 2] << 16) + (int)(data[4 + 1] << 8) + (int)(data[4]); int vDis1 = (int)(data[0]); int vDis2 = (int)(data[1]); - if (vDis1 != 0 || vDis2 != 0) - { + if (vDis1 != 0 || vDis2 != 0) { int normalHealth = 0; - if (vDis1 - vDis2 != 0) - { + if (vDis1 - vDis2 != 0) { normalHealth = (iDis * tDis) / (vDis1 - vDis2); } - else - { + else { normalHealth = 0; } - if (normalHealth >= 0) - { - if (fullValue - normalHealth >= 0) - { + if (normalHealth >= 0) { + if (fullValue - normalHealth >= 0) { plpHealth_percentage = (normalHealth / fullValue) * 100; } - else - { + else { plpHealth_percentage = 100; } } - else - { + else { plpHealth_percentage = 0; } printf("Capacitor health for PLP: %d%%\n", plpHealth_percentage); } } } - if (plpHealth_percentage >= 0) - { - if (txtPLPHealth == PLP_ERROR_DATA_EXPIRED) - { + if (plpHealth_percentage >= 0) { + if (txtPLPHealth == PLP_ERROR_DATA_EXPIRED) { recordPLPValue(dev->name, plpHealth_percentage, true); } - else - { + else { recordPLPValue(dev->name, plpHealth_percentage, false); } } @@ -299,15 +282,13 @@ int readUsefulPLPValue(const char *device) snprintf(logFilePath, sizeof(logFilePath), "%s", PLPRecordPath); file = fopen(logFilePath, "r"); - if (!file) - { + if (!file) { return PLP_ERROR_NO_MATCH; } while (fgets(str, sizeof(str), file)) { - if (strncmp(str, device, strlen(device)) == 0) - { + if (strncmp(str, device, strlen(device)) == 0) { strcpy(matchedStr, str); break; } @@ -315,8 +296,7 @@ int readUsefulPLPValue(const char *device) fclose(file); - if (matchedStr[0] == '\0') - { + if (matchedStr[0] == '\0') { return PLP_ERROR_NO_MATCH; } @@ -331,14 +311,12 @@ int readUsefulPLPValue(const char *device) time_t t_current = time(NULL); int timeDiff = difftime(t_current, t); - if (timeDiff <= PLPDataExpiredTime) - { + if (timeDiff <= PLPDataExpiredTime) { token = strtok(NULL, "#"); ret = atoi(token); return ret; } - else - { + else { return PLP_ERROR_DATA_EXPIRED; } } @@ -352,8 +330,7 @@ void recordPLPValue(const char *device, int value, bool isReplace) time_t ct = time(0); char *timeStr = ctime(&ct); - if (timeStr == NULL) - { + if (timeStr == NULL) { perror("Error getting current time"); return; } @@ -362,12 +339,10 @@ void recordPLPValue(const char *device, int value, bool isReplace) char line[256]; sprintf(line, "%s#%s#%d", device, timeStr, value); - if (isReplace) - { + if (isReplace) { FILE *filein = fopen(logFilePath, "r"); FILE *fileout = fopen(tempFilePath, "w"); - if (filein == NULL || fileout == NULL) - { + if (filein == NULL || fileout == NULL) { perror("Error opening file"); if (filein != NULL) fclose(filein); @@ -379,32 +354,26 @@ void recordPLPValue(const char *device, int value, bool isReplace) char str[256]; while (fgets(str, sizeof(str), filein)) { - if (strncmp(str, device, strlen(device)) == 0) - { + if (strncmp(str, device, strlen(device)) == 0) { fprintf(fileout, "%s\n", line); } - else - { + else { fprintf(fileout, "%s", str); } } fclose(filein); fclose(fileout); - if (remove(logFilePath) == 0) - { + if (remove(logFilePath) == 0) { rename(tempFilePath, logFilePath); } - else - { + else { remove(tempFilePath); } } - else - { + else { FILE *out = fopen(logFilePath, "a"); - if (out == NULL) - { + if (out == NULL) { perror("Error opening file"); return; } From 25d6e908559e787b758cbbd0c0ee128c2841877c Mon Sep 17 00:00:00 2001 From: JerryWang Date: Tue, 22 Oct 2024 15:59:06 +0800 Subject: [PATCH 4/6] Build error fixed --- plugins/transcend/transcend-nvme.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/transcend/transcend-nvme.c b/plugins/transcend/transcend-nvme.c index 96d4aa4db..1978f66a8 100644 --- a/plugins/transcend/transcend-nvme.c +++ b/plugins/transcend/transcend-nvme.c @@ -58,7 +58,7 @@ const char *string_list[] = { "SSD420P", "MSA470P", "MSA452P"}; -const int list_size = ARRAY_SIZE(string_list) / sizeof(string_list[0]); +const int list_size = sizeof(string_list) / sizeof(string_list[0]); static int getPLPHealth(int argc, char **argv, struct command *cmd, struct plugin *plugin); static int readUsefulPLPValue(const char *device); From 69701d50e1f5eba389069f85dcf2755179b7316a Mon Sep 17 00:00:00 2001 From: JerryWang Date: Tue, 22 Oct 2024 16:33:02 +0800 Subject: [PATCH 5/6] Build error fixed --- plugins/transcend/transcend-nvme.c | 5 ++--- plugins/transcend/transcend-nvme.h | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/plugins/transcend/transcend-nvme.c b/plugins/transcend/transcend-nvme.c index 1978f66a8..3376c765a 100644 --- a/plugins/transcend/transcend-nvme.c +++ b/plugins/transcend/transcend-nvme.c @@ -33,8 +33,7 @@ static const __u32 DW10_ID = 0x01; static const int iDis = 20; static const double fullValue = 170; -enum PLPErrorCode -{ +enum PLPErrorCode { PLP_ERROR_NO_MATCH = -1, PLP_ERROR_DATA_EXPIRED = -2 }; @@ -58,7 +57,7 @@ const char *string_list[] = { "SSD420P", "MSA470P", "MSA452P"}; -const int list_size = sizeof(string_list) / sizeof(string_list[0]); +const int list_size = ARRAY_SIZE(string_list) / sizeof(string_list[0]); static int getPLPHealth(int argc, char **argv, struct command *cmd, struct plugin *plugin); static int readUsefulPLPValue(const char *device); diff --git a/plugins/transcend/transcend-nvme.h b/plugins/transcend/transcend-nvme.h index c89027731..289562f0f 100644 --- a/plugins/transcend/transcend-nvme.h +++ b/plugins/transcend/transcend-nvme.h @@ -4,7 +4,7 @@ #if !defined(TRANSCEND_NVME) || defined(CMD_HEADER_MULTI_READ) #define TRANSCEND_NVME - +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) #include "cmd.h" From 3f47f5c8de579d18056ca344e640a635c2f1eb17 Mon Sep 17 00:00:00 2001 From: JerryWang Date: Tue, 22 Oct 2024 16:44:11 +0800 Subject: [PATCH 6/6] Fixed for coding sytle --- plugins/transcend/transcend-nvme.c | 33 ++++++++++-------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/plugins/transcend/transcend-nvme.c b/plugins/transcend/transcend-nvme.c index 3376c765a..25d1209aa 100644 --- a/plugins/transcend/transcend-nvme.c +++ b/plugins/transcend/transcend-nvme.c @@ -88,8 +88,7 @@ static int getHealthValue(int argc, char **argv, struct command *cmd, struct plu if (percent_used > 100 || percent_used < 0) { printf("0%%\n"); - } - else { + } else { healthvalue = 100 - percent_used; printf("%d%%\n", healthvalue); } @@ -200,8 +199,7 @@ static int getPLPHealth(int argc, char **argv, struct command *cmd, struct plugi if (result) { printf("\nThis device is not support.\n"); return -1; - } - else { + } else { char modelName[40]; const char *model_str_byte; model_str_byte = format_char_array((char *)modelName, sizeof(modelName), dataID, sizeof(dataID)); @@ -215,8 +213,7 @@ static int getPLPHealth(int argc, char **argv, struct command *cmd, struct plugi if (txtPLPHealth >= 0) { plpHealth_percentage = txtPLPHealth; printf("Capacitor health for PLP: %d%%\n", plpHealth_percentage); - } - else { + } else { unsigned char data[512]; struct nvme_passthru_cmd nvmecmd; @@ -230,8 +227,7 @@ static int getPLPHealth(int argc, char **argv, struct command *cmd, struct plugi if (result) { printf("\nGet PLP Health Fail.\n"); return PLP_ERROR_NO_MATCH; - } - else { + } else { int tDis = (int)(data[4 + 3] << 24) + (int)(data[4 + 2] << 16) + (int)(data[4 + 1] << 8) + (int)(data[4]); int vDis1 = (int)(data[0]); int vDis2 = (int)(data[1]); @@ -240,19 +236,16 @@ static int getPLPHealth(int argc, char **argv, struct command *cmd, struct plugi if (vDis1 - vDis2 != 0) { normalHealth = (iDis * tDis) / (vDis1 - vDis2); - } - else { + } else { normalHealth = 0; } if (normalHealth >= 0) { if (fullValue - normalHealth >= 0) { plpHealth_percentage = (normalHealth / fullValue) * 100; - } - else { + } else { plpHealth_percentage = 100; } - } - else { + } else { plpHealth_percentage = 0; } printf("Capacitor health for PLP: %d%%\n", plpHealth_percentage); @@ -314,8 +307,7 @@ int readUsefulPLPValue(const char *device) token = strtok(NULL, "#"); ret = atoi(token); return ret; - } - else { + } else { return PLP_ERROR_DATA_EXPIRED; } } @@ -355,8 +347,7 @@ void recordPLPValue(const char *device, int value, bool isReplace) { if (strncmp(str, device, strlen(device)) == 0) { fprintf(fileout, "%s\n", line); - } - else { + } else { fprintf(fileout, "%s", str); } } @@ -365,12 +356,10 @@ void recordPLPValue(const char *device, int value, bool isReplace) if (remove(logFilePath) == 0) { rename(tempFilePath, logFilePath); - } - else { + } else { remove(tempFilePath); } - } - else { + } else { FILE *out = fopen(logFilePath, "a"); if (out == NULL) { perror("Error opening file");