/** * @param val value to box to native long. * @return native long. */ @NotNull private static NativeLong nl(long val) { if (val % NL_CACHE_DIVISOR == 0 && val < CACHED_LONGS * NL_CACHE_DIVISOR) { int cacheIdx = (int)(val / NL_CACHE_DIVISOR); NativeLong curCached = nativeLongCache.get(cacheIdx); if (curCached != null) return curCached; NativeLong nl = new NativeLong(val); nativeLongCache.compareAndSet(cacheIdx, null, nl); return nl; } return new NativeLong(val); }
private String readLine(int fd, long addr, String prefix) throws IOException { if(LOGGER.isLoggable(FINEST)) LOGGER.finest("Reading "+prefix+" at "+addr); Memory m = new Memory(1); byte ch = 1; ByteArrayOutputStream buf = new ByteArrayOutputStream(); int i = 0; while(true) { if (i++ > LINE_LENGTH_LIMIT) { LOGGER.finest("could not find end of line, giving up"); throw new IOException("could not find end of line, giving up"); } LIBC.pread(fd, m, new NativeLong(1), new NativeLong(addr)); ch = m.getByte(0); if (ch == 0) break; buf.write(ch); addr++; } String line = buf.toString(); if(LOGGER.isLoggable(FINEST)) LOGGER.finest(prefix+" was "+line); return line; } }
private String readLine(int fd, long addr, String prefix) throws IOException { if(LOGGER.isLoggable(FINEST)) LOGGER.finest("Reading "+prefix+" at "+addr); Memory m = new Memory(1); byte ch = 1; ByteArrayOutputStream buf = new ByteArrayOutputStream(); int i = 0; while(true) { if (i++ > LINE_LENGTH_LIMIT) { LOGGER.finest("could not find end of line, giving up"); throw new IOException("could not find end of line, giving up"); } long r = LIBC.pread(fd, m, new NativeLong(1), new NativeLong(addr)); ch = m.getByte(0); if (ch == 0) break; buf.write(ch); addr++; } String line = buf.toString(); if(LOGGER.isLoggable(FINEST)) LOGGER.finest(prefix+" was "+line); return line; } }
/** * Unlock the given region. Does not report failures. */ public static void munlock(Pointer addr, long len) { if(Delegate.munlock(addr, new NativeLong(len)) != 0) { if(logger.isDebugEnabled()) logger.debug("munlocking failed with errno:" + errno.strerror()); } else { if(logger.isDebugEnabled()) logger.debug("munlocking region"); } }
/** * Lock the given region. Does not report failures. */ public static void mlock(Pointer addr, long len) { int res = Delegate.mlock(addr, new NativeLong(len)); if(res != 0) { if(logger.isDebugEnabled()) { logger.debug("Mlock failed probably because of insufficient privileges, errno:" + errno.strerror() + ", return value:" + res); } } else { if(logger.isDebugEnabled()) logger.debug("Mlock successfull"); } }
public void mlock() { final long beginTime = System.currentTimeMillis(); final long address = ((DirectBuffer) (this.mappedByteBuffer)).address(); Pointer pointer = new Pointer(address); { int ret = LibC.INSTANCE.mlock(pointer, new NativeLong(this.fileSize)); log.info("mlock {} {} {} ret = {} time consuming = {}", address, this.fileName, this.fileSize, ret, System.currentTimeMillis() - beginTime); } { int ret = LibC.INSTANCE.madvise(pointer, new NativeLong(this.fileSize), LibC.MADV_WILLNEED); log.info("madvise {} {} {} ret = {} time consuming = {}", address, this.fileName, this.fileSize, ret, System.currentTimeMillis() - beginTime); } }
/** * Unmap the given region. Returns 0 on success or -1 (and sets * errno) on failure. */ public static int munmap(Pointer addr, long len) throws IOException { int result = Delegate.munmap(addr, new NativeLong(len)); if(result != 0) { if(logger.isDebugEnabled()) logger.debug(errno.strerror()); throw new IOException("munmap failed: " + errno.strerror()); } return result; }
public void munlock() { final long beginTime = System.currentTimeMillis(); final long address = ((DirectBuffer) (this.mappedByteBuffer)).address(); Pointer pointer = new Pointer(address); int ret = LibC.INSTANCE.munlock(pointer, new NativeLong(this.fileSize)); log.info("munlock {} {} {} ret = {} time consuming = {}", address, this.fileName, this.fileSize, ret, System.currentTimeMillis() - beginTime); }
/** * It's a heavy init method. */ public void init() { for (int i = 0; i < poolSize; i++) { ByteBuffer byteBuffer = ByteBuffer.allocateDirect(fileSize); final long address = ((DirectBuffer) byteBuffer).address(); Pointer pointer = new Pointer(address); LibC.INSTANCE.mlock(pointer, new NativeLong(fileSize)); availableBuffers.offer(byteBuffer); } }
public void destroy() { for (ByteBuffer byteBuffer : availableBuffers) { final long address = ((DirectBuffer) byteBuffer).address(); Pointer pointer = new Pointer(address); LibC.INSTANCE.munlock(pointer, new NativeLong(fileSize)); } }
LIBC.pread(fd, m, new NativeLong(psize), new NativeLong(pr_argp)); long argp = b64 ? m.getLong(0) : to64(m.getInt(0)); LIBC.pread(fd, m, new NativeLong(psize), new NativeLong(argp+(n*psize))); long addr = b64 ? m.getLong(0) : to64(m.getInt(0));
public synchronized List<String> getArguments() { if(arguments!=null) return arguments; arguments = new ArrayList<String>(argc); if (argc == 0) { return arguments; } int psize = b64 ? 8 : 4; Memory m = new Memory(psize); try { if(LOGGER.isLoggable(FINER)) LOGGER.finer("Reading "+getFile("as")); int fd = LIBC.open(getFile("as").getAbsolutePath(), 0); try { for( int n=0; n<argc; n++ ) { // read a pointer to one entry LIBC.pread(fd, m, new NativeLong(psize), new NativeLong(argp+n*psize)); long addr = b64 ? m.getLong(0) : to64(m.getInt(0)); arguments.add(readLine(fd, addr, "argv["+ n +"]")); } } finally { LIBC.close(fd); } } catch (IOException | LastErrorException e) { // failed to read. this can happen under normal circumstances (most notably permission denied) // so don't report this as an error. } arguments = Collections.unmodifiableList(arguments); return arguments; }
/** * Map the given region of the given file descriptor into memory. * Returns a Pointer to the newly mapped memory throws an * IOException on error. */ public static Pointer mmap(long len, int prot, int flags, int fildes, long off) throws IOException { // we don't really have a need to change the recommended pointer. Pointer addr = new Pointer(0); Pointer result = Delegate.mmap(addr, new NativeLong(len), prot, flags, fildes, new NativeLong(off)); if(Pointer.nativeValue(result) == -1) { if(logger.isDebugEnabled()) logger.debug(errno.strerror()); throw new IOException("mmap failed: " + errno.strerror()); } return result; }
LIBC.pread(fd, m, new NativeLong(psize), new NativeLong(pr_envp)); long envp = b64 ? m.getLong(0) : to64(m.getInt(0)); LIBC.pread(fd, m, new NativeLong(psize), new NativeLong(envp+(n*psize))); long addr = b64 ? m.getLong(0) : to64(m.getInt(0));
for( int n=0; ; n++ ) { LIBC.pread(fd, m, new NativeLong(psize), new NativeLong(envp+n*psize)); long addr = b64 ? m.getLong(0) : to64(m.getInt(0)); if (addr == 0) // completed the walk
/** * Allocates align memory for use with O_DIRECT and returns native byte buffer. * @param fsBlockSize alignment, FS ans OS block size. * @param size capacity. * @return byte buffer, to be released by {@link #free(ByteBuffer)}. */ public static ByteBuffer allocate(int fsBlockSize, int size) { assert fsBlockSize > 0; assert size > 0; PointerByReference refToPtr = new PointerByReference(); int retVal = IgniteNativeIoLib.posix_memalign(refToPtr, new NativeLong(fsBlockSize), new NativeLong(size)); if (retVal != 0) throw new IgniteOutOfMemoryException("Failed to allocate memory: " + IgniteNativeIoLib.strerror(retVal)); return GridUnsafe.wrapPointer(Pointer.nativeValue(refToPtr.getValue()), size); }
final IntByReference nBytes = new IntByReference(0); try { if (WSAIoctl(new NativeLong(sockfd), SIO_KEEPALIVE_VALS, optValue, optSize, null, 0, nBytes, null, null) != 0) {
public void munlock() { final long beginTime = System.currentTimeMillis(); final long address = ((DirectBuffer) (this.mappedByteBuffer)).address(); Pointer pointer = new Pointer(address); int ret = LibC.INSTANCE.munlock(pointer, new NativeLong(this.fileSize)); log.info("munlock {} {} {} ret = {} time consuming = {}", address, this.fileName, this.fileSize, ret, System.currentTimeMillis() - beginTime); }
/** * Indirect the native pointer as a pointer to <code>long</code>. This is * equivalent to the expression * <code>*((long *)((char *)Pointer + offset))</code>. * * @param offset byte offset from pointer to perform the indirection * @return the <code>long</code> value being pointed to */ public NativeLong getNativeLong(long offset) { return new NativeLong(NativeLong.SIZE == 8 ? getLong(offset) : getInt(offset)); }
public void destroy() { for (ByteBuffer byteBuffer : availableBuffers) { final long address = ((DirectBuffer) byteBuffer).address(); Pointer pointer = new Pointer(address); LibC.INSTANCE.munlock(pointer, new NativeLong(fileSize)); } }