Skip to content

Commit 298bce1

Browse files
committed
8256367: [windows] Better logging for some system calls
Reviewed-by: iklam
1 parent 1e9a432 commit 298bce1

File tree

1 file changed

+107
-21
lines changed

1 file changed

+107
-21
lines changed

src/hotspot/os/windows/os_windows.cpp

Lines changed: 107 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,91 @@ static inline double fileTimeAsDouble(FILETIME* time) {
178178

179179
// Implementation of os
180180

181+
#define RANGE_FORMAT "[" PTR_FORMAT "-" PTR_FORMAT ")"
182+
#define RANGE_FORMAT_ARGS(p, len) p2i(p), p2i((address)p + len)
183+
184+
// A number of wrappers for more frequently used system calls, to add standard logging.
185+
186+
struct PreserveLastError {
187+
const DWORD v;
188+
PreserveLastError() : v(::GetLastError()) {}
189+
~PreserveLastError() { ::SetLastError(v); }
190+
};
191+
192+
// Logging wrapper for VirtualAlloc
193+
static LPVOID virtualAlloc(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect) {
194+
LPVOID result = ::VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect);
195+
if (result != NULL) {
196+
log_trace(os)("VirtualAlloc(" PTR_FORMAT ", " SIZE_FORMAT ", %x, %x) returned " PTR_FORMAT "%s.",
197+
p2i(lpAddress), dwSize, flAllocationType, flProtect, p2i(result),
198+
((lpAddress != NULL && result != lpAddress) ? " <different base!>" : ""));
199+
} else {
200+
PreserveLastError ple;
201+
log_info(os)("VirtualAlloc(" PTR_FORMAT ", " SIZE_FORMAT ", %x, %x) failed (%u).",
202+
p2i(lpAddress), dwSize, flAllocationType, flProtect, ple.v);
203+
}
204+
return result;
205+
}
206+
207+
// Logging wrapper for VirtualFree
208+
static BOOL virtualFree(LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType) {
209+
BOOL result = ::VirtualFree(lpAddress, dwSize, dwFreeType);
210+
if (result != FALSE) {
211+
log_trace(os)("VirtualFree(" PTR_FORMAT ", " SIZE_FORMAT ", %x) succeeded",
212+
p2i(lpAddress), dwSize, dwFreeType);
213+
} else {
214+
PreserveLastError ple;
215+
log_info(os)("VirtualFree(" PTR_FORMAT ", " SIZE_FORMAT ", %x) failed (%u).",
216+
p2i(lpAddress), dwSize, dwFreeType, ple.v);
217+
}
218+
return result;
219+
}
220+
221+
// Logging wrapper for VirtualAllocExNuma
222+
static LPVOID virtualAllocExNuma(HANDLE hProcess, LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType,
223+
DWORD flProtect, DWORD nndPreferred) {
224+
LPVOID result = ::VirtualAllocExNuma(hProcess, lpAddress, dwSize, flAllocationType, flProtect, nndPreferred);
225+
if (result != NULL) {
226+
log_trace(os)("VirtualAllocExNuma(" PTR_FORMAT ", " SIZE_FORMAT ", %x, %x, %x) returned " PTR_FORMAT "%s.",
227+
p2i(lpAddress), dwSize, flAllocationType, flProtect, nndPreferred, p2i(result),
228+
((lpAddress != NULL && result != lpAddress) ? " <different base!>" : ""));
229+
} else {
230+
PreserveLastError ple;
231+
log_info(os)("VirtualAllocExNuma(" PTR_FORMAT ", " SIZE_FORMAT ", %x, %x, %x) failed (%u).",
232+
p2i(lpAddress), dwSize, flAllocationType, flProtect, nndPreferred, ple.v);
233+
}
234+
return result;
235+
}
236+
237+
// Logging wrapper for MapViewOfFileEx
238+
static LPVOID mapViewOfFileEx(HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh,
239+
DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap, LPVOID lpBaseAddress) {
240+
LPVOID result = ::MapViewOfFileEx(hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh,
241+
dwFileOffsetLow, dwNumberOfBytesToMap, lpBaseAddress);
242+
if (result != NULL) {
243+
log_trace(os)("MapViewOfFileEx(" PTR_FORMAT ", " SIZE_FORMAT ") returned " PTR_FORMAT "%s.",
244+
p2i(lpBaseAddress), dwNumberOfBytesToMap, p2i(result),
245+
((lpBaseAddress != NULL && result != lpBaseAddress) ? " <different base!>" : ""));
246+
} else {
247+
PreserveLastError ple;
248+
log_info(os)("MapViewOfFileEx(" PTR_FORMAT ", " SIZE_FORMAT ") failed (%u).",
249+
p2i(lpBaseAddress), dwNumberOfBytesToMap, ple.v);
250+
}
251+
return result;
252+
}
253+
254+
// Logging wrapper for UnmapViewOfFile
255+
static BOOL unmapViewOfFile(LPCVOID lpBaseAddress) {
256+
BOOL result = ::UnmapViewOfFile(lpBaseAddress);
257+
if (result != FALSE) {
258+
log_trace(os)("UnmapViewOfFile(" PTR_FORMAT ") succeeded", p2i(lpBaseAddress));
259+
} else {
260+
PreserveLastError ple;
261+
log_info(os)("UnmapViewOfFile(" PTR_FORMAT ") failed (%u).", p2i(lpBaseAddress), ple.v);
262+
}
263+
return result;
264+
}
265+
181266
bool os::unsetenv(const char* name) {
182267
assert(name != NULL, "Null pointer");
183268
return (SetEnvironmentVariable(name, NULL) == TRUE);
@@ -340,7 +425,6 @@ int os::get_native_stack(address* stack, int frames, int toSkip) {
340425
return captured;
341426
}
342427

343-
344428
// os::current_stack_base()
345429
//
346430
// Returns the base of the stack, which is the stack's
@@ -2900,7 +2984,7 @@ static char* allocate_pages_individually(size_t bytes, char* addr, DWORD flags,
29002984
// Overflowed.
29012985
return NULL;
29022986
}
2903-
p_buf = (char *) VirtualAlloc(addr,
2987+
p_buf = (char *) virtualAlloc(addr,
29042988
size_of_reserve, // size of Reserve
29052989
MEM_RESERVE,
29062990
PAGE_READWRITE);
@@ -2946,15 +3030,15 @@ static char* allocate_pages_individually(size_t bytes, char* addr, DWORD flags,
29463030
p_new = NULL;
29473031
} else {
29483032
if (!UseNUMAInterleaving) {
2949-
p_new = (char *) VirtualAlloc(next_alloc_addr,
3033+
p_new = (char *) virtualAlloc(next_alloc_addr,
29503034
bytes_to_rq,
29513035
flags,
29523036
prot);
29533037
} else {
29543038
// get the next node to use from the used_node_list
29553039
assert(numa_node_list_holder.get_count() > 0, "Multiple NUMA nodes expected");
29563040
DWORD node = numa_node_list_holder.get_node_list_entry(count % numa_node_list_holder.get_count());
2957-
p_new = (char *)VirtualAllocExNuma(hProc, next_alloc_addr, bytes_to_rq, flags, prot, node);
3041+
p_new = (char *)virtualAllocExNuma(hProc, next_alloc_addr, bytes_to_rq, flags, prot, node);
29583042
}
29593043
}
29603044

@@ -3100,7 +3184,7 @@ char* os::map_memory_to_file(char* base, size_t size, int fd) {
31003184
return NULL;
31013185
}
31023186

3103-
LPVOID addr = MapViewOfFileEx(fileMapping, FILE_MAP_WRITE, 0, 0, size, base);
3187+
LPVOID addr = mapViewOfFileEx(fileMapping, FILE_MAP_WRITE, 0, 0, size, base);
31043188

31053189
CloseHandle(fileMapping);
31063190

@@ -3127,14 +3211,19 @@ void os::split_reserved_memory(char *base, size_t size, size_t split) {
31273211
assert(is_aligned(base, os::vm_allocation_granularity()), "Sanity");
31283212
assert(is_aligned(split_address, os::vm_allocation_granularity()), "Sanity");
31293213

3130-
release_memory(base, size);
3131-
attempt_reserve_memory_at(base, split);
3132-
attempt_reserve_memory_at(split_address, size - split);
3214+
const bool rc = release_memory(base, size) &&
3215+
(attempt_reserve_memory_at(base, split) != NULL) &&
3216+
(attempt_reserve_memory_at(split_address, size - split) != NULL);
3217+
if (!rc) {
3218+
log_warning(os)("os::split_reserved_memory failed for [" RANGE_FORMAT ")",
3219+
RANGE_FORMAT_ARGS(base, size));
3220+
assert(false, "os::split_reserved_memory failed for [" RANGE_FORMAT ")",
3221+
RANGE_FORMAT_ARGS(base, size));
3222+
}
31333223

31343224
// NMT: nothing to do here. Since Windows implements the split by
31353225
// releasing and re-reserving memory, the parts are already registered
31363226
// as individual mappings with NMT.
3137-
31383227
}
31393228

31403229
// Multiple threads can race in this code but it's not possible to unmap small sections of
@@ -3198,7 +3287,7 @@ char* os::pd_attempt_reserve_memory_at(char* addr, size_t bytes) {
31983287
// will go thru reserve_memory_special rather than thru here.
31993288
bool use_individual = (UseNUMAInterleaving && !UseLargePages);
32003289
if (!use_individual) {
3201-
res = (char*)VirtualAlloc(addr, bytes, MEM_RESERVE, PAGE_READWRITE);
3290+
res = (char*)virtualAlloc(addr, bytes, MEM_RESERVE, PAGE_READWRITE);
32023291
} else {
32033292
elapsedTimer reserveTimer;
32043293
if (Verbose && PrintMiscellaneous) reserveTimer.start();
@@ -3277,7 +3366,7 @@ char* os::pd_reserve_memory_special(size_t bytes, size_t alignment, char* addr,
32773366

32783367
// normal policy just allocate it all at once
32793368
DWORD flag = MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES;
3280-
char * res = (char *)VirtualAlloc(addr, bytes, flag, prot);
3369+
char * res = (char *)virtualAlloc(addr, bytes, flag, prot);
32813370

32823371
return res;
32833372
}
@@ -3314,7 +3403,7 @@ bool os::pd_commit_memory(char* addr, size_t bytes, bool exec) {
33143403
// is always within a reserve covered by a single VirtualAlloc
33153404
// in that case we can just do a single commit for the requested size
33163405
if (!UseNUMAInterleaving) {
3317-
if (VirtualAlloc(addr, bytes, MEM_COMMIT, PAGE_READWRITE) == NULL) {
3406+
if (virtualAlloc(addr, bytes, MEM_COMMIT, PAGE_READWRITE) == NULL) {
33183407
NOT_PRODUCT(warn_fail_commit_memory(addr, bytes, exec);)
33193408
return false;
33203409
}
@@ -3339,7 +3428,7 @@ bool os::pd_commit_memory(char* addr, size_t bytes, bool exec) {
33393428
MEMORY_BASIC_INFORMATION alloc_info;
33403429
VirtualQuery(next_alloc_addr, &alloc_info, sizeof(alloc_info));
33413430
size_t bytes_to_rq = MIN2(bytes_remaining, (size_t)alloc_info.RegionSize);
3342-
if (VirtualAlloc(next_alloc_addr, bytes_to_rq, MEM_COMMIT,
3431+
if (virtualAlloc(next_alloc_addr, bytes_to_rq, MEM_COMMIT,
33433432
PAGE_READWRITE) == NULL) {
33443433
NOT_PRODUCT(warn_fail_commit_memory(next_alloc_addr, bytes_to_rq,
33453434
exec);)
@@ -3391,11 +3480,11 @@ bool os::pd_uncommit_memory(char* addr, size_t bytes) {
33913480
}
33923481
assert((size_t) addr % os::vm_page_size() == 0, "uncommit on page boundaries");
33933482
assert(bytes % os::vm_page_size() == 0, "uncommit in page-sized chunks");
3394-
return (VirtualFree(addr, bytes, MEM_DECOMMIT) != 0);
3483+
return (virtualFree(addr, bytes, MEM_DECOMMIT) == TRUE);
33953484
}
33963485

33973486
bool os::pd_release_memory(char* addr, size_t bytes) {
3398-
return VirtualFree(addr, 0, MEM_RELEASE) != 0;
3487+
return virtualFree(addr, 0, MEM_RELEASE) != 0;
33993488
}
34003489

34013490
bool os::pd_create_stack_guard_pages(char* addr, size_t size) {
@@ -4941,10 +5030,9 @@ char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset,
49415030
// we might consider DLLizing the shared archive with a proper PE
49425031
// header so that mapping executable + sharing is possible.
49435032

4944-
base = (char*) VirtualAlloc(addr, bytes, MEM_COMMIT | MEM_RESERVE,
5033+
base = (char*) virtualAlloc(addr, bytes, MEM_COMMIT | MEM_RESERVE,
49455034
PAGE_READWRITE);
49465035
if (base == NULL) {
4947-
log_info(os)("VirtualAlloc() failed: GetLastError->%ld.", GetLastError());
49485036
CloseHandle(hFile);
49495037
return NULL;
49505038
}
@@ -4976,10 +5064,9 @@ char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset,
49765064
}
49775065

49785066
DWORD access = read_only ? FILE_MAP_READ : FILE_MAP_COPY;
4979-
base = (char*)MapViewOfFileEx(hMap, access, 0, (DWORD)file_offset,
5067+
base = (char*)mapViewOfFileEx(hMap, access, 0, (DWORD)file_offset,
49805068
(DWORD)bytes, addr);
49815069
if (base == NULL) {
4982-
log_info(os)("MapViewOfFileEx() failed: GetLastError->%ld.", GetLastError());
49835070
CloseHandle(hMap);
49845071
CloseHandle(hFile);
49855072
return NULL;
@@ -5051,9 +5138,8 @@ bool os::pd_unmap_memory(char* addr, size_t bytes) {
50515138
return pd_release_memory(addr, bytes);
50525139
}
50535140

5054-
BOOL result = UnmapViewOfFile(addr);
5141+
BOOL result = unmapViewOfFile(addr);
50555142
if (result == 0) {
5056-
log_info(os)("UnmapViewOfFile() failed: GetLastError->%ld.", GetLastError());
50575143
return false;
50585144
}
50595145
return true;

0 commit comments

Comments
 (0)