use of sun.misc.Unsafe in project openj9 by eclipse.
the class Test123 method testCmvc194280.
@Test
public void testCmvc194280() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String classAsPath = "";
Test123 test = new Test123();
Class<?> c = test.getClass();
String className = c.getName();
classAsPath = className.replace('.', '/') + ".class";
InputStream stream = c.getClassLoader().getResourceAsStream(classAsPath);
byte[] raw = null;
try {
raw = IOUtils.readFully(stream, 1024, false);
} catch (IOException ioe) {
ioe.printStackTrace();
AssertJUnit.fail("Unable to read the Test123 class from path = " + classAsPath);
}
Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafeInstance.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafeInstance.get(Unsafe.class);
Class<?> defineClass = unsafe.defineClass("org.openj9.test.regression.Test123", raw, 0, raw.length, null, null);
AssertJUnit.assertEquals("An unexpected classloader was used to load class Test123 (Expect the bootstrap classloader)", null, defineClass.getClassLoader());
}
use of sun.misc.Unsafe in project openj9 by eclipse.
the class Test_Package method test_GetPackages.
/**
* @throws SecurityException
* @throws NoSuchFieldException
* @tests java.lang.Package#getPackages()
*/
@Test
public void test_GetPackages() {
try {
final boolean isJava8 = System.getProperty("java.specification.version").equals("1.8");
String PACKAGE_NAME = "org.openj9.resources.packagetest";
String PACKAGE = "/org/openj9/resources/packagetest/";
InputStream classFile = Test_Package.class.getResourceAsStream(PACKAGE + "ClassGenerated.class");
byte[] classFileBytes = new byte[classFile.available()];
classFile.read(classFileBytes);
Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafeInstance.setAccessible(true);
Unsafe unsafe = (Unsafe) theUnsafeInstance.get(Unsafe.class);
/* Create a generated class from org.openj9.resources.packagetest package */
Class<?> generatedClass = unsafe.defineClass("org.openj9.resources.packagetest.ClassGenerated", classFileBytes, 0, classFileBytes.length, null, null);
if (generatedClass == null) {
Assert.fail("Failed to generate class - org.openj9.resources.packagetest.ClassGenerated");
}
if (isJava8) {
/* NullPointerException should not be thrown */
assertFalse(findPackage(PACKAGE_NAME), // $NON-NLS-1$
"For generated classes, package information should not be available");
} else {
/* Java 9 adds all classes to a package */
assertTrue(findPackage(PACKAGE_NAME), // $NON-NLS-1$
"Package information should be available for all classes");
}
/* Load a class from org.openj9.resources.packagetest package - not generated */
Class<?> nonGeneratedClass = Class.forName("org.openj9.resources.packagetest.ClassNonGenerated");
if (nonGeneratedClass == null) {
Assert.fail("Failed to load non-generated class - org.openj9.resources.packagetest.ClassNonGenerated");
}
if (!findPackage(PACKAGE_NAME)) {
Assert.fail("For non-generated classes, package information should be available");
}
/* Generate a proxy class - Example: com/sun/proxy/$Proxy */
AnnoPackageString.class.getAnnotations();
/* NullPointerException should not be thrown when executing Package.getPackages() */
Package.getPackages();
} catch (IOException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | ClassNotFoundException ex) {
// $NON-NLS-1$
Assert.fail("Unexpected exception thrown: " + ex.getMessage());
}
}
use of sun.misc.Unsafe in project openj9 by eclipse.
the class Cmvc199515 method testCmvc199515.
@Test
public void testCmvc199515() throws Exception {
Random rng = new Random(0);
for (int arraySize = 1; arraySize <= 128; arraySize++) {
long[] dataset = new long[arraySize];
for (int i = 0; i < arraySize; i++) {
if ((i % 2) == 0) {
/* NaN doubles (0x7FF exponent) */
dataset[i] = 0x7FF0000000000000L | rng.nextLong();
} else {
/* signed zero and subnormal doubles (0x000 exponent) */
dataset[i] = 0x000FFFFFFFFFFFFFL & rng.nextLong();
}
}
/* test arraycopy */
long[] testLongArrayCopy = new long[arraySize];
System.arraycopy(dataset, 0, testLongArrayCopy, 0, arraySize);
for (int i = 0; i < arraySize; i++) {
AssertJUnit.assertEquals("System.arraycopy failed dataset[" + i + "] != testLongArrayCopy[" + i + "]", dataset[i], testLongArrayCopy[i]);
}
/* test arraycopy copyBackwardU64 */
if ((arraySize % 2) == 0) {
System.arraycopy(testLongArrayCopy, 0, testLongArrayCopy, arraySize / 2, arraySize / 2);
for (int i = 0; i < arraySize / 2; i++) {
int j = arraySize / 2 + i;
AssertJUnit.assertEquals("System.arraycopy failed testLongArrayCopy[" + i + "] != testLongArrayCopy[" + j + "]", testLongArrayCopy[i], testLongArrayCopy[j]);
}
}
/* test array clone */
long[] testLongArrayClone = dataset.clone();
for (int i = 0; i < arraySize; i++) {
AssertJUnit.assertEquals("clone array failed dataset[" + i + "] != testLongArrayClone[" + i + "]", dataset[i], testLongArrayClone[i]);
}
/* test Unsafe copyMemory */
long[] testLongArrayUnsafeCopy = new long[arraySize];
Unsafe myUnsafe = getUnsafeInstance();
myUnsafe.copyMemory(dataset, myUnsafe.arrayBaseOffset(dataset.getClass()), testLongArrayUnsafeCopy, myUnsafe.arrayBaseOffset(testLongArrayUnsafeCopy.getClass()), 8 * arraySize);
for (int i = 0; i < arraySize; i++) {
AssertJUnit.assertEquals("Unsafe copyMemory dataset[" + i + "] != testLongArrayUnsafeCopy[" + i + "]", dataset[i], testLongArrayUnsafeCopy[i]);
}
}
}
use of sun.misc.Unsafe in project lombok by rzwitserloot.
the class LombokProcessor method getUnsafe.
private static Unsafe getUnsafe() {
try {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
return (Unsafe) theUnsafe.get(null);
} catch (Exception e) {
return null;
}
}
use of sun.misc.Unsafe in project flink by apache.
the class ListViaRangeSpeedMiniBenchmark method main.
public static void main(String[] args) throws Exception {
final File rocksDir = new File("/tmp/rdb");
FileUtils.deleteDirectory(rocksDir);
final Options options = new Options().setCompactionStyle(CompactionStyle.LEVEL).setLevelCompactionDynamicLevelBytes(true).setIncreaseParallelism(4).setUseFsync(false).setMaxOpenFiles(-1).setDisableDataSync(true).setCreateIfMissing(true).setMergeOperator(new StringAppendOperator());
final WriteOptions write_options = new WriteOptions().setSync(false).setDisableWAL(true);
final RocksDB rocksDB = RocksDB.open(options, rocksDir.getAbsolutePath());
final String key = "key";
final String value = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ7890654321";
final byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
final byte[] valueBytes = value.getBytes(StandardCharsets.UTF_8);
final byte[] keyTemplate = Arrays.copyOf(keyBytes, keyBytes.length + 4);
final Unsafe unsafe = MemoryUtils.UNSAFE;
final long offset = unsafe.arrayBaseOffset(byte[].class) + keyTemplate.length - 4;
final int num = 50000;
System.out.println("begin insert");
final long beginInsert = System.nanoTime();
for (int i = 0; i < num; i++) {
unsafe.putInt(keyTemplate, offset, i);
rocksDB.put(write_options, keyTemplate, valueBytes);
}
final long endInsert = System.nanoTime();
System.out.println("end insert - duration: " + ((endInsert - beginInsert) / 1_000_000) + " ms");
final byte[] resultHolder = new byte[num * valueBytes.length];
final long beginGet = System.nanoTime();
final RocksIterator iterator = rocksDB.newIterator();
int pos = 0;
// seek to start
unsafe.putInt(keyTemplate, offset, 0);
iterator.seek(keyTemplate);
// mark end
unsafe.putInt(keyTemplate, offset, -1);
// iterate
while (iterator.isValid()) {
byte[] currKey = iterator.key();
if (samePrefix(keyBytes, currKey)) {
byte[] currValue = iterator.value();
System.arraycopy(currValue, 0, resultHolder, pos, currValue.length);
pos += currValue.length;
iterator.next();
} else {
break;
}
}
final long endGet = System.nanoTime();
System.out.println("end get - duration: " + ((endGet - beginGet) / 1_000_000) + " ms");
}
Aggregations