Merge branch 'stable-2.15' into stable-2.16
* stable-2.15: Rewrite OS bean provider to avoid using reflection Change-Id: I8e6ad355a06bef294f7bd7589005e196d518f66b
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (C) 2017 The Android Open Source Project
|
||||
// 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.
|
||||
@@ -14,50 +14,24 @@
|
||||
|
||||
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();
|
||||
|
||||
class OperatingSystemMXBeanReflectionBased implements OperatingSystemMXBeanInterface {
|
||||
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)
|
||||
OperatingSystemMXBeanReflectionBased(OperatingSystemMXBean sys)
|
||||
throws ReflectiveOperationException {
|
||||
this.sys = sys;
|
||||
getProcessCpuTime = sys.getClass().getMethod("getProcessCpuTime", new Class<?>[] {});
|
||||
getProcessCpuTime = sys.getClass().getMethod("getProcessCpuTime");
|
||||
getProcessCpuTime.setAccessible(true);
|
||||
getOpenFileDescriptorCount =
|
||||
sys.getClass().getMethod("getOpenFileDescriptorCount", new Class<?>[] {});
|
||||
getOpenFileDescriptorCount = sys.getClass().getMethod("getOpenFileDescriptorCount");
|
||||
getOpenFileDescriptorCount.setAccessible(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getProcessCpuTime() {
|
||||
try {
|
||||
return (long) getProcessCpuTime.invoke(sys, new Object[] {});
|
||||
@@ -66,6 +40,7 @@ class OperatingSystemMXBeanProvider {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getOpenFileDescriptorCount() {
|
||||
try {
|
||||
return (long) getOpenFileDescriptorCount.invoke(sys, new Object[] {});
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -64,7 +64,7 @@ public class ProcMetricModule extends MetricModule {
|
||||
}
|
||||
|
||||
private void procCpuUsage(MetricMaker metrics) {
|
||||
final OperatingSystemMXBeanProvider provider = OperatingSystemMXBeanProvider.Factory.create();
|
||||
OperatingSystemMXBeanInterface provider = OperatingSystemMXBeanFactory.create();
|
||||
|
||||
if (provider == null) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user