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, 2013, 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 <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) && defined(HAVE_CPUMASK_COPY)
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) && defined(HAVE_CPUMASK_COPY)
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) && defined(HAVE_CPUMASK_COPY)
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 set_mems_allowed(*nodemask);
661 cfs_schedule(); /* switch to allowed CPU */
666 /* don't need to set affinity because all online CPUs are covered */
669 EXPORT_SYMBOL(cfs_cpt_bind);
672 * Choose max to \a number CPUs from \a node and set them in \a cpt.
673 * We always prefer to choose CPU in the same core/socket.
676 cfs_cpt_choose_ncpus(struct cfs_cpt_table *cptab, int cpt,
677 cpumask_t *node, int number)
679 cpumask_t *socket = NULL;
680 cpumask_t *core = NULL;
686 if (number >= cpus_weight(*node)) {
687 while (!cpus_empty(*node)) {
688 cpu = first_cpu(*node);
690 rc = cfs_cpt_set_cpu(cptab, cpt, cpu);
693 cpu_clear(cpu, *node);
698 /* allocate scratch buffer */
699 LIBCFS_ALLOC(socket, cpumask_size());
700 LIBCFS_ALLOC(core, cpumask_size());
701 if (socket == NULL || core == NULL) {
706 while (!cpus_empty(*node)) {
707 cpu = first_cpu(*node);
709 /* get cpumask for cores in the same socket */
710 cfs_cpu_core_siblings(cpu, socket);
711 cpus_and(*socket, *socket, *node);
713 LASSERT(!cpus_empty(*socket));
715 while (!cpus_empty(*socket)) {
718 /* get cpumask for hts in the same core */
719 cfs_cpu_ht_siblings(cpu, core);
720 cpus_and(*core, *core, *node);
722 LASSERT(!cpus_empty(*core));
724 for_each_cpu_mask(i, *core) {
725 cpu_clear(i, *socket);
728 rc = cfs_cpt_set_cpu(cptab, cpt, i);
737 cpu = first_cpu(*socket);
743 LIBCFS_FREE(socket, cpumask_size());
745 LIBCFS_FREE(core, cpumask_size());
749 #define CPT_WEIGHT_MIN 4u
752 cfs_cpt_num_estimate(void)
754 unsigned nnode = num_online_nodes();
755 unsigned ncpu = num_online_cpus();
758 if (ncpu <= CPT_WEIGHT_MIN) {
763 /* generate reasonable number of CPU partitions based on total number
764 * of CPUs, Preferred N should be power2 and match this condition:
765 * 2 * (N - 1)^2 < NCPUS <= 2 * N^2 */
766 for (ncpt = 2; ncpu > 2 * ncpt * ncpt; ncpt <<= 1) {}
768 if (ncpt <= nnode) { /* fat numa system */
772 } else { /* ncpt > nnode */
773 while ((nnode << 1) <= ncpt)
780 #if (BITS_PER_LONG == 32)
781 /* config many CPU partitions on 32-bit system could consume
783 ncpt = min(2U, ncpt);
785 while (ncpu % ncpt != 0)
786 ncpt--; /* worst case is 1 */
791 static struct cfs_cpt_table *
792 cfs_cpt_table_create(int ncpt)
794 struct cfs_cpt_table *cptab = NULL;
795 cpumask_t *mask = NULL;
801 rc = cfs_cpt_num_estimate();
805 if (ncpt > num_online_cpus() || ncpt > 4 * rc) {
806 CWARN("CPU partition number %d is larger than suggested "
807 "value (%d), your system may have performance"
808 "issue or run out of memory while under pressure\n",
812 if (num_online_cpus() % ncpt != 0) {
813 CERROR("CPU number %d is not multiple of cpu_npartition %d, "
814 "please try different cpu_npartitions value or"
815 "set pattern string by cpu_pattern=STRING\n",
816 (int)num_online_cpus(), ncpt);
820 cptab = cfs_cpt_table_alloc(ncpt);
822 CERROR("Failed to allocate CPU map(%d)\n", ncpt);
826 num = num_online_cpus() / ncpt;
828 CERROR("CPU changed while setting CPU partition\n");
832 LIBCFS_ALLOC(mask, cpumask_size());
834 CERROR("Failed to allocate scratch cpumask\n");
838 for_each_online_node(i) {
839 cfs_node_to_cpumask(i, mask);
841 while (!cpus_empty(*mask)) {
842 struct cfs_cpu_partition *part;
848 part = &cptab->ctb_parts[cpt];
850 n = num - cpus_weight(*part->cpt_cpumask);
853 rc = cfs_cpt_choose_ncpus(cptab, cpt, mask, n);
857 LASSERT(num >= cpus_weight(*part->cpt_cpumask));
858 if (num == cpus_weight(*part->cpt_cpumask))
864 num != cpus_weight(*cptab->ctb_parts[ncpt - 1].cpt_cpumask)) {
865 CERROR("Expect %d(%d) CPU partitions but got %d(%d), "
866 "CPU hotplug/unplug while setting?\n",
867 cptab->ctb_nparts, num, cpt,
868 cpus_weight(*cptab->ctb_parts[ncpt - 1].cpt_cpumask));
872 LIBCFS_FREE(mask, cpumask_size());
877 CERROR("Failed to setup CPU-partition-table with %d "
878 "CPU-partitions, online HW nodes: %d, HW cpus: %d.\n",
879 ncpt, num_online_nodes(), num_online_cpus());
882 LIBCFS_FREE(mask, cpumask_size());
885 cfs_cpt_table_free(cptab);
890 static struct cfs_cpt_table *
891 cfs_cpt_table_create_pattern(char *pattern)
893 struct cfs_cpt_table *cptab;
900 for (ncpt = 0;; ncpt++) { /* quick scan bracket */
901 str = strchr(str, '[');
907 str = cfs_trimwhite(pattern);
908 if (*str == 'n' || *str == 'N') {
914 (node && ncpt > num_online_nodes()) ||
915 (!node && ncpt > num_online_cpus())) {
916 CERROR("Invalid pattern %s, or too many partitions %d\n",
921 high = node ? MAX_NUMNODES - 1 : NR_CPUS - 1;
923 cptab = cfs_cpt_table_alloc(ncpt);
925 CERROR("Failed to allocate cpu partition table\n");
929 for (str = cfs_trimwhite(pattern), c = 0;; c++) {
930 struct cfs_range_expr *range;
931 struct cfs_expr_list *el;
932 char *bracket = strchr(str, '[');
938 if (bracket == NULL) {
940 CERROR("Invalid pattern %s\n", str);
942 } else if (c != ncpt) {
943 CERROR("expect %d partitions but found %d\n",
950 if (sscanf(str, "%u%n", &cpt, &n) < 1) {
951 CERROR("Invalid cpu pattern %s\n", str);
955 if (cpt < 0 || cpt >= ncpt) {
956 CERROR("Invalid partition id %d, total partitions %d\n",
961 if (cfs_cpt_weight(cptab, cpt) != 0) {
962 CERROR("Partition %d has already been set.\n", cpt);
966 str = cfs_trimwhite(str + n);
967 if (str != bracket) {
968 CERROR("Invalid pattern %s\n", str);
972 bracket = strchr(str, ']');
973 if (bracket == NULL) {
974 CERROR("missing right bracket for cpt %d, %s\n",
979 if (cfs_expr_list_parse(str, (bracket - str) + 1,
980 0, high, &el) != 0) {
981 CERROR("Can't parse number range: %s\n", str);
985 cfs_list_for_each_entry(range, &el->el_exprs, re_link) {
986 for (i = range->re_lo; i <= range->re_hi; i++) {
987 if ((i - range->re_lo) % range->re_stride != 0)
990 rc = node ? cfs_cpt_set_node(cptab, cpt, i) :
991 cfs_cpt_set_cpu(cptab, cpt, i);
993 cfs_expr_list_free(el);
999 cfs_expr_list_free(el);
1001 if (!cfs_cpt_online(cptab, cpt)) {
1002 CERROR("No online CPU is found on partition %d\n", cpt);
1006 str = cfs_trimwhite(bracket + 1);
1012 cfs_cpt_table_free(cptab);
1016 #ifdef CONFIG_HOTPLUG_CPU
1018 cfs_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
1020 unsigned int cpu = (unsigned long)hcpu;
1024 case CPU_DEAD_FROZEN:
1026 case CPU_ONLINE_FROZEN:
1027 spin_lock(&cpt_data.cpt_lock);
1028 cpt_data.cpt_version++;
1029 spin_unlock(&cpt_data.cpt_lock);
1031 CWARN("Lustre: can't support CPU hotplug well now, "
1032 "performance and stability could be impacted"
1033 "[CPU %u notify: %lx]\n", cpu, action);
1039 static struct notifier_block cfs_cpu_notifier = {
1040 .notifier_call = cfs_cpu_notify,
1049 if (cfs_cpt_table != NULL)
1050 cfs_cpt_table_free(cfs_cpt_table);
1052 #ifdef CONFIG_HOTPLUG_CPU
1053 unregister_hotcpu_notifier(&cfs_cpu_notifier);
1055 if (cpt_data.cpt_cpumask != NULL)
1056 LIBCFS_FREE(cpt_data.cpt_cpumask, cpumask_size());
1062 LASSERT(cfs_cpt_table == NULL);
1064 memset(&cpt_data, 0, sizeof(cpt_data));
1066 LIBCFS_ALLOC(cpt_data.cpt_cpumask, cpumask_size());
1067 if (cpt_data.cpt_cpumask == NULL) {
1068 CERROR("Failed to allocate scratch buffer\n");
1072 spin_lock_init(&cpt_data.cpt_lock);
1073 sema_init(&cpt_data.cpt_mutex, 1);
1075 #ifdef CONFIG_HOTPLUG_CPU
1076 register_hotcpu_notifier(&cfs_cpu_notifier);
1079 if (*cpu_pattern != 0) {
1080 cfs_cpt_table = cfs_cpt_table_create_pattern(cpu_pattern);
1081 if (cfs_cpt_table == NULL) {
1082 CERROR("Failed to create cptab from pattern %s\n",
1088 cfs_cpt_table = cfs_cpt_table_create(cpu_npartitions);
1089 if (cfs_cpt_table == NULL) {
1090 CERROR("Failed to create ptable with npartitions %d\n",
1096 spin_lock(&cpt_data.cpt_lock);
1097 if (cfs_cpt_table->ctb_version != cpt_data.cpt_version) {
1098 spin_unlock(&cpt_data.cpt_lock);
1099 CERROR("CPU hotplug/unplug during setup\n");
1102 spin_unlock(&cpt_data.cpt_lock);
1104 LCONSOLE(0, "HW CPU cores: %d, npartitions: %d\n",
1105 num_online_cpus(), cfs_cpt_number(cfs_cpt_table));