Merge branch 'stable-2.15' into stable-2.16
* stable-2.15:
Rewrite OS bean provider to avoid using reflection
Change-Id: I8e6ad355a06bef294f7bd7589005e196d518f66b
diff --git a/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanFactory.java b/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanFactory.java
new file mode 100644
index 0000000..9befe16a
--- /dev/null
+++ b/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanFactory.java
@@ -0,0 +1,49 @@
+// Copyright (C) 2017 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.metrics.proc;
+
+import com.google.common.flogger.FluentLogger;
+import com.sun.management.UnixOperatingSystemMXBean;
+import java.lang.management.ManagementFactory;
+import java.lang.management.OperatingSystemMXBean;
+import java.util.Arrays;
+
+@SuppressWarnings("restriction")
+class OperatingSystemMXBeanFactory {
+ private static final FluentLogger logger = FluentLogger.forEnclosingClass();
+
+ static OperatingSystemMXBeanInterface create() {
+ OperatingSystemMXBean sys = ManagementFactory.getOperatingSystemMXBean();
+ if (sys instanceof UnixOperatingSystemMXBean) {
+ return new OperatingSystemMXBeanUnixNative((UnixOperatingSystemMXBean) sys);
+ }
+
+ for (String name :
+ Arrays.asList(
+ "com.sun.management.UnixOperatingSystemMXBean",
+ "com.ibm.lang.management.UnixOperatingSystemMXBean")) {
+ try {
+ Class<?> impl = Class.forName(name);
+ if (impl.isInstance(sys)) {
+ return new OperatingSystemMXBeanReflectionBased(sys);
+ }
+ } catch (ReflectiveOperationException e) {
+ logger.atFine().withCause(e).log("No implementation for %s", name);
+ }
+ }
+ logger.atWarning().log("No implementation of UnixOperatingSystemMXBean found");
+ return null;
+ }
+}
diff --git a/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanInterface.java b/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanInterface.java
new file mode 100644
index 0000000..b7d6ebf
--- /dev/null
+++ b/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanInterface.java
@@ -0,0 +1,21 @@
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.metrics.proc;
+
+interface OperatingSystemMXBeanInterface {
+ long getProcessCpuTime();
+
+ long getOpenFileDescriptorCount();
+}
diff --git a/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanProvider.java b/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanProvider.java
deleted file mode 100644
index 10d589a..0000000
--- a/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanProvider.java
+++ /dev/null
@@ -1,76 +0,0 @@
-// Copyright (C) 2017 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.google.gerrit.metrics.proc;
-
-import com.google.common.flogger.FluentLogger;
-import java.lang.management.ManagementFactory;
-import java.lang.management.OperatingSystemMXBean;
-import java.lang.reflect.Method;
-import java.util.Arrays;
-
-class OperatingSystemMXBeanProvider {
- private static final FluentLogger logger = FluentLogger.forEnclosingClass();
-
- private final OperatingSystemMXBean sys;
- private final Method getProcessCpuTime;
- private final Method getOpenFileDescriptorCount;
-
- static class Factory {
- static OperatingSystemMXBeanProvider create() {
- OperatingSystemMXBean sys = ManagementFactory.getOperatingSystemMXBean();
- for (String name :
- Arrays.asList(
- "com.sun.management.UnixOperatingSystemMXBean",
- "com.ibm.lang.management.UnixOperatingSystemMXBean")) {
- try {
- Class<?> impl = Class.forName(name);
- if (impl.isInstance(sys)) {
- return new OperatingSystemMXBeanProvider(sys);
- }
- } catch (ReflectiveOperationException e) {
- logger.atFine().withCause(e).log("No implementation for %s", name);
- }
- }
- logger.atWarning().log("No implementation of UnixOperatingSystemMXBean found");
- return null;
- }
- }
-
- private OperatingSystemMXBeanProvider(OperatingSystemMXBean sys)
- throws ReflectiveOperationException {
- this.sys = sys;
- getProcessCpuTime = sys.getClass().getMethod("getProcessCpuTime", new Class<?>[] {});
- getProcessCpuTime.setAccessible(true);
- getOpenFileDescriptorCount =
- sys.getClass().getMethod("getOpenFileDescriptorCount", new Class<?>[] {});
- getOpenFileDescriptorCount.setAccessible(true);
- }
-
- public long getProcessCpuTime() {
- try {
- return (long) getProcessCpuTime.invoke(sys, new Object[] {});
- } catch (ReflectiveOperationException e) {
- return -1;
- }
- }
-
- public long getOpenFileDescriptorCount() {
- try {
- return (long) getOpenFileDescriptorCount.invoke(sys, new Object[] {});
- } catch (ReflectiveOperationException e) {
- return -1;
- }
- }
-}
diff --git a/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanReflectionBased.java b/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanReflectionBased.java
new file mode 100644
index 0000000..8dc54ab
--- /dev/null
+++ b/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanReflectionBased.java
@@ -0,0 +1,51 @@
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.metrics.proc;
+
+import java.lang.management.OperatingSystemMXBean;
+import java.lang.reflect.Method;
+
+class OperatingSystemMXBeanReflectionBased implements OperatingSystemMXBeanInterface {
+ private final OperatingSystemMXBean sys;
+ private final Method getProcessCpuTime;
+ private final Method getOpenFileDescriptorCount;
+
+ OperatingSystemMXBeanReflectionBased(OperatingSystemMXBean sys)
+ throws ReflectiveOperationException {
+ this.sys = sys;
+ getProcessCpuTime = sys.getClass().getMethod("getProcessCpuTime");
+ getProcessCpuTime.setAccessible(true);
+ getOpenFileDescriptorCount = sys.getClass().getMethod("getOpenFileDescriptorCount");
+ getOpenFileDescriptorCount.setAccessible(true);
+ }
+
+ @Override
+ public long getProcessCpuTime() {
+ try {
+ return (long) getProcessCpuTime.invoke(sys, new Object[] {});
+ } catch (ReflectiveOperationException e) {
+ return -1;
+ }
+ }
+
+ @Override
+ public long getOpenFileDescriptorCount() {
+ try {
+ return (long) getOpenFileDescriptorCount.invoke(sys, new Object[] {});
+ } catch (ReflectiveOperationException e) {
+ return -1;
+ }
+ }
+}
diff --git a/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanUnixNative.java b/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanUnixNative.java
new file mode 100644
index 0000000..a7f5bba
--- /dev/null
+++ b/java/com/google/gerrit/metrics/proc/OperatingSystemMXBeanUnixNative.java
@@ -0,0 +1,36 @@
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.metrics.proc;
+
+import com.sun.management.UnixOperatingSystemMXBean;
+
+@SuppressWarnings("restriction")
+class OperatingSystemMXBeanUnixNative implements OperatingSystemMXBeanInterface {
+ private final UnixOperatingSystemMXBean sys;
+
+ OperatingSystemMXBeanUnixNative(UnixOperatingSystemMXBean sys) {
+ this.sys = sys;
+ }
+
+ @Override
+ public long getProcessCpuTime() {
+ return sys.getProcessCpuTime();
+ }
+
+ @Override
+ public long getOpenFileDescriptorCount() {
+ return sys.getOpenFileDescriptorCount();
+ }
+}
diff --git a/java/com/google/gerrit/metrics/proc/ProcMetricModule.java b/java/com/google/gerrit/metrics/proc/ProcMetricModule.java
index 6b9176b..5af2672 100644
--- a/java/com/google/gerrit/metrics/proc/ProcMetricModule.java
+++ b/java/com/google/gerrit/metrics/proc/ProcMetricModule.java
@@ -64,7 +64,7 @@
}
private void procCpuUsage(MetricMaker metrics) {
- final OperatingSystemMXBeanProvider provider = OperatingSystemMXBeanProvider.Factory.create();
+ OperatingSystemMXBeanInterface provider = OperatingSystemMXBeanFactory.create();
if (provider == null) {
return;