Skip to content

Commit 6071605

Browse files
committed
move 'tempFile' to implementors of AbstractLongMap.Data; CacheDirectory is now responsible for construction of various buffers
1 parent 42f7c64 commit 6071605

File tree

5 files changed

+88
-39
lines changed

5 files changed

+88
-39
lines changed

visualvm/libs.profiler/lib.profiler.heap/nbproject/project.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222
# questions.
2323

2424
is.autoload=true
25+
javac.source=1.8

visualvm/libs.profiler/lib.profiler.heap/src/org/graalvm/visualvm/lib/jfluid/heap/AbstractLongMap.java

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ abstract class Entry {
4949

5050
private final int VALUE_SIZE;
5151
final int ENTRY_SIZE;
52-
private File tempFile;
5352
long fileSize;
5453
private long keys;
5554
final int KEY_SIZE;
@@ -70,27 +69,15 @@ abstract class Entry {
7069
VALUE_SIZE = valueSize;
7170
ENTRY_SIZE = KEY_SIZE + VALUE_SIZE;
7271
fileSize = keys * ENTRY_SIZE;
73-
tempFile = cacheDir.createTempFile("NBProfiler", ".map"); // NOI18N
74-
75-
RandomAccessFile file = new RandomAccessFile(tempFile, "rw"); // NOI18N
76-
if (Boolean.getBoolean("org.graalvm.visualvm.lib.jfluid.heap.zerofile")) { // NOI18N
77-
byte[] zeros = new byte[512*1024];
78-
while(file.length()<fileSize) {
79-
file.write(zeros);
80-
}
81-
file.write(zeros,0,(int)(fileSize-file.length()));
82-
}
83-
file.setLength(fileSize);
84-
setDumpBuffer(file);
85-
file.close();
8672
cacheDirectory = cacheDir;
73+
dumpBuffer = cacheDir.createDumpBuffer(fileSize, ENTRY_SIZE);
8774
}
8875

8976
//~ Methods ------------------------------------------------------------------------------------------------------------------
9077

9178
protected void finalize() throws Throwable {
9279
if (cacheDirectory.isTemporary()) {
93-
tempFile.delete();
80+
dumpBuffer.deleteFile();
9481
}
9582
super.finalize();
9683
}
@@ -129,18 +116,18 @@ Entry put(long key, long value) {
129116
}
130117
}
131118

132-
private void setDumpBuffer(RandomAccessFile file) throws IOException {
119+
static Data getDumpBuffer(File f, RandomAccessFile file, int entrySize) throws IOException {
133120
long length = file.length();
134121

135122
try {
136123
if (length > Integer.MAX_VALUE) {
137-
dumpBuffer = new LongMemoryMappedData(file, length, ENTRY_SIZE);
124+
return new LongMemoryMappedData(f, file, length, entrySize);
138125
} else {
139-
dumpBuffer = new MemoryMappedData(file, length);
126+
return new MemoryMappedData(f, file, length);
140127
}
141128
} catch (IOException ex) {
142129
if (ex.getCause() instanceof OutOfMemoryError) {
143-
dumpBuffer = new FileData(file, length);
130+
return new FileData(f, file, length, entrySize);
144131
} else {
145132
throw ex;
146133
}
@@ -183,23 +170,19 @@ void writeToStream(DataOutputStream out) throws IOException {
183170
out.writeInt(ID_SIZE);
184171
out.writeInt(FOFFSET_SIZE);
185172
out.writeInt(VALUE_SIZE);
186-
out.writeUTF(tempFile.getAbsolutePath());
187-
dumpBuffer.force(tempFile);
173+
dumpBuffer.writeToStream(out);
188174
}
189175

190176
AbstractLongMap(DataInputStream dis, CacheDirectory cacheDir) throws IOException {
191177
keys = dis.readLong();
192178
ID_SIZE = dis.readInt();
193179
FOFFSET_SIZE = dis.readInt();
194180
VALUE_SIZE = dis.readInt();
195-
tempFile = cacheDir.getCacheFile(dis.readUTF());
196181

197182
KEY_SIZE = ID_SIZE;
198183
ENTRY_SIZE = KEY_SIZE + VALUE_SIZE;
199184
fileSize = keys * ENTRY_SIZE;
200-
RandomAccessFile file = new RandomAccessFile(tempFile, "rw"); // NOI18N
201-
setDumpBuffer(file);
202-
file.close();
185+
dumpBuffer = Data.readFromStream(dis, cacheDir, ENTRY_SIZE);
203186
cacheDirectory = cacheDir;
204187
}
205188

@@ -228,6 +211,13 @@ private static boolean isLinux() {
228211

229212
interface Data {
230213
//~ Methods --------------------------------------------------------------------------------------------------------------
214+
static Data readFromStream(DataInputStream dis, CacheDirectory cacheDir, int entrySize) throws IOException {
215+
File tempFile = cacheDir.getCacheFile(dis.readUTF());
216+
RandomAccessFile file = new RandomAccessFile(tempFile, "rw"); // NOI18N
217+
Data dumpBuffer = getDumpBuffer(tempFile, file, entrySize);
218+
file.close();
219+
return dumpBuffer;
220+
}
231221

232222
byte getByte(long index);
233223

@@ -241,23 +231,52 @@ interface Data {
241231

242232
void putLong(long index, long data);
243233

244-
void force(File bufferFile) throws IOException;
234+
void force() throws IOException;
235+
236+
void writeToStream(DataOutputStream out) throws IOException;
237+
238+
void deleteFile();
245239
}
246240

247-
private class FileData implements Data {
241+
private static abstract class AbstractData implements Data {
242+
243+
File bufferFile;
244+
245+
private AbstractData(File file) {
246+
bufferFile = file;
247+
}
248+
249+
//---- Serialization support
250+
public void writeToStream(DataOutputStream out) throws IOException {
251+
out.writeUTF(bufferFile.getAbsolutePath());
252+
force();
253+
}
254+
255+
public void deleteFile() {
256+
bufferFile.delete();
257+
}
258+
259+
}
260+
261+
private static class FileData extends AbstractData {
248262
//~ Instance fields ------------------------------------------------------------------------------------------------------
249263

250264
RandomAccessFile file;
251265
byte[] buf;
252266
boolean bufferModified;
253267
long offset;
268+
int entrySize;
269+
long fileSize;
254270
final static int BUFFER_SIZE = 128;
255271

256272
//~ Constructors ---------------------------------------------------------------------------------------------------------
257273

258-
FileData(RandomAccessFile f, long length) throws IOException {
274+
FileData(File fl, RandomAccessFile f, long length, int entry) throws IOException {
275+
super(fl);
259276
file = f;
260-
buf = new byte[ENTRY_SIZE*BUFFER_SIZE];
277+
fileSize = length;
278+
entrySize = entry;
279+
buf = new byte[entrySize*BUFFER_SIZE];
261280
}
262281

263282
//~ Methods --------------------------------------------------------------------------------------------------------------
@@ -318,7 +337,7 @@ public synchronized void putLong(long index, long data) {
318337
}
319338

320339
private int loadBufferIfNeeded(long index) {
321-
int i = (int) (index % (ENTRY_SIZE * BUFFER_SIZE));
340+
int i = (int) (index % (entrySize * BUFFER_SIZE));
322341
long newOffset = index - i;
323342

324343
if (offset != newOffset) {
@@ -354,12 +373,12 @@ private void flush() throws IOException {
354373
}
355374

356375
@Override
357-
public void force(File bufferFile) throws IOException {
376+
public void force() throws IOException {
358377
flush();
359378
}
360379
}
361380

362-
private static class MemoryMappedData implements Data {
381+
private static class MemoryMappedData extends AbstractData {
363382

364383
private static final FileChannel.MapMode MAP_MODE = isLinux() ? FileChannel.MapMode.PRIVATE : FileChannel.MapMode.READ_WRITE;
365384

@@ -369,8 +388,9 @@ private static class MemoryMappedData implements Data {
369388

370389
//~ Constructors ---------------------------------------------------------------------------------------------------------
371390

372-
MemoryMappedData(RandomAccessFile file, long length)
391+
MemoryMappedData(File f, RandomAccessFile file, long length)
373392
throws IOException {
393+
super(f);
374394
buf = createBuffer(file, length);
375395
}
376396

@@ -401,7 +421,7 @@ public void putLong(long index, long data) {
401421
}
402422

403423
@Override
404-
public void force(File bufferFile) throws IOException {
424+
public void force() throws IOException {
405425
if (MAP_MODE == FileChannel.MapMode.PRIVATE) {
406426
File newBufferFile = new File(bufferFile.getAbsolutePath()+".new"); // NOI18N
407427
int length = buf.capacity();
@@ -423,7 +443,7 @@ private static MappedByteBuffer createBuffer(RandomAccessFile file, long length)
423443
}
424444
}
425445

426-
private static class LongMemoryMappedData implements Data {
446+
private static class LongMemoryMappedData extends AbstractData {
427447

428448
private static int BUFFER_SIZE_BITS = 30;
429449
private static long BUFFER_SIZE = 1L << BUFFER_SIZE_BITS;
@@ -438,8 +458,9 @@ private static class LongMemoryMappedData implements Data {
438458

439459
//~ Constructors ---------------------------------------------------------------------------------------------------------
440460

441-
LongMemoryMappedData(RandomAccessFile file, long length, int entry)
461+
LongMemoryMappedData(File f, RandomAccessFile file, long length, int entry)
442462
throws IOException {
463+
super(f);
443464
dumpBuffer = createBuffers(file, length);
444465
entrySize = entry;
445466
}
@@ -479,7 +500,7 @@ private int getBufferOffset(long index) {
479500
}
480501

481502
@Override
482-
public void force(File bufferFile) throws IOException{
503+
public void force() throws IOException{
483504
if (MemoryMappedData.MAP_MODE == FileChannel.MapMode.PRIVATE) {
484505
File newBufferFile = new File(bufferFile.getAbsolutePath()+".new"); // NOI18N
485506
long length = bufferFile.length();

visualvm/libs.profiler/lib.profiler.heap/src/org/graalvm/visualvm/lib/jfluid/heap/CacheDirectory.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.File;
2828
import java.io.FileNotFoundException;
2929
import java.io.IOException;
30+
import java.io.RandomAccessFile;
3031

3132
/**
3233
*
@@ -139,6 +140,32 @@ void setDirty(boolean dirty) {
139140
}
140141
}
141142

143+
HprofByteBuffer createHprofByteBuffer(File dumpFile) throws IOException{
144+
return HprofByteBuffer.createHprofByteBuffer(dumpFile);
145+
}
146+
147+
AbstractLongMap.Data createDumpBuffer(long fileSize, int entrySize) throws IOException {
148+
AbstractLongMap.Data dumpBuffer;
149+
File tempFile = createTempFile("NBProfiler", ".map"); // NOI18N
150+
151+
RandomAccessFile file = new RandomAccessFile(tempFile, "rw"); // NOI18N
152+
if (Boolean.getBoolean("org.graalvm.visualvm.lib.jfluid.heap.zerofile")) { // NOI18N
153+
byte[] zeros = new byte[512*1024];
154+
while(file.length()<fileSize) {
155+
file.write(zeros);
156+
}
157+
file.write(zeros,0,(int)(fileSize-file.length()));
158+
}
159+
file.setLength(fileSize);
160+
dumpBuffer = AbstractLongMap.getDumpBuffer(tempFile, file, entrySize);
161+
file.close();
162+
return dumpBuffer;
163+
}
164+
165+
NumberList createNumberList(int idSize) throws IOException {
166+
return new NumberList(idSize, this);
167+
}
168+
142169
private static boolean isFileR(File f) {
143170
return f.exists() && f.isFile() && f.canRead();
144171
}

visualvm/libs.profiler/lib.profiler.heap/src/org/graalvm/visualvm/lib/jfluid/heap/HprofHeap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class HprofHeap implements Heap {
138138

139139
HprofHeap(File dumpFile, int seg, CacheDirectory cacheDir) throws FileNotFoundException, IOException {
140140
cacheDirectory = cacheDir;
141-
dumpBuffer = HprofByteBuffer.createHprofByteBuffer(dumpFile);
141+
dumpBuffer = cacheDir.createHprofByteBuffer(dumpFile);
142142
segment = seg;
143143
fillTagBounds(dumpBuffer.getHeaderSize());
144144
heapDumpSegment = computeHeapDumpStart();

visualvm/libs.profiler/lib.profiler.heap/src/org/graalvm/visualvm/lib/jfluid/heap/LongMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public int hashCode() {
245245

246246
LongMap(int size,int idSize,int foffsetSize,CacheDirectory cacheDir) throws FileNotFoundException, IOException {
247247
super(size,idSize,foffsetSize,foffsetSize + 4 + 1 + idSize + foffsetSize, cacheDir);
248-
referenceList = new NumberList(ID_SIZE, cacheDir);
248+
referenceList = cacheDir.createNumberList(ID_SIZE);
249249
}
250250

251251
//~ Methods ------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)