Search in sources :

Example 1 with SizeT

use of org.elasticsearch.bootstrap.JNAKernel32Library.SizeT in project elasticsearch by elastic.

the class JNANatives method tryVirtualLock.

static void tryVirtualLock() {
    JNAKernel32Library kernel = JNAKernel32Library.getInstance();
    Pointer process = null;
    try {
        process = kernel.GetCurrentProcess();
        // By default, Windows limits the number of pages that can be locked.
        // Thus, we need to first increase the working set size of the JVM by
        // the amount of memory we wish to lock, plus a small overhead (1MB).
        SizeT size = new SizeT(JvmInfo.jvmInfo().getMem().getHeapInit().getBytes() + (1024 * 1024));
        if (!kernel.SetProcessWorkingSetSize(process, size, size)) {
            logger.warn("Unable to lock JVM memory. Failed to set working set size. Error code {}", Native.getLastError());
        } else {
            JNAKernel32Library.MemoryBasicInformation memInfo = new JNAKernel32Library.MemoryBasicInformation();
            long address = 0;
            while (kernel.VirtualQueryEx(process, new Pointer(address), memInfo, memInfo.size()) != 0) {
                boolean lockable = memInfo.State.longValue() == JNAKernel32Library.MEM_COMMIT && (memInfo.Protect.longValue() & JNAKernel32Library.PAGE_NOACCESS) != JNAKernel32Library.PAGE_NOACCESS && (memInfo.Protect.longValue() & JNAKernel32Library.PAGE_GUARD) != JNAKernel32Library.PAGE_GUARD;
                if (lockable) {
                    kernel.VirtualLock(memInfo.BaseAddress, new SizeT(memInfo.RegionSize.longValue()));
                }
                // Move to the next region
                address += memInfo.RegionSize.longValue();
            }
            LOCAL_MLOCKALL = true;
        }
    } catch (UnsatisfiedLinkError e) {
    // this will have already been logged by Kernel32Library, no need to repeat it
    } finally {
        if (process != null) {
            kernel.CloseHandle(process);
        }
    }
}
Also used : SizeT(org.elasticsearch.bootstrap.JNAKernel32Library.SizeT) Pointer(com.sun.jna.Pointer)

Example 2 with SizeT

use of org.elasticsearch.bootstrap.JNAKernel32Library.SizeT in project crate by crate.

the class JNANatives method tryVirtualLock.

static void tryVirtualLock() {
    JNAKernel32Library kernel = JNAKernel32Library.getInstance();
    Pointer process = null;
    try {
        process = kernel.GetCurrentProcess();
        // By default, Windows limits the number of pages that can be locked.
        // Thus, we need to first increase the working set size of the JVM by
        // the amount of memory we wish to lock, plus a small overhead (1MB).
        SizeT size = new SizeT(JvmInfo.jvmInfo().getMem().getHeapInit().getBytes() + (1024 * 1024));
        if (!kernel.SetProcessWorkingSetSize(process, size, size)) {
            LOGGER.warn("Unable to lock JVM memory. Failed to set working set size. Error code {}", Native.getLastError());
        } else {
            JNAKernel32Library.MemoryBasicInformation memInfo = new JNAKernel32Library.MemoryBasicInformation();
            long address = 0;
            while (kernel.VirtualQueryEx(process, new Pointer(address), memInfo, memInfo.size()) != 0) {
                boolean lockable = memInfo.State.longValue() == JNAKernel32Library.MEM_COMMIT && (memInfo.Protect.longValue() & JNAKernel32Library.PAGE_NOACCESS) != JNAKernel32Library.PAGE_NOACCESS && (memInfo.Protect.longValue() & JNAKernel32Library.PAGE_GUARD) != JNAKernel32Library.PAGE_GUARD;
                if (lockable) {
                    kernel.VirtualLock(memInfo.BaseAddress, new SizeT(memInfo.RegionSize.longValue()));
                }
                // Move to the next region
                address += memInfo.RegionSize.longValue();
            }
            LOCAL_MLOCKALL = true;
        }
    } catch (UnsatisfiedLinkError e) {
    // this will have already been logged by Kernel32Library, no need to repeat it
    } finally {
        if (process != null) {
            kernel.CloseHandle(process);
        }
    }
}
Also used : SizeT(org.elasticsearch.bootstrap.JNAKernel32Library.SizeT) Pointer(com.sun.jna.Pointer)

Aggregations

Pointer (com.sun.jna.Pointer)2 SizeT (org.elasticsearch.bootstrap.JNAKernel32Library.SizeT)2