4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA
24 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011, 2012, Whamcloud, Inc.
28 * This file is part of Lustre, http://www.lustre.org/
29 * Lustre is a trademark of Sun Microsystems, Inc.
31 * Author: liang@whamcloud.com
34 #define DEBUG_SUBSYSTEM S_LNET
36 #include <linux/cpu.h>
37 #include <linux/sched.h>
38 #include <libcfs/libcfs.h>
43 * modparam for setting number of partitions
45 * 0 : estimate best value based on cores or NUMA nodes
46 * 1 : disable multiple partitions
47 * >1 : specify number of partitions
49 /* NB: we set it to 1 for now, multiple partitions will be enabled after
50 * all smp node affinity code landed */
51 static int cpu_npartitions = 1;
52 CFS_MODULE_PARM(cpu_npartitions, "i", int, 0444, "# of CPU partitions");
55 * modparam for setting CPU partitions patterns:
57 * i.e: "0[0,1,2,3] 1[4,5,6,7]", number before bracket is CPU partition ID,
58 * number in bracket is processor ID (core or HT)
60 * i.e: "N 0[0,1] 1[2,3]" the first character 'n' means numbers in bracket
61 * are NUMA node ID, number before bracket is CPU partition ID.
63 * NB: If user specified cpu_pattern, cpu_npartitions will be ignored
65 static char *cpu_pattern = "";
66 CFS_MODULE_PARM(cpu_pattern, "s", charp, 0444, "CPU partitions pattern");
69 /* serialize hotplug etc */
71 /* reserved for hotplug */
72 unsigned long cpt_version;
73 /* mutex to protect cpt_cpumask */
74 struct semaphore cpt_mutex;
75 /* scratch buffer for set/unset_node */
76 cpumask_t *cpt_cpumask;
79 static struct cfs_cpt_data cpt_data;
82 cfs_cpu_core_siblings(int cpu, cpumask_t *mask)
84 #if defined(HAVE_TOPOLOGY_CORE_CPUMASK)
85 /* return cpumask of cores in the same socket */
86 cpumask_copy(mask, topology_core_cpumask(cpu));
87 #elif defined(HAVE_TOPOLOGY_CORE_SIBLINGS)
88 *mask = topology_core_siblings(cpu);
94 EXPORT_SYMBOL(cfs_cpu_core_siblings);
96 /* return number of cores in the same socket of \a cpu */
98 cfs_cpu_core_nsiblings(int cpu)
102 down(&cpt_data.cpt_mutex);
104 cfs_cpu_core_siblings(cpu, cpt_data.cpt_cpumask);
105 num = cpus_weight(*cpt_data.cpt_cpumask);
107 up(&cpt_data.cpt_mutex);
111 EXPORT_SYMBOL(cfs_cpu_core_nsiblings);
113 /* return cpumask of HTs in the same core */
115 cfs_cpu_ht_siblings(int cpu, cpumask_t *mask)
117 #if defined(HAVE_TOPOLOGY_THREAD_CPUMASK)
118 cpumask_copy(mask, topology_thread_cpumask(cpu));
119 #elif defined(HAVE_TOPOLOGY_THREAD_SIBLINGS)
120 *mask = topology_thread_siblings(cpu);
126 EXPORT_SYMBOL(cfs_cpu_ht_siblings);
128 /* return number of HTs in the same core of \a cpu */
130 cfs_cpu_ht_nsiblings(int cpu)
134 down(&cpt_data.cpt_mutex);
136 cfs_cpu_ht_siblings(cpu, cpt_data.cpt_cpumask);
137 num = cpus_weight(*cpt_data.cpt_cpumask);
139 up(&cpt_data.cpt_mutex);
143 EXPORT_SYMBOL(cfs_cpu_ht_nsiblings);
146 cfs_node_to_cpumask(int node, cpumask_t *mask)
148 #if defined(HAVE_CPUMASK_OF_NODE)
149 cpumask_copy(mask, cpumask_of_node(node));
150 #elif defined(HAVE_NODE_TO_CPUMASK)
151 *mask = node_to_cpumask(node);
154 for_each_online_cpu(i) {
155 if (cpu_to_node(i) == node)
160 EXPORT_SYMBOL(cfs_node_to_cpumask);
163 cfs_cpt_table_free(struct cfs_cpt_table *cptab)
167 if (cptab->ctb_cpu2cpt != NULL) {
168 LIBCFS_FREE(cptab->ctb_cpu2cpt,
169 num_possible_cpus() *
170 sizeof(cptab->ctb_cpu2cpt[0]));
173 for (i = 0; cptab->ctb_parts != NULL && i < cptab->ctb_nparts; i++) {
174 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
176 if (part->cpt_nodemask != NULL) {
177 LIBCFS_FREE(part->cpt_nodemask,
178 sizeof(*part->cpt_nodemask));
181 if (part->cpt_cpumask != NULL)
182 LIBCFS_FREE(part->cpt_cpumask, cpumask_size());
185 if (cptab->ctb_parts != NULL) {
186 LIBCFS_FREE(cptab->ctb_parts,
187 cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
190 if (cptab->ctb_nodemask != NULL)
191 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
192 if (cptab->ctb_cpumask != NULL)
193 LIBCFS_FREE(cptab->ctb_cpumask, cpumask_size());
195 LIBCFS_FREE(cptab, sizeof(*cptab));
197 EXPORT_SYMBOL(cfs_cpt_table_free);
199 struct cfs_cpt_table *
200 cfs_cpt_table_alloc(unsigned int ncpt)
202 struct cfs_cpt_table *cptab;
205 LIBCFS_ALLOC(cptab, sizeof(*cptab));
209 cptab->ctb_nparts = ncpt;
211 LIBCFS_ALLOC(cptab->ctb_cpumask, cpumask_size());
212 LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
214 if (cptab->ctb_cpumask == NULL || cptab->ctb_nodemask == NULL)
217 LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
218 num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
219 if (cptab->ctb_cpu2cpt == NULL)
222 memset(cptab->ctb_cpu2cpt, -1,
223 num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
225 LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
226 if (cptab->ctb_parts == NULL)
229 for (i = 0; i < ncpt; i++) {
230 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
232 LIBCFS_ALLOC(part->cpt_cpumask, cpumask_size());
233 LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
234 if (part->cpt_cpumask == NULL || part->cpt_nodemask == NULL)
238 spin_lock(&cpt_data.cpt_lock);
239 /* Reserved for hotplug */
240 cptab->ctb_version = cpt_data.cpt_version;
241 spin_unlock(&cpt_data.cpt_lock);
246 cfs_cpt_table_free(cptab);
249 EXPORT_SYMBOL(cfs_cpt_table_alloc);
252 cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
259 for (i = 0; i < cptab->ctb_nparts; i++) {
261 rc = snprintf(tmp, len, "%d\t: ", i);
271 for_each_cpu_mask(j, *cptab->ctb_parts[i].cpt_cpumask) {
272 rc = snprintf(tmp, len, "%d ", j);
292 EXPORT_SYMBOL(cfs_cpt_table_print);
295 cfs_cpt_number(struct cfs_cpt_table *cptab)
297 return cptab->ctb_nparts;
299 EXPORT_SYMBOL(cfs_cpt_number);
302 cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
304 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
306 return cpt == CFS_CPT_ANY ?
307 cpus_weight(*cptab->ctb_cpumask) :
308 cpus_weight(*cptab->ctb_parts[cpt].cpt_cpumask);
310 EXPORT_SYMBOL(cfs_cpt_weight);
313 cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
315 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
317 return cpt == CFS_CPT_ANY ?
318 any_online_cpu(*cptab->ctb_cpumask) != NR_CPUS :
319 any_online_cpu(*cptab->ctb_parts[cpt].cpt_cpumask) != NR_CPUS;
321 EXPORT_SYMBOL(cfs_cpt_online);
324 cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
326 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
328 return cpt == CFS_CPT_ANY ?
329 cptab->ctb_cpumask : cptab->ctb_parts[cpt].cpt_cpumask;
331 EXPORT_SYMBOL(cfs_cpt_cpumask);
334 cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
336 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
338 return cpt == CFS_CPT_ANY ?
339 cptab->ctb_nodemask : cptab->ctb_parts[cpt].cpt_nodemask;
341 EXPORT_SYMBOL(cfs_cpt_nodemask);
344 cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
348 LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
350 if (cpu < 0 || cpu >= NR_CPUS || !cpu_online(cpu)) {
351 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
355 if (cptab->ctb_cpu2cpt[cpu] != -1) {
356 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
357 cpu, cptab->ctb_cpu2cpt[cpu]);
361 cptab->ctb_cpu2cpt[cpu] = cpt;
363 LASSERT(!cpu_isset(cpu, *cptab->ctb_cpumask));
364 LASSERT(!cpu_isset(cpu, *cptab->ctb_parts[cpt].cpt_cpumask));
366 cpu_set(cpu, *cptab->ctb_cpumask);
367 cpu_set(cpu, *cptab->ctb_parts[cpt].cpt_cpumask);
369 node = cpu_to_node(cpu);
371 /* first CPU of @node in this CPT table */
372 if (!node_isset(node, *cptab->ctb_nodemask))
373 node_set(node, *cptab->ctb_nodemask);
375 /* first CPU of @node in this partition */
376 if (!node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask))
377 node_set(node, *cptab->ctb_parts[cpt].cpt_nodemask);
381 EXPORT_SYMBOL(cfs_cpt_set_cpu);
384 cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
389 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
391 if (cpu < 0 || cpu >= NR_CPUS) {
392 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
396 if (cpt == CFS_CPT_ANY) {
397 /* caller doesn't know the partition ID */
398 cpt = cptab->ctb_cpu2cpt[cpu];
399 if (cpt < 0) { /* not set in this CPT-table */
400 CDEBUG(D_INFO, "Try to unset cpu %d which is "
401 "not in CPT-table %p\n", cpt, cptab);
405 } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
407 "CPU %d is not in cpu-partition %d\n", cpu, cpt);
411 LASSERT(cpu_isset(cpu, *cptab->ctb_parts[cpt].cpt_cpumask));
412 LASSERT(cpu_isset(cpu, *cptab->ctb_cpumask));
414 cpu_clear(cpu, *cptab->ctb_parts[cpt].cpt_cpumask);
415 cpu_clear(cpu, *cptab->ctb_cpumask);
416 cptab->ctb_cpu2cpt[cpu] = -1;
418 node = cpu_to_node(cpu);
420 LASSERT(node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask));
421 LASSERT(node_isset(node, *cptab->ctb_nodemask));
423 for_each_cpu_mask(i, *cptab->ctb_parts[cpt].cpt_cpumask) {
424 /* this CPT has other CPU belonging to this node? */
425 if (cpu_to_node(i) == node)
430 node_clear(node, *cptab->ctb_parts[cpt].cpt_nodemask);
432 for_each_cpu_mask(i, *cptab->ctb_cpumask) {
433 /* this CPT-table has other CPU belonging to this node? */
434 if (cpu_to_node(i) == node)
439 node_clear(node, *cptab->ctb_nodemask);
443 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
446 cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
450 if (cpus_weight(*mask) == 0 || any_online_cpu(*mask) == NR_CPUS) {
451 CDEBUG(D_INFO, "No online CPU is found in the CPU mask "
452 "for CPU partition %d\n", cpt);
456 for_each_cpu_mask(i, *mask) {
457 if (!cfs_cpt_set_cpu(cptab, cpt, i))
463 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
466 cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
470 for_each_cpu_mask(i, *mask)
471 cfs_cpt_unset_cpu(cptab, cpt, i);
473 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
476 cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
481 if (node < 0 || node >= MAX_NUMNODES) {
483 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
487 down(&cpt_data.cpt_mutex);
489 mask = cpt_data.cpt_cpumask;
490 cfs_node_to_cpumask(node, mask);
492 rc = cfs_cpt_set_cpumask(cptab, cpt, mask);
494 up(&cpt_data.cpt_mutex);
498 EXPORT_SYMBOL(cfs_cpt_set_node);
501 cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
505 if (node < 0 || node >= MAX_NUMNODES) {
507 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
511 down(&cpt_data.cpt_mutex);
513 mask = cpt_data.cpt_cpumask;
514 cfs_node_to_cpumask(node, mask);
516 cfs_cpt_unset_cpumask(cptab, cpt, mask);
518 up(&cpt_data.cpt_mutex);
520 EXPORT_SYMBOL(cfs_cpt_unset_node);
523 cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
527 for_each_node_mask(i, *mask) {
528 if (!cfs_cpt_set_node(cptab, cpt, i))
534 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
537 cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
541 for_each_node_mask(i, *mask)
542 cfs_cpt_unset_node(cptab, cpt, i);
544 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
547 cfs_cpt_clear(struct cfs_cpt_table *cptab, int cpt)
552 if (cpt == CFS_CPT_ANY) {
553 last = cptab->ctb_nparts - 1;
559 for (; cpt <= last; cpt++) {
560 for_each_cpu_mask(i, *cptab->ctb_parts[cpt].cpt_cpumask)
561 cfs_cpt_unset_cpu(cptab, cpt, i);
564 EXPORT_SYMBOL(cfs_cpt_clear);
567 cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
574 /* convert CPU partition ID to HW node id */
576 if (cpt < 0 || cpt >= cptab->ctb_nparts) {
577 mask = cptab->ctb_nodemask;
578 rotor = cptab->ctb_spread_rotor++;
580 mask = cptab->ctb_parts[cpt].cpt_nodemask;
581 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
584 weight = nodes_weight(*mask);
589 for_each_node_mask(node, *mask) {
597 EXPORT_SYMBOL(cfs_cpt_spread_node);
600 cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
602 int cpu = smp_processor_id();
603 int cpt = cptab->ctb_cpu2cpt[cpu];
609 /* don't return negative value for safety of upper layer,
610 * instead we shadow the unknown cpu to a valid partition ID */
611 cpt = cpu % cptab->ctb_nparts;
616 EXPORT_SYMBOL(cfs_cpt_current);
619 cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
621 LASSERT(cpu >= 0 && cpu < NR_CPUS);
623 return cptab->ctb_cpu2cpt[cpu];
625 EXPORT_SYMBOL(cfs_cpt_of_cpu);
628 cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
631 nodemask_t *nodemask;
635 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
637 if (cpt == CFS_CPT_ANY) {
638 cpumask = cptab->ctb_cpumask;
639 nodemask = cptab->ctb_nodemask;
641 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
642 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
645 if (any_online_cpu(*cpumask) == NR_CPUS) {
646 CERROR("No online CPU found in CPU partition %d, did someone "
647 "do CPU hotplug on system? You might need to reload "
648 "Lustre modules to keep system working well.\n", cpt);
652 for_each_online_cpu(i) {
653 if (cpu_isset(i, *cpumask))
656 #ifdef HAVE_SET_CPUS_ALLOWED
657 rc = set_cpus_allowed(cfs_current(), *cpumask);
659 rc = set_cpus_allowed_ptr(cfs_current(), cpumask);
661 #ifdef HAVE_SET_MEMS_ALLOWED
662 set_mems_allowed(*nodemask);
665 cfs_schedule(); /* switch to allowed CPU */
670 /* don't need to set affinity baecause all online CPUs are covered */
673 EXPORT_SYMBOL(cfs_cpt_bind);
676 * Choose max to \a number CPUs from \a node and set them in \a cpt.
677 * We always prefer to choose CPU in the same core/socket.
680 cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
681 cpumask_t *node, int number)
683 cpumask_t *socket = NULL;
684 cpumask_t *core = NULL;
690 if (number >= cpus_weight(*node)) {
691 while (!cpus_empty(*node)) {
692 cpu = first_cpu(*node);
694 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
697 cpu_clear(cpu, *node);
702 /* allocate scratch buffer */
703 LIBCFS_ALLOC(socket, cpumask_size());
704 LIBCFS_ALLOC(core, cpumask_size());
705 if (socket == NULL || core == NULL) {
710 while (!cpus_empty(*node)) {
711 cpu = first_cpu(*node);
713 /* get cpumask for cores in the same socket */
714 cfs_cpu_core_siblings(cpu, socket);
715 cpus_and(*socket, *socket, *node);
717 LASSERT(!cpus_empty(*socket));
719 while (!cpus_empty(*socket)) {
722 /* get cpumask for hts in the same core */
723 cfs_cpu_ht_siblings(cpu, core);
724 cpus_and(*core, *core, *node);
726 LASSERT(!cpus_empty(*core));
728 for_each_cpu_mask(i, *core) {
729 cpu_clear(i, *socket);
732 rc = cfs_cpt_set_cpu(cptab, cpt, i);
741 cpu = first_cpu(*socket);
747 LIBCFS_FREE(socket, cpumask_size());
749 LIBCFS_FREE(core, cpumask_size());
753 #define CPT_WEIGHT_MIN 4u
756 cfs_cpt_num_estimate(void)
758 unsigned nnode = num_online_nodes();
759 unsigned ncpu = num_online_cpus();
762 if (ncpu <= CPT_WEIGHT_MIN) {
767 /* generate reasonable number of CPU partitions based on total number
768 * of CPUs, Preferred N should be power2 and match this condition:
769 * 2 * (N - 1)^2 < NCPUS <= 2 * N^2 */
770 for (ncpt = 2; ncpu > 2 * ncpt * ncpt; ncpt <<= 1) {}
772 if (ncpt <= nnode) { /* fat numa system */
776 } else { /* ncpt > nnode */
777 while ((nnode << 1) <= ncpt)
784 #if (BITS_PER_LONG == 32)
785 /* config many CPU partitions on 32-bit system could consume
787 ncpt = min(2U, ncpt);
789 while (ncpu % ncpt != 0)
790 ncpt--; /* worst case is 1 */
795 static struct cfs_cpt_table *
796 cfs_cpt_table_create(int ncpt)
798 struct cfs_cpt_table *cptab = NULL;
799 cpumask_t *mask = NULL;
805 rc = cfs_cpt_num_estimate();
810 CWARN("CPU partition number %d is larger than suggested "
811 "value(%d), your system may have performance"
812 "issue or run out of memory while under pressure\n",
816 if (num_online_cpus() % ncpt != 0) {
817 CERROR("CPU number %d is not multiple of cpu_npartition %d, "
818 "please try different cpu_npartitions value or"
819 "set pattern string by cpu_pattern=STRING\n",
820 (int)num_online_cpus(), ncpt);
824 cptab = cfs_cpt_table_alloc(ncpt);
826 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
830 num = num_online_cpus() / ncpt;
832 CERROR("CPU changed while setting CPU partition\n");
836 LIBCFS_ALLOC(mask, cpumask_size());
838 CERROR("Failed to allocate scratch cpumask\n");
842 for_each_online_node(i) {
843 cfs_node_to_cpumask(i, mask);
845 while (!cpus_empty(*mask)) {
846 struct cfs_cpu_partition *part;
852 part = &cptab->ctb_parts[cpt];
854 n = num - cpus_weight(*part->cpt_cpumask);
857 rc = cfs_cpt_choose_ncpus(cptab, cpt, mask, n);
861 LASSERT(num >= cpus_weight(*part->cpt_cpumask));
862 if (num == cpus_weight(*part->cpt_cpumask))
868 num != cpus_weight(*cptab->ctb_parts[ncpt - 1].cpt_cpumask)) {
869 CERROR("Expect %d(%d) CPU partitions but got %d(%d), "
870 "CPU hotplug/unplug while setting?\n",
871 cptab->ctb_nparts, num, cpt,
872 cpus_weight(*cptab->ctb_parts[ncpt - 1].cpt_cpumask));
876 LIBCFS_FREE(mask, cpumask_size());
881 CERROR("Failed to setup CPU-partition-table with %d "
882 "CPU-partitions, online HW nodes: %d, HW cpus: %d.\n",
883 ncpt, num_online_nodes(), num_online_cpus());
886 LIBCFS_FREE(mask, cpumask_size());
889 cfs_cpt_table_free(cptab);
894 static struct cfs_cpt_table *
895 cfs_cpt_table_create_pattern(char *pattern)
897 struct cfs_cpt_table *cptab;
904 for (ncpt = 0;; ncpt++) { /* quick scan bracket */
905 str = strchr(str, '[');
911 str = cfs_trimwhite(pattern);
912 if (*str == 'n' || *str == 'N') {
918 (node && ncpt > num_online_nodes()) ||
919 (!node && ncpt > num_online_cpus())) {
920 CERROR("Invalid pattern %s, or too many partitions %d\n",
925 high = node ? MAX_NUMNODES - 1 : NR_CPUS - 1;
927 cptab = cfs_cpt_table_alloc(ncpt);
929 CERROR("Failed to allocate cpu partition table\n");
933 for (str = cfs_trimwhite(pattern), c = 0;; c++) {
934 struct cfs_range_expr *range;
935 struct cfs_expr_list *el;
936 char *bracket = strchr(str, '[');
942 if (bracket == NULL) {
944 CERROR("Invalid pattern %s\n", str);
946 } else if (c != ncpt) {
947 CERROR("expect %d partitions but found %d\n",
954 if (sscanf(str, "%u%n", &cpt, &n) < 1) {
955 CERROR("Invalid cpu pattern %s\n", str);
959 if (cpt < 0 || cpt >= ncpt) {
960 CERROR("Invalid partition id %d, total partitions %d\n",
965 if (cfs_cpt_weight(cptab, cpt) != 0) {
966 CERROR("Partition %d has already been set.\n", cpt);
970 str = cfs_trimwhite(str + n);
971 if (str != bracket) {
972 CERROR("Invalid pattern %s\n", str);
976 bracket = strchr(str, ']');
977 if (bracket == NULL) {
978 CERROR("missing right bracket for cpt %d, %s\n",
983 if (cfs_expr_list_parse(str, (bracket - str) + 1,
984 0, high, &el) != 0) {
985 CERROR("Can't parse number range: %s\n", str);
989 cfs_list_for_each_entry(range, &el->el_exprs, re_link) {
990 for (i = range->re_lo; i <= range->re_hi; i++) {
991 if ((i - range->re_lo) % range->re_stride != 0)
994 rc = node ? cfs_cpt_set_node(cptab, cpt, i) :
995 cfs_cpt_set_cpu(cptab, cpt, i);
997 cfs_expr_list_free(el);
1003 cfs_expr_list_free(el);
1005 if (!cfs_cpt_online(cptab, cpt)) {
1006 CERROR("No online CPU is found on partition %d\n", cpt);
1010 str = cfs_trimwhite(bracket + 1);
1016 cfs_cpt_table_free(cptab);
1020 #ifdef CONFIG_HOTPLUG_CPU
1022 cfs_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1024 unsigned int cpu = (unsigned long)hcpu;
1028 case CPU_DEAD_FROZEN:
1030 case CPU_ONLINE_FROZEN:
1031 spin_lock(&cpt_data.cpt_lock);
1032 cpt_data.cpt_version++;
1033 spin_unlock(&cpt_data.cpt_lock);
1035 CWARN("Lustre: can't support CPU hotplug well now, "
1036 "performance and stability could be impacted"
1037 "[CPU %u notify: %lx]\n", cpu, action);
1043 static struct notifier_block cfs_cpu_notifier = {
1044 .notifier_call = cfs_cpu_notify,
1053 if (cfs_cpt_table != NULL)
1054 cfs_cpt_table_free(cfs_cpt_table);
1056 #ifdef CONFIG_HOTPLUG_CPU
1057 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1059 if (cpt_data.cpt_cpumask != NULL)
1060 LIBCFS_FREE(cpt_data.cpt_cpumask, cpumask_size());
1066 LASSERT(cfs_cpt_table == NULL);
1068 memset(&cpt_data, 0, sizeof(cpt_data));
1070 LIBCFS_ALLOC(cpt_data.cpt_cpumask, cpumask_size());
1071 if (cpt_data.cpt_cpumask == NULL) {
1072 CERROR("Failed to allocate scratch buffer\n");
1076 spin_lock_init(&cpt_data.cpt_lock);
1077 sema_init(&cpt_data.cpt_mutex, 1);
1079 #ifdef CONFIG_HOTPLUG_CPU
1080 register_hotcpu_notifier(&cfs_cpu_notifier);
1083 if (*cpu_pattern != 0) {
1084 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern);
1085 if (cfs_cpt_table == NULL) {
1086 CERROR("Failed to create cptab from pattern %s\n",
1092 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1093 if (cfs_cpt_table == NULL) {
1094 CERROR("Failed to create ptable with npartitions %d\n",
1100 spin_lock(&cpt_data.cpt_lock);
1101 if (cfs_cpt_table->ctb_version != cpt_data.cpt_version) {
1102 spin_unlock(&cpt_data.cpt_lock);
1103 CERROR("CPU hotplug/unplug during setup\n");
1106 spin_unlock(&cpt_data.cpt_lock);
1108 LCONSOLE(0, "HW CPU cores: %d, npartitions: %d\n",
1109 num_online_cpus(), cfs_cpt_number(cfs_cpt_table));