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

提供JvmUtils #1698

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
改成构建时生成
  • Loading branch information
loongs-zhang committed Feb 23, 2021
commit aa31870eeacb4d55b5542c383e299dc70a52d123
30 changes: 29 additions & 1 deletion arthas-beans/README_CN.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
## 如果JvmUtils不支持您的系统或者在使用时遇到`java.lang.UnsatisfiedLinkError`
# linux
打64位linux包(默认):
```
mvn clean compile package -DskipTests=true -Plinux64
```
打32位linux包:
```
mvn clean compile package -DskipTests=true -Plinux32
```
#macos
打64位macos包
```
mvn clean compile package -DskipTests=true -Pmacos64
```
打32位macos包:
```
mvn clean compile package -DskipTests=true -Pmacos32
```
#windows
打64位windows包:
```
mvn clean compile package -DskipTests=true -Pwindows64
```
打32位windows包:
```
mvn clean compile package -DskipTests=true -Pwindows32
```

#### 如果JvmUtils不支持您的系统或者在使用时遇到`java.lang.UnsatisfiedLinkError`

1. 在您的系统上安装合适版本的g++编译器

Expand Down
163 changes: 147 additions & 16 deletions arthas-beans/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,162 @@
<artifactId>arthas-beans</artifactId>
<name>arthas-beans</name>

<profiles>
<!-- linux -->
<profile>
<id>linux32</id>
<activation>
<property>
<name>platform</name>
<value>linux32</value>
</property>
</activation>
<properties>
<os_name>linux</os_name>
<os_arch_option>-m32</os_arch_option>
<lib_name>libjnilibrary.so</lib_name>
</properties>
</profile>
<profile>
<id>linux64</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>platform</name>
<value>linux64</value>
</property>
</activation>
<properties>
<os_name>linux</os_name>
<os_arch_option>-m64</os_arch_option>
<lib_name>libjnilibrary.so</lib_name>
</properties>
</profile>

<!-- windows -->
<profile>
<id>windows32</id>
<activation>
<property>
<name>platform</name>
<value>windows32</value>
</property>
</activation>
<properties>
<os_name>win32</os_name>
<os_arch_option>-m32</os_arch_option>
<lib_name>jnilibrary.dll</lib_name>
</properties>
</profile>
<profile>
<id>windows64</id>
<activation>
<property>
<name>platform</name>
<value>windows64</value>
</property>
</activation>
<properties>
<os_name>win32</os_name>
<os_arch_option>-m64</os_arch_option>
<lib_name>jnilibrary.dll</lib_name>
</properties>
</profile>

<!-- macos -->
<profile>
<id>macos32</id>
<activation>
<property>
<name>platform</name>
<value>macos32</value>
</property>
</activation>
<properties>
<os_name>darwin</os_name>
<os_arch_option>-m32</os_arch_option>
<lib_name>libjnilibrary.dylib</lib_name>
</properties>
</profile>
<profile>
<id>macos64</id>
<activation>
<property>
<name>platform</name>
<value>macos64</value>
</property>
</activation>
<properties>
<os_name>darwin</os_name>
<os_arch_option>-m64</os_arch_option>
<lib_name>libjnilibrary.dylib</lib_name>
</properties>
</profile>
</profiles>

<build>
<finalName>arthas-beans</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>1.0-alpha-11</version>
<extensions>true</extensions>
<configuration>
<javahOS>${os_name}</javahOS>
<sources>
<source>
<directory>src/main/native/src</directory>
<fileNames>
<fileName>jni-library.cpp</fileName>
</fileNames>
</source>
<source>
<directory>src/main/native/include</directory>
</source>
</sources>

<compilerProvider>generic-classic</compilerProvider>
<compilerExecutable>g++</compilerExecutable>
<compilerStartOptions>
<compilerStartOption>${os_arch_option}</compilerStartOption>
<compilerStartOption>-fpic</compilerStartOption>
<compilerStartOption>-shared</compilerStartOption>
<compilerStartOption>-o</compilerStartOption>
</compilerStartOptions>

<linkerOutputDirectory>target</linkerOutputDirectory>
<linkerExecutable>g++</linkerExecutable>
<linkerStartOptions>
<linkerStartOption>${os_arch_option}</linkerStartOption>
<linkerStartOption>-shared</linkerStartOption>
</linkerStartOptions>
<linkerEndOptions>
<linkerEndOption>-o ${project.build.directory}/classes/${lib_name}</linkerEndOption>
</linkerEndOptions>
</configuration>
<executions>
<execution>
<id>javah</id>
<phase>compile</phase>
<goals>
<goal>initialize</goal>
<goal>compile</goal>
<goal>link</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand All @@ -35,20 +178,8 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>1.2.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.zeroturnaround</groupId>
<artifactId>zt-zip</artifactId>
<groupId>org.scijava</groupId>
<artifactId>native-lib-loader</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
64 changes: 3 additions & 61 deletions arthas-beans/src/main/java/com/vdian/vclub/JvmUtils.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package com.vdian.vclub;

import com.taobao.arthas.common.AnsiLog;
import com.taobao.arthas.common.OSUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.zeroturnaround.zip.ZipUtil;
import org.scijava.nativelib.NativeLoader;

import javax.servlet.ServletContext;
import java.io.File;
import java.util.LinkedList;

/**
Expand All @@ -18,36 +13,11 @@
*/
public class JvmUtils {

private static final PathMatchingResourcePatternResolver LOADER = new PathMatchingResourcePatternResolver();

private static String getSoLibName() throws Error {
String so = null;
if (OSUtils.isLinux()) {
so = "jni-lib-linux-x64.so";
} else if (OSUtils.isMac()) {
so = "jni-lib-macos-x64.so";
} else if (OSUtils.isWindows()) {
//todo
}
if (null == so) {
throw new Error("MemoryAnalyzer is not supported in your operating system!");
}
return so;
}
private final static String LIB_NAME = "jnilibrary";

static {
try {
String so = getSoLibName();
Resource[] resources = LOADER.getResources("classpath*:/cpp/" + so);
if (null == resources || resources.length == 0) {
throw new IllegalStateException("jni-lib not found !");
}
for (Resource resource : resources) {
try {
System.load(resource.getURL().getPath());
} catch (Throwable ignored) {
}
}
NativeLoader.loadLibrary(LIB_NAME);
AnsiLog.warn("checkResult->" + check() + ", jni-lib available !");
} catch (Throwable t) {
AnsiLog.error("load jni-lib failed:" + t.getMessage(), t);
Expand Down Expand Up @@ -92,32 +62,4 @@ public static LinkedList<Class> getAllClasses() {
return getInstances(Class.class);
}

public static String init(ServletContext servletContext) {
try {
String dirPath = servletContext.getRealPath("/WEB-INF/lib");
String jarPath = dirPath + "/" + getJarName(dirPath);
//解压到lib文件夹下
ZipUtil.unpack(new File(jarPath), new File(dirPath));
String so = getSoLibName();
AnsiLog.warn("try to load " + dirPath + "/cpp/" + so);
System.load(dirPath + "/cpp/" + so);
return check();
} catch (Throwable t) {
t.printStackTrace();
AnsiLog.error("load jni-lib failed with exception:{}", t.getMessage(), t);
}
return null;
}

private static String getJarName(String dirPath) {
File dir = new File(dirPath);
if (dir.exists()) {
for (String name : dir.list()) {
if (name.startsWith("arthas-beans-")) {
return name;
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(cpp)

set(CMAKE_CXX_STANDARD 14)

add_library(jni-lib SHARED jni-library.cpp)
add_library(jni-lib SHARED src/jni-library.cpp)

#使用环境变量来include,把不同系统的兼容问题交给jdk解决
include_directories("$ENV{JAVA_HOME}/include")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <jni.h>
#include <jni_md.h>
#include <jvmti.h>
#include "head/com_vdian_vclub_JvmUtils.h"
#include "com_vdian_vclub_JvmUtils.h"

extern "C"
JNIEXPORT jstring JNICALL
Expand Down
Loading