src/hotspot/os_cpu/linux_x86/gc/z/zPhysicalMemoryBacking_linux_x86.cpp
author stefank
Mon, 26 Aug 2019 09:13:38 +0200
changeset 57875 427b38332f20
parent 54834 39ba09047e19
permissions -rw-r--r--
8229836: Remove include of globals.hpp from allocation.hpp Reviewed-by: coleenp, kbarrett
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     1
/*
54094
ed3c6f07faab 8219633: ZGC: Rename ZPageSizeMin to ZGranuleSize
pliden
parents: 50979
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     4
 *
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     7
 * published by the Free Software Foundation.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     8
 *
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    13
 * accompanied this code).
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    14
 *
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    18
 *
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    21
 * questions.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    22
 */
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    23
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    24
#include "precompiled.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    25
#include "gc/z/zAddress.inline.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    26
#include "gc/z/zBackingFile_linux_x86.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    27
#include "gc/z/zErrno.hpp"
54094
ed3c6f07faab 8219633: ZGC: Rename ZPageSizeMin to ZGranuleSize
pliden
parents: 50979
diff changeset
    28
#include "gc/z/zGlobals.hpp"
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    29
#include "gc/z/zLargePages.inline.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    30
#include "gc/z/zMemory.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    31
#include "gc/z/zNUMA.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    32
#include "gc/z/zPhysicalMemory.inline.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    33
#include "gc/z/zPhysicalMemoryBacking_linux_x86.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    34
#include "logging/log.hpp"
57875
427b38332f20 8229836: Remove include of globals.hpp from allocation.hpp
stefank
parents: 54834
diff changeset
    35
#include "runtime/globals.hpp"
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    36
#include "runtime/init.hpp"
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    37
#include "runtime/os.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    38
#include "utilities/align.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    39
#include "utilities/debug.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    40
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    41
#include <stdio.h>
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    42
#include <sys/mman.h>
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    43
#include <sys/types.h>
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    44
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    45
//
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    46
// Support for building on older Linux systems
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    47
//
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    48
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    49
// madvise(2) flags
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    50
#ifndef MADV_HUGEPAGE
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    51
#define MADV_HUGEPAGE                        14
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    52
#endif
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    53
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    54
// Proc file entry for max map mount
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    55
#define ZFILENAME_PROC_MAX_MAP_COUNT         "/proc/sys/vm/max_map_count"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    56
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    57
bool ZPhysicalMemoryBacking::is_initialized() const {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    58
  return _file.is_initialized();
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    59
}
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    60
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    61
void ZPhysicalMemoryBacking::warn_available_space(size_t max) const {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    62
  // Note that the available space on a tmpfs or a hugetlbfs filesystem
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    63
  // will be zero if no size limit was specified when it was mounted.
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    64
  const size_t available = _file.available();
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    65
  if (available == 0) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    66
    // No size limit set, skip check
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    67
    log_info(gc, init)("Available space on backing filesystem: N/A");
50979
270b11dadbaf 8205924: ZGC: Premature OOME due to failure to expand backing file
pliden
parents: 50525
diff changeset
    68
    return;
270b11dadbaf 8205924: ZGC: Premature OOME due to failure to expand backing file
pliden
parents: 50525
diff changeset
    69
  }
270b11dadbaf 8205924: ZGC: Premature OOME due to failure to expand backing file
pliden
parents: 50525
diff changeset
    70
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    71
  log_info(gc, init)("Available space on backing filesystem: " SIZE_FORMAT "M", available / M);
50979
270b11dadbaf 8205924: ZGC: Premature OOME due to failure to expand backing file
pliden
parents: 50525
diff changeset
    72
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    73
  // Warn if the filesystem doesn't currently have enough space available to hold
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    74
  // the max heap size. The max heap size will be capped if we later hit this limit
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    75
  // when trying to expand the heap.
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    76
  if (available < max) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    77
    log_warning(gc)("***** WARNING! INCORRECT SYSTEM CONFIGURATION DETECTED! *****");
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    78
    log_warning(gc)("Not enough space available on the backing filesystem to hold the current max Java heap");
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    79
    log_warning(gc)("size (" SIZE_FORMAT "M). Please adjust the size of the backing filesystem accordingly "
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    80
                    "(available", max / M);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    81
    log_warning(gc)("space is currently " SIZE_FORMAT "M). Continuing execution with the current filesystem "
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    82
                    "size could", available / M);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    83
    log_warning(gc)("lead to a premature OutOfMemoryError being thrown, due to failure to map memory.");
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    84
  }
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    85
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    86
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
    87
void ZPhysicalMemoryBacking::warn_max_map_count(size_t max) const {
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    88
  const char* const filename = ZFILENAME_PROC_MAX_MAP_COUNT;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    89
  FILE* const file = fopen(filename, "r");
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    90
  if (file == NULL) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    91
    // Failed to open file, skip check
50979
270b11dadbaf 8205924: ZGC: Premature OOME due to failure to expand backing file
pliden
parents: 50525
diff changeset
    92
    log_debug(gc, init)("Failed to open %s", filename);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    93
    return;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    94
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    95
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    96
  size_t actual_max_map_count = 0;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    97
  const int result = fscanf(file, SIZE_FORMAT, &actual_max_map_count);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    98
  fclose(file);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    99
  if (result != 1) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   100
    // Failed to read file, skip check
50979
270b11dadbaf 8205924: ZGC: Premature OOME due to failure to expand backing file
pliden
parents: 50525
diff changeset
   101
    log_debug(gc, init)("Failed to read %s", filename);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   102
    return;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   103
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   104
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   105
  // The required max map count is impossible to calculate exactly since subsystems
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   106
  // other than ZGC are also creating memory mappings, and we have no control over that.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   107
  // However, ZGC tends to create the most mappings and dominate the total count.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   108
  // In the worst cases, ZGC will map each granule three times, i.e. once per heap view.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   109
  // We speculate that we need another 20% to allow for non-ZGC subsystems to map memory.
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   110
  const size_t required_max_map_count = (max / ZGranuleSize) * 3 * 1.2;
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   111
  if (actual_max_map_count < required_max_map_count) {
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   112
    log_warning(gc)("***** WARNING! INCORRECT SYSTEM CONFIGURATION DETECTED! *****");
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   113
    log_warning(gc)("The system limit on number of memory mappings per process might be too low for the given");
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   114
    log_warning(gc)("max Java heap size (" SIZE_FORMAT "M). Please adjust %s to allow for at",
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   115
                    max / M, filename);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   116
    log_warning(gc)("least " SIZE_FORMAT " mappings (current limit is " SIZE_FORMAT "). Continuing execution "
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   117
                    "with the current", required_max_map_count, actual_max_map_count);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   118
    log_warning(gc)("limit could lead to a fatal error, due to failure to map memory.");
50979
270b11dadbaf 8205924: ZGC: Premature OOME due to failure to expand backing file
pliden
parents: 50525
diff changeset
   119
  }
270b11dadbaf 8205924: ZGC: Premature OOME due to failure to expand backing file
pliden
parents: 50525
diff changeset
   120
}
270b11dadbaf 8205924: ZGC: Premature OOME due to failure to expand backing file
pliden
parents: 50525
diff changeset
   121
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   122
void ZPhysicalMemoryBacking::warn_commit_limits(size_t max) const {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   123
  // Warn if available space is too low
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   124
  warn_available_space(max);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   125
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   126
  // Warn if max map count is too low
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   127
  warn_max_map_count(max);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   128
}
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   129
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   130
bool ZPhysicalMemoryBacking::supports_uncommit() {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   131
  assert(!is_init_completed(), "Invalid state");
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   132
  assert(_file.size() >= ZGranuleSize, "Invalid size");
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   133
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   134
  // Test if uncommit is supported by uncommitting and then re-committing a granule
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   135
  return commit(uncommit(ZGranuleSize)) == ZGranuleSize;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   136
}
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   137
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   138
size_t ZPhysicalMemoryBacking::commit(size_t size) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   139
  size_t committed = 0;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   140
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   141
  // Fill holes in the backing file
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   142
  while (committed < size) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   143
    size_t allocated = 0;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   144
    const size_t remaining = size - committed;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   145
    const uintptr_t start = _uncommitted.alloc_from_front_at_most(remaining, &allocated);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   146
    if (start == UINTPTR_MAX) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   147
      // No holes to commit
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   148
      break;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   149
    }
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   150
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   151
    // Try commit hole
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   152
    const size_t filled = _file.commit(start, allocated);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   153
    if (filled > 0) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   154
      // Successful or partialy successful
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   155
      _committed.free(start, filled);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   156
      committed += filled;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   157
    }
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   158
    if (filled < allocated) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   159
      // Failed or partialy failed
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   160
      _uncommitted.free(start + filled, allocated - filled);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   161
      return committed;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   162
    }
50979
270b11dadbaf 8205924: ZGC: Premature OOME due to failure to expand backing file
pliden
parents: 50525
diff changeset
   163
  }
270b11dadbaf 8205924: ZGC: Premature OOME due to failure to expand backing file
pliden
parents: 50525
diff changeset
   164
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   165
  // Expand backing file
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   166
  if (committed < size) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   167
    const size_t remaining = size - committed;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   168
    const uintptr_t start = _file.size();
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   169
    const size_t expanded = _file.commit(start, remaining);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   170
    if (expanded > 0) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   171
      // Successful or partialy successful
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   172
      _committed.free(start, expanded);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   173
      committed += expanded;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   174
    }
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   175
  }
50979
270b11dadbaf 8205924: ZGC: Premature OOME due to failure to expand backing file
pliden
parents: 50525
diff changeset
   176
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   177
  return committed;
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   178
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   179
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   180
size_t ZPhysicalMemoryBacking::uncommit(size_t size) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   181
  size_t uncommitted = 0;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   182
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   183
  // Punch holes in backing file
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   184
  while (uncommitted < size) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   185
    size_t allocated = 0;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   186
    const size_t remaining = size - uncommitted;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   187
    const uintptr_t start = _committed.alloc_from_back_at_most(remaining, &allocated);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   188
    assert(start != UINTPTR_MAX, "Allocation should never fail");
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   189
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   190
    // Try punch hole
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   191
    const size_t punched = _file.uncommit(start, allocated);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   192
    if (punched > 0) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   193
      // Successful or partialy successful
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   194
      _uncommitted.free(start, punched);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   195
      uncommitted += punched;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   196
    }
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   197
    if (punched < allocated) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   198
      // Failed or partialy failed
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   199
      _committed.free(start + punched, allocated - punched);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   200
      return uncommitted;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   201
    }
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   202
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   203
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   204
  return uncommitted;
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   205
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   206
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   207
ZPhysicalMemory ZPhysicalMemoryBacking::alloc(size_t size) {
54094
ed3c6f07faab 8219633: ZGC: Rename ZPageSizeMin to ZGranuleSize
pliden
parents: 50979
diff changeset
   208
  assert(is_aligned(size, ZGranuleSize), "Invalid size");
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   209
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   210
  ZPhysicalMemory pmem;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   211
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   212
  // Allocate segments
54094
ed3c6f07faab 8219633: ZGC: Rename ZPageSizeMin to ZGranuleSize
pliden
parents: 50979
diff changeset
   213
  for (size_t allocated = 0; allocated < size; allocated += ZGranuleSize) {
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   214
    const uintptr_t start = _committed.alloc_from_front(ZGranuleSize);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   215
    assert(start != UINTPTR_MAX, "Allocation should never fail");
54094
ed3c6f07faab 8219633: ZGC: Rename ZPageSizeMin to ZGranuleSize
pliden
parents: 50979
diff changeset
   216
    pmem.add_segment(ZPhysicalMemorySegment(start, ZGranuleSize));
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   217
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   218
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   219
  return pmem;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   220
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   221
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   222
void ZPhysicalMemoryBacking::free(const ZPhysicalMemory& pmem) {
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   223
  const size_t nsegments = pmem.nsegments();
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   224
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   225
  // Free segments
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   226
  for (size_t i = 0; i < nsegments; i++) {
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   227
    const ZPhysicalMemorySegment& segment = pmem.segment(i);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   228
    _committed.free(segment.start(), segment.size());
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   229
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   230
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   231
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   232
void ZPhysicalMemoryBacking::map_failed(ZErrno err) const {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   233
  if (err == ENOMEM) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   234
    fatal("Failed to map memory. Please check the system limit on number of "
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   235
          "memory mappings allowed per process (see %s)", ZFILENAME_PROC_MAX_MAP_COUNT);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   236
  } else {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   237
    fatal("Failed to map memory (%s)", err.to_string());
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   238
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   239
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   240
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   241
void ZPhysicalMemoryBacking::advise_view(uintptr_t addr, size_t size, int advice) const {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   242
  if (madvise((void*)addr, size, advice) == -1) {
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   243
    ZErrno err;
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   244
    log_error(gc)("Failed to advise on memory (advice %d, %s)", advice, err.to_string());
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   245
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   246
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   247
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   248
void ZPhysicalMemoryBacking::pretouch_view(uintptr_t addr, size_t size) const {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   249
  const size_t page_size = ZLargePages::is_explicit() ? os::large_page_size() : os::vm_page_size();
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   250
  os::pretouch_memory((void*)addr, (void*)(addr + size), page_size);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   251
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   252
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   253
void ZPhysicalMemoryBacking::map_view(const ZPhysicalMemory& pmem, uintptr_t addr, bool pretouch) const {
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   254
  const size_t nsegments = pmem.nsegments();
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   255
  size_t size = 0;
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   256
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   257
  // Map segments
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   258
  for (size_t i = 0; i < nsegments; i++) {
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   259
    const ZPhysicalMemorySegment& segment = pmem.segment(i);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   260
    const uintptr_t segment_addr = addr + size;
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   261
    const void* const res = mmap((void*)segment_addr, segment.size(), PROT_READ|PROT_WRITE, MAP_FIXED|MAP_SHARED, _file.fd(), segment.start());
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   262
    if (res == MAP_FAILED) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   263
      ZErrno err;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   264
      map_failed(err);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   265
    }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   266
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   267
    size += segment.size();
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   268
  }
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   269
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   270
  // Advise on use of transparent huge pages before touching it
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   271
  if (ZLargePages::is_transparent()) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   272
    advise_view(addr, size, MADV_HUGEPAGE);
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   273
  }
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   274
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   275
  // NUMA interleave memory before touching it
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   276
  ZNUMA::memory_interleave(addr, size);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   277
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   278
  // Pre-touch memory
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   279
  if (pretouch) {
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   280
    pretouch_view(addr, size);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   281
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   282
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   283
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   284
void ZPhysicalMemoryBacking::unmap_view(const ZPhysicalMemory& pmem, uintptr_t addr) const {
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   285
  // Note that we must keep the address space reservation intact and just detach
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   286
  // the backing memory. For this reason we map a new anonymous, non-accessible
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   287
  // and non-reserved page over the mapping instead of actually unmapping.
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   288
  const void* const res = mmap((void*)addr, pmem.size(), PROT_NONE, MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE|MAP_NORESERVE, -1, 0);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   289
  if (res == MAP_FAILED) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   290
    ZErrno err;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   291
    map_failed(err);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   292
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   293
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   294
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   295
uintptr_t ZPhysicalMemoryBacking::nmt_address(uintptr_t offset) const {
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   296
  // From an NMT point of view we treat the first heap view (marked0) as committed
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   297
  return ZAddress::marked0(offset);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   298
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   299
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   300
void ZPhysicalMemoryBacking::map(const ZPhysicalMemory& pmem, uintptr_t offset) const {
54159
7c23a4432610 8220569: ZGC: Rename and rework ZUnmapBadViews to ZVerifyViews
pliden
parents: 54094
diff changeset
   301
  if (ZVerifyViews) {
7c23a4432610 8220569: ZGC: Rename and rework ZUnmapBadViews to ZVerifyViews
pliden
parents: 54094
diff changeset
   302
    // Map good view
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   303
    map_view(pmem, ZAddress::good(offset), AlwaysPreTouch);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   304
  } else {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   305
    // Map all views
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   306
    map_view(pmem, ZAddress::marked0(offset), AlwaysPreTouch);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   307
    map_view(pmem, ZAddress::marked1(offset), AlwaysPreTouch);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   308
    map_view(pmem, ZAddress::remapped(offset), AlwaysPreTouch);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   309
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   310
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   311
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   312
void ZPhysicalMemoryBacking::unmap(const ZPhysicalMemory& pmem, uintptr_t offset) const {
54159
7c23a4432610 8220569: ZGC: Rename and rework ZUnmapBadViews to ZVerifyViews
pliden
parents: 54094
diff changeset
   313
  if (ZVerifyViews) {
7c23a4432610 8220569: ZGC: Rename and rework ZUnmapBadViews to ZVerifyViews
pliden
parents: 54094
diff changeset
   314
    // Unmap good view
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   315
    unmap_view(pmem, ZAddress::good(offset));
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   316
  } else {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   317
    // Unmap all views
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   318
    unmap_view(pmem, ZAddress::marked0(offset));
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   319
    unmap_view(pmem, ZAddress::marked1(offset));
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   320
    unmap_view(pmem, ZAddress::remapped(offset));
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   321
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   322
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   323
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   324
void ZPhysicalMemoryBacking::debug_map(const ZPhysicalMemory& pmem, uintptr_t offset) const {
54159
7c23a4432610 8220569: ZGC: Rename and rework ZUnmapBadViews to ZVerifyViews
pliden
parents: 54094
diff changeset
   325
  // Map good view
7c23a4432610 8220569: ZGC: Rename and rework ZUnmapBadViews to ZVerifyViews
pliden
parents: 54094
diff changeset
   326
  assert(ZVerifyViews, "Should be enabled");
7c23a4432610 8220569: ZGC: Rename and rework ZUnmapBadViews to ZVerifyViews
pliden
parents: 54094
diff changeset
   327
  map_view(pmem, ZAddress::good(offset), false /* pretouch */);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   328
}
54159
7c23a4432610 8220569: ZGC: Rename and rework ZUnmapBadViews to ZVerifyViews
pliden
parents: 54094
diff changeset
   329
54834
39ba09047e19 8222480: Implementation: JEP 351: ZGC: Uncommit Unused Memory
pliden
parents: 54159
diff changeset
   330
void ZPhysicalMemoryBacking::debug_unmap(const ZPhysicalMemory& pmem, uintptr_t offset) const {
54159
7c23a4432610 8220569: ZGC: Rename and rework ZUnmapBadViews to ZVerifyViews
pliden
parents: 54094
diff changeset
   331
  // Unmap good view
7c23a4432610 8220569: ZGC: Rename and rework ZUnmapBadViews to ZVerifyViews
pliden
parents: 54094
diff changeset
   332
  assert(ZVerifyViews, "Should be enabled");
7c23a4432610 8220569: ZGC: Rename and rework ZUnmapBadViews to ZVerifyViews
pliden
parents: 54094
diff changeset
   333
  unmap_view(pmem, ZAddress::good(offset));
7c23a4432610 8220569: ZGC: Rename and rework ZUnmapBadViews to ZVerifyViews
pliden
parents: 54094
diff changeset
   334
}