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