Skip to content

Commit 45cc2c5

Browse files
ZideChen0wenlingz
authored andcommitted
acrn-dm: implement cpu_affinity command line argument
User has a chance to specify VCPU affinity through acrn-dm command line argument. Examples of the command line: 3 PCPUs: 1/2/3 --cpu_affinity 1-3 5 PCPUs: 2/3/6/7/8 --cpu_affinity 2,3,6-8 8 PCPUs: 2/3/6/7/9/10/11/12 --cpu_affinity 2,3,6-7,9,10-12 The specified pCPUs must be included in the guest VM's statically defined vm_config[].cpu_affinity_bitmap. Tracked-On: #4616 Signed-off-by: Zide Chen <zide.chen@intel.com>
1 parent 0805eb9 commit 45cc2c5

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

devicemodel/core/main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ usage(int code)
164164
#endif
165165
" --vsbl: vsbl file path\n"
166166
" --ovmf: ovmf file path\n"
167+
" --pcpu_list: list of pCPUs assigned to this VM\n"
167168
" --part_info: guest partition info file path\n"
168169
" --enable_trusty: enable trusty for guest\n"
169170
" --debugexit: enable debug exit function\n"
@@ -718,6 +719,7 @@ sig_handler_term(int signo)
718719
enum {
719720
CMD_OPT_VSBL = 1000,
720721
CMD_OPT_OVMF,
722+
CMD_OPT_PCPU_LIST,
721723
CMD_OPT_PART_INFO,
722724
CMD_OPT_TRUSTY_ENABLE,
723725
CMD_OPT_VIRTIO_POLL_ENABLE,
@@ -757,6 +759,7 @@ static struct option long_options[] = {
757759
#endif
758760
{"vsbl", required_argument, 0, CMD_OPT_VSBL},
759761
{"ovmf", required_argument, 0, CMD_OPT_OVMF},
762+
{"pcpu_list", required_argument, 0, CMD_OPT_PCPU_LIST},
760763
{"part_info", required_argument, 0, CMD_OPT_PART_INFO},
761764
{"enable_trusty", no_argument, 0,
762765
CMD_OPT_TRUSTY_ENABLE},
@@ -874,6 +877,10 @@ main(int argc, char *argv[])
874877
errx(EX_USAGE, "invalid ovmf param %s", optarg);
875878
skip_pci_mem64bar_workaround = true;
876879
break;
880+
case CMD_OPT_PCPU_LIST:
881+
if (acrn_parse_pcpu_list(optarg) != 0)
882+
errx(EX_USAGE, "invalid pcpu param %s", optarg);
883+
break;
877884
case CMD_OPT_PART_INFO:
878885
if (acrn_parse_guest_part_info(optarg) != 0) {
879886
errx(EX_USAGE,

devicemodel/core/vmmapi.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,82 @@ check_api(int fd)
8585
}
8686

8787
static int devfd = -1;
88+
static uint64_t cpu_affinity_bitmap = 0UL;
89+
90+
static void add_one_pcpu(int pcpu_id)
91+
{
92+
if (cpu_affinity_bitmap & (1UL << pcpu_id)) {
93+
pr_err("%s: pcpu_id %d has been allocated to this VM.\n", __func__, pcpu_id);
94+
return;
95+
}
96+
97+
cpu_affinity_bitmap |= (1UL << pcpu_id);
98+
}
99+
100+
/*
101+
* example options:
102+
* --pcpu_list 1,2,3
103+
* --pcpu_list 1-3
104+
* --pcpu_list 1,3,4-6
105+
* --pcpu_list 1,3,4-6,9
106+
*/
107+
int acrn_parse_pcpu_list(char *opt)
108+
{
109+
char *str, *cp;
110+
int pcpu_id;
111+
int pcpu_start, pcpu_end;
112+
113+
cp = strdup(opt);
114+
if (!cp) {
115+
pr_err("%s: strdup returns NULL\n", __func__);
116+
return -1;
117+
}
118+
119+
while (cp) {
120+
str = strpbrk(cp, ",-");
121+
122+
/* no more entries delimited by ',' or '-' */
123+
if (!str) {
124+
if (!dm_strtoi(cp, NULL, 10, &pcpu_id)) {
125+
add_one_pcpu(pcpu_id);
126+
break;
127+
}
128+
}
129+
130+
if (*str == ',') {
131+
/* after this, 'cp' points to the character after ',' */
132+
str = strsep(&cp, ",");
133+
134+
/* parse the entry before ',' */
135+
if (dm_strtoi(str, NULL, 10, &pcpu_id)) {
136+
return -1;
137+
}
138+
add_one_pcpu(pcpu_id);
139+
}
140+
141+
if (*str == '-') {
142+
str = strsep(&cp, "-");
143+
144+
/* parse the entry before and after '-' respectively */
145+
if (dm_strtoi(str, NULL, 10, &pcpu_start) || dm_strtoi(cp, NULL, 10, &pcpu_end)) {
146+
return -1;
147+
}
148+
149+
if (pcpu_end <= pcpu_start) {
150+
return -1;
151+
}
152+
153+
for (; pcpu_start <= pcpu_end; pcpu_start++) {
154+
add_one_pcpu(pcpu_start);
155+
}
156+
157+
/* skip the ',' after pcpu_end */
158+
str = strsep(&cp, ",");
159+
}
160+
}
161+
162+
return 0;
163+
}
88164

89165
struct vmctx *
90166
vm_create(const char *name, uint64_t req_buf, int *vcpu_num)
@@ -150,6 +226,9 @@ vm_create(const char *name, uint64_t req_buf, int *vcpu_num)
150226
create_vm.vm_flag &= (~GUEST_FLAG_IO_COMPLETION_POLLING);
151227
}
152228

229+
/* command line arguments specified CPU affinity could overwrite HV's static configuration */
230+
create_vm.cpu_affinity = cpu_affinity_bitmap;
231+
153232
if (is_rtvm) {
154233
create_vm.vm_flag |= GUEST_FLAG_RT;
155234
create_vm.vm_flag |= GUEST_FLAG_IO_COMPLETION_POLLING;

devicemodel/include/vmmapi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ int vm_set_ptdev_intx_info(struct vmctx *ctx, uint16_t virt_bdf,
134134
int vm_reset_ptdev_intx_info(struct vmctx *ctx, uint16_t virt_bdf,
135135
uint16_t phys_bdf, int virt_pin, bool pic_pin);
136136

137+
int acrn_parse_pcpu_list(char *arg);
137138
int vm_create_vcpu(struct vmctx *ctx, uint16_t vcpu_id);
138139
int vm_set_vcpu_regs(struct vmctx *ctx, struct acrn_set_vcpu_regs *cpu_regs);
139140

0 commit comments

Comments
 (0)