]> granicus.if.org Git - sysstat/commitdiff
A_PWR_BAT: Add new option and basic definitions for sar and sadc
authorSebastien GODARD <sysstat@users.noreply.github.com>
Wed, 28 Dec 2022 09:25:15 +0000 (10:25 +0100)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Wed, 28 Dec 2022 09:25:15 +0000 (10:25 +0100)
Add new activity A_PWR_BAT.
It will enable the user to monitor battery capacity, variation and
status.

Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
activity.c
common.c
common.h
sa.h
sa_common.c
sar.c

index b531b5cc33c171c6a23492e1e5c28b84c82e86f8..058cd1792c8df204c2577a0058368e482d31c36b 100644 (file)
@@ -2004,7 +2004,53 @@ struct activity psi_mem_act = {
        .buf            = {NULL, NULL, NULL},
        .bitmap         = NULL
 };
-/* wrap_detect_psi */
+
+/* Battery capacity */
+struct activity pwr_bat_act = {
+       .id             = A_PWR_BAT,
+       .options        = AO_COUNTED + AO_GRAPH_PER_ITEM,
+       .magic          = ACTIVITY_MAGIC_BASE,
+       .group          = G_POWER,
+#ifdef SOURCE_SADC
+       .f_count_index  = 13,   /* wrap_get_bat_nr() */
+       .f_count2_index = -1,
+       .f_read         = wrap_read_bat,
+#endif
+#ifdef SOURCE_SAR
+       .f_print        = print_pwr_bat_stats,
+       .f_print_avg    = print_avg_pwr_bat_stats,
+#endif
+#if defined(SOURCE_SAR) || defined(SOURCE_SADF)
+       .hdr_line       = "BAT;%cap;%cap/min;sts",
+#endif
+       .gtypes_nr      = {STATS_PWR_BAT_ULL, STATS_PWR_BAT_UL, STATS_PWR_BAT_U},
+       .ftypes_nr      = {0, 0, 0},
+#ifdef SOURCE_SADF
+       .f_render       = NULL, // FIXME
+       .f_xml_print    = NULL, // FIXME
+       .f_json_print   = NULL, // FIXME
+       .f_svg_print    = NULL, // FIXME
+       .f_raw_print    = NULL, // FIXME
+       .f_pcp_print    = NULL, // FIXME
+       .f_count_new    = NULL,
+       .item_list      = NULL,
+       .desc           = "Batteries capacity",
+#endif
+       .name           = "A_PWR_BAT",
+       .item_list_sz   = 0,
+       .g_nr           = 1,
+       .nr_ini         = -1,
+       .nr2            = 1,
+       .nr_max         = MAX_NR_BATS,
+       .nr             = {-1, -1, -1},
+       .nr_allocated   = 0,
+       .fsize          = STATS_PWR_BAT_SIZE,
+       .msize          = STATS_PWR_BAT_SIZE,
+       .opt_flags      = 0,
+       .buf            = {NULL, NULL, NULL},
+       .bitmap         = NULL
+};
+
 #ifdef SOURCE_SADC
 /*
  * Array of functions used to count number of items.
@@ -2022,7 +2068,8 @@ __nr_t (*f_count[NR_F_COUNT]) (struct activity *) = {
        wrap_get_filesystem_nr, /* 9 */
        wrap_get_fchost_nr,     /* 10 */
        wrap_detect_psi,        /* 11 */
-       wrap_get_freq_nr        /* 12 */
+       wrap_get_freq_nr,       /* 12 */
+       wrap_get_bat_nr         /* 13 */
 };
 #endif
 
@@ -2071,6 +2118,7 @@ struct activity *act[NR_ACT] = {
        &pwr_temp_act,
        &pwr_in_act,
        &pwr_wghfreq_act,
+       &pwr_bat_act,
        &pwr_usb_act,   /* AO_CLOSE_MARKUP */
        /* </power-management> */
        &filesystem_act,
index c94cabff58bfe0ae659d5a1f1c808ea43f6395dd..32febe96f8d4f7d74380338f5b69932ce3bd7753 100644 (file)
--- a/common.c
+++ b/common.c
@@ -65,6 +65,8 @@ char sc_int_stat[MAX_SGR_LEN] = C_BOLD_BLUE;
 char sc_item_name[MAX_SGR_LEN] = C_LIGHT_GREEN;
 char sc_sa_restart[MAX_SGR_LEN] = C_LIGHT_RED;
 char sc_sa_comment[MAX_SGR_LEN] = C_LIGHT_YELLOW;
+char sc_trend_pos[MAX_SGR_LEN] = C_BOLD_GREEN;
+char sc_trend_neg[MAX_SGR_LEN] = C_BOLD_RED;
 char sc_normal[MAX_SGR_LEN] = C_NORMAL;
 
 /*
@@ -1214,6 +1216,8 @@ void init_colors(void)
                strcpy(sc_item_name, "");
                strcpy(sc_sa_comment, "");
                strcpy(sc_sa_restart, "");
+               strcpy(sc_trend_pos, "");
+               strcpy(sc_trend_neg, "");
                strcpy(sc_normal, "");
 
                return;
@@ -1256,6 +1260,12 @@ void init_colors(void)
                        case 'R':
                                snprintf(sc_sa_restart, MAX_SGR_LEN, "\e[%sm", p + 2);
                                break;
+                       case '+':
+                               snprintf(sc_trend_pos, MAX_SGR_LEN, "\e[%sm", p + 2);
+                               break;
+                       case '-':
+                               snprintf(sc_trend_neg, MAX_SGR_LEN, "\e[%sm", p + 2);
+                               break;
                }
        }
 }
@@ -1557,6 +1567,30 @@ void cprintf_s(int type, char *format, char *string)
        printf("%s", sc_normal);
 }
 
+/*
+ * **************************************************************************
+ * Print trend string using selected color.
+ *
+ * IN:
+ * @trend      Trend (TRUE: positive; FALSE: negative).
+ * @format     Output format.
+ * @tstring    String to display.
+ ***************************************************************************
+ */
+void cprintf_tr(int trend, char *format, char *tstring)
+{
+       if (trend) {
+               printf("%s", sc_trend_pos);
+       }
+       else {
+               printf("%s", sc_trend_neg);
+       }
+
+       printf(format, tstring);
+
+       printf("%s", sc_normal);
+}
+
 /*
  ***************************************************************************
  * Parse a string containing a numerical value (e.g. CPU or IRQ number).
index 2c37fcec56677285868a4987183be3be02dff651..bd5deb4aa2fadf8010022bdb1934def7f7204f60 100644 (file)
--- a/common.h
+++ b/common.h
@@ -120,6 +120,15 @@ enum {
 #define DIGITS                 "0123456789"
 #define XDIGITS                        "0123456789-"
 
+/* Batteries status */
+enum {
+       BAT_STS_UNKNOWN         = 0,
+       BAT_STS_CHARGING        = 1,
+       BAT_STS_DISCHARGING     = 2,
+       BAT_STS_NOTCHARGING     = 3,
+       BAT_STS_FULL            = 4
+};
+
 /*
  ***************************************************************************
  * Macro functions definitions.
@@ -203,6 +212,7 @@ extern char persistent_name_type[MAX_FILE_LEN];
 #define C_LIGHT_RED    "\e[31;22m"
 #define C_BOLD_RED     "\e[31;1m"
 #define C_LIGHT_GREEN  "\e[32;22m"
+#define C_BOLD_GREEN   "\e[32;1m"
 #define C_LIGHT_YELLOW "\e[33;22m"
 #define C_BOLD_MAGENTA "\e[35;1m"
 #define C_BOLD_BLUE    "\e[34;1m"
@@ -291,6 +301,8 @@ void cprintf_u64
        (int, int, int, ...);
 void cprintf_x
        (int, int, ...);
+void cprintf_tr
+       (int, char *, char *);
 char *device_name
        (char *);
 char *get_device_name
diff --git a/sa.h b/sa.h
index e59a7281b9b1aa041bee28be50a2438f0b8f156e..50b6315bde06794303817e2c437938e61da9134f 100644 (file)
--- a/sa.h
+++ b/sa.h
  */
 
 /* Number of activities */
-#define NR_ACT         42
+#define NR_ACT         43
 /* The value below is used for sanity check */
 #define MAX_NR_ACT     256
 
 /* Number of functions used to count items */
-#define NR_F_COUNT     13
+#define NR_F_COUNT     14
 
 /* Activities */
 enum {
@@ -70,7 +70,8 @@ enum {
        A_NET_SOFT      = 39,
        A_PSI_CPU       = 40,
        A_PSI_IO        = 41,
-       A_PSI_MEM       = 42
+       A_PSI_MEM       = 42,
+       A_PWR_BAT       = 43
 };
 
 /* Macro used to flag an activity that should be collected */
@@ -233,6 +234,7 @@ enum {
 #define K_IPV6         "IPV6"
 #define K_POWER                "POWER"
 #define K_USB          "USB"
+#define K_BAT          "BAT"
 
 #define K_SKIP_EMPTY   "skipempty"
 #define K_AUTOSCALE    "autoscale"
@@ -286,6 +288,7 @@ enum {
 #define MAX_NR_USB             65536
 #define MAX_NR_FS              (65536 * 4096)
 #define MAX_NR_FCHOSTS         65536
+#define MAX_NR_BATS            4096
 
 /* NR_MAX is the upper limit used for unknown activities */
 #define NR_MAX         (65536 * 4096)
@@ -1413,6 +1416,8 @@ __nr_t wrap_get_fchost_nr
        (struct activity *);
 __nr_t wrap_detect_psi
        (struct activity *);
+__nr_t wrap_get_bat_nr
+       (struct activity *);
 
 /* Functions used to read activities statistics */
 __read_funct_t wrap_read_stat_cpu
@@ -1499,6 +1504,8 @@ __read_funct_t wrap_read_psiio
        (struct activity *);
 __read_funct_t wrap_read_psimem
        (struct activity *);
+__read_funct_t wrap_read_bat
+       (struct activity *);
 
 /* Other functions */
 int check_alt_sa_dir
index 5b4aeac2ebf1b1b3ed00c7e30b90979f5a1e007c..35251003cbab85a14c795f2407c8426d0e844894 100644 (file)
@@ -2476,6 +2476,9 @@ int parse_sar_m_opt(char *argv[], int *opt, struct activity *act[])
                else if (!strcmp(t, K_USB)) {
                        SELECT_ACTIVITY(A_PWR_USB);
                }
+               else if (!strcmp(t, K_BAT)) {
+                       SELECT_ACTIVITY(A_PWR_BAT);
+               }
                else if (!strcmp(t, K_ALL)) {
                        SELECT_ACTIVITY(A_PWR_CPU);
                        SELECT_ACTIVITY(A_PWR_FAN);
@@ -2483,6 +2486,7 @@ int parse_sar_m_opt(char *argv[], int *opt, struct activity *act[])
                        SELECT_ACTIVITY(A_PWR_TEMP);
                        SELECT_ACTIVITY(A_PWR_FREQ);
                        SELECT_ACTIVITY(A_PWR_USB);
+                       SELECT_ACTIVITY(A_PWR_BAT);
                }
                else
                        return 1;
diff --git a/sar.c b/sar.c
index c66c3b1f778b20adf61502f1f742ef362788f610..4b25930aa2d48fa0e62ecec14d3bf63279102f23 100644 (file)
--- a/sar.c
+++ b/sar.c
@@ -158,6 +158,7 @@ void display_help(char *progname)
        printf(_("\t-m { <keyword> [,...] | ALL }\n"
                 "\t\tPower management statistics [A_PWR_...]\n"
                 "\t\tKeywords are:\n"
+                "\t\tBatteries capacity\n"
                 "\t\tCPU\tCPU instantaneous clock frequency\n"
                 "\t\tFAN\tFans speed\n"
                 "\t\tFREQ\tCPU average clock frequency\n"