Skip to content
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
4 changes: 0 additions & 4 deletions core-shaded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
</dependency>
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-ffi</artifactId>
</dependency>
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-posix</artifactId>
Expand Down
4 changes: 0 additions & 4 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@
These dependencies are recommended but not mandatory, the driver will fall back to pure-Java
implementations if they are not available at runtime.
-->
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-ffi</artifactId>
</dependency>
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-posix</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class PlatformInfoFinder {
private static final String MAVEN_IGNORE_LINE = "The following files have been resolved:";
private static final Pattern DEPENDENCY_SPLIT_REGEX = Pattern.compile(":");
static final String UNVERIFIED_RUNTIME_VERSION = "UNVERIFIED";
public static final String UNKNOWN = "UNKNOWN";
private final Function<DependencyFromFile, URL> propertiesUrlProvider;

@SuppressWarnings("UnnecessaryLambda")
Expand Down Expand Up @@ -214,7 +213,7 @@ private boolean lineWithDependencyInfo(String line) {

private CPUS getCpuInfo() {
int numberOfProcessors = Runtime.getRuntime().availableProcessors();
String model = Native.isPlatformAvailable() ? Native.getCPU() : UNKNOWN;
String model = Native.getCpu();
return new CPUS(numberOfProcessors, model);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright DataStax, Inc.
*
* 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.datastax.oss.driver.internal.core.os;

import java.util.Locale;

public class CpuInfo {

/* Copied from equivalent op in jnr.ffi.Platform. We have to have this here as it has to be defined
* before its (multiple) uses in determineCpu() */
private static final Locale LOCALE = Locale.ENGLISH;

/* The remainder of this class is largely based on jnr.ffi.Platform in jnr-ffi version 2.1.10.
* We copy it manually here in order to avoid introducing an extra dependency merely for the sake of
* evaluating some system properties.
*
* jnr-ffi copyright notice follows:
*
* Copyright (C) 2008-2010 Wayne Meissner
*
* This file is part of the JNR 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.
*/
/** The supported CPU architectures. */
public enum Cpu {
/*
* <b>Note</b> The names of the enum values are used in other parts of the
* code to determine where to find the native stub library. Do NOT rename.
*/

/** 32 bit legacy Intel */
I386,

/** 64 bit AMD (aka EM64T/X64) */
X86_64,

/** 32 bit Power PC */
PPC,

/** 64 bit Power PC */
PPC64,

/** 64 bit Power PC little endian */
PPC64LE,

/** 32 bit Sun sparc */
SPARC,

/** 64 bit Sun sparc */
SPARCV9,

/** IBM zSeries S/390 */
S390X,

/** 32 bit MIPS (used by nestedvm) */
MIPS32,

/** 32 bit ARM */
ARM,

/** 64 bit ARM */
AARCH64,

/**
* Unknown CPU architecture. A best effort will be made to infer architecture specific values
* such as address and long size.
*/
UNKNOWN;

@Override
public String toString() {
return name().toLowerCase(LOCALE);
}
}

public static Cpu determineCpu() {
String archString = System.getProperty("os.arch");
if (equalsIgnoreCase("x86", archString)
|| equalsIgnoreCase("i386", archString)
|| equalsIgnoreCase("i86pc", archString)
|| equalsIgnoreCase("i686", archString)) {
return Cpu.I386;
} else if (equalsIgnoreCase("x86_64", archString) || equalsIgnoreCase("amd64", archString)) {
return Cpu.X86_64;
} else if (equalsIgnoreCase("ppc", archString) || equalsIgnoreCase("powerpc", archString)) {
return Cpu.PPC;
} else if (equalsIgnoreCase("ppc64", archString) || equalsIgnoreCase("powerpc64", archString)) {
if ("little".equals(System.getProperty("sun.cpu.endian"))) {
return Cpu.PPC64LE;
}
return Cpu.PPC64;
} else if (equalsIgnoreCase("ppc64le", archString)
|| equalsIgnoreCase("powerpc64le", archString)) {
return Cpu.PPC64LE;
} else if (equalsIgnoreCase("s390", archString) || equalsIgnoreCase("s390x", archString)) {
return Cpu.S390X;
} else if (equalsIgnoreCase("aarch64", archString)) {
return Cpu.AARCH64;
} else if (equalsIgnoreCase("arm", archString) || equalsIgnoreCase("armv7l", archString)) {
return Cpu.ARM;
}

// Try to find by lookup up in the CPU list
for (Cpu cpu : Cpu.values()) {
if (equalsIgnoreCase(cpu.name(), archString)) {
return cpu;
}
}

return Cpu.UNKNOWN;
}

private static boolean equalsIgnoreCase(String s1, String s2) {
return s1.equalsIgnoreCase(s2)
|| s1.toUpperCase(LOCALE).equals(s2.toUpperCase(LOCALE))
|| s1.toLowerCase(LOCALE).equals(s2.toLowerCase(LOCALE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright DataStax, Inc.
*
* 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.datastax.oss.driver.internal.core.os;

import java.util.Optional;

/** A no-op NativeImpl implementation; useful if we can't load one of the others */
public class EmptyLibc implements Libc {

@Override
public boolean available() {
return false;
}

@Override
public Optional<Long> gettimeofday() {
return Optional.empty();
}

@Override
public Optional<Integer> getpid() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright DataStax, Inc.
*
* 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.datastax.oss.driver.internal.core.os;

import java.util.Optional;
import java.util.function.Consumer;
import jnr.posix.POSIX;
import jnr.posix.POSIXFactory;
import jnr.posix.Timeval;
import jnr.posix.util.DefaultPOSIXHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JnrLibc implements Libc {

private static final Logger LOG = LoggerFactory.getLogger(JnrLibc.class);

private final Optional<POSIX> posix;

public JnrLibc() {

this.posix = loadPosix();
}

@Override
public Optional<Long> gettimeofday() {

return this.posix.flatMap(this::gettimeofdayImpl);
}

@Override
public Optional<Integer> getpid() {

return this.posix.map(POSIX::getpid);
}

@Override
public boolean available() {
return this.posix.isPresent();
}

private Optional<POSIX> loadPosix() {

try {
return Optional.of(POSIXFactory.getPOSIX(new DefaultPOSIXHandler(), true))
.flatMap(p -> catchAll(p, posix -> posix.getpid(), "Error calling getpid()"))
.flatMap(p -> catchAll(p, this::gettimeofdayImpl, "Error calling gettimeofday()"));
} catch (Throwable t) {
LOG.debug("Error loading POSIX", t);
return Optional.empty();
}
}

private Optional<POSIX> catchAll(POSIX posix, Consumer<POSIX> fn, String debugStr) {
try {
fn.accept(posix);
return Optional.of(posix);
} catch (Throwable t) {

LOG.debug(debugStr, t);
return Optional.empty();
}
}

private Optional<Long> gettimeofdayImpl(POSIX posix) {

Timeval tv = posix.allocateTimeval();
int rv = posix.gettimeofday(tv);
if (rv != 0) {
LOG.debug("Expected 0 return value from gettimeofday(), observed " + rv);
return Optional.empty();
}
return Optional.of(tv.sec() * 1_000_000 + tv.usec());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright DataStax, Inc.
*
* 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.datastax.oss.driver.internal.core.os;

import java.util.Optional;

public interface Libc {

/* Maintained to allow Native.isXAvailable() functionality without trying to make a native call if
* the underlying support _is_ available. */
boolean available();

Optional<Long> gettimeofday();

Optional<Integer> getpid();
}
Loading