Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 1 | /* |
| 2 | * NUMA parameter parsing routines |
| 3 | * |
| 4 | * Copyright (c) 2014 Fujitsu Ltd. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "sysemu/sysemu.h" |
| 26 | #include "exec/cpu-common.h" |
| 27 | #include "qemu/bitmap.h" |
| 28 | #include "qom/cpu.h" |
Wanlong Gao | 2b631ec | 2014-05-14 17:43:06 +0800 | [diff] [blame] | 29 | #include "qemu/error-report.h" |
| 30 | #include "include/exec/cpu-common.h" /* for RAM_ADDR_FMT */ |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame^] | 31 | #include "qapi-visit.h" |
| 32 | #include "qapi/opts-visitor.h" |
| 33 | #include "qapi/dealloc-visitor.h" |
| 34 | #include "qapi/qmp/qerror.h" |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 35 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame^] | 36 | QemuOptsList qemu_numa_opts = { |
| 37 | .name = "numa", |
| 38 | .implied_opt_name = "type", |
| 39 | .head = QTAILQ_HEAD_INITIALIZER(qemu_numa_opts.head), |
| 40 | .desc = { { 0 } } /* validated with OptsVisitor */ |
| 41 | }; |
| 42 | |
| 43 | static void numa_node_parse(NumaNodeOptions *node, QemuOpts *opts, Error **errp) |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 44 | { |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame^] | 45 | uint16_t nodenr; |
| 46 | uint16List *cpus = NULL; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 47 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame^] | 48 | if (node->has_nodeid) { |
| 49 | nodenr = node->nodeid; |
| 50 | } else { |
| 51 | nodenr = nb_numa_nodes; |
| 52 | } |
| 53 | |
| 54 | if (nodenr >= MAX_NODES) { |
| 55 | error_setg(errp, "Max number of NUMA nodes reached: %" |
| 56 | PRIu16 "\n", nodenr); |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame^] | 60 | for (cpus = node->cpus; cpus; cpus = cpus->next) { |
| 61 | if (cpus->value > MAX_CPUMASK_BITS) { |
| 62 | error_setg(errp, "CPU number %" PRIu16 " is bigger than %d", |
| 63 | cpus->value, MAX_CPUMASK_BITS); |
| 64 | return; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 65 | } |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame^] | 66 | bitmap_set(numa_info[nodenr].node_cpu, cpus->value, 1); |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 67 | } |
| 68 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame^] | 69 | if (node->has_mem) { |
| 70 | uint64_t mem_size = node->mem; |
| 71 | const char *mem_str = qemu_opt_get(opts, "mem"); |
| 72 | /* Fix up legacy suffix-less format */ |
| 73 | if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) { |
| 74 | mem_size <<= 20; |
| 75 | } |
| 76 | numa_info[nodenr].node_mem = mem_size; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 77 | } |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 78 | } |
| 79 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame^] | 80 | int numa_init_func(QemuOpts *opts, void *opaque) |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 81 | { |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame^] | 82 | NumaOptions *object = NULL; |
| 83 | Error *err = NULL; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 84 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame^] | 85 | { |
| 86 | OptsVisitor *ov = opts_visitor_new(opts); |
| 87 | visit_type_NumaOptions(opts_get_visitor(ov), &object, NULL, &err); |
| 88 | opts_visitor_cleanup(ov); |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 89 | } |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 90 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame^] | 91 | if (err) { |
| 92 | goto error; |
| 93 | } |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 94 | |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame^] | 95 | switch (object->kind) { |
| 96 | case NUMA_OPTIONS_KIND_NODE: |
| 97 | numa_node_parse(object->node, opts, &err); |
| 98 | if (err) { |
| 99 | goto error; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 100 | } |
| 101 | nb_numa_nodes++; |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame^] | 102 | break; |
| 103 | default: |
| 104 | abort(); |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 105 | } |
Wanlong Gao | 0042109 | 2014-05-14 17:43:08 +0800 | [diff] [blame^] | 106 | |
| 107 | return 0; |
| 108 | |
| 109 | error: |
| 110 | qerror_report_err(err); |
| 111 | error_free(err); |
| 112 | |
| 113 | if (object) { |
| 114 | QapiDeallocVisitor *dv = qapi_dealloc_visitor_new(); |
| 115 | visit_type_NumaOptions(qapi_dealloc_get_visitor(dv), |
| 116 | &object, NULL, NULL); |
| 117 | qapi_dealloc_visitor_cleanup(dv); |
| 118 | } |
| 119 | |
| 120 | return -1; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void set_numa_nodes(void) |
| 124 | { |
| 125 | if (nb_numa_nodes > 0) { |
Wanlong Gao | 2b631ec | 2014-05-14 17:43:06 +0800 | [diff] [blame] | 126 | uint64_t numa_total; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 127 | int i; |
| 128 | |
| 129 | if (nb_numa_nodes > MAX_NODES) { |
| 130 | nb_numa_nodes = MAX_NODES; |
| 131 | } |
| 132 | |
| 133 | /* If no memory size if given for any node, assume the default case |
| 134 | * and distribute the available memory equally across all nodes |
| 135 | */ |
| 136 | for (i = 0; i < nb_numa_nodes; i++) { |
Wanlong Gao | 8c85901 | 2014-05-14 17:43:07 +0800 | [diff] [blame] | 137 | if (numa_info[i].node_mem != 0) { |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 138 | break; |
| 139 | } |
| 140 | } |
| 141 | if (i == nb_numa_nodes) { |
| 142 | uint64_t usedmem = 0; |
| 143 | |
| 144 | /* On Linux, the each node's border has to be 8MB aligned, |
| 145 | * the final node gets the rest. |
| 146 | */ |
| 147 | for (i = 0; i < nb_numa_nodes - 1; i++) { |
Wanlong Gao | 8c85901 | 2014-05-14 17:43:07 +0800 | [diff] [blame] | 148 | numa_info[i].node_mem = (ram_size / nb_numa_nodes) & |
| 149 | ~((1 << 23UL) - 1); |
| 150 | usedmem += numa_info[i].node_mem; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 151 | } |
Wanlong Gao | 8c85901 | 2014-05-14 17:43:07 +0800 | [diff] [blame] | 152 | numa_info[i].node_mem = ram_size - usedmem; |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 153 | } |
| 154 | |
Wanlong Gao | 2b631ec | 2014-05-14 17:43:06 +0800 | [diff] [blame] | 155 | numa_total = 0; |
| 156 | for (i = 0; i < nb_numa_nodes; i++) { |
Wanlong Gao | 8c85901 | 2014-05-14 17:43:07 +0800 | [diff] [blame] | 157 | numa_total += numa_info[i].node_mem; |
Wanlong Gao | 2b631ec | 2014-05-14 17:43:06 +0800 | [diff] [blame] | 158 | } |
| 159 | if (numa_total != ram_size) { |
| 160 | error_report("total memory for NUMA nodes (%" PRIu64 ")" |
| 161 | " should equal RAM size (" RAM_ADDR_FMT ")", |
| 162 | numa_total, ram_size); |
| 163 | exit(1); |
| 164 | } |
| 165 | |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 166 | for (i = 0; i < nb_numa_nodes; i++) { |
Wanlong Gao | 8c85901 | 2014-05-14 17:43:07 +0800 | [diff] [blame] | 167 | if (!bitmap_empty(numa_info[i].node_cpu, MAX_CPUMASK_BITS)) { |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 168 | break; |
| 169 | } |
| 170 | } |
| 171 | /* assigning the VCPUs round-robin is easier to implement, guest OSes |
| 172 | * must cope with this anyway, because there are BIOSes out there in |
| 173 | * real machines which also use this scheme. |
| 174 | */ |
| 175 | if (i == nb_numa_nodes) { |
| 176 | for (i = 0; i < max_cpus; i++) { |
Wanlong Gao | 8c85901 | 2014-05-14 17:43:07 +0800 | [diff] [blame] | 177 | set_bit(i, numa_info[i % nb_numa_nodes].node_cpu); |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void set_numa_modes(void) |
| 184 | { |
| 185 | CPUState *cpu; |
| 186 | int i; |
| 187 | |
| 188 | CPU_FOREACH(cpu) { |
| 189 | for (i = 0; i < nb_numa_nodes; i++) { |
Wanlong Gao | 8c85901 | 2014-05-14 17:43:07 +0800 | [diff] [blame] | 190 | if (test_bit(cpu->cpu_index, numa_info[i].node_cpu)) { |
Wanlong Gao | 96d0e26 | 2014-05-14 17:43:05 +0800 | [diff] [blame] | 191 | cpu->numa_node = i; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |