bhyve(8): allow cpu pinning using N-M:X-Y ranges

bhyve's -p allows to pin guest's virtual CPU vcpu to hostcpu, however
this becomes very tedious work when you have to pin more than a single
CPU.

This allows to pass a range to -p, e.g. -p 0-3:4-7 which will pin the
cpus 0:4, 1:5, 2:6, 3:7. The ranges must be equal and the CPU numbers
must be ascending.

Sponsored by: Armenian Bioinformatics Institute
Reviewed by:	corvink, markj
Tested by:	bnovkov
MFC after:	3 weeks
Differential Revision:	https://reviews.freebsd.org/D54937
This commit is contained in:
Antranig Vartanian
2026-06-05 17:34:18 +02:00
committed by Bojan Novković
parent b5a96894f6
commit 8f6c577c9f
4 changed files with 39 additions and 14 deletions
+3 -2
View File
@@ -100,7 +100,8 @@ bhyve_usage(int code)
"Usage: %s [-CDHhSW]\n"
" %*s [-c [[cpus=]numcpus][,sockets=n][,cores=n][,threads=n]]\n"
" %*s [-k config_file] [-m mem] [-o var=value]\n"
" %*s [-p vcpu:hostcpu] [-r file] [-s pci] [-U uuid] vmname\n"
" %*s [-p vcpuN[-vcpuM]]:hostcpuX[-hostcpuY]\n"
" %*s [-r file] [-s pci] [-U uuid] vmname\n"
" -C: include guest memory in core file\n"
" -c: number of CPUs and/or topology specification\n"
" -D: destroy on power-off\n"
@@ -116,7 +117,7 @@ bhyve_usage(int code)
" -U: UUID\n"
" -W: force virtio to use single-vector MSI\n",
progname, (int)strlen(progname), "", (int)strlen(progname), "",
(int)strlen(progname), "");
(int)strlen(progname), "", (int)strlen(progname), "");
exit(code);
}
+3 -2
View File
@@ -78,7 +78,8 @@ bhyve_usage(int code)
"Usage: %s [-aCDeHhPSuWwxY]\n"
" %*s [-c [[cpus=]numcpus][,sockets=n][,cores=n][,threads=n]]\n"
" %*s [-G port] [-k config_file] [-l lpc] [-m mem] [-o var=value]\n"
" %*s [-p vcpu:hostcpu] [-r file] [-s pci] [-U uuid] vmname\n"
" %*s [-p vcpuN[-vcpuM]]:hostcpuX[-hostcpuY]\n"
" %*s [-r file] [-s pci] [-U uuid] vmname\n"
" -a: local apic is in xAPIC mode (deprecated)\n"
" -C: include guest memory in core file\n"
" -c: number of CPUs and/or topology specification\n"
@@ -108,7 +109,7 @@ bhyve_usage(int code)
" -x: local APIC is in x2APIC mode\n"
" -Y: disable MPtable generation\n",
progname, (int)strlen(progname), "", (int)strlen(progname), "",
(int)strlen(progname), "");
(int)strlen(progname), "", (int)strlen(progname), "");
exit(code);
}
+30 -8
View File
@@ -357,18 +357,12 @@ calc_topology(void)
guest_ncpus = ncpus;
}
int
bhyve_pincpu_parse(const char *opt)
static int
bhyve_pincpu(int vcpu, int pcpu)
{
const char *value;
char *newval;
char key[16];
int vcpu, pcpu;
if (sscanf(opt, "%d:%d", &vcpu, &pcpu) != 2) {
fprintf(stderr, "invalid format: %s\n", opt);
return (-1);
}
if (vcpu < 0) {
fprintf(stderr, "invalid vcpu '%d'\n", vcpu);
@@ -395,6 +389,34 @@ bhyve_pincpu_parse(const char *opt)
return (0);
}
int
bhyve_pincpu_parse(const char *opt)
{
int vcpu_first, vcpu_last, pcpu_first, pcpu_last;
int vcpu, pcpu;
if (sscanf(opt, "%d-%d:%d-%d", &vcpu_first, &vcpu_last, &pcpu_first, &pcpu_last) == 4) {
if (vcpu_first > vcpu_last || pcpu_first > pcpu_last) {
fprintf(stderr, "invalid range (must be ascending): %s\n", opt);
return (-1);
}
if ((vcpu_last - vcpu_first) != (pcpu_last - pcpu_first)) {
fprintf(stderr, "range sizes do not match: %s\n", opt);
return (-1);
}
for (vcpu = vcpu_first, pcpu = pcpu_first; vcpu <= vcpu_last; vcpu++, pcpu++)
if (bhyve_pincpu(vcpu, pcpu) != 0)
return (-1);
return (0);
}
if (sscanf(opt, "%d:%d", &vcpu, &pcpu) == 2)
return (bhyve_pincpu(vcpu, pcpu));
fprintf(stderr, "invalid format: %s\n", opt);
return (-1);
}
static void
parse_cpuset(int vcpu, const char *list, cpuset_t *set)
{
+3 -2
View File
@@ -95,7 +95,8 @@ bhyve_usage(int code)
"Usage: %s [-CDHhSW]\n"
" %*s [-c [[cpus=]numcpus][,sockets=n][,cores=n][,threads=n]]\n"
" %*s [-k config_file] [-m mem] [-o var=value]\n"
" %*s [-p vcpu:hostcpu] [-r file] [-s pci] [-U uuid] vmname\n"
" %*s [-p vcpuN[-vcpuM]]:hostcpuX[-hostcpuY]\n"
" %*s [-r file] [-s pci] [-U uuid] vmname\n"
" -C: include guest memory in core file\n"
" -c: number of CPUs and/or topology specification\n"
" -D: destroy on power-off\n"
@@ -110,7 +111,7 @@ bhyve_usage(int code)
" -U: UUID\n"
" -W: force virtio to use single-vector MSI\n",
progname, (int)strlen(progname), "", (int)strlen(progname), "",
(int)strlen(progname), "");
(int)strlen(progname), "", (int)strlen(progname), "");
exit(code);
}