< prev index next >
src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java
Print this page
*** 864,903 ****
}
// -- Memory-mapped buffers --
! private static class Unmapper
implements Runnable
{
// may be required to close file
private static final NativeDispatcher nd = new FileDispatcherImpl();
- // keep track of mapped buffer usage
- static volatile int count;
- static volatile long totalSize;
- static volatile long totalCapacity;
-
private volatile long address;
! private final long size;
! private final int cap;
private final FileDescriptor fd;
private Unmapper(long address, long size, int cap,
FileDescriptor fd)
{
assert (address != 0);
this.address = address;
this.size = size;
this.cap = cap;
this.fd = fd;
-
- synchronized (Unmapper.class) {
- count++;
- totalSize += size;
- totalCapacity += cap;
- }
}
public void run() {
if (address == 0)
return;
--- 864,892 ----
}
// -- Memory-mapped buffers --
! private static abstract class Unmapper
implements Runnable
{
// may be required to close file
private static final NativeDispatcher nd = new FileDispatcherImpl();
private volatile long address;
! protected final long size;
! protected final int cap;
private final FileDescriptor fd;
private Unmapper(long address, long size, int cap,
FileDescriptor fd)
{
assert (address != 0);
this.address = address;
this.size = size;
this.cap = cap;
this.fd = fd;
}
public void run() {
if (address == 0)
return;
*** 911,921 ****
} catch (IOException ignore) {
// nothing we can do
}
}
! synchronized (Unmapper.class) {
count--;
totalSize -= size;
totalCapacity -= cap;
}
}
--- 900,966 ----
} catch (IOException ignore) {
// nothing we can do
}
}
! decrement_stats();
! }
! protected abstract void increment_stats();
! protected abstract void decrement_stats();
! }
!
! private static class DefaultUnmapper extends Unmapper {
!
! // keep track of non-persistent mapped buffer usage
! static volatile int count;
! static volatile long totalSize;
! static volatile long totalCapacity;
!
! public DefaultUnmapper(long address, long size, int cap,
! FileDescriptor fd) {
! super(address, size, cap, fd);
! increment_stats();
! }
!
! protected void increment_stats() {
! synchronized (DefaultUnmapper.class) {
! count++;
! totalSize += size;
! totalCapacity += cap;
! }
! }
! protected void decrement_stats() {
! synchronized (DefaultUnmapper.class) {
! count--;
! totalSize -= size;
! totalCapacity -= cap;
! }
! }
! }
!
! private static class PersistentUnmapper extends Unmapper {
!
! // keep track of mapped buffer usage
! static volatile int count;
! static volatile long totalSize;
! static volatile long totalCapacity;
!
! public PersistentUnmapper(long address, long size, int cap,
! FileDescriptor fd) {
! super(address, size, cap, fd);
! increment_stats();
! }
!
! protected void increment_stats() {
! synchronized (PersistentUnmapper.class) {
! count++;
! totalSize += size;
! totalCapacity += cap;
! }
! }
! protected void decrement_stats() {
! synchronized (PersistentUnmapper.class) {
count--;
totalSize -= size;
totalCapacity -= cap;
}
}
*** 945,960 ****
throw new IllegalArgumentException("Position + size overflow");
if (size > Integer.MAX_VALUE)
throw new IllegalArgumentException("Size exceeds Integer.MAX_VALUE");
int imode = -1;
if (mode == MapMode.READ_ONLY)
imode = MAP_RO;
else if (mode == MapMode.READ_WRITE)
imode = MAP_RW;
! else if (mode == MapMode.PRIVATE)
imode = MAP_PV;
assert (imode >= 0);
if ((mode != MapMode.READ_ONLY) && !writable)
throw new NonWritableChannelException();
if (!readable)
throw new NonReadableChannelException();
--- 990,1014 ----
throw new IllegalArgumentException("Position + size overflow");
if (size > Integer.MAX_VALUE)
throw new IllegalArgumentException("Size exceeds Integer.MAX_VALUE");
int imode = -1;
+ boolean isPersistent = false;
if (mode == MapMode.READ_ONLY)
imode = MAP_RO;
else if (mode == MapMode.READ_WRITE)
imode = MAP_RW;
! else if (mode == MapMode.PRIVATE) {
imode = MAP_PV;
+ } else if (mode == MapMode.READ_ONLY_PERSISTENT) {
+ imode = MAP_RO;
+ isPersistent = true;
+ } else if (mode == MapMode.READ_WRITE_PERSISTENT) {
+ imode = MAP_RW;
+ isPersistent = true;
+ }
+
assert (imode >= 0);
if ((mode != MapMode.READ_ONLY) && !writable)
throw new NonWritableChannelException();
if (!readable)
throw new NonReadableChannelException();
*** 993,1024 ****
if (size == 0) {
addr = 0;
// a valid file descriptor is not required
FileDescriptor dummy = new FileDescriptor();
if ((!writable) || (imode == MAP_RO))
! return Util.newMappedByteBufferR(0, 0, dummy, null);
else
! return Util.newMappedByteBuffer(0, 0, dummy, null);
}
pagePosition = (int)(position % allocationGranularity);
long mapPosition = position - pagePosition;
mapSize = size + pagePosition;
try {
// If map0 did not throw an exception, the address is valid
! addr = map0(imode, mapPosition, mapSize);
} catch (OutOfMemoryError x) {
// An OutOfMemoryError may indicate that we've exhausted
// memory so force gc and re-attempt map
System.gc();
try {
Thread.sleep(100);
} catch (InterruptedException y) {
Thread.currentThread().interrupt();
}
try {
! addr = map0(imode, mapPosition, mapSize);
} catch (OutOfMemoryError y) {
// After a second OOME, fail
throw new IOException("Map failed", y);
}
}
--- 1047,1078 ----
if (size == 0) {
addr = 0;
// a valid file descriptor is not required
FileDescriptor dummy = new FileDescriptor();
if ((!writable) || (imode == MAP_RO))
! return Util.newMappedByteBufferR(0, 0, dummy, null, isPersistent);
else
! return Util.newMappedByteBuffer(0, 0, dummy, null, isPersistent);
}
pagePosition = (int)(position % allocationGranularity);
long mapPosition = position - pagePosition;
mapSize = size + pagePosition;
try {
// If map0 did not throw an exception, the address is valid
! addr = map0(imode, mapPosition, mapSize, isPersistent);
} catch (OutOfMemoryError x) {
// An OutOfMemoryError may indicate that we've exhausted
// memory so force gc and re-attempt map
System.gc();
try {
Thread.sleep(100);
} catch (InterruptedException y) {
Thread.currentThread().interrupt();
}
try {
! addr = map0(imode, mapPosition, mapSize, isPersistent);
} catch (OutOfMemoryError y) {
// After a second OOME, fail
throw new IOException("Map failed", y);
}
}
*** 1035,1055 ****
}
assert (IOStatus.checkAll(addr));
assert (addr % allocationGranularity == 0);
int isize = (int)size;
! Unmapper um = new Unmapper(addr, mapSize, isize, mfd);
if ((!writable) || (imode == MAP_RO)) {
return Util.newMappedByteBufferR(isize,
addr + pagePosition,
mfd,
! um);
} else {
return Util.newMappedByteBuffer(isize,
addr + pagePosition,
mfd,
! um);
}
} finally {
threads.remove(ti);
endBlocking(IOStatus.checkAll(addr));
}
--- 1089,1113 ----
}
assert (IOStatus.checkAll(addr));
assert (addr % allocationGranularity == 0);
int isize = (int)size;
! Unmapper um = (isPersistent
! ? new PersistentUnmapper(addr, mapSize, isize, mfd)
! : new DefaultUnmapper(addr, mapSize, isize, mfd));
if ((!writable) || (imode == MAP_RO)) {
return Util.newMappedByteBufferR(isize,
addr + pagePosition,
mfd,
! um,
! isPersistent);
} else {
return Util.newMappedByteBuffer(isize,
addr + pagePosition,
mfd,
! um,
! isPersistent);
}
} finally {
threads.remove(ti);
endBlocking(IOStatus.checkAll(addr));
}
*** 1065,1083 ****
public String getName() {
return "mapped";
}
@Override
public long getCount() {
! return Unmapper.count;
}
@Override
public long getTotalCapacity() {
! return Unmapper.totalCapacity;
}
@Override
public long getMemoryUsed() {
! return Unmapper.totalSize;
}
};
}
// -- Locks --
--- 1123,1166 ----
public String getName() {
return "mapped";
}
@Override
public long getCount() {
! return DefaultUnmapper.count;
! }
! @Override
! public long getTotalCapacity() {
! return DefaultUnmapper.totalCapacity;
! }
! @Override
! public long getMemoryUsed() {
! return DefaultUnmapper.totalSize;
! }
! };
! }
!
! /**
! * Invoked by sun.management.ManagementFactoryHelper to create the management
! * interface for persistent mapped buffers.
! */
! public static JavaNioAccess.BufferPool getPersistentMappedBufferPool() {
! return new JavaNioAccess.BufferPool() {
! @Override
! public String getName() {
! return "mapped_persistent";
! }
! @Override
! public long getCount() {
! return PersistentUnmapper.count;
}
@Override
public long getTotalCapacity() {
! return PersistentUnmapper.totalCapacity;
}
@Override
public long getMemoryUsed() {
! return PersistentUnmapper.totalSize;
}
};
}
// -- Locks --
*** 1199,1209 ****
}
// -- Native methods --
// Creates a new mapping
! private native long map0(int prot, long position, long length)
throws IOException;
// Removes an existing mapping
private static native int unmap0(long address, long length);
--- 1282,1292 ----
}
// -- Native methods --
// Creates a new mapping
! private native long map0(int prot, long position, long length, boolean isPersistent)
throws IOException;
// Removes an existing mapping
private static native int unmap0(long address, long length);
< prev index next >