Search in sources :

Example 21 with Unsafe

use of sun.misc.Unsafe in project hazelcast by hazelcast.

the class UnsafeUtil method findUnsafe.

private static Unsafe findUnsafe() {
    try {
        return Unsafe.getUnsafe();
    } catch (SecurityException se) {
        return AccessController.doPrivileged(new PrivilegedAction<Unsafe>() {

            @Override
            public Unsafe run() {
                try {
                    Class<Unsafe> type = Unsafe.class;
                    try {
                        Field field = type.getDeclaredField("theUnsafe");
                        field.setAccessible(true);
                        return type.cast(field.get(type));
                    } catch (Exception e) {
                        for (Field field : type.getDeclaredFields()) {
                            if (type.isAssignableFrom(field.getType())) {
                                field.setAccessible(true);
                                return type.cast(field.get(type));
                            }
                        }
                    }
                } catch (Throwable t) {
                    throw rethrow(t);
                }
                throw new RuntimeException("Unsafe unavailable");
            }
        });
    }
}
Also used : Field(java.lang.reflect.Field) PrivilegedAction(java.security.PrivilegedAction) Unsafe(sun.misc.Unsafe)

Example 22 with Unsafe

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");
}
Also used : Options(org.rocksdb.Options) WriteOptions(org.rocksdb.WriteOptions) WriteOptions(org.rocksdb.WriteOptions) RocksDB(org.rocksdb.RocksDB) StringAppendOperator(org.rocksdb.StringAppendOperator) Unsafe(sun.misc.Unsafe) RocksIterator(org.rocksdb.RocksIterator) File(java.io.File)

Example 23 with Unsafe

use of sun.misc.Unsafe in project caffeine by ben-manes.

the class UnsafeAccess method load.

static Unsafe load(String openJdk, String android) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    Field field;
    try {
        // try OpenJDK field name
        field = Unsafe.class.getDeclaredField(openJdk);
    } catch (NoSuchFieldException e) {
        try {
            // try Android field name
            field = Unsafe.class.getDeclaredField(android);
        } catch (NoSuchFieldException e2) {
            // try to create a new instance
            Constructor<Unsafe> unsafeConstructor = Unsafe.class.getDeclaredConstructor();
            unsafeConstructor.setAccessible(true);
            return unsafeConstructor.newInstance();
        }
    }
    field.setAccessible(true);
    return (Unsafe) field.get(null);
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 24 with Unsafe

use of sun.misc.Unsafe in project groovy-core by groovy.

the class FastStringUtils method loadUnsafe.

/**
     * @return Unsafe
     */
private static Unsafe loadUnsafe() {
    try {
        Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
        unsafeField.setAccessible(true);
        return (Unsafe) unsafeField.get(null);
    } catch (Exception e) {
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) Unsafe(sun.misc.Unsafe)

Example 25 with Unsafe

use of sun.misc.Unsafe in project es6draft by anba.

the class UnsafeHolder method initializeUnsafe.

private static Unsafe initializeUnsafe() {
    try {
        return Unsafe.getUnsafe();
    } catch (SecurityException e) {
        try {
            return AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
                Field f = Unsafe.class.getDeclaredField("theUnsafe");
                f.setAccessible(true);
                return (Unsafe) f.get(null);
            });
        } catch (PrivilegedActionException e2) {
            throw new ExceptionInInitializerError(e2.getException());
        }
    }
}
Also used : Field(java.lang.reflect.Field) PrivilegedActionException(java.security.PrivilegedActionException) Unsafe(sun.misc.Unsafe) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction)

Aggregations

Unsafe (sun.misc.Unsafe)39 Field (java.lang.reflect.Field)29 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)5 PrivilegedActionException (java.security.PrivilegedActionException)4 PrivilegedAction (java.security.PrivilegedAction)2 File (java.io.File)1 Options (org.rocksdb.Options)1 RocksDB (org.rocksdb.RocksDB)1 RocksIterator (org.rocksdb.RocksIterator)1 StringAppendOperator (org.rocksdb.StringAppendOperator)1 WriteOptions (org.rocksdb.WriteOptions)1