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 static int cpu_npartitions;
50 CFS_MODULE_PARM(cpu_npartitions, "i", int, 0444, "# of CPU partitions");
53 * modparam for setting CPU partitions patterns:
55 * i.e: "0[0,1,2,3] 1[4,5,6,7]", number before bracket is CPU partition ID,
56 * number in bracket is processor ID (core or HT)
58 * i.e: "N 0[0,1] 1[2,3]" the first character 'n' means numbers in bracket
59 * are NUMA node ID, number before bracket is CPU partition ID.
61 * NB: If user specified cpu_pattern, cpu_npartitions will be ignored
63 static char *cpu_pattern = "";
64 CFS_MODULE_PARM(cpu_pattern, "s", charp, 0444, "CPU partitions pattern");
67 /* serialize hotplug etc */
69 /* reserved for hotplug */
70 unsigned long cpt_version;
71 /* mutex to protect cpt_cpumask */
72 struct semaphore cpt_mutex;
73 /* scratch buffer for set/unset_node */
74 cpumask_t *cpt_cpumask;
77 static struct cfs_cpt_data cpt_data;
80 cfs_cpu_core_siblings(int cpu, cpumask_t *mask)
82 #if defined(HAVE_TOPOLOGY_CORE_CPUMASK)
83 /* return cpumask of cores in the same socket */
84 cpumask_copy(mask, topology_core_cpumask(cpu));
85 #elif defined(HAVE_TOPOLOGY_CORE_SIBLINGS)
86 *mask = topology_core_siblings(cpu);
92 EXPORT_SYMBOL(cfs_cpu_core_siblings);
94 /* return number of cores in the same socket of \a cpu */
96 cfs_cpu_core_nsiblings(int cpu)
100 down(&cpt_data.cpt_mutex);
102 cfs_cpu_core_siblings(cpu, cpt_data.cpt_cpumask);
103 num = cpus_weight(*cpt_data.cpt_cpumask);
105 up(&cpt_data.cpt_mutex);
109 EXPORT_SYMBOL(cfs_cpu_core_nsiblings);
111 /* return cpumask of HTs in the same core */
113 cfs_cpu_ht_siblings(int cpu, cpumask_t *mask)
115 #if defined(HAVE_TOPOLOGY_THREAD_CPUMASK)
116 cpumask_copy(mask, topology_thread_cpumask(cpu));
117 #elif defined(HAVE_TOPOLOGY_THREAD_SIBLINGS)
118 *mask = topology_thread_siblings(cpu);
124 EXPORT_SYMBOL(cfs_cpu_ht_siblings);
126 /* return number of HTs in the same core of \a cpu */
128 cfs_cpu_ht_nsiblings(int cpu)
132 down(&cpt_data.cpt_mutex);
134 cfs_cpu_ht_siblings(cpu, cpt_data.cpt_cpumask);
135 num = cpus_weight(*cpt_data.cpt_cpumask);
137 up(&cpt_data.cpt_mutex);
141 EXPORT_SYMBOL(cfs_cpu_ht_nsiblings);
144 cfs_node_to_cpumask(int node, cpumask_t *mask)
146 #if defined(HAVE_CPUMASK_OF_NODE)
147 cpumask_copy(mask, cpumask_of_node(node));
148 #elif defined(HAVE_NODE_TO_CPUMASK)
149 *mask = node_to_cpumask(node);
152 for_each_online_cpu(i) {
153 if (cpu_to_node(i) == node)
158 EXPORT_SYMBOL(cfs_node_to_cpumask);
161 cfs_cpt_table_free(struct cfs_cpt_table *cptab)
165 if (cptab->ctb_cpu2cpt != NULL) {
166 LIBCFS_FREE(cptab->ctb_cpu2cpt,
167 num_possible_cpus() *
168 sizeof(cptab->ctb_cpu2cpt[0]));
171 for (i = 0; cptab->ctb_parts != NULL && i < cptab->ctb_nparts; i++) {
172 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
174 if (part->cpt_nodemask != NULL) {
175 LIBCFS_FREE(part->cpt_nodemask,
176 sizeof(*part->cpt_nodemask));
179 if (part->cpt_cpumask != NULL)
180 LIBCFS_FREE(part->cpt_cpumask, cpumask_size());
183 if (cptab->ctb_parts != NULL) {
184 LIBCFS_FREE(cptab->ctb_parts,
185 cptab->ctb_nparts * sizeof(cptab->ctb_parts[0]));
188 if (cptab->ctb_nodemask != NULL)
189 LIBCFS_FREE(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
190 if (cptab->ctb_cpumask != NULL)
191 LIBCFS_FREE(cptab->ctb_cpumask, cpumask_size());
193 LIBCFS_FREE(cptab, sizeof(*cptab));
195 EXPORT_SYMBOL(cfs_cpt_table_free);
197 struct cfs_cpt_table *
198 cfs_cpt_table_alloc(unsigned int ncpt)
200 struct cfs_cpt_table *cptab;
203 LIBCFS_ALLOC(cptab, sizeof(*cptab));
207 cptab->ctb_nparts = ncpt;
209 LIBCFS_ALLOC(cptab->ctb_cpumask, cpumask_size());
210 LIBCFS_ALLOC(cptab->ctb_nodemask, sizeof(*cptab->ctb_nodemask));
212 if (cptab->ctb_cpumask == NULL || cptab->ctb_nodemask == NULL)
215 LIBCFS_ALLOC(cptab->ctb_cpu2cpt,
216 num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
217 if (cptab->ctb_cpu2cpt == NULL)
220 memset(cptab->ctb_cpu2cpt, -1,
221 num_possible_cpus() * sizeof(cptab->ctb_cpu2cpt[0]));
223 LIBCFS_ALLOC(cptab->ctb_parts, ncpt * sizeof(cptab->ctb_parts[0]));
224 if (cptab->ctb_parts == NULL)
227 for (i = 0; i < ncpt; i++) {
228 struct cfs_cpu_partition *part = &cptab->ctb_parts[i];
230 LIBCFS_ALLOC(part->cpt_cpumask, cpumask_size());
231 LIBCFS_ALLOC(part->cpt_nodemask, sizeof(*part->cpt_nodemask));
232 if (part->cpt_cpumask == NULL || part->cpt_nodemask == NULL)
236 spin_lock(&cpt_data.cpt_lock);
237 /* Reserved for hotplug */
238 cptab->ctb_version = cpt_data.cpt_version;
239 spin_unlock(&cpt_data.cpt_lock);
244 cfs_cpt_table_free(cptab);
247 EXPORT_SYMBOL(cfs_cpt_table_alloc);
250 cfs_cpt_table_print(struct cfs_cpt_table *cptab, char *buf, int len)
257 for (i = 0; i < cptab->ctb_nparts; i++) {
259 rc = snprintf(tmp, len, "%d\t: ", i);
269 for_each_cpu_mask(j, *cptab->ctb_parts[i].cpt_cpumask) {
270 rc = snprintf(tmp, len, "%d ", j);
290 EXPORT_SYMBOL(cfs_cpt_table_print);
293 cfs_cpt_number(struct cfs_cpt_table *cptab)
295 return cptab->ctb_nparts;
297 EXPORT_SYMBOL(cfs_cpt_number);
300 cfs_cpt_weight(struct cfs_cpt_table *cptab, int cpt)
302 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
304 return cpt == CFS_CPT_ANY ?
305 cpus_weight(*cptab->ctb_cpumask) :
306 cpus_weight(*cptab->ctb_parts[cpt].cpt_cpumask);
308 EXPORT_SYMBOL(cfs_cpt_weight);
311 cfs_cpt_online(struct cfs_cpt_table *cptab, int cpt)
313 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
315 return cpt == CFS_CPT_ANY ?
316 any_online_cpu(*cptab->ctb_cpumask) != NR_CPUS :
317 any_online_cpu(*cptab->ctb_parts[cpt].cpt_cpumask) != NR_CPUS;
319 EXPORT_SYMBOL(cfs_cpt_online);
322 cfs_cpt_cpumask(struct cfs_cpt_table *cptab, int cpt)
324 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
326 return cpt == CFS_CPT_ANY ?
327 cptab->ctb_cpumask : cptab->ctb_parts[cpt].cpt_cpumask;
329 EXPORT_SYMBOL(cfs_cpt_cpumask);
332 cfs_cpt_nodemask(struct cfs_cpt_table *cptab, int cpt)
334 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
336 return cpt == CFS_CPT_ANY ?
337 cptab->ctb_nodemask : cptab->ctb_parts[cpt].cpt_nodemask;
339 EXPORT_SYMBOL(cfs_cpt_nodemask);
342 cfs_cpt_set_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
346 LASSERT(cpt >= 0 && cpt < cptab->ctb_nparts);
348 if (cpu < 0 || cpu >= NR_CPUS || !cpu_online(cpu)) {
349 CDEBUG(D_INFO, "CPU %d is invalid or it's offline\n", cpu);
353 if (cptab->ctb_cpu2cpt[cpu] != -1) {
354 CDEBUG(D_INFO, "CPU %d is already in partition %d\n",
355 cpu, cptab->ctb_cpu2cpt[cpu]);
359 cptab->ctb_cpu2cpt[cpu] = cpt;
361 LASSERT(!cpu_isset(cpu, *cptab->ctb_cpumask));
362 LASSERT(!cpu_isset(cpu, *cptab->ctb_parts[cpt].cpt_cpumask));
364 cpu_set(cpu, *cptab->ctb_cpumask);
365 cpu_set(cpu, *cptab->ctb_parts[cpt].cpt_cpumask);
367 node = cpu_to_node(cpu);
369 /* first CPU of @node in this CPT table */
370 if (!node_isset(node, *cptab->ctb_nodemask))
371 node_set(node, *cptab->ctb_nodemask);
373 /* first CPU of @node in this partition */
374 if (!node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask))
375 node_set(node, *cptab->ctb_parts[cpt].cpt_nodemask);
379 EXPORT_SYMBOL(cfs_cpt_set_cpu);
382 cfs_cpt_unset_cpu(struct cfs_cpt_table *cptab, int cpt, int cpu)
387 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
389 if (cpu < 0 || cpu >= NR_CPUS) {
390 CDEBUG(D_INFO, "Invalid CPU id %d\n", cpu);
394 if (cpt == CFS_CPT_ANY) {
395 /* caller doesn't know the partition ID */
396 cpt = cptab->ctb_cpu2cpt[cpu];
397 if (cpt < 0) { /* not set in this CPT-table */
398 CDEBUG(D_INFO, "Try to unset cpu %d which is "
399 "not in CPT-table %p\n", cpt, cptab);
403 } else if (cpt != cptab->ctb_cpu2cpt[cpu]) {
405 "CPU %d is not in cpu-partition %d\n", cpu, cpt);
409 LASSERT(cpu_isset(cpu, *cptab->ctb_parts[cpt].cpt_cpumask));
410 LASSERT(cpu_isset(cpu, *cptab->ctb_cpumask));
412 cpu_clear(cpu, *cptab->ctb_parts[cpt].cpt_cpumask);
413 cpu_clear(cpu, *cptab->ctb_cpumask);
414 cptab->ctb_cpu2cpt[cpu] = -1;
416 node = cpu_to_node(cpu);
418 LASSERT(node_isset(node, *cptab->ctb_parts[cpt].cpt_nodemask));
419 LASSERT(node_isset(node, *cptab->ctb_nodemask));
421 for_each_cpu_mask(i, *cptab->ctb_parts[cpt].cpt_cpumask) {
422 /* this CPT has other CPU belonging to this node? */
423 if (cpu_to_node(i) == node)
428 node_clear(node, *cptab->ctb_parts[cpt].cpt_nodemask);
430 for_each_cpu_mask(i, *cptab->ctb_cpumask) {
431 /* this CPT-table has other CPU belonging to this node? */
432 if (cpu_to_node(i) == node)
437 node_clear(node, *cptab->ctb_nodemask);
441 EXPORT_SYMBOL(cfs_cpt_unset_cpu);
444 cfs_cpt_set_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
448 if (cpus_weight(*mask) == 0 || any_online_cpu(*mask) == NR_CPUS) {
449 CDEBUG(D_INFO, "No online CPU is found in the CPU mask "
450 "for CPU partition %d\n", cpt);
454 for_each_cpu_mask(i, *mask) {
455 if (!cfs_cpt_set_cpu(cptab, cpt, i))
461 EXPORT_SYMBOL(cfs_cpt_set_cpumask);
464 cfs_cpt_unset_cpumask(struct cfs_cpt_table *cptab, int cpt, cpumask_t *mask)
468 for_each_cpu_mask(i, *mask)
469 cfs_cpt_unset_cpu(cptab, cpt, i);
471 EXPORT_SYMBOL(cfs_cpt_unset_cpumask);
474 cfs_cpt_set_node(struct cfs_cpt_table *cptab, int cpt, int node)
479 if (node < 0 || node >= MAX_NUMNODES) {
481 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
485 down(&cpt_data.cpt_mutex);
487 mask = cpt_data.cpt_cpumask;
488 cfs_node_to_cpumask(node, mask);
490 rc = cfs_cpt_set_cpumask(cptab, cpt, mask);
492 up(&cpt_data.cpt_mutex);
496 EXPORT_SYMBOL(cfs_cpt_set_node);
499 cfs_cpt_unset_node(struct cfs_cpt_table *cptab, int cpt, int node)
503 if (node < 0 || node >= MAX_NUMNODES) {
505 "Invalid NUMA id %d for CPU partition %d\n", node, cpt);
509 down(&cpt_data.cpt_mutex);
511 mask = cpt_data.cpt_cpumask;
512 cfs_node_to_cpumask(node, mask);
514 cfs_cpt_unset_cpumask(cptab, cpt, mask);
516 up(&cpt_data.cpt_mutex);
518 EXPORT_SYMBOL(cfs_cpt_unset_node);
521 cfs_cpt_set_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
525 for_each_node_mask(i, *mask) {
526 if (!cfs_cpt_set_node(cptab, cpt, i))
532 EXPORT_SYMBOL(cfs_cpt_set_nodemask);
535 cfs_cpt_unset_nodemask(struct cfs_cpt_table *cptab, int cpt, nodemask_t *mask)
539 for_each_node_mask(i, *mask)
540 cfs_cpt_unset_node(cptab, cpt, i);
542 EXPORT_SYMBOL(cfs_cpt_unset_nodemask);
545 cfs_cpt_clear(struct cfs_cpt_table *cptab, int cpt)
550 if (cpt == CFS_CPT_ANY) {
551 last = cptab->ctb_nparts - 1;
557 for (; cpt <= last; cpt++) {
558 for_each_cpu_mask(i, *cptab->ctb_parts[cpt].cpt_cpumask)
559 cfs_cpt_unset_cpu(cptab, cpt, i);
562 EXPORT_SYMBOL(cfs_cpt_clear);
565 cfs_cpt_spread_node(struct cfs_cpt_table *cptab, int cpt)
572 /* convert CPU partition ID to HW node id */
574 if (cpt < 0 || cpt >= cptab->ctb_nparts) {
575 mask = cptab->ctb_nodemask;
576 rotor = cptab->ctb_spread_rotor++;
578 mask = cptab->ctb_parts[cpt].cpt_nodemask;
579 rotor = cptab->ctb_parts[cpt].cpt_spread_rotor++;
582 weight = nodes_weight(*mask);
587 for_each_node_mask(node, *mask) {
595 EXPORT_SYMBOL(cfs_cpt_spread_node);
598 cfs_cpt_current(struct cfs_cpt_table *cptab, int remap)
600 int cpu = smp_processor_id();
601 int cpt = cptab->ctb_cpu2cpt[cpu];
607 /* don't return negative value for safety of upper layer,
608 * instead we shadow the unknown cpu to a valid partition ID */
609 cpt = cpu % cptab->ctb_nparts;
614 EXPORT_SYMBOL(cfs_cpt_current);
617 cfs_cpt_of_cpu(struct cfs_cpt_table *cptab, int cpu)
619 LASSERT(cpu >= 0 && cpu < NR_CPUS);
621 return cptab->ctb_cpu2cpt[cpu];
623 EXPORT_SYMBOL(cfs_cpt_of_cpu);
626 cfs_cpt_bind(struct cfs_cpt_table *cptab, int cpt)
629 nodemask_t *nodemask;
633 LASSERT(cpt == CFS_CPT_ANY || (cpt >= 0 && cpt < cptab->ctb_nparts));
635 if (cpt == CFS_CPT_ANY) {
636 cpumask = cptab->ctb_cpumask;
637 nodemask = cptab->ctb_nodemask;
639 cpumask = cptab->ctb_parts[cpt].cpt_cpumask;
640 nodemask = cptab->ctb_parts[cpt].cpt_nodemask;
643 if (any_online_cpu(*cpumask) == NR_CPUS) {
644 CERROR("No online CPU found in CPU partition %d, did someone "
645 "do CPU hotplug on system? You might need to reload "
646 "Lustre modules to keep system working well.\n", cpt);
650 for_each_online_cpu(i) {
651 if (cpu_isset(i, *cpumask))
654 #ifdef HAVE_SET_CPUS_ALLOWED
655 rc = set_cpus_allowed(cfs_current(), *cpumask);
657 rc = set_cpus_allowed_ptr(cfs_current(), cpumask);
659 #ifdef HAVE_SET_MEMS_ALLOWED
660 set_mems_allowed(*nodemask);
663 cfs_schedule(); /* switch to allowed CPU */
668 /* don't need to set affinity baecause all online CPUs are covered */
671 EXPORT_SYMBOL(cfs_cpt_bind);
674 * Choose max to \a number CPUs from \a node and set them in \a cpt.
675 * We always prefer to choose CPU in the same core/socket.
678 cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
679 cpumask_t *node, int number)
681 cpumask_t *socket = NULL;
682 cpumask_t *core = NULL;
688 if (number >= cpus_weight(*node)) {
689 while (!cpus_empty(*node)) {
690 cpu = first_cpu(*node);
692 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
695 cpu_clear(cpu, *node);
700 /* allocate scratch buffer */
701 LIBCFS_ALLOC(socket, cpumask_size());
702 LIBCFS_ALLOC(core, cpumask_size());
703 if (socket == NULL || core == NULL) {
708 while (!cpus_empty(*node)) {
709 cpu = first_cpu(*node);
711 /* get cpumask for cores in the same socket */
712 cfs_cpu_core_siblings(cpu, socket);
713 cpus_and(*socket, *socket, *node);
715 LASSERT(!cpus_empty(*socket));
717 while (!cpus_empty(*socket)) {
720 /* get cpumask for hts in the same core */
721 cfs_cpu_ht_siblings(cpu, core);
722 cpus_and(*core, *core, *node);
724 LASSERT(!cpus_empty(*core));
726 for_each_cpu_mask(i, *core) {
727 cpu_clear(i, *socket);
730 rc = cfs_cpt_set_cpu(cptab, cpt, i);
739 cpu = first_cpu(*socket);
745 LIBCFS_FREE(socket, cpumask_size());
747 LIBCFS_FREE(core, cpumask_size());
751 #define CPT_WEIGHT_MIN 4u
754 cfs_cpt_num_estimate(void)
756 unsigned nnode = num_online_nodes();
757 unsigned ncpu = num_online_cpus();
760 if (ncpu <= CPT_WEIGHT_MIN) {
765 /* generate reasonable number of CPU partitions based on total number
766 * of CPUs, Preferred N should be power2 and match this condition:
767 * 2 * (N - 1)^2 < NCPUS <= 2 * N^2 */
768 for (ncpt = 2; ncpu > 2 * ncpt * ncpt; ncpt <<= 1) {}
770 if (ncpt <= nnode) { /* fat numa system */
774 } else { /* ncpt > nnode */
775 while ((nnode << 1) <= ncpt)
782 #if (BITS_PER_LONG == 32)
783 /* config many CPU partitions on 32-bit system could consume
785 ncpt = min(2U, ncpt);
787 while (ncpu % ncpt != 0)
788 ncpt--; /* worst case is 1 */
793 static struct cfs_cpt_table *
794 cfs_cpt_table_create(int ncpt)
796 struct cfs_cpt_table *cptab = NULL;
797 cpumask_t *mask = NULL;
803 rc = cfs_cpt_num_estimate();
808 CWARN("CPU partition number %d is larger than suggested "
809 "value(%d), your system may have performance"
810 "issue or run out of memory while under pressure\n",
814 if (num_online_cpus() % ncpt != 0) {
815 CERROR("CPU number %d is not multiple of cpu_npartition %d, "
816 "please try different cpu_npartitions value or"
817 "set pattern string by cpu_pattern=STRING\n",
818 (int)num_online_cpus(), ncpt);
822 cptab = cfs_cpt_table_alloc(ncpt);
824 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
828 num = num_online_cpus() / ncpt;
830 CERROR("CPU changed while setting CPU partition\n");
834 LIBCFS_ALLOC(mask, cpumask_size());
836 CERROR("Failed to allocate scratch cpumask\n");
840 for_each_online_node(i) {
841 cfs_node_to_cpumask(i, mask);
843 while (!cpus_empty(*mask)) {
844 struct cfs_cpu_partition *part;
850 part = &cptab->ctb_parts[cpt];
852 n = num - cpus_weight(*part->cpt_cpumask);
855 rc = cfs_cpt_choose_ncpus(cptab, cpt, mask, n);
859 LASSERT(num >= cpus_weight(*part->cpt_cpumask));
860 if (num == cpus_weight(*part->cpt_cpumask))
866 num != cpus_weight(*cptab->ctb_parts[ncpt - 1].cpt_cpumask)) {
867 CERROR("Expect %d(%d) CPU partitions but got %d(%d), "
868 "CPU hotplug/unplug while setting?\n",
869 cptab->ctb_nparts, num, cpt,
870 cpus_weight(*cptab->ctb_parts[ncpt - 1].cpt_cpumask));
874 LIBCFS_FREE(mask, cpumask_size());
879 CERROR("Failed to setup CPU-partition-table with %d "
880 "CPU-partitions, online HW nodes: %d, HW cpus: %d.\n",
881 ncpt, num_online_nodes(), num_online_cpus());
884 LIBCFS_FREE(mask, cpumask_size());
887 cfs_cpt_table_free(cptab);
892 static struct cfs_cpt_table *
893 cfs_cpt_table_create_pattern(char *pattern)
895 struct cfs_cpt_table *cptab;
902 for (ncpt = 0;; ncpt++) { /* quick scan bracket */
903 str = strchr(str, '[');
909 str = cfs_trimwhite(pattern);
910 if (*str == 'n' || *str == 'N') {
916 (node && ncpt > num_online_nodes()) ||
917 (!node && ncpt > num_online_cpus())) {
918 CERROR("Invalid pattern %s, or too many partitions %d\n",
923 high = node ? MAX_NUMNODES - 1 : NR_CPUS - 1;
925 cptab = cfs_cpt_table_alloc(ncpt);
927 CERROR("Failed to allocate cpu partition table\n");
931 for (str = cfs_trimwhite(pattern), c = 0;; c++) {
932 struct cfs_range_expr *range;
933 struct cfs_expr_list *el;
934 char *bracket = strchr(str, '[');
940 if (bracket == NULL) {
942 CERROR("Invalid pattern %s\n", str);
944 } else if (c != ncpt) {
945 CERROR("expect %d partitions but found %d\n",
952 if (sscanf(str, "%u%n", &cpt, &n) < 1) {
953 CERROR("Invalid cpu pattern %s\n", str);
957 if (cpt < 0 || cpt >= ncpt) {
958 CERROR("Invalid partition id %d, total partitions %d\n",
963 if (cfs_cpt_weight(cptab, cpt) != 0) {
964 CERROR("Partition %d has already been set.\n", cpt);
968 str = cfs_trimwhite(str + n);
969 if (str != bracket) {
970 CERROR("Invalid pattern %s\n", str);
974 bracket = strchr(str, ']');
975 if (bracket == NULL) {
976 CERROR("missing right bracket for cpt %d, %s\n",
981 if (cfs_expr_list_parse(str, (bracket - str) + 1,
982 0, high, &el) != 0) {
983 CERROR("Can't parse number range: %s\n", str);
987 cfs_list_for_each_entry(range, &el->el_exprs, re_link) {
988 for (i = range->re_lo; i <= range->re_hi; i++) {
989 if ((i - range->re_lo) % range->re_stride != 0)
992 rc = node ? cfs_cpt_set_node(cptab, cpt, i) :
993 cfs_cpt_set_cpu(cptab, cpt, i);
995 cfs_expr_list_free(el);
1001 cfs_expr_list_free(el);
1003 if (!cfs_cpt_online(cptab, cpt)) {
1004 CERROR("No online CPU is found on partition %d\n", cpt);
1008 str = cfs_trimwhite(bracket + 1);
1014 cfs_cpt_table_free(cptab);
1018 #ifdef CONFIG_HOTPLUG_CPU
1020 cfs_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1022 unsigned int cpu = (unsigned long)hcpu;
1026 case CPU_DEAD_FROZEN:
1028 case CPU_ONLINE_FROZEN:
1029 spin_lock(&cpt_data.cpt_lock);
1030 cpt_data.cpt_version++;
1031 spin_unlock(&cpt_data.cpt_lock);
1033 CWARN("Lustre: can't support CPU hotplug well now, "
1034 "performance and stability could be impacted"
1035 "[CPU %u notify: %lx]\n", cpu, action);
1041 static struct notifier_block cfs_cpu_notifier = {
1042 .notifier_call = cfs_cpu_notify,
1051 if (cfs_cpt_table != NULL)
1052 cfs_cpt_table_free(cfs_cpt_table);
1054 #ifdef CONFIG_HOTPLUG_CPU
1055 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1057 if (cpt_data.cpt_cpumask != NULL)
1058 LIBCFS_FREE(cpt_data.cpt_cpumask, cpumask_size());
1064 LASSERT(cfs_cpt_table == NULL);
1066 memset(&cpt_data, 0, sizeof(cpt_data));
1068 LIBCFS_ALLOC(cpt_data.cpt_cpumask, cpumask_size());
1069 if (cpt_data.cpt_cpumask == NULL) {
1070 CERROR("Failed to allocate scratch buffer\n");
1074 spin_lock_init(&cpt_data.cpt_lock);
1075 sema_init(&cpt_data.cpt_mutex, 1);
1077 #ifdef CONFIG_HOTPLUG_CPU
1078 register_hotcpu_notifier(&cfs_cpu_notifier);
1081 if (*cpu_pattern != 0) {
1082 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern);
1083 if (cfs_cpt_table == NULL) {
1084 CERROR("Failed to create cptab from pattern %s\n",
1090 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1091 if (cfs_cpt_table == NULL) {
1092 CERROR("Failed to create ptable with npartitions %d\n",
1098 spin_lock(&cpt_data.cpt_lock);
1099 if (cfs_cpt_table->ctb_version != cpt_data.cpt_version) {
1100 spin_unlock(&cpt_data.cpt_lock);
1101 CERROR("CPU hotplug/unplug during setup\n");
1104 spin_unlock(&cpt_data.cpt_lock);
1106 LCONSOLE(0, "HW CPU cores: %d, npartitions: %d\n",
1107 num_online_cpus(), cfs_cpt_number(cfs_cpt_table));