From 9fc47ca3f332f7ecf35bcd9a24e2418de83b041e Mon Sep 17 00:00:00 2001
From: Matthias Sohn <matthias.sohn@sap.com>
Date: Fri, 6 Mar 2020 13:15:10 +0100
Subject: [PATCH 1/4] Add more metrics about Java threads

Add
- current live daemon threads count
- peak live thread count
- total number of threads started since JVM started

Change-Id: I1a47821bcf96971ef8c76bccc89a30c6f18c59e4
---
 Documentation/metrics.txt                     |  3 +++
 .../gerrit/metrics/proc/ProcMetricModule.java | 21 +++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/Documentation/metrics.txt b/Documentation/metrics.txt
index e6654c3de4..f8e062ce09 100644
--- a/Documentation/metrics.txt
+++ b/Documentation/metrics.txt
@@ -45,6 +45,9 @@ objects needing finalization.
 * `proc/jvm/gc/count`: Number of GCs.
 * `proc/jvm/gc/time`: Approximate accumulated GC elapsed time.
 * `proc/jvm/thread/num_live`: Current live thread count.
+* `proc/jvm/thread/num_daemon_live`: Current live daemon threads count.
+* `proc/jvm/thread/num_peak_live`: Peak live thread count since the Java virtual machine started or peak was reset.
+* `proc/jvm/thread/num_total_started`: Total number of threads created and also started since the Java virtual machine started.
 
 === Caches
 
diff --git a/java/com/google/gerrit/metrics/proc/ProcMetricModule.java b/java/com/google/gerrit/metrics/proc/ProcMetricModule.java
index 5af2672a95..664c0dff8b 100644
--- a/java/com/google/gerrit/metrics/proc/ProcMetricModule.java
+++ b/java/com/google/gerrit/metrics/proc/ProcMetricModule.java
@@ -195,5 +195,26 @@ public class ProcMetricModule extends MetricModule {
         Integer.class,
         new Description("Current live thread count").setGauge().setUnit("threads"),
         thread::getThreadCount);
+    metrics.newCallbackMetric(
+        "proc/jvm/thread/num_daemon_live",
+        Integer.class,
+        new Description("Current live daemon threads count").setGauge().setUnit("threads"),
+        thread::getDaemonThreadCount);
+    metrics.newCallbackMetric(
+        "proc/jvm/thread/num_peak_live",
+        Integer.class,
+        new Description(
+                "Peak live thread count since the Java virtual machine started or peak was reset")
+            .setGauge()
+            .setUnit("threads"),
+        thread::getPeakThreadCount);
+    metrics.newCallbackMetric(
+        "proc/jvm/thread/num_total_started",
+        Long.class,
+        new Description(
+                "Total number of threads created and also started since the Java virtual machine started")
+            .setGauge()
+            .setUnit("threads"),
+        thread::getTotalStartedThreadCount);
   }
 }

From e2b3f8e0886ab137d996680cd672e449a2e7db5b Mon Sep 17 00:00:00 2001
From: Matthias Sohn <matthias.sohn@sap.com>
Date: Fri, 6 Mar 2020 13:22:26 +0100
Subject: [PATCH 2/4] Proc metrics: add available number of cores

Change-Id: Iae424c757369132b9e8a79e32f4c1224ecc85a27
---
 Documentation/metrics.txt                                 | 1 +
 java/com/google/gerrit/metrics/proc/ProcMetricModule.java | 5 +++++
 2 files changed, 6 insertions(+)

diff --git a/Documentation/metrics.txt b/Documentation/metrics.txt
index f8e062ce09..38b516c104 100644
--- a/Documentation/metrics.txt
+++ b/Documentation/metrics.txt
@@ -33,6 +33,7 @@ split up by update type (create+replace, and autoclose)
 
 * `proc/birth_timestamp`: Time at which the Gerrit process started.
 * `proc/uptime`: Uptime of the Gerrit process.
+* `proc/cpu/num_cores`: Number of processors available to the Java virtual machine.
 * `proc/cpu/usage`: CPU time used by the Gerrit process.
 * `proc/num_open_fds`: Number of open file descriptors.
 * `proc/jvm/memory/heap_committed`: Amount of memory guaranteed for user objects.
diff --git a/java/com/google/gerrit/metrics/proc/ProcMetricModule.java b/java/com/google/gerrit/metrics/proc/ProcMetricModule.java
index 664c0dff8b..ccbf16fe28 100644
--- a/java/com/google/gerrit/metrics/proc/ProcMetricModule.java
+++ b/java/com/google/gerrit/metrics/proc/ProcMetricModule.java
@@ -90,6 +90,11 @@ public class ProcMetricModule extends MetricModule {
           new Description("Number of open file descriptors").setGauge().setUnit("fds"),
           provider::getOpenFileDescriptorCount);
     }
+    metrics.newCallbackMetric(
+        "proc/cpu/num_cores",
+        Integer.class,
+        new Description("Number of processors available to the Java virtual machine").setGauge(),
+        Runtime.getRuntime()::availableProcessors);
   }
 
   private void procJvmMemory(MetricMaker metrics) {

From fa8de0231d42fece960aff39f38c4cabbc2e1eb4 Mon Sep 17 00:00:00 2001
From: Matthias Sohn <matthias.sohn@sap.com>
Date: Fri, 6 Mar 2020 13:39:44 +0100
Subject: [PATCH 3/4] Proc metrics: add average system load for the last minute

Change-Id: I12d74ea28f7f8f52c16d53236ecca6582df351b7
---
 Documentation/metrics.txt                          |  1 +
 .../gerrit/metrics/proc/ProcMetricModule.java      | 14 ++++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/Documentation/metrics.txt b/Documentation/metrics.txt
index 38b516c104..f729f549da 100644
--- a/Documentation/metrics.txt
+++ b/Documentation/metrics.txt
@@ -35,6 +35,7 @@ split up by update type (create+replace, and autoclose)
 * `proc/uptime`: Uptime of the Gerrit process.
 * `proc/cpu/num_cores`: Number of processors available to the Java virtual machine.
 * `proc/cpu/usage`: CPU time used by the Gerrit process.
+* `proc/cpu/system_load`: System load average for the last minute.
 * `proc/num_open_fds`: Number of open file descriptors.
 * `proc/jvm/memory/heap_committed`: Amount of memory guaranteed for user objects.
 * `proc/jvm/memory/heap_used`: Amount of memory holding user objects.
diff --git a/java/com/google/gerrit/metrics/proc/ProcMetricModule.java b/java/com/google/gerrit/metrics/proc/ProcMetricModule.java
index ccbf16fe28..be70b8c4e8 100644
--- a/java/com/google/gerrit/metrics/proc/ProcMetricModule.java
+++ b/java/com/google/gerrit/metrics/proc/ProcMetricModule.java
@@ -29,6 +29,7 @@ import java.lang.management.GarbageCollectorMXBean;
 import java.lang.management.ManagementFactory;
 import java.lang.management.MemoryMXBean;
 import java.lang.management.MemoryUsage;
+import java.lang.management.OperatingSystemMXBean;
 import java.lang.management.ThreadMXBean;
 import java.util.concurrent.TimeUnit;
 
@@ -38,6 +39,7 @@ public class ProcMetricModule extends MetricModule {
     buildLabel(metrics);
     procUptime(metrics);
     procCpuUsage(metrics);
+    procCpuLoad(metrics);
     procJvmGc(metrics);
     procJvmMemory(metrics);
     procJvmThread(metrics);
@@ -97,6 +99,18 @@ public class ProcMetricModule extends MetricModule {
         Runtime.getRuntime()::availableProcessors);
   }
 
+  private void procCpuLoad(MetricMaker metrics) {
+    OperatingSystemMXBean provider =
+        ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
+    if (provider.getSystemLoadAverage() != -1) {
+      metrics.newCallbackMetric(
+          "proc/cpu/system_load",
+          Double.class,
+          new Description("System load average for the last minute").setGauge(),
+          provider::getSystemLoadAverage);
+    }
+  }
+
   private void procJvmMemory(MetricMaker metrics) {
     CallbackMetric0<Long> heapCommitted =
         metrics.newCallbackMetric(

From fa10e3353c343318aa427dc01b48a81bdd69f957 Mon Sep 17 00:00:00 2001
From: David Pursehouse <dpursehouse@collab.net>
Date: Sun, 8 Mar 2020 13:33:13 +0900
Subject: [PATCH 4/4] Update recommended version of buildifier to 2.0.0

 $ buildifier --version
 buildifier version: 2.0.0
 buildifier scm revision: 2797858400171ffaa3074c22925b05ed54b634f1

With this version there are no formatting changes from the previously
used version 1.0.0.

Change-Id: Idc13d4311ef9273804f39813f8e75b209f7c5503
---
 Documentation/dev-contributing.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Documentation/dev-contributing.txt b/Documentation/dev-contributing.txt
index 1454b81458..ab7f2af591 100644
--- a/Documentation/dev-contributing.txt
+++ b/Documentation/dev-contributing.txt
@@ -171,7 +171,7 @@ To format Java source code, Gerrit uses the
 link:https://github.com/google/google-java-format[`google-java-format`]
 tool (version 1.7), and to format Bazel BUILD, WORKSPACE and .bzl files the
 link:https://github.com/bazelbuild/buildtools/tree/master/buildifier[`buildifier`]
-tool (version 1.0.0). Unused dependencies are found and removed using the
+tool (version 2.0.0). Unused dependencies are found and removed using the
 link:https://github.com/bazelbuild/buildtools/tree/master/unused_deps[`unused_deps`]
 build tool, a sibling of `buildifier`.