]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
cpu plugin: Simplify the configuration options available.
authorFlorian Forster <octo@collectd.org>
Thu, 4 Jan 2024 16:58:52 +0000 (17:58 +0100)
committerFlorian Forster <octo@collectd.org>
Mon, 22 Jan 2024 15:07:57 +0000 (16:07 +0100)
 *  The options `ReportUsage`, `ReportUtilization`, and `ReportNumCpu` control
    which metrics are emitted on a high level. Other options no longer influence
    *what* is being collected. This also allows to report usage and utilization
    metrics simultaneously.
 *  The documentation has been updated to reflect that the plugin no longer
    emits a percentage, but a ratio for utilization metrics.

src/collectd.conf.in
src/collectd.conf.pod
src/cpu.c

index 791236d04143252574636c701bda5e356e60ad9d..18dd82ef6101128112905392ed1a4bba985d76b2 100644 (file)
 #</Plugin>
 
 #<Plugin cpu>
+#  ReportUsage true
+#  ReportUtilization true
 #  ReportByCpu true
 #  ReportByState true
-#  ValuesPercentage false
 #  ReportNumCpu false
 #  ReportGuestState false
 #  SubtractGuestState true
index 2cf343c5185d41f0bdf85970f331d287c69121c3..d0956097b1f730ed09a9976273a3b53afa704ef8 100644 (file)
@@ -1760,8 +1760,10 @@ F</proc/sys/net/ipv4/netfilter> instead of F</proc/sys/net/netfilter/>.
 
 =head2 Plugin C<cpu>
 
-The I<CPU plugin> collects CPU usage metrics. By default, CPU usage is reported
-as Jiffies, using the C<cpu> type. Two aggregations are available:
+The I<CPU plugin> collects CPU usage and utilization metrics.
+By default, both CPU usage and utilization are reported.
+
+For CPU utilization, two aggregations are available:
 
 =over 4
 
@@ -1776,34 +1778,38 @@ Sum, per-CPU, over all non-idle states of a CPU, creating an "active" state.
 =back
 
 The two aggregations can be combined, leading to I<collectd> only emitting a
-single "active" metric for the entire system. As soon as one of these
-aggregations (or both) is enabled, the I<cpu plugin> will report a percentage,
-rather than Jiffies. In addition, you can request individual, per-state,
-per-CPU metrics to be reported as percentage.
+single "active" metric for the entire system.
 
 The following configuration options are available:
 
 =over 4
 
+=item B<ReportUsage> B<true>|B<false>
+
+Sets whether CPU usage (in jiffies/seconds) is reported. Defaults to B<true>.
+
+=item B<ReportUtilization> B<true>|B<false>
+
+Sets whether CPU utilization (a ratio) is reported. Defaults to B<true>.
+
+When enabled, the B<ReportByState> and B<ReportByCpu> options can be used to
+report aggregated data.
+
 =item B<ReportByState> B<true>|B<false>
 
 When set to B<true>, the default, reports per-state metrics, e.g. "system",
-"user" and "idle".
-When set to B<false>, aggregates (sums) all I<non-idle> states into one
-"active" metric.
+"user" and "idle". When set to B<false>, aggregates (sums) all I<non-idle>
+states into one "active" metric.
 
-=item B<ReportByCpu> B<true>|B<false>
+This option only has an effect if B<ReportUtilization> is true.
 
-When set to B<true>, the default, reports per-CPU (per-core) metrics.
-When set to B<false>, instead of reporting metrics for individual CPUs, only a
-global sum of CPU states is emitted.
+=item B<ReportByCpu> B<true>|B<false>
 
-=item B<ValuesPercentage> B<false>|B<true>
+When set to B<true>, the default, reports per-CPU (per-core) metrics. When set
+to B<false>, instead of reporting metrics for individual CPUs, only a global
+sum of CPU states is emitted.
 
-This option is only considered when both, B<ReportByCpu> and B<ReportByState>
-are set to B<true>. In this case, by default, metrics will be reported as
-Jiffies. By setting this option to B<true>, you can request percentage values
-in the un-aggregated (per-CPU, per-state) mode as well.
+This option only has an effect if B<ReportUtilization> is true.
 
 =item B<ReportNumCpu> B<false>|B<true>
 
index e8595774762291fca7d92675a8f4f87da549ab45..c1b47836e2bafb222dd96fc3883dfc361c5362d0 100644 (file)
--- a/src/cpu.c
+++ b/src/cpu.c
@@ -203,22 +203,28 @@ static size_t global_cpu_num;
 
 static bool report_by_cpu = true;
 static bool report_by_state = true;
-static bool report_percent;
+static bool report_usage = true;
+static bool report_utilization = true;
 static bool report_num_cpu;
 static bool report_guest;
 static bool subtract_guest = true;
 
-static const char *config_keys[] = {"ReportByCpu",      "ReportByState",
-                                    "ReportNumCpu",     "ValuesPercentage",
-                                    "ReportGuestState", "SubtractGuestState"};
+static const char *config_keys[] = {
+    "ReportByCpu",        "ReportByState",    "ReportNumCpu",
+    "ReportUtilization",  "ValuesPercentage", "ReportGuestState",
+    "SubtractGuestState",
+};
 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
 
 static int cpu_config(char const *key, char const *value) /* {{{ */
 {
   if (strcasecmp(key, "ReportByCpu") == 0)
     report_by_cpu = IS_TRUE(value);
-  else if (strcasecmp(key, "ValuesPercentage") == 0)
-    report_percent = IS_TRUE(value);
+  else if (strcasecmp(key, "ReportUsage") == 0)
+    report_usage = IS_TRUE(value);
+  else if (strcasecmp(key, "ReportUtilization") == 0 ||
+           strcasecmp(key, "ValuesPercentage") == 0)
+    report_utilization = IS_TRUE(value);
   else if (strcasecmp(key, "ReportByState") == 0)
     report_by_state = IS_TRUE(value);
   else if (strcasecmp(key, "ReportNumCpu") == 0)
@@ -518,7 +524,7 @@ static void cpu_reset(void) /* {{{ */
 } /* }}} void cpu_reset */
 
 /* Legacy behavior: Dispatches the raw derive values without any aggregation. */
-static void cpu_commit_without_aggregation(void) /* {{{ */
+static void cpu_commit_usage(void) /* {{{ */
 {
   metric_family_t fam = {
       .name = "system.cpu.time",
@@ -552,23 +558,12 @@ static void cpu_commit_without_aggregation(void) /* {{{ */
 
   metric_reset(&m);
   metric_family_metric_reset(&fam);
-} /* }}} void cpu_commit_without_aggregation */
+} /* }}} void cpu_commit_usage */
 
-/* Aggregates the internal state and dispatches the metrics. */
-static void cpu_commit(void) /* {{{ */
-{
+static void cpu_commit_utilization(void) {
   gauge_t global_rates[STATE_MAX] = {
       NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN /* Batman! */
   };
-
-  if (report_num_cpu)
-    cpu_commit_num_cpu((gauge_t)global_cpu_num);
-
-  if (report_by_state && report_by_cpu && !report_percent) {
-    cpu_commit_without_aggregation();
-    return;
-  }
-
   aggregate(global_rates);
 
   gauge_t global_rate_sum = global_rates[STATE_ACTIVE];
@@ -590,6 +585,22 @@ static void cpu_commit(void) /* {{{ */
 
     cpu_commit_one((int)cpu_num, local_rates, global_rate_sum);
   }
+}
+
+/* Aggregates the internal state and dispatches the metrics. */
+static void cpu_commit(void) /* {{{ */
+{
+  if (report_num_cpu) {
+    cpu_commit_num_cpu((gauge_t)global_cpu_num);
+  }
+
+  if (report_usage) {
+    cpu_commit_usage();
+  }
+
+  if (report_utilization) {
+    cpu_commit_utilization();
+  }
 } /* }}} void cpu_commit */
 
 /* Adds a derive value to the internal state. This should be used by each read