Skip to content

Commit

Permalink
backport of bddf48380e658df630fecad5eda40106a24b6e1c
Browse files Browse the repository at this point in the history
  • Loading branch information
elifaslan1 committed May 30, 2024
1 parent c35d30d commit a048a68
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 52 deletions.
6 changes: 0 additions & 6 deletions src/hotspot/os/bsd/os_bsd.inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,6 @@ inline size_t os::restartable_read(int fd, void *buf, unsigned int nBytes) {
return res;
}

inline size_t os::write(int fd, const void *buf, unsigned int nBytes) {
size_t res;
RESTARTABLE((size_t) ::write(fd, buf, (size_t) nBytes), res);
return res;
}

inline int os::close(int fd) {
return ::close(fd);
}
Expand Down
43 changes: 15 additions & 28 deletions src/hotspot/os/linux/perfMemory_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,38 +84,25 @@ static void save_memory_to_file(char* addr, size_t size) {
const char* destfile = PerfMemory::get_perfdata_file_path();
assert(destfile[0] != '\0', "invalid PerfData file path");

int result;
int fd;

RESTARTABLE(os::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR),
result);
if (result == OS_ERR) {
if (PrintMiscellaneous && Verbose) {
warning("Could not create Perfdata save file: %s: %s\n",
destfile, os::strerror(errno));
}
RESTARTABLE(os::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR), fd);
if (fd == OS_ERR) {
warning("Could not create Perfdata save file: %s: %s\n",
destfile, os::strerror(errno));
} else {
int fd = result;
ssize_t result;

for (size_t remaining = size; remaining > 0;) {

RESTARTABLE(::write(fd, addr, remaining), result);
if (result == OS_ERR) {
if (PrintMiscellaneous && Verbose) {
warning("Could not write Perfdata save file: %s: %s\n",
destfile, os::strerror(errno));
}
break;
}

remaining -= (size_t)result;
addr += result;
bool successful_write = os::write(fd, addr, size);
if (!successful_write) {
warning("Could not write Perfdata save file: %s: %s\n",
destfile, os::strerror(errno));
}


result = ::close(fd);
if (PrintMiscellaneous && Verbose) {
if (result == OS_ERR) {
warning("Could not close %s: %s\n", destfile, os::strerror(errno));
}
if (result == OS_ERR) {
warning("Could not close %s: %s\n", destfile, os::strerror(errno));
}
}
FREE_C_HEAP_ARRAY(char, destfile);
Expand Down Expand Up @@ -983,11 +970,11 @@ static int create_sharedmem_file(const char* dirname, const char* filename, size
int zero_int = 0;
result = (int)os::seek_to_file_offset(fd, (jlong)(seekpos));
if (result == -1 ) break;
RESTARTABLE(::write(fd, &zero_int, 1), result);
if (result != 1) {
if (!os::write(fd, &zero_int, 1)) {
if (errno == ENOSPC) {
warning("Insufficient space for shared memory file:\n %s\nTry using the -Djava.io.tmpdir= option to select an alternate temp location.\n", filename);
}
result = OS_ERR;
break;
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/hotspot/os/posix/os_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,12 @@ FILE* os::open(int fd, const char* mode) {
return ::fdopen(fd, mode);
}

ssize_t os::pd_write(int fd, const void *buf, size_t nBytes) {
ssize_t res;
RESTARTABLE(::write(fd, buf, nBytes), res);
return res;
}

void os::flockfile(FILE* fp) {
::flockfile(fp);
}
Expand Down
23 changes: 23 additions & 0 deletions src/hotspot/os/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4645,6 +4645,29 @@ FILE* os::open(int fd, const char* mode) {
return ::_fdopen(fd, mode);
}

ssize_t os::pd_write(int fd, const void *buf, size_t nBytes) {
ssize_t original_len = (ssize_t)nBytes;
while (nBytes > 0) {
unsigned int len = nBytes > INT_MAX ? INT_MAX : (unsigned int)nBytes;
// On Windows, ::write takes 'unsigned int' no of bytes, so nBytes should be split if larger.
ssize_t written_bytes = ::write(fd, buf, len);
if (written_bytes < 0) {
return OS_ERR;
}
nBytes -= written_bytes;
buf = (char *)buf + written_bytes;
}
return original_len;
}

void os::exit(int num) {
win32::exit_process_or_thread(win32::EPT_PROCESS, num);
}

void os::_exit(int num) {
win32::exit_process_or_thread(win32::EPT_PROCESS_DIE, num);
}

// Is a (classpath) directory empty?
bool os::dir_is_empty(const char* path) {
errno_t err;
Expand Down
14 changes: 7 additions & 7 deletions src/hotspot/share/jfr/writers/jfrStreamWriterHost.inline.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -76,14 +76,14 @@ inline void StreamWriterHost<Adapter, AP>::write_bytes(const u1* buf, intptr_t l
assert(len >= 0, "invariant");
while (len > 0) {
const unsigned int nBytes = len > INT_MAX ? INT_MAX : (unsigned int)len;
const ssize_t num_written = (ssize_t)os::write(_fd, buf, nBytes);
if (errno == ENOSPC) {
const bool successful_write = os::write(_fd, buf, nBytes);
if (!successful_write && errno == ENOSPC) {
JfrJavaSupport::abort("Failed to write to jfr stream because no space left on device", false);
}
guarantee(num_written > 0, "Nothing got written, or os::write() failed");
_stream_pos += num_written;
len -= num_written;
buf += num_written;
guarantee(successful_write, "Not all the bytes got written, or os::write() failed");
_stream_pos += nBytes;
len -= nBytes;
buf += nBytes;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/share/prims/jvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2825,8 +2825,8 @@ void jio_print(const char* s, size_t len) {
if (Arguments::vfprintf_hook() != NULL) {
jio_fprintf(defaultStream::output_stream(), "%.*s", (int)len, s);
} else {
// Make an unused local variable to avoid warning from gcc 4.x compiler.
size_t count = ::write(defaultStream::output_fd(), s, (int)len);
// Make an unused local variable to avoid warning from gcc compiler.
bool dummy = os::write(defaultStream::output_fd(), s, len);
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/hotspot/share/runtime/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,21 @@ bool os::file_exists(const char* filename) {
return os::stat(filename, &statbuf) == 0;
}

bool os::write(int fd, const void *buf, size_t nBytes) {
ssize_t res;

while (nBytes > 0) {
res = pd_write(fd, buf, nBytes);
if (res == OS_ERR) {
return false;
}
buf = (void *)((char *)buf + res);
nBytes -= res;
}

return true;
}

/*
* Splits a path, based on its separator, the number of
* elements is returned back in n.
Expand Down
5 changes: 4 additions & 1 deletion src/hotspot/share/runtime/os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ class os: AllStatic {
// Get summary strings for system information in buffer provided
static void get_summary_cpu_info(char* buf, size_t buflen);
static void get_summary_os_info(char* buf, size_t buflen);
// Returns number of bytes written on success, OS_ERR on failure.
static ssize_t pd_write(int fd, const void *buf, size_t nBytes);

static void initialize_initial_active_processor_count();

Expand Down Expand Up @@ -606,7 +608,8 @@ class os: AllStatic {
static size_t read(int fd, void *buf, unsigned int nBytes);
static size_t read_at(int fd, void *buf, unsigned int nBytes, jlong offset);
static size_t restartable_read(int fd, void *buf, unsigned int nBytes);
static size_t write(int fd, const void *buf, unsigned int nBytes);
// Writes the bytes completely. Returns true on success, false otherwise.
static bool write(int fd, const void *buf, size_t nBytes);

// Reading directories.
static DIR* opendir(const char* dirname);
Expand Down
10 changes: 2 additions & 8 deletions src/hotspot/share/services/heapDumperCompression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,8 @@ char const* FileWriter::write_buf(char* buf, ssize_t size) {
assert(_fd >= 0, "Must be open");
assert(size > 0, "Must write at least one byte");

while (size > 0) {
ssize_t n = os::write(_fd, buf, (uint) size);
if (n <= 0) {
return os::strerror(errno);
}

buf += n;
size -= n;
if (!os::write(_fd, buf, (size_t)size)) {
return os::strerror(errno);
}

return NULL;
Expand Down

0 comments on commit a048a68

Please sign in to comment.