@Test @DisplayName("virt2phys(long)") void virt2phys() { for (val huge : Arrays.asList(false, true)) { for (val lock : Arrays.asList(false, true)) { val bytes = createSize(0xFFFF); val virt = mmanager.allocate(bytes, huge, lock); mmanager.putByte(virt, mmanager.getByte(virt)); val phys = mmanager.virt2phys(virt); val softly = new SoftAssertions(); softly.assertThat(phys).isNotZero(); softly.assertThat(phys & (mmanager.getPageSize() - 1)).isEqualTo(virt & (mmanager.getPageSize() - 1)); mmanager.free(virt, bytes, huge, lock); softly.assertAll(); } } }
@Override @Contract(pure = true) @SuppressWarnings("PMD.DataflowAnomalyAnalysis") @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE") public long virt2phys(final long address) { if (!OPTIMIZED && address == 0) { throw new IllegalArgumentException("The parameter 'address' MUST NOT be 0."); } if (!System.getProperty("os.name").toLowerCase(Locale.getDefault()).contains("lin")) { if (DEBUG >= LOG_WARN) log.warn("Cannot translate virtual addresses in a non-Linux OS."); return 0; } if (DEBUG >= LOG_TRACE) log.trace("Translating virtual address 0x{} to physical address.", leftPad(address)); if (DEBUG >= LOG_DEBUG) log.debug("Parsing file '{}'.", PAGEMAP_PATH); try ( val pagemap = new RandomAccessFile(PAGEMAP_PATH, "r"); val channel = pagemap.getChannel().position(Long.divideUnsigned(address, pageSize) * Long.BYTES) ) { val buffer = ByteBuffer.allocate(Long.BYTES).order(ByteOrder.nativeOrder()); channel.read(buffer); val mask = pageSize - 1; val offset = address & mask; val phys = buffer.flip().getLong(); return phys * pageSize + offset; } catch (final FileNotFoundException e) { if (DEBUG >= LOG_ERROR) log.error("The file '{}' cannot be found.", PAGEMAP_PATH, e); } catch (final IOException e) { if (DEBUG >= LOG_ERROR) { log.error("The file '{}' cannot be read, closed or we read past its size.", PAGEMAP_PATH, e); } } return 0; }
SmartJniMemoryManager extends JniMemoryManager { @Override @Contract(pure = true) @SuppressWarnings("PMD.DataflowAnomalyAnalysis") @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE") public long virt2phys(final long address) { if (!OPTIMIZED && address == 0) { throw new IllegalArgumentException("The parameter 'address' MUST NOT be 0."); } if (!System.getProperty("os.name").toLowerCase(Locale.getDefault()).contains("lin")) { if (DEBUG >= LOG_WARN) log.warn("Cannot translate virtual addresses in a non-Linux OS."); return 0; } if (DEBUG >= LOG_TRACE) log.trace("Translating virtual address 0x{} to physical address.", leftPad(address)); if (DEBUG >= LOG_DEBUG) log.debug("Parsing file '{}'.", PAGEMAP_PATH); try ( val pagemap = new RandomAccessFile(PAGEMAP_PATH, "r"); val channel = pagemap.getChannel().position(Long.divideUnsigned(address, pageSize) * Long.BYTES) ) { val buffer = ByteBuffer.allocate(Long.BYTES).order(ByteOrder.nativeOrder()); channel.read(buffer); val mask = pageSize - 1; val offset = address & mask; val phys = buffer.flip().getLong(); return phys * pageSize + offset; } catch (final FileNotFoundException e) { if (DEBUG >= LOG_ERROR) log.error("The file '{}' cannot be found.", PAGEMAP_PATH, e); } catch (final IOException e) { if (DEBUG >= LOG_ERROR) { log.error("The file '{}' cannot be read, closed or we read past its size.", PAGEMAP_PATH, e); } } return 0; } }
SmartJniMemoryManager extends JniMemoryManager { @Override @Contract(pure = true) @SuppressWarnings("PMD.DataflowAnomalyAnalysis") @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE") public long virt2phys(final long address) { if (!OPTIMIZED && address == 0) { throw new IllegalArgumentException("The parameter 'address' MUST NOT be 0."); } if (!System.getProperty("os.name").toLowerCase(Locale.getDefault()).contains("lin")) { if (DEBUG >= LOG_WARN) log.warn("Cannot translate virtual addresses in a non-Linux OS."); return 0; } if (DEBUG >= LOG_TRACE) log.trace("Translating virtual address 0x{} to physical address.", leftPad(address)); if (DEBUG >= LOG_DEBUG) log.debug("Parsing file '{}'.", PAGEMAP_PATH); try ( val pagemap = new RandomAccessFile(PAGEMAP_PATH, "r"); val channel = pagemap.getChannel().position(Long.divideUnsigned(address, pageSize) * Long.BYTES) ) { val buffer = ByteBuffer.allocate(Long.BYTES).order(ByteOrder.nativeOrder()); channel.read(buffer); val mask = pageSize - 1; val offset = address & mask; val phys = buffer.flip().getLong(); return phys * pageSize + offset; } catch (final FileNotFoundException e) { if (DEBUG >= LOG_ERROR) log.error("The file '{}' cannot be found.", PAGEMAP_PATH, e); } catch (final IOException e) { if (DEBUG >= LOG_ERROR) { log.error("The file '{}' cannot be read, closed or we read past its size.", PAGEMAP_PATH, e); } } return 0; } private  SmartJniMemoryManager(); }
SmartJniMemoryManager extends JniMemoryManager { @Override @Contract(pure = true) @SuppressWarnings("PMD.DataflowAnomalyAnalysis") @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE") public long virt2phys(final long address) { if (!OPTIMIZED && address == 0) { throw new IllegalArgumentException("The parameter 'address' MUST NOT be 0."); } if (!System.getProperty("os.name").toLowerCase(Locale.getDefault()).contains("lin")) { if (DEBUG >= LOG_WARN) log.warn("Cannot translate virtual addresses in a non-Linux OS."); return 0; } if (DEBUG >= LOG_TRACE) log.trace("Translating virtual address 0x{} to physical address.", leftPad(address)); if (DEBUG >= LOG_DEBUG) log.debug("Parsing file '{}'.", PAGEMAP_PATH); try ( val pagemap = new RandomAccessFile(PAGEMAP_PATH, "r"); val channel = pagemap.getChannel().position(Long.divideUnsigned(address, pageSize) * Long.BYTES) ) { val buffer = ByteBuffer.allocate(Long.BYTES).order(ByteOrder.nativeOrder()); channel.read(buffer); val mask = pageSize - 1; val offset = address & mask; val phys = buffer.flip().getLong(); return phys * pageSize + offset; } catch (final FileNotFoundException e) { if (DEBUG >= LOG_ERROR) log.error("The file '{}' cannot be found.", PAGEMAP_PATH, e); } catch (final IOException e) { if (DEBUG >= LOG_ERROR) { log.error("The file '{}' cannot be read, closed or we read past its size.", PAGEMAP_PATH, e); } } return 0; } private  SmartJniMemoryManager(); @Override @Contract(pure = true) long getPageSize(); @Override @Contract(pure = true) @SuppressWarnings("PMD.DataflowAnomalyAnalysis") long getHugepageSize(); @Override @Contract(pure = true) @SuppressWarnings("PMD.DataflowAnomalyAnalysis") @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE") long virt2phys(final long address); }
SmartJniMemoryManager extends JniMemoryManager { @Override @Contract(pure = true) @SuppressWarnings("PMD.DataflowAnomalyAnalysis") @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE") public long virt2phys(final long address) { if (!OPTIMIZED && address == 0) { throw new IllegalArgumentException("The parameter 'address' MUST NOT be 0."); } if (!System.getProperty("os.name").toLowerCase(Locale.getDefault()).contains("lin")) { if (DEBUG >= LOG_WARN) log.warn("Cannot translate virtual addresses in a non-Linux OS."); return 0; } if (DEBUG >= LOG_TRACE) log.trace("Translating virtual address 0x{} to physical address.", leftPad(address)); if (DEBUG >= LOG_DEBUG) log.debug("Parsing file '{}'.", PAGEMAP_PATH); try ( val pagemap = new RandomAccessFile(PAGEMAP_PATH, "r"); val channel = pagemap.getChannel().position(Long.divideUnsigned(address, pageSize) * Long.BYTES) ) { val buffer = ByteBuffer.allocate(Long.BYTES).order(ByteOrder.nativeOrder()); channel.read(buffer); val mask = pageSize - 1; val offset = address & mask; val phys = buffer.flip().getLong(); return phys * pageSize + offset; } catch (final FileNotFoundException e) { if (DEBUG >= LOG_ERROR) log.error("The file '{}' cannot be found.", PAGEMAP_PATH, e); } catch (final IOException e) { if (DEBUG >= LOG_ERROR) { log.error("The file '{}' cannot be read, closed or we read past its size.", PAGEMAP_PATH, e); } } return 0; } private  SmartJniMemoryManager(); @Override @Contract(pure = true) long getPageSize(); @Override @Contract(pure = true) @SuppressWarnings("PMD.DataflowAnomalyAnalysis") long getHugepageSize(); @Override @Contract(pure = true) @SuppressWarnings("PMD.DataflowAnomalyAnalysis") @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE") long virt2phys(final long address);  }