]> granicus.if.org Git - sysstat/commitdiff
sadf: A_PWR_BAT: Display batteries stats in XML format
authorSebastien GODARD <sysstat@users.noreply.github.com>
Wed, 28 Dec 2022 14:18:51 +0000 (15:18 +0100)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Wed, 28 Dec 2022 14:18:51 +0000 (15:18 +0100)
Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
activity.c
xml_stats.c
xml_stats.h

index 9b8f679d3de829c9a6984648b0f2aac3f166ba62..df5758645b1868753544a736546ade79e0e5914e 100644 (file)
@@ -2027,7 +2027,7 @@ struct activity pwr_bat_act = {
        .ftypes_nr      = {0, 0, 0},
 #ifdef SOURCE_SADF
        .f_render       = render_pwr_bat_stats,
-       .f_xml_print    = NULL, // FIXME
+       .f_xml_print    = xml_print_pwr_bat_stats,
        .f_json_print   = NULL, // FIXME
        .f_svg_print    = NULL, // FIXME
        .f_raw_print    = NULL, // FIXME
index d48524fab17683690cf6fbec91b36ad1be7d970d..635c78eb07a9c9a4c772cc6fa1f9b64e0b3cc68e 100644 (file)
@@ -36,6 +36,7 @@
 #endif
 
 extern uint64_t flags;
+extern char bat_status[][16];
 
 /*
  ***************************************************************************
@@ -2472,3 +2473,56 @@ close_xml_markup:
                xml_markup_psi(tab, CLOSE_XML_MARKUP);
        }
 }
+
+/*
+ * **************************************************************************
+ * Display battery statistics in XML.
+ *
+ * IN:
+ * @a          Activity structure with statistics.
+ * @curr       Index in array for current sample statistics.
+ * @tab                Indentation in XML output.
+ * @itv                Interval of time in 1/100th of a second.
+ ***************************************************************************
+ */
+__print_funct_t xml_print_pwr_bat_stats(struct activity *a, int curr, int tab,
+                                       unsigned long long itv)
+{
+       int i;
+       struct stats_pwr_bat *spbc, *spbp;
+
+       if (!IS_SELECTED(a->options) || (a->nr[curr] <= 0))
+               goto close_xml_markup;
+
+       xml_markup_power_management(tab, OPEN_XML_MARKUP);
+       tab++;
+
+       xprintf(tab++, "<battery unit=\"minute\">");
+
+       for (i = 0; i < a->nr[curr]; i++) {
+
+               spbc = (struct stats_pwr_bat *) ((char *) a->buf[curr] + i * a->msize);
+               spbp = (struct stats_pwr_bat *) ((char *) a->buf[!curr] + i * a->msize);
+
+               /* Battery status code should not be greater than or equal to BAT_STS_NR */
+               if (spbc->status >= BAT_STS_NR) {
+                       spbc->status = 0;
+               }
+               xprintf(tab, "<bat number=\"%d\" "
+                            "percent-capacity=\"%u\" "
+                            "variation=\"%.2f\" "
+                            "status=\"%s\"/>",
+                       spbc->bat_id,
+                       (unsigned int) spbc->capacity,
+                       (double) (spbc->capacity - spbp->capacity) * 6000 / itv,
+                       bat_status[(unsigned int) spbc->status]);
+       }
+
+       xprintf(--tab, "</battery>");
+       tab--;
+
+close_xml_markup:
+       if (CLOSE_MARKUP(a->options)) {
+               xml_markup_power_management(tab, CLOSE_XML_MARKUP);
+       }
+}
index aa1cf69c10e310b0450453554ad3c824651b6c3a..6c7c2f360cd5127a4f25688dffa05199d72c9f41 100644 (file)
@@ -99,5 +99,7 @@ __print_funct_t xml_print_psiio_stats
        (struct activity *, int, int, unsigned long long);
 __print_funct_t xml_print_psimem_stats
        (struct activity *, int, int, unsigned long long);
+__print_funct_t xml_print_pwr_bat_stats
+       (struct activity *, int, int, unsigned long long);
 
 #endif /* _XML_STATS_H */