dm: set iothread nice value to PRIO_MIN

To improve the performance of the virtual device who utilizes iothread
(such as virtio-blk), this patch sets iothread nice value to PRIO_MIN,
so that it could get higher priority on scheduling.

This patch does:
 - introduce `set_thread_priority` to set the priority of the current running
   thread.
   The priority could be any value in the range PRIO_MIN to PRIO_MAX.
   Lower numerical value causes more favorable scheduling.

 - set iothread nice value to PRIO_MIN.

Tracked-On: #8612

Signed-off-by: Jian Jun Chen <jian.jun.chen@intel.com>
Signed-off-by: Shiqing Gao <shiqing.gao@intel.com>
Acked-by: Wang, Yu1 <yu1.wang@intel.com>
This commit is contained in:
Jian Jun Chen 2023-07-06 11:13:35 +08:00 committed by acrnsi-robot
parent 7e6a239646
commit 63d41a75fa
3 changed files with 72 additions and 0 deletions

View File

@ -18,6 +18,7 @@
#include "iothread.h"
#include "log.h"
#include "mevent.h"
#include "dm.h"
#define MEVENT_MAX 64
@ -35,6 +36,8 @@ io_thread(void *arg)
int i, n;
struct iothread_ctx *ioctx_x = (struct iothread_ctx *)arg;
set_thread_priority(PRIO_IOTHREAD, true);
while(ioctx_x->started) {
n = epoll_wait(ioctx_x->epfd, eventlist, MEVENT_MAX, -1);
if (n < 0) {

View File

@ -247,6 +247,64 @@ high_bios_size(void)
return roundup2(size, 2 * MB);
}
/*
* set nice value of current pthread
* input range: [-20, 19]
* Lower priorities cause more favorable scheduling.
*/
void
set_thread_priority(int priority, bool reset_on_fork)
{
int ret, policy;
char tname[MAXCOMLEN + 1];
struct sched_param sp = { .sched_priority = 0 };
memset(tname, 0, sizeof(tname));
pthread_getname_np(pthread_self(), tname, sizeof(tname));
policy = sched_getscheduler(0);
if (policy == -1) {
pr_err("%s(%s), sched_getscheduler failed, errno = %d\n",
__func__, tname, errno);
}
if ((policy & SCHED_RESET_ON_FORK) && !reset_on_fork)
policy &= ~SCHED_RESET_ON_FORK;
else if (((policy & SCHED_RESET_ON_FORK) == 0) && reset_on_fork)
policy |= SCHED_RESET_ON_FORK;
ret = sched_setscheduler(0, policy, &sp);
if (ret == -1) {
pr_err("%s(%s), sched_setscheduler failed, errno = %d\n",
__func__, tname, errno);
}
errno = 0;
ret = getpriority(PRIO_PROCESS, 0);
if (errno && (ret == -1)) {
pr_err("%s(%s), getpriority failed, errno = %d\n",
__func__, tname, errno);
} else {
pr_info("%s(%s), orig prio = %d\n",
__func__, tname, ret);
}
ret = setpriority(PRIO_PROCESS, 0, priority);
if (ret) {
pr_err("%s(%s), setpriority failed, errno = %d\n",
__func__, tname, errno);
}
ret = getpriority(PRIO_PROCESS, 0);
if (ret != priority) {
pr_err("%s(%s), getpriority(%d) != setpriority(%d)\n",
__func__, tname, ret, priority);
} else {
pr_info("%s(%s), new priority = %d\n",
__func__, tname, ret);
}
}
static void *
start_thread(void *param)
{

View File

@ -30,6 +30,8 @@
#define _DM_H_
#include <stdbool.h>
#include <sys/resource.h>
#include "types.h"
#include "dm_string.h"
#include "acrn_common.h"
@ -52,6 +54,14 @@ extern bool ssram;
extern bool vtpm2;
extern bool is_winvm;
enum acrn_thread_prio {
PRIO_VCPU = PRIO_MIN,
PRIO_IOTHREAD = PRIO_MIN,
PRIO_VIRTIO_SND,
PRIO_VIRTIO_IPU,
PRIO_VIRTIO_GPU
};
/**
* @brief Convert guest physical address to host virtual address
*
@ -67,4 +77,5 @@ int guest_cpu_num(void);
size_t high_bios_size(void);
void init_debugexit(void);
void deinit_debugexit(void);
void set_thread_priority(int priority, bool reset_on_fork);
#endif