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) 2012, Intel Corporation.
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 "../../../include/linux/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 static int cpu_npartitions;
50 module_param(cpu_npartitions, int, 0444);
51 MODULE_PARM_DESC(cpu_npartitions, "# of CPU partitions");
54 * modparam for setting CPU partitions patterns:
56 * i.e: "0[0,1,2,3] 1[4,5,6,7]", number before bracket is CPU partition ID,
57 * number in bracket is processor ID (core or HT)
59 * i.e: "N 0[0,1] 1[2,3]" the first character 'N' means numbers in bracket
60 * are NUMA node ID, number before bracket is CPU partition ID.
62 * NB: If user specified cpu_pattern, cpu_npartitions will be ignored
64 static char *cpu_pattern = "";
65 module_param(cpu_pattern, charp, 0444);
66 MODULE_PARM_DESC(cpu_pattern, "CPU partitions pattern");
69 /* serialize hotplug etc */
71 /* reserved for hotplug */
72 unsigned long cpt_version;
73 /* mutex to protect cpt_cpumask */
74 struct mutex cpt_mutex;
75 /* scratch buffer for set/unset_node */
76 cpumask_t *cpt_cpumask;
79 static struct cfs_cpt_data cpt_data;
81 static void cfs_cpu_core_siblings(int cpu, cpumask_t *mask)
83 /* return cpumask of cores in the same socket */
84 cpumask_copy(mask, topology_core_cpumask(cpu));
87 /* return cpumask of HTs in the same core */
88 static void cfs_cpu_ht_siblings(int cpu, cpumask_t *mask)
90 cpumask_copy(mask, topology_sibling_cpumask(cpu));
93 static void cfs_node_to_cpumask(int node, cpumask_t *mask)
95 cpumask_copy(mask, cpumask_of_node(node));
99 cfs_cpt_table_free(struct cfs_cpt_table *cptab)
103 if (cptab->ctb_cpu2cpt != NULL) {
104 LIBCFS_FREE(cptab->ctb_cpu2cpt,
105 num_possible_cpus() *
106 sizeof(cptab->ctb_cpu2cpt[0]));
109 for (i = 0; cptab->ctb_parts != NULL && i < cptab->ctb_nparts; i++) {
110 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
112 if (part->cpt_nodemask != NULL) {
113 LIBCFS_FREE(part->cpt_nodemask,
114 sizeof(*part->cpt_nodemask));
117 if (part->cpt_cpumask != NULL)
118 LIBCFS_FREE(part->cpt_cpumask, cpumask_size());
121 if (cptab->ctb_parts != NULL) {
122 LIBCFS_FREE(cptab->ctb_parts,
123 cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
126 if (cptab->ctb_nodemask != NULL)
127 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
128 if (cptab->ctb_cpumask != NULL)
129 LIBCFS_FREE(cptab->ctb_cpumask, cpumask_size());
131 LIBCFS_FREE(cptab, sizeof(*cptab));
133 EXPORT_SYMBOL(cfs_cpt_table_free);
135 struct cfs_cpt_table *
136 cfs_cpt_table_alloc(unsigned int ncpt)
138 struct cfs_cpt_table *cptab;
141 LIBCFS_ALLOC(cptab, sizeof(*cptab));
145 cptab->ctb_nparts = ncpt;
147 LIBCFS_ALLOC(cptab->ctb_cpumask, cpumask_size());
148 LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
150 if (cptab->ctb_cpumask == NULL || cptab->ctb_nodemask == NULL)
153 LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
154 num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
155 if (cptab->ctb_cpu2cpt == NULL)
158 memset(cptab->ctb_cpu2cpt, -1,
159 num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
161 LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
162 if (cptab->ctb_parts == NULL)
165 for (i = 0; i < ncpt; i++) {
166 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
168 LIBCFS_ALLOC(part->cpt_cpumask, cpumask_size());
169 LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
170 if (part->cpt_cpumask == NULL || part->cpt_nodemask == NULL)
174 spin_lock(&cpt_data.cpt_lock);
175 /* Reserved for hotplug */
176 cptab->ctb_version = cpt_data.cpt_version;
177 spin_unlock(&cpt_data.cpt_lock);
182 cfs_cpt_table_free(cptab);
185 EXPORT_SYMBOL(cfs_cpt_table_alloc);
188 cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
195 for (i = 0; i < cptab->ctb_nparts; i++) {
197 rc = snprintf(tmp, len, "%d\t: ", i);
207 for_each_cpu(j, cptab->ctb_parts[i].cpt_cpumask) {
208 rc = snprintf(tmp, len, "%d ", j);
228 EXPORT_SYMBOL(cfs_cpt_table_print);
231 cfs_cpt_number(struct cfs_cpt_table *cptab)
233 return cptab->ctb_nparts;
235 EXPORT_SYMBOL(cfs_cpt_number);
238 cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
240 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
242 return cpt == CFS_CPT_ANY ?
243 cpumask_weight(cptab->ctb_cpumask) :
244 cpumask_weight(cptab->ctb_parts[cpt].cpt_cpumask);
246 EXPORT_SYMBOL(cfs_cpt_weight);
249 cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
251 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
253 return cpt == CFS_CPT_ANY ?
254 cpumask_any_and(cptab->ctb_cpumask,
255 cpu_online_mask) < nr_cpu_ids :
256 cpumask_any_and(cptab->ctb_parts[cpt].cpt_cpumask,
257 cpu_online_mask) < nr_cpu_ids;
259 EXPORT_SYMBOL(cfs_cpt_online);
262 cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
264 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
266 return cpt == CFS_CPT_ANY ?
267 cptab->ctb_cpumask : cptab->ctb_parts[cpt].cpt_cpumask;
269 EXPORT_SYMBOL(cfs_cpt_cpumask);
272 cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
274 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
276 return cpt == CFS_CPT_ANY ?
277 cptab->ctb_nodemask : cptab->ctb_parts[cpt].cpt_nodemask;
279 EXPORT_SYMBOL(cfs_cpt_nodemask);
282 cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
286 LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
288 if (cpu < 0 || cpu >= nr_cpu_ids || !cpu_online(cpu)) {
289 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
293 if (cptab->ctb_cpu2cpt[cpu] != -1) {
294 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
295 cpu, cptab->ctb_cpu2cpt[cpu]);
299 cptab->ctb_cpu2cpt[cpu] = cpt;
301 LASSERT(!cpumask_test_cpu(cpu, cptab->ctb_cpumask));
302 LASSERT(!cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
304 cpumask_set_cpu(cpu, cptab->ctb_cpumask);
305 cpumask_set_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
307 node = cpu_to_node(cpu);
309 /* first CPU of @node in this CPT table */
310 if (!node_isset(node, *cptab->ctb_nodemask))
311 node_set(node, *cptab->ctb_nodemask);
313 /* first CPU of @node in this partition */
314 if (!node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask))
315 node_set(node, *cptab->ctb_parts[cpt].cpt_nodemask);
319 EXPORT_SYMBOL(cfs_cpt_set_cpu);
322 cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
327 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
329 if (cpu < 0 || cpu >= nr_cpu_ids) {
330 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
334 if (cpt == CFS_CPT_ANY) {
335 /* caller doesn't know the partition ID */
336 cpt = cptab->ctb_cpu2cpt[cpu];
337 if (cpt < 0) { /* not set in this CPT-table */
338 CDEBUG(D_INFO, "Try to unset cpu %d which is not in CPT-table %p\n",
343 } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
345 "CPU %d is not in cpu-partition %d\n", cpu, cpt);
349 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask));
350 LASSERT(cpumask_test_cpu(cpu, cptab->ctb_cpumask));
352 cpumask_clear_cpu(cpu, cptab->ctb_parts[cpt].cpt_cpumask);
353 cpumask_clear_cpu(cpu, cptab->ctb_cpumask);
354 cptab->ctb_cpu2cpt[cpu] = -1;
356 node = cpu_to_node(cpu);
358 LASSERT(node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask));
359 LASSERT(node_isset(node, *cptab->ctb_nodemask));
361 for_each_cpu(i, cptab->ctb_parts[cpt].cpt_cpumask) {
362 /* this CPT has other CPU belonging to this node? */
363 if (cpu_to_node(i) == node)
368 node_clear(node, *cptab->ctb_parts[cpt].cpt_nodemask);
370 for_each_cpu(i, cptab->ctb_cpumask) {
371 /* this CPT-table has other CPU belonging to this node? */
372 if (cpu_to_node(i) == node)
377 node_clear(node, *cptab->ctb_nodemask);
381 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
384 cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
388 if (cpumask_weight(mask) == 0 ||
389 cpumask_any_and(mask, cpu_online_mask) >= nr_cpu_ids) {
390 CDEBUG(D_INFO, "No online CPU is found in the CPU mask for CPU partition %d\n",
395 for_each_cpu(i, mask) {
396 if (!cfs_cpt_set_cpu(cptab, cpt, i))
402 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
405 cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
409 for_each_cpu(i, mask)
410 cfs_cpt_unset_cpu(cptab, cpt, i);
412 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
415 cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
420 if (node < 0 || node >= MAX_NUMNODES) {
422 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
426 mutex_lock(&cpt_data.cpt_mutex);
428 mask = cpt_data.cpt_cpumask;
429 cfs_node_to_cpumask(node, mask);
431 rc = cfs_cpt_set_cpumask(cptab, cpt, mask);
433 mutex_unlock(&cpt_data.cpt_mutex);
437 EXPORT_SYMBOL(cfs_cpt_set_node);
440 cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
444 if (node < 0 || node >= MAX_NUMNODES) {
446 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
450 mutex_lock(&cpt_data.cpt_mutex);
452 mask = cpt_data.cpt_cpumask;
453 cfs_node_to_cpumask(node, mask);
455 cfs_cpt_unset_cpumask(cptab, cpt, mask);
457 mutex_unlock(&cpt_data.cpt_mutex);
459 EXPORT_SYMBOL(cfs_cpt_unset_node);
462 cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
466 for_each_node_mask(i, *mask) {
467 if (!cfs_cpt_set_node(cptab, cpt, i))
473 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
476 cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
480 for_each_node_mask(i, *mask)
481 cfs_cpt_unset_node(cptab, cpt, i);
483 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
486 cfs_cpt_clear(struct cfs_cpt_table *cptab, int cpt)
491 if (cpt == CFS_CPT_ANY) {
492 last = cptab->ctb_nparts - 1;
498 for (; cpt <= last; cpt++) {
499 for_each_cpu(i, cptab->ctb_parts[cpt].cpt_cpumask)
500 cfs_cpt_unset_cpu(cptab, cpt, i);
503 EXPORT_SYMBOL(cfs_cpt_clear);
506 cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
513 /* convert CPU partition ID to HW node id */
515 if (cpt < 0 || cpt >= cptab->ctb_nparts) {
516 mask = cptab->ctb_nodemask;
517 rotor = cptab->ctb_spread_rotor++;
519 mask = cptab->ctb_parts[cpt].cpt_nodemask;
520 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
523 weight = nodes_weight(*mask);
528 for_each_node_mask(node, *mask) {
536 EXPORT_SYMBOL(cfs_cpt_spread_node);
539 cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
541 int cpu = smp_processor_id();
542 int cpt = cptab->ctb_cpu2cpt[cpu];
548 /* don't return negative value for safety of upper layer,
549 * instead we shadow the unknown cpu to a valid partition ID */
550 cpt = cpu % cptab->ctb_nparts;
555 EXPORT_SYMBOL(cfs_cpt_current);
558 cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
560 LASSERT(cpu >= 0 && cpu < nr_cpu_ids);
562 return cptab->ctb_cpu2cpt[cpu];
564 EXPORT_SYMBOL(cfs_cpt_of_cpu);
567 cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
570 nodemask_t *nodemask;
574 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
576 if (cpt == CFS_CPT_ANY) {
577 cpumask = cptab->ctb_cpumask;
578 nodemask = cptab->ctb_nodemask;
580 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
581 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
584 if (cpumask_any_and(cpumask, cpu_online_mask) >= nr_cpu_ids) {
585 CERROR("No online CPU found in CPU partition %d, did someone do CPU hotplug on system? You might need to reload Lustre modules to keep system working well.\n",
590 for_each_online_cpu(i) {
591 if (cpumask_test_cpu(i, cpumask))
594 rc = set_cpus_allowed_ptr(current, cpumask);
595 set_mems_allowed(*nodemask);
597 schedule(); /* switch to allowed CPU */
602 /* don't need to set affinity because all online CPUs are covered */
605 EXPORT_SYMBOL(cfs_cpt_bind);
608 * Choose max to \a number CPUs from \a node and set them in \a cpt.
609 * We always prefer to choose CPU in the same core/socket.
612 cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
613 cpumask_t *node, int number)
615 cpumask_t *socket = NULL;
616 cpumask_t *core = NULL;
622 if (number >= cpumask_weight(node)) {
623 while (!cpumask_empty(node)) {
624 cpu = cpumask_first(node);
626 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
629 cpumask_clear_cpu(cpu, node);
634 /* allocate scratch buffer */
635 LIBCFS_ALLOC(socket, cpumask_size());
636 LIBCFS_ALLOC(core, cpumask_size());
637 if (socket == NULL || core == NULL) {
642 while (!cpumask_empty(node)) {
643 cpu = cpumask_first(node);
645 /* get cpumask for cores in the same socket */
646 cfs_cpu_core_siblings(cpu, socket);
647 cpumask_and(socket, socket, node);
649 LASSERT(!cpumask_empty(socket));
651 while (!cpumask_empty(socket)) {
654 /* get cpumask for hts in the same core */
655 cfs_cpu_ht_siblings(cpu, core);
656 cpumask_and(core, core, node);
658 LASSERT(!cpumask_empty(core));
660 for_each_cpu(i, core) {
661 cpumask_clear_cpu(i, socket);
662 cpumask_clear_cpu(i, node);
664 rc = cfs_cpt_set_cpu(cptab, cpt, i);
673 cpu = cpumask_first(socket);
679 LIBCFS_FREE(socket, cpumask_size());
681 LIBCFS_FREE(core, cpumask_size());
685 #define CPT_WEIGHT_MIN 4u
688 cfs_cpt_num_estimate(void)
690 unsigned nnode = num_online_nodes();
691 unsigned ncpu = num_online_cpus();
694 if (ncpu <= CPT_WEIGHT_MIN) {
699 /* generate reasonable number of CPU partitions based on total number
700 * of CPUs, Preferred N should be power2 and match this condition:
701 * 2 * (N - 1)^2 < NCPUS <= 2 * N^2 */
702 for (ncpt = 2; ncpu > 2 * ncpt * ncpt; ncpt <<= 1)
705 if (ncpt <= nnode) { /* fat numa system */
709 } else { /* ncpt > nnode */
710 while ((nnode << 1) <= ncpt)
717 #if (BITS_PER_LONG == 32)
718 /* config many CPU partitions on 32-bit system could consume
720 ncpt = min(2U, ncpt);
722 while (ncpu % ncpt != 0)
723 ncpt--; /* worst case is 1 */
728 static struct cfs_cpt_table *
729 cfs_cpt_table_create(int ncpt)
731 struct cfs_cpt_table *cptab = NULL;
732 cpumask_t *mask = NULL;
738 rc = cfs_cpt_num_estimate();
742 if (ncpt > num_online_cpus() || ncpt > 4 * rc) {
743 CWARN("CPU partition number %d is larger than suggested value (%d), your system may have performance issue or run out of memory while under pressure\n",
747 if (num_online_cpus() % ncpt != 0) {
748 CERROR("CPU number %d is not multiple of cpu_npartition %d, please try different cpu_npartitions value or set pattern string by cpu_pattern=STRING\n",
749 (int)num_online_cpus(), ncpt);
753 cptab = cfs_cpt_table_alloc(ncpt);
755 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
759 num = num_online_cpus() / ncpt;
761 CERROR("CPU changed while setting CPU partition\n");
765 LIBCFS_ALLOC(mask, cpumask_size());
767 CERROR("Failed to allocate scratch cpumask\n");
771 for_each_online_node(i) {
772 cfs_node_to_cpumask(i, mask);
774 while (!cpumask_empty(mask)) {
775 struct cfs_cpu_partition *part;
781 part = &cptab->ctb_parts[cpt];
783 n = num - cpumask_weight(part->cpt_cpumask);
786 rc = cfs_cpt_choose_ncpus(cptab, cpt, mask, n);
790 LASSERT(num >= cpumask_weight(part->cpt_cpumask));
791 if (num == cpumask_weight(part->cpt_cpumask))
797 num != cpumask_weight(cptab->ctb_parts[ncpt - 1].cpt_cpumask)) {
798 CERROR("Expect %d(%d) CPU partitions but got %d(%d), CPU hotplug/unplug while setting?\n",
799 cptab->ctb_nparts, num, cpt,
800 cpumask_weight(cptab->ctb_parts[ncpt - 1].cpt_cpumask));
804 LIBCFS_FREE(mask, cpumask_size());
809 CERROR("Failed to setup CPU-partition-table with %d CPU-partitions, online HW nodes: %d, HW cpus: %d.\n",
810 ncpt, num_online_nodes(), num_online_cpus());
813 LIBCFS_FREE(mask, cpumask_size());
816 cfs_cpt_table_free(cptab);
821 static struct cfs_cpt_table *
822 cfs_cpt_table_create_pattern(char *pattern)
824 struct cfs_cpt_table *cptab;
831 for (ncpt = 0;; ncpt++) { /* quick scan bracket */
832 str = strchr(str, '[');
838 str = cfs_trimwhite(pattern);
839 if (*str == 'n' || *str == 'N') {
845 (node && ncpt > num_online_nodes()) ||
846 (!node && ncpt > num_online_cpus())) {
847 CERROR("Invalid pattern %s, or too many partitions %d\n",
852 high = node ? MAX_NUMNODES - 1 : nr_cpu_ids - 1;
854 cptab = cfs_cpt_table_alloc(ncpt);
856 CERROR("Failed to allocate cpu partition table\n");
860 for (str = cfs_trimwhite(pattern), c = 0;; c++) {
861 struct cfs_range_expr *range;
862 struct cfs_expr_list *el;
863 char *bracket = strchr(str, '[');
869 if (bracket == NULL) {
871 CERROR("Invalid pattern %s\n", str);
873 } else if (c != ncpt) {
874 CERROR("expect %d partitions but found %d\n",
881 if (sscanf(str, "%d%n", &cpt, &n) < 1) {
882 CERROR("Invalid cpu pattern %s\n", str);
886 if (cpt < 0 || cpt >= ncpt) {
887 CERROR("Invalid partition id %d, total partitions %d\n",
892 if (cfs_cpt_weight(cptab, cpt) != 0) {
893 CERROR("Partition %d has already been set.\n", cpt);
897 str = cfs_trimwhite(str + n);
898 if (str != bracket) {
899 CERROR("Invalid pattern %s\n", str);
903 bracket = strchr(str, ']');
904 if (bracket == NULL) {
905 CERROR("missing right bracket for cpt %d, %s\n",
910 if (cfs_expr_list_parse(str, (bracket - str) + 1,
911 0, high, &el) != 0) {
912 CERROR("Can't parse number range: %s\n", str);
916 list_for_each_entry(range, &el->el_exprs, re_link) {
917 for (i = range->re_lo; i <= range->re_hi; i++) {
918 if ((i - range->re_lo) % range->re_stride != 0)
921 rc = node ? cfs_cpt_set_node(cptab, cpt, i) :
922 cfs_cpt_set_cpu(cptab, cpt, i);
924 cfs_expr_list_free(el);
930 cfs_expr_list_free(el);
932 if (!cfs_cpt_online(cptab, cpt)) {
933 CERROR("No online CPU is found on partition %d\n", cpt);
937 str = cfs_trimwhite(bracket + 1);
943 cfs_cpt_table_free(cptab);
947 #ifdef CONFIG_HOTPLUG_CPU
949 cfs_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
951 unsigned int cpu = (unsigned long)hcpu;
956 case CPU_DEAD_FROZEN:
958 case CPU_ONLINE_FROZEN:
959 spin_lock(&cpt_data.cpt_lock);
960 cpt_data.cpt_version++;
961 spin_unlock(&cpt_data.cpt_lock);
963 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) {
964 CDEBUG(D_INFO, "CPU changed [cpu %u action %lx]\n",
969 mutex_lock(&cpt_data.cpt_mutex);
970 /* if all HTs in a core are offline, it may break affinity */
971 cfs_cpu_ht_siblings(cpu, cpt_data.cpt_cpumask);
972 warn = cpumask_any_and(cpt_data.cpt_cpumask,
973 cpu_online_mask) >= nr_cpu_ids;
974 mutex_unlock(&cpt_data.cpt_mutex);
975 CDEBUG(warn ? D_WARNING : D_INFO,
976 "Lustre: can't support CPU plug-out well now, performance and stability could be impacted [CPU %u action: %lx]\n",
983 static struct notifier_block cfs_cpu_notifier = {
984 .notifier_call = cfs_cpu_notify,
993 if (cfs_cpt_table != NULL)
994 cfs_cpt_table_free(cfs_cpt_table);
996 #ifdef CONFIG_HOTPLUG_CPU
997 unregister_hotcpu_notifier(&cfs_cpu_notifier);
999 if (cpt_data.cpt_cpumask != NULL)
1000 LIBCFS_FREE(cpt_data.cpt_cpumask, cpumask_size());
1006 LASSERT(cfs_cpt_table == NULL);
1008 memset(&cpt_data, 0, sizeof(cpt_data));
1010 LIBCFS_ALLOC(cpt_data.cpt_cpumask, cpumask_size());
1011 if (cpt_data.cpt_cpumask == NULL) {
1012 CERROR("Failed to allocate scratch buffer\n");
1016 spin_lock_init(&cpt_data.cpt_lock);
1017 mutex_init(&cpt_data.cpt_mutex);
1019 #ifdef CONFIG_HOTPLUG_CPU
1020 register_hotcpu_notifier(&cfs_cpu_notifier);
1023 if (*cpu_pattern != 0) {
1024 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern);
1025 if (cfs_cpt_table == NULL) {
1026 CERROR("Failed to create cptab from pattern %s\n",
1032 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1033 if (cfs_cpt_table == NULL) {
1034 CERROR("Failed to create ptable with npartitions %d\n",
1040 spin_lock(&cpt_data.cpt_lock);
1041 if (cfs_cpt_table->ctb_version != cpt_data.cpt_version) {
1042 spin_unlock(&cpt_data.cpt_lock);
1043 CERROR("CPU hotplug/unplug during setup\n");
1046 spin_unlock(&cpt_data.cpt_lock);
1048 LCONSOLE(0, "HW CPU cores: %d, npartitions: %d\n",
1049 num_online_cpus(), cfs_cpt_number(cfs_cpt_table));