Skip to content

Commit 9b6aa53

Browse files
committed
8267666: Add option to jcmd GC.heap_dump to use existing file
Backport-of: 7cbb67a3f8adc83a5b51c092a66480d7b22a6bea
1 parent de786bb commit 9b6aa53

11 files changed

Lines changed: 27 additions & 23 deletions

File tree

src/hotspot/os/aix/os_aix.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,9 +2665,7 @@ int os::open(const char *path, int oflag, int mode) {
26652665
// create binary file, rewriting existing file if required
26662666
int os::create_binary_file(const char* path, bool rewrite_existing) {
26672667
int oflags = O_WRONLY | O_CREAT;
2668-
if (!rewrite_existing) {
2669-
oflags |= O_EXCL;
2670-
}
2668+
oflags |= rewrite_existing ? O_TRUNC : O_EXCL;
26712669
return ::open64(path, oflags, S_IREAD | S_IWRITE);
26722670
}
26732671

src/hotspot/os/bsd/os_bsd.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,9 +2355,7 @@ int os::open(const char *path, int oflag, int mode) {
23552355
// create binary file, rewriting existing file if required
23562356
int os::create_binary_file(const char* path, bool rewrite_existing) {
23572357
int oflags = O_WRONLY | O_CREAT;
2358-
if (!rewrite_existing) {
2359-
oflags |= O_EXCL;
2360-
}
2358+
oflags |= rewrite_existing ? O_TRUNC : O_EXCL;
23612359
return ::open(path, oflags, S_IREAD | S_IWRITE);
23622360
}
23632361

src/hotspot/os/linux/os_linux.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4968,9 +4968,7 @@ int os::open(const char *path, int oflag, int mode) {
49684968
// create binary file, rewriting existing file if required
49694969
int os::create_binary_file(const char* path, bool rewrite_existing) {
49704970
int oflags = O_WRONLY | O_CREAT;
4971-
if (!rewrite_existing) {
4972-
oflags |= O_EXCL;
4973-
}
4971+
oflags |= rewrite_existing ? O_TRUNC : O_EXCL;
49744972
return ::open64(path, oflags, S_IREAD | S_IWRITE);
49754973
}
49764974

src/hotspot/os/windows/os_windows.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4732,9 +4732,7 @@ bool os::dir_is_empty(const char* path) {
47324732
// create binary file, rewriting existing file if required
47334733
int os::create_binary_file(const char* path, bool rewrite_existing) {
47344734
int oflags = _O_CREAT | _O_WRONLY | _O_BINARY;
4735-
if (!rewrite_existing) {
4736-
oflags |= _O_EXCL;
4737-
}
4735+
oflags |= rewrite_existing ? _O_TRUNC : _O_EXCL;
47384736
return ::open(path, oflags, _S_IREAD | _S_IWRITE);
47394737
}
47404738

src/hotspot/share/services/diagnosticCommand.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,13 @@ HeapDumpDCmd::HeapDumpDCmd(outputStream* output, bool heap) :
468468
"BOOLEAN", false, "false"),
469469
_gzip("-gz", "If specified, the heap dump is written in gzipped format "
470470
"using the given compression level. 1 (recommended) is the fastest, "
471-
"9 the strongest compression.", "INT", false, "1") {
471+
"9 the strongest compression.", "INT", false, "1"),
472+
_overwrite("-overwrite", "If specified, the dump file will be overwritten if it exists",
473+
"BOOLEAN", false, "false") {
472474
_dcmdparser.add_dcmd_option(&_all);
473475
_dcmdparser.add_dcmd_argument(&_filename);
474476
_dcmdparser.add_dcmd_option(&_gzip);
477+
_dcmdparser.add_dcmd_option(&_overwrite);
475478
}
476479

477480
void HeapDumpDCmd::execute(DCmdSource source, TRAPS) {
@@ -490,7 +493,7 @@ void HeapDumpDCmd::execute(DCmdSource source, TRAPS) {
490493
// This helps reduces the amount of unreachable objects in the dump
491494
// and makes it easier to browse.
492495
HeapDumper dumper(!_all.value() /* request GC if _all is false*/);
493-
dumper.dump(_filename.value(), output(), (int) level);
496+
dumper.dump(_filename.value(), output(), (int) level, _overwrite.value());
494497
}
495498

496499
ClassHistogramDCmd::ClassHistogramDCmd(outputStream* output, bool heap) :

src/hotspot/share/services/diagnosticCommand.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ class HeapDumpDCmd : public DCmdWithParser {
314314
DCmdArgument<char*> _filename;
315315
DCmdArgument<bool> _all;
316316
DCmdArgument<jlong> _gzip;
317+
DCmdArgument<bool> _overwrite;
317318
public:
318319
HeapDumpDCmd(outputStream* output, bool heap);
319320
static const char* name() {

src/hotspot/share/services/heapDumper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,7 @@ void VM_HeapDumper::dump_stack_traces() {
19051905
}
19061906

19071907
// dump the heap to given path.
1908-
int HeapDumper::dump(const char* path, outputStream* out, int compression) {
1908+
int HeapDumper::dump(const char* path, outputStream* out, int compression, bool overwrite) {
19091909
assert(path != NULL && strlen(path) > 0, "path missing");
19101910

19111911
// print message in interactive case
@@ -1928,7 +1928,7 @@ int HeapDumper::dump(const char* path, outputStream* out, int compression) {
19281928
}
19291929
}
19301930

1931-
DumpWriter writer(new (std::nothrow) FileWriter(path), compressor);
1931+
DumpWriter writer(new (std::nothrow) FileWriter(path, overwrite), compressor);
19321932

19331933
if (writer.error() != NULL) {
19341934
set_error(writer.error());

src/hotspot/share/services/heapDumper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class HeapDumper : public StackObj {
7171
// dumps the heap to the specified file, returns 0 if success.
7272
// additional info is written to out if not NULL.
7373
// compression >= 0 creates a gzipped file with the given compression level.
74-
int dump(const char* path, outputStream* out = NULL, int compression = -1);
74+
int dump(const char* path, outputStream* out = NULL, int compression = -1, bool overwrite = false);
7575

7676
// returns error message (resource allocated), or NULL if no error
7777
char* error_as_C_string() const;

src/hotspot/share/services/heapDumperCompression.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
char const* FileWriter::open_writer() {
3535
assert(_fd < 0, "Must not already be open");
3636

37-
_fd = os::create_binary_file(_path, false); // don't replace existing file
37+
_fd = os::create_binary_file(_path, _overwrite);
3838

3939
if (_fd < 0) {
4040
return os::strerror(errno);

src/hotspot/share/services/heapDumperCompression.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ class AbstractWriter : public CHeapObj<mtInternal> {
6161
class FileWriter : public AbstractWriter {
6262
private:
6363
char const* _path;
64+
bool _overwrite;
6465
int _fd;
6566

6667
public:
67-
FileWriter(char const* path) : _path(path), _fd(-1) { }
68+
FileWriter(char const* path, bool overwrite) : _path(path), _overwrite(overwrite), _fd(-1) { }
6869

6970
~FileWriter();
7071

0 commit comments

Comments
 (0)