1/*
2 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24#include "gc/z/zErrno.hpp"
25#include "gc/z/zCPU.hpp"
26#include "gc/z/zNUMA.hpp"
27#include "runtime/os.hpp"
28#include "utilities/debug.hpp"
29
30#include <unistd.h>
31#include <sys/syscall.h>
32
33#ifndef MPOL_F_NODE
34#define MPOL_F_NODE (1<<0) // Return next IL mode instead of node mask
35#endif
36
37#ifndef MPOL_F_ADDR
38#define MPOL_F_ADDR (1<<1) // Look up VMA using address
39#endif
40
41static int z_get_mempolicy(uint32_t* mode, const unsigned long *nmask, unsigned long maxnode, uintptr_t addr, int flags) {
42 return syscall(SYS_get_mempolicy, mode, nmask, maxnode, addr, flags);
43}
44
45void ZNUMA::initialize_platform() {
46 _enabled = UseNUMA;
47}
48
49uint32_t ZNUMA::count() {
50 if (!_enabled) {
51 // NUMA support not enabled
52 return 1;
53 }
54
55 return os::Linux::numa_max_node() + 1;
56}
57
58uint32_t ZNUMA::id() {
59 if (!_enabled) {
60 // NUMA support not enabled
61 return 0;
62 }
63
64 return os::Linux::get_node_by_cpu(ZCPU::id());
65}
66
67uint32_t ZNUMA::memory_id(uintptr_t addr) {
68 if (!_enabled) {
69 // NUMA support not enabled, assume everything belongs to node zero
70 return 0;
71 }
72
73 uint32_t id = (uint32_t)-1;
74
75 if (z_get_mempolicy(&id, NULL, 0, addr, MPOL_F_NODE | MPOL_F_ADDR) == -1) {
76 ZErrno err;
77 fatal("Failed to get NUMA id for memory at " PTR_FORMAT " (%s)", addr, err.to_string());
78 }
79
80 assert(id < count(), "Invalid NUMA id");
81
82 return id;
83}
84