Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize jvmti related #356

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.dromara.dynamictp.common.util.ReflectionUtil;
import org.dromara.dynamictp.jvmti.JVMTI;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -63,7 +62,7 @@ protected void initialize() {
super.initialize();

List<StarlightClient> starlightClients = Lists.newArrayList();
starlightClients.addAll(Arrays.asList(JVMTI.getInstances(StarlightClient.class)));
starlightClients.addAll(JVMTI.getInstances(StarlightClient.class));
if (CollectionUtils.isEmpty(starlightClients)) {
log.warn("Cannot find beans of type StarlightClient.");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.grpc.internal.ServerImpl;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.dromara.dynamictp.adapter.common.AbstractDtpAdapter;
import org.dromara.dynamictp.common.properties.DtpProperties;
import org.dromara.dynamictp.common.util.ReflectionUtil;
Expand Down Expand Up @@ -59,7 +59,7 @@ public void refresh(DtpProperties dtpProperties) {
@Override
protected void initialize() {
val beans = JVMTI.getInstances(ServerImpl.class);
if (ArrayUtils.isEmpty(beans)) {
if (CollectionUtils.isEmpty(beans)) {
log.warn("Cannot find beans of type ServerImpl.");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService;
Expand Down Expand Up @@ -70,7 +71,7 @@ protected void initialize() {
public void adaptConsumerExecutors() {

val beans = JVMTI.getInstances(DefaultMQPushConsumer.class);
if (ArrayUtils.isEmpty(beans)) {
if (CollectionUtils.isEmpty(beans)) {
log.warn("Cannot find beans of type DefaultMQPushConsumer.");
return;
}
Expand All @@ -97,7 +98,7 @@ public void adaptConsumerExecutors() {
public void adaptProducerExecutors() {

val beans = JVMTI.getInstances(DefaultMQProducer.class);
if (ArrayUtils.isEmpty(beans)) {
if (CollectionUtils.isEmpty(beans)) {
log.warn("Cannot find beans of type DefaultMQProducer.");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ public void run() {
double processCpuUsage = (double) elapsedCpu / elapsedTime / cpuCores;
prevProcessCpuTime = newProcessCpuTime;
prevUpTime = newUpTime;
currProcessCpuUsage = Math.min(processCpuUsage, 1);
currProcessCpuUsage = processCpuUsage;
} catch (Throwable e) {
log.warn("Get system metrics error.", e);
log.error("Get system metrics error.", e);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions example/example-consul-cloud/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

<dependency>
<groupId>org.dromara.dynamictp</groupId>
<artifactId>dynamic-tp-extension-skywalking</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
package org.dromara.dynamictp.extension.skywalking.init;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.dromara.dynamictp.core.support.init.DtpInitializer;
import org.dromara.dynamictp.jvmti.JVMTI;

import java.util.Objects;
import java.util.List;

import static org.dromara.dynamictp.common.constant.DynamicTpConst.DTP_EXECUTE_ENHANCED;
import static org.dromara.dynamictp.common.constant.DynamicTpConst.FALSE_STR;
Expand Down Expand Up @@ -49,8 +50,8 @@ public String getName() {
@Override
public void init() {
try {
ClassLoader[] classLoaders = JVMTI.getInstances(ClassLoader.class);
if (Objects.isNull(classLoaders)) {
List<ClassLoader> classLoaders = JVMTI.getInstances(ClassLoader.class);
if (CollectionUtils.isEmpty(classLoaders)) {
return;
}
for (ClassLoader cl : classLoaders) {
Expand All @@ -65,7 +66,7 @@ public void init() {
}
}
} catch (Throwable e) {
log.error("DynamicTp {} init error", getName(), e);
log.error("DynamicTp {} init failed", getName(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
package org.dromara.dynamictp.jvmti;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.scijava.nativelib.JniExtractor;
import org.scijava.nativelib.NativeLoader;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Use JVMTI technology to implement some things that Java code can't do.
Expand All @@ -36,18 +41,22 @@ public class JVMTI {

private static final String LIB_NAME = "JniLibrary";

private static final AtomicBoolean AVAILABLE = new AtomicBoolean(false);

static {
try {
final JniExtractor extractor = NativeLoader.getJniExtractor();
final String path = extractor.extractJni("", LIB_NAME).getAbsolutePath();
System.load(path);
AVAILABLE.set(true);
} catch (Throwable ignored) {
try {
File path = new File(JVMTI.class.getProtectionDomain().getCodeSource().getLocation().getPath());
String libPath = new File(path, JVMTIUtils.detectLibName()).getAbsolutePath();
System.load(libPath);
AVAILABLE.set(true);
} catch (Throwable t) {
log.error("JVMTI init failed !", t);
log.error("JVMTI initialization failed, currently only linux and macos are supported!", t);
}
}
}
Expand All @@ -61,14 +70,14 @@ public class JVMTI {
* @throws RuntimeException if find many instances
*/
public static <T> T getInstance(final Class<T> klass) {
final T[] instances = getInstances0(klass, 1);
if (null == instances || instances.length == 0) {
final List<T> instances = getInstances(klass, 1);
if (CollectionUtils.isEmpty(instances)) {
return null;
}
if (instances.length > 1) {
if (instances.size() > 1) {
throw new RuntimeException("expect only one instance, actually find many instances !");
}
return instances[0];
return instances.get(0);
}

/**
Expand All @@ -80,8 +89,8 @@ public static <T> T getInstance(final Class<T> klass) {
* @param <T> class type
* @return current surviving instances
*/
public static <T> T[] getInstances(final Class<T> klass) {
return getInstances0(klass, -1);
public static <T> List<T> getInstances(final Class<T> klass) {
return getInstances(klass, -1);
}

/**
Expand All @@ -95,8 +104,11 @@ public static <T> T[] getInstances(final Class<T> klass) {
* It is recommended to pass in a small {@code limit} value which is larger than 0.
* @return current surviving instances
*/
public static <T> T[] getInstances(final Class<T> klass, final int limit) {
return getInstances0(klass, limit);
public static <T> List<T> getInstances(final Class<T> klass, final int limit) {
if (!AVAILABLE.get()) {
return Collections.emptyList();
}
return Arrays.asList(getInstances0(klass, limit));
}

/**
Expand Down
Loading