-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require Java 11 (minimum) at run time
- Loading branch information
Showing
35 changed files
with
54 additions
and
638 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
63 changes: 10 additions & 53 deletions
63
classloader/src/main/java/io/smallrye/common/classloader/ClassDefiner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,26 @@ | ||
package io.smallrye.common.classloader; | ||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.security.ProtectionDomain; | ||
|
||
import sun.misc.Unsafe; | ||
|
||
public class ClassDefiner { | ||
private static final Unsafe unsafe; | ||
private static final Method defineClass; | ||
public static Class<?> defineClass(MethodHandles.Lookup lookup, Class<?> parent, String className, byte[] classBytes) { | ||
SecurityManager sm = System.getSecurityManager(); | ||
if (sm != null) { | ||
sm.checkPermission(DefineClassPermission.getInstance()); | ||
} | ||
|
||
static { | ||
unsafe = AccessController.doPrivileged(new PrivilegedAction<Unsafe>() { | ||
public Unsafe run() { | ||
return AccessController.doPrivileged(new PrivilegedAction<Class<?>>() { | ||
@Override | ||
public Class<?> run() { | ||
try { | ||
final Field field = Unsafe.class.getDeclaredField("theUnsafe"); | ||
field.setAccessible(true); | ||
return (Unsafe) field.get(null); | ||
MethodHandles.Lookup privateLookupIn = MethodHandles.privateLookupIn(parent, lookup); | ||
return privateLookupIn.defineClass(classBytes); | ||
} catch (IllegalAccessException e) { | ||
throw new IllegalAccessError(e.getMessage()); | ||
} catch (NoSuchFieldException e) { | ||
throw new NoSuchFieldError(e.getMessage()); | ||
} | ||
} | ||
}); | ||
|
||
defineClass = AccessController.doPrivileged(new PrivilegedAction<Method>() { | ||
@Override | ||
public Method run() { | ||
try { | ||
return Unsafe.class.getMethod("defineClass", String.class, byte[].class, int.class, int.class, | ||
ClassLoader.class, ProtectionDomain.class); | ||
} catch (NoSuchMethodException e) { | ||
throw new NoSuchMethodError(e.getMessage()); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public static Class<?> defineClass(MethodHandles.Lookup lookup, Class<?> parent, String className, byte[] classBytes) { | ||
SecurityManager sm = System.getSecurityManager(); | ||
if (sm != null) { | ||
sm.checkPermission(DefineClassPermission.getInstance()); | ||
} | ||
|
||
// The className for Unsafe.defineClass must match the one in classBytes, so we can try to verify the package | ||
// with the parent | ||
String parentPkg = parent.getPackage().getName(); | ||
String classPkg = className.substring(0, className.lastIndexOf('.')); | ||
|
||
if (!parentPkg.equals(classPkg)) { | ||
throw new IllegalArgumentException("Class not in same package as lookup class"); | ||
} | ||
|
||
try { | ||
return (Class<?>) defineClass.invoke(unsafe, className, classBytes, 0, classBytes.length, parent.getClassLoader(), | ||
null); | ||
} catch (IllegalAccessException e) { | ||
throw new IllegalAccessError(e.getMessage()); | ||
} catch (InvocationTargetException e) { | ||
throw new IllegalStateException(e.getMessage()); | ||
} | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
classloader/src/main/java9/io/smallrye/common/classloader/ClassDefiner.java
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
79 changes: 2 additions & 77 deletions
79
cpu/src/main/java/io/smallrye/common/cpu/ProcessorInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,13 @@ | ||
package io.smallrye.common.cpu; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.util.Locale; | ||
|
||
/** | ||
* Provides general information about the processors on this host. | ||
* Provides general information about the processors on this host (Java 9 version). | ||
*/ | ||
public class ProcessorInfo { | ||
private ProcessorInfo() { | ||
} | ||
|
||
private static final String CPUS_ALLOWED = "Cpus_allowed:"; | ||
|
||
/** | ||
* Returns the number of processors available to this process. On most operating systems this method | ||
* simply delegates to {@link Runtime#availableProcessors()}. However, on Linux, this strategy | ||
* is insufficient, since the JVM does not take into consideration the process' CPU set affinity | ||
* which is employed by cgroups and numactl. Therefore this method will analyze the Linux proc filesystem | ||
* to make the determination. Since the CPU affinity of a process can be change at any time, this method does | ||
* not cache the result. Calls should be limited accordingly. | ||
* <br> | ||
* Note tha on Linux, both SMT units (Hyper-Threading) and CPU cores are counted as a processor. | ||
* | ||
* @return the available processors on this system. | ||
*/ | ||
public static int availableProcessors() { | ||
if (System.getSecurityManager() != null) { | ||
return AccessController.doPrivileged((PrivilegedAction<Integer>) () -> Integer.valueOf(determineProcessors())) | ||
.intValue(); | ||
} | ||
|
||
return determineProcessors(); | ||
} | ||
|
||
private static int determineProcessors() { | ||
int javaProcs = Runtime.getRuntime().availableProcessors(); | ||
if (!isLinux()) { | ||
return javaProcs; | ||
} | ||
|
||
int maskProcs = 0; | ||
|
||
try { | ||
maskProcs = readCPUMask(); | ||
} catch (Exception e) { | ||
// yum | ||
} | ||
|
||
return maskProcs > 0 ? Math.min(javaProcs, maskProcs) : javaProcs; | ||
} | ||
|
||
private static int readCPUMask() throws IOException { | ||
try (FileInputStream stream = new FileInputStream("/proc/self/status")) { | ||
try (InputStreamReader inputReader = new InputStreamReader(stream, StandardCharsets.US_ASCII)) { | ||
try (BufferedReader reader = new BufferedReader(inputReader)) { | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
if (line.startsWith(CPUS_ALLOWED)) { | ||
int count = 0; | ||
int start = CPUS_ALLOWED.length(); | ||
for (int i = start; i < line.length(); i++) { | ||
final int v = Character.digit(line.charAt(i), 16); | ||
if (v != -1) { | ||
count += Integer.bitCount(v); | ||
} | ||
} | ||
return count; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
private static boolean isLinux() { | ||
String osArch = System.getProperty("os.name", "unknown").toLowerCase(Locale.US); | ||
return (osArch.contains("linux")); | ||
return Runtime.getRuntime().availableProcessors(); | ||
} | ||
} |
13 changes: 0 additions & 13 deletions
13
cpu/src/main/java9/io/smallrye/common/cpu/ProcessorInfo.java
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.