Skip to content

Commit

Permalink
Merge pull request #94 from dmlloyd/java11
Browse files Browse the repository at this point in the history
Drop MR JAR code for <11
  • Loading branch information
dmlloyd authored Jan 12, 2024
2 parents 1d39dc7 + 5a85ad0 commit ba46e5f
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 378 deletions.
12 changes: 6 additions & 6 deletions src/main/java/org/wildfly/common/archive/JDKSpecific.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
final class JDKSpecific {
private JDKSpecific() {}

static ByteBuffer inflate(final Inflater inflater, final ByteBuffer[] bufs, final long offset, final int compSize, final int uncompSize) throws DataFormatException, IOException {
static ByteBuffer inflate(final Inflater inflater, final ByteBuffer[] bufs, long offset, final int compSize, final int uncompSize) throws DataFormatException, IOException {
int cnt = 0;
byte[] b = new byte[min(16384, compSize)];
byte[] out = new byte[uncompSize];
int op = 0;
while (cnt < compSize) {
int rem = compSize - cnt;
final int acnt = min(rem, b.length);
Archive.readBytes(bufs, offset, b, 0, acnt);
cnt += acnt;
inflater.setInput(b, 0, acnt);
final ByteBuffer buf = bufs[Archive.bufIdx(offset + cnt)].duplicate();
buf.position(Archive.bufOffs(offset + cnt));
buf.limit(min(buf.capacity(), buf.position() + rem));
cnt += buf.remaining();
inflater.setInput(buf);
do {
op += inflater.inflate(out, op, uncompSize - op);
} while (! inflater.needsInput());
Expand Down
88 changes: 4 additions & 84 deletions src/main/java/org/wildfly/common/cpu/ProcessorInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.wildfly.common.cpu;
/*
* JBoss, Home of Professional Open Source.
* Copyright 2015 Red Hat, Inc., and individual contributors
* Copyright 2017 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,95 +16,16 @@
* limitations under the License.
*/

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Locale;
package org.wildfly.common.cpu;

/**
* Provides general information about the processors on this host.
*
* @author Jason T. Greene
* 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:";
private static final byte[] BITS = new byte[]{0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
private static final Charset ASCII = Charset.forName("US-ASCII");

/**
* 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 {
final FileInputStream stream = new FileInputStream("/proc/self/status");
final InputStreamReader inputReader = new InputStreamReader(stream, 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++) {
char ch = line.charAt(i);
if (ch >= '0' && ch <= '9') {
count += BITS[ch - '0'];
} else if (ch >= 'a' && ch <= 'f') {
count += BITS[ch - 'a' + 10];
} else if (ch >= 'A' && ch <= 'F') {
count += BITS[ch - 'A' + 10];
}
}
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();
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/wildfly/common/lock/JDKSpecific.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public Unsafe run() {
}

static void onSpinWait() {
// n/a
Thread.onSpinWait();
}
}
29 changes: 6 additions & 23 deletions src/main/java/org/wildfly/common/os/GetProcessInfoAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import static java.lang.Math.max;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.security.PrivilegedAction;

/**
Expand All @@ -32,27 +30,9 @@ final class GetProcessInfoAction implements PrivilegedAction<Object[]> {
}

public Object[] run() {
long pid = - 1L;
String processName = "<unknown>";
final RuntimeMXBean runtime;
try {
runtime = ManagementFactory.getPlatformMXBean(RuntimeMXBean.class);
} catch (Exception ignored) {
return new Object[] { Long.valueOf(pid), processName };
}
// TODO: on Java 9, use ProcessHandle.current().pid()
// Process ID
final String name = runtime.getName();
if (name != null) {
final int idx = name.indexOf('@');
if (idx != - 1) try {
pid = Long.parseLong(name.substring(0, idx));
} catch (NumberFormatException ignored) {
}
}
// TODO: on Java 9, maybe ProcessHandle.current().info().commandLine() or .command() instead
// Process name
processName = System.getProperty("jboss.process.name");
final ProcessHandle processHandle = ProcessHandle.current();
final long pid = processHandle.pid();
String processName = System.getProperty("jboss.process.name");
if (processName == null) {
final String classPath = System.getProperty("java.class.path");
final String commandLine = System.getProperty("sun.java.command");
Expand Down Expand Up @@ -96,6 +76,9 @@ public Object[] run() {
}
}
}
if (processName == null) {
processName = processHandle.info().command().orElse(null);
}
if (processName == null) {
processName = "<unknown>";
}
Expand Down
32 changes: 1 addition & 31 deletions src/main/java/org/wildfly/common/os/ProcessRedirect.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,10 @@

package org.wildfly.common.os;

import java.io.File;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Locale;

/**
* Process redirections that work on all JDK versions.
*/
public final class ProcessRedirect {
private ProcessRedirect() {}

/**
* Get the "discard" process redirection target.
*
* @return the discarding redirection target
*/
public static ProcessBuilder.Redirect discard() {
return ProcessBuilder.Redirect.to(new File(isWindows() ? "NUL" : "/dev/null"));
}

private static boolean isWindows() {
final SecurityManager sm = System.getSecurityManager();
return (sm == null ? getOsName() : getOsNamePrivileged()).toLowerCase(Locale.ROOT).contains("windows");
}

private static String getOsNamePrivileged() {
return AccessController.doPrivileged(new PrivilegedAction<String>() {
public String run() {
return getOsName();
}
});
}

private static String getOsName() {
return System.getProperty("os.name", "unknown");
return ProcessBuilder.Redirect.DISCARD;
}
}
35 changes: 0 additions & 35 deletions src/main/java11/org/wildfly/common/archive/JDKSpecific.java

This file was deleted.

31 changes: 0 additions & 31 deletions src/main/java9/org/wildfly/common/cpu/ProcessorInfo.java

This file was deleted.

53 changes: 0 additions & 53 deletions src/main/java9/org/wildfly/common/lock/JDKSpecific.java

This file was deleted.

Loading

0 comments on commit ba46e5f

Please sign in to comment.