Skip to content

Commit 2c7fc85

Browse files
author
Thomas Schatzl
committed
8254972: Fix pretouch chunk calculations
Reviewed-by: sjohanss, kbarrett
1 parent d128191 commit 2c7fc85

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

src/hotspot/share/gc/shared/pretouchTask.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,47 +28,61 @@
2828
#include "runtime/globals.hpp"
2929
#include "runtime/os.hpp"
3030

31-
PretouchTask::PretouchTask(const char* task_name, char* start_address, char* end_address, size_t page_size) :
31+
PretouchTask::PretouchTask(const char* task_name,
32+
char* start_address,
33+
char* end_address,
34+
size_t page_size,
35+
size_t chunk_size) :
3236
AbstractGangTask(task_name),
3337
_cur_addr(start_address),
3438
_start_addr(start_address),
3539
_end_addr(end_address),
36-
_page_size(0) {
37-
#ifdef LINUX
38-
_page_size = UseTransparentHugePages ? (size_t)os::vm_page_size(): page_size;
39-
#else
40-
_page_size = page_size;
41-
#endif
40+
_page_size(page_size),
41+
_chunk_size(chunk_size) {
42+
43+
assert(chunk_size >= page_size,
44+
"Chunk size " SIZE_FORMAT " is smaller than page size " SIZE_FORMAT,
45+
chunk_size, page_size);
4246
}
4347

4448
size_t PretouchTask::chunk_size() {
4549
return PreTouchParallelChunkSize;
4650
}
4751

4852
void PretouchTask::work(uint worker_id) {
49-
size_t const actual_chunk_size = MAX2(chunk_size(), _page_size);
50-
5153
while (true) {
52-
char* touch_addr = Atomic::fetch_and_add(&_cur_addr, actual_chunk_size);
54+
char* touch_addr = Atomic::fetch_and_add(&_cur_addr, _chunk_size);
5355
if (touch_addr < _start_addr || touch_addr >= _end_addr) {
5456
break;
5557
}
5658

57-
char* end_addr = touch_addr + MIN2(actual_chunk_size, pointer_delta(_end_addr, touch_addr, sizeof(char)));
59+
char* end_addr = touch_addr + MIN2(_chunk_size, pointer_delta(_end_addr, touch_addr, sizeof(char)));
5860

5961
os::pretouch_memory(touch_addr, end_addr, _page_size);
6062
}
6163
}
6264

6365
void PretouchTask::pretouch(const char* task_name, char* start_address, char* end_address,
6466
size_t page_size, WorkGang* pretouch_gang) {
65-
PretouchTask task(task_name, start_address, end_address, page_size);
67+
68+
#ifdef LINUX
69+
// When using THP we need to always pre-touch using small pages as the OS will
70+
// initially always use small pages.
71+
page_size = UseTransparentHugePages ? (size_t)os::vm_page_size() : page_size;
72+
#endif
73+
size_t chunk_size = MAX2(PretouchTask::chunk_size(), page_size);
74+
75+
PretouchTask task(task_name, start_address, end_address, page_size, chunk_size);
6676
size_t total_bytes = pointer_delta(end_address, start_address, sizeof(char));
6777

78+
if (total_bytes == 0) {
79+
return;
80+
}
81+
6882
if (pretouch_gang != NULL) {
69-
size_t num_chunks = MAX2((size_t)1, total_bytes / MAX2(PretouchTask::chunk_size(), page_size));
83+
size_t num_chunks = (total_bytes + chunk_size - 1) / chunk_size;
7084

71-
uint num_workers = MIN2((uint)num_chunks, pretouch_gang->total_workers());
85+
uint num_workers = (uint)MIN2(num_chunks, (size_t)pretouch_gang->total_workers());
7286
log_debug(gc, heap)("Running %s with %u workers for " SIZE_FORMAT " work units pre-touching " SIZE_FORMAT "B.",
7387
task.name(), num_workers, num_chunks, total_bytes);
7488

src/hotspot/share/gc/shared/pretouchTask.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ class PretouchTask : public AbstractGangTask {
3232
char* const _start_addr;
3333
char* const _end_addr;
3434
size_t _page_size;
35+
size_t _chunk_size;
3536

3637
public:
37-
PretouchTask(const char* task_name, char* start_address, char* end_address, size_t page_size);
38+
PretouchTask(const char* task_name, char* start_address, char* end_address, size_t page_size, size_t chunk_size);
3839

3940
virtual void work(uint worker_id);
4041

0 commit comments

Comments
 (0)