--- old/src/java.base/share/classes/java/nio/MappedByteBuffer.java 2018-07-25 11:31:32.291932095 +0100 +++ new/src/java.base/share/classes/java/nio/MappedByteBuffer.java 2018-07-25 11:31:32.014931268 +0100 @@ -77,18 +77,32 @@ // operations if valid; null if the buffer is not mapped. private final FileDescriptor fd; + // A flag true if this buffer is mapped against persistent memory + // via a call to FileChannel.map_persistent and false if it is + // some other sort of file mapped via a call to FileChannel.map. + // this flag only determines the behaviour of force operations. + private final boolean isPersistent; + // This should only be invoked by the DirectByteBuffer constructors // MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private - FileDescriptor fd) - { + FileDescriptor fd, boolean isPersistent) { super(mark, pos, lim, cap); this.fd = fd; + this.isPersistent = isPersistent; + } + + MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private + boolean isPersistent) { + super(mark, pos, lim, cap); + this.fd = null; + this.isPersistent = isPersistent; } MappedByteBuffer(int mark, int pos, int lim, int cap) { // package-private super(mark, pos, lim, cap); this.fd = null; + this.isPersistent = false; } // Returns the distance (in bytes) of the buffer from the page aligned address @@ -108,6 +122,21 @@ } /** + * Tells whether this buffer was mapped against persistent memory + * by calling {@link java.nio.channels.FileChannel#map_persistent + * FileChannel.map_persistent} or mapped against some other form + * of device file using {@link java.nio.channels.FileChannel#map + * FileChannel.map}. + * + * @return true if the file was mapped using {@link + * java.nio.channels.FileChannel#map_persistent + * FileChannel.map_persistent} otherwise false. + */ + public boolean isPersistent() { + return isPersistent; + } + + /** * Tells whether or not this buffer's content is resident in physical * memory. * @@ -129,6 +158,10 @@ if (fd == null) { return true; } + // a persistent mapped buffer is always loaded + if (isPersistent()) { + return true; + } if ((address == 0) || (capacity() == 0)) return true; long offset = mappingOffset(); @@ -153,6 +186,10 @@ if (fd == null) { return this; } + // no need to load a persistent mapped buffer + if (isPersistent()) { + return this; + } if ((address == 0) || (capacity() == 0)) return this; long offset = mappingOffset(); @@ -202,12 +239,60 @@ * @return This buffer */ public final MappedByteBuffer force() { + return force(0, capacity()); + } + + /** + * Forces any changes made to some subregion of this buffer's + * content to be written to the storage device containing the + * mapped file. + * + *

If the file mapped into this buffer resides on a local storage + * device then when this method returns it is guaranteed that all changes + * made to the buffer since it was created, or since this method was last + * invoked, will have been written to that device. + * + *

If the file does not reside on a local device then no such guarantee + * is made. + * + *

If this buffer was not mapped in read/write mode ({@link + * java.nio.channels.FileChannel.MapMode#READ_WRITE}) then invoking this + * method has no effect.

+ * + * @param from + * The offset to the first byte in the buffer subregion + * that is to be written back to storage + * + * @param to + * The offset to the first byte beyond the buffer subregion + * that is to be written back to storage + * + * @return This buffer + */ + public final MappedByteBuffer force(long from, long to) { if (fd == null) { return this; } if ((address != 0) && (capacity() != 0)) { + // check inputs + if (from < 0 || from >= capacity()) { + throw new IllegalArgumentException(); + } + if (to < from || to > capacity()) { + throw new IllegalArgumentException(); + } + long offset = mappingOffset(); - force0(fd, mappingAddress(offset), mappingLength(offset)); + long a = mappingAddress(offset) + from; + long length = offset + to; + if (isPersistent) { + // simply force writeback of associated cache lines + Unsafe unsafe = Unsafe.getUnsafe(); + unsafe.writebackMemory(a, length); + } else { + // writeback using device associated with fd + force0(fd, a, length); + } } return this; }