Task scheduling method and apparatus
When scheduling a task to be executed by a target virtual machine, a scheduler first obtains at least one top-priority task in a to-be-executed task queue of the first target virtual machine to first ensure that the top-priority task is preferentially executed, determines a first task with a minimum virtual runtime from the at least one top-priority task, and controls the target virtual machine to execute the first task. This ensures fair task scheduling of at least one task with the same priority. The scheduler can ensure both preferential scheduling of a high-priority task and fair scheduling of tasks with the same priority when scheduling the task on the first target virtual machine.
1 . A task scheduling method, comprising:
obtaining priorities of a plurality of tasks to be executed by a target virtual machine;
determining one or more tasks having a highest priority among the plurality of tasks based on the priorities of the plurality of tasks;
determining, from the one or more tasks having the highest priority, a first task with a minimum virtual runtime, the virtual runtime being determined based on an actual execution time of the task and a weight of the task relative to weights of other tasks to be executed, wherein the weight of each task is predetermined based on a priority of the task; and
controlling the target virtual machine to execute the first task.
2 . The method according to claim 1 , wherein the obtaining priorities of a plurality of tasks to be executed by a target virtual machine comprises:
determining, based on a to-be-executed task queue stored in a first scheduler of the target virtual machine, the priorities of the plurality of tasks to be executed by the target virtual machine.
3 . The method according to claim 2 , wherein the determining at least one top-priority task in the plurality of tasks comprises:
traversing priorities of all tasks in the to-be-executed task queue stored in the first scheduler, and determining the at least one top-priority task from the to-be-executed task queue.
4 . The method according to claim 2 , wherein the determining at least one top-priority task in the plurality of tasks comprises:
determining the at least one top-priority task in the plurality of tasks based on priority record information stored in the first scheduler, wherein the priority record information records priorities of all tasks in the to-be-executed task queue in the first scheduler.
5 . The method according to claim 2 , wherein the determining at least one top-priority task in the plurality of tasks comprises:
determining the at least one top-priority task based on a task in a queue that is stored in the first scheduler and corresponds to a top priority, wherein the first scheduler stores the to-be-executed tasks by using a plurality of queues, and each of the plurality of queues corresponds to a different priority.
6 . A task scheduling apparatus, comprising at least one processor and at least one memory storing instructions that, when executed by the at least one processor, cause the task scheduling apparatus to perform operations comprising:
obtaining priorities of a plurality of tasks to be executed by a target virtual machine;
determining one or more tasks having a highest priority among the plurality of tasks based on the priorities of the plurality of tasks;
determining from the one or more tasks having the highest priority, a first task with a minimum virtual runtime, the virtual runtime being determined based on an actual execution time of the task and a weight of the task relative to weights of other tasks to be executed, wherein the weight of each task is predetermined based on a priority of the task; and
controlling the target virtual machine to execute the first task.
7 . The task scheduling apparatus according to claim 6 , wherein the obtaining of the priorities of the plurality of tasks to be executed by the target virtual machine comprises:
determining, based on a to-be-executed task queue stored in a first scheduler of the target virtual machine, the priorities of the plurality of tasks to be executed by the target virtual machine.
8 . The task scheduling apparatus according to claim 7 , wherein the obtaining of the priorities of the plurality of tasks to be executed by the target virtual machine further comprises:
traversing priorities of all tasks in the to-be-executed task queue stored in the first scheduler, and determining the at least one top-priority task from the to-be-executed task queue.
9 . The task scheduling apparatus according to claim 7 , wherein the obtaining of the priorities of the plurality of tasks to be executed by the target virtual machine further comprises:
determining the at least one top-priority task in the plurality of tasks based on priority record information stored in the first scheduler, wherein the priority record information records priorities of all tasks in the to-be-executed task queue in the first scheduler.
10 . The task scheduling apparatus according to claim 7 , wherein the obtaining of the priorities of the plurality of tasks to be executed by the target virtual machine further comprises:
determining the at least one top-priority task based on a task in a queue that is stored in the first scheduler and corresponds to a top priority, wherein the first scheduler stores the to-be-executed tasks by using a plurality of queues, and each of the plurality of queues corresponds to a different priority.
11 . A non-transitory computer readable storage medium storing a computer program including instructions, that when executed by a computer, cause the computer to perform operations comprising:
obtaining priorities of a plurality of tasks to be executed by a target virtual machine;
determining one or more tasks having a highest priority among the plurality of tasks;
determining, from the one or more tasks having the highest priority, a first task with a minimum virtual runtime, the virtual runtime being determined based on an actual execution time of the task and a weight of the task relative to weights of other tasks to be executed, wherein the weight of each task is predetermined based on a priority of the task; and
controlling the target virtual machine to execute the first task.
12 . The non-transitory computer readable storage medium according to claim 11 , wherein the obtaining of the priorities of the plurality of tasks to be executed by the target virtual machine comprises:
determining, based on a to-be-executed task queue stored in a first scheduler of the target virtual machine, the priorities of the plurality of tasks to be executed by the target virtual machine.
13 . The non-transitory computer readable storage medium according to claim 12 , wherein the obtaining of the priorities of the plurality of tasks to be executed by the target virtual machine further comprises:
traversing priorities of all tasks in the to-be-executed task queue stored in the first scheduler, and determining the at least one top-priority task from the to-be-executed task queue.
14 . The non-transitory computer readable storage medium according to claim 12 , wherein the obtaining of the priorities of the plurality of tasks to be executed by the target virtual machine further comprises:
determining the at least one top-priority task in the plurality of tasks based on priority record information stored in the first scheduler, wherein the priority record information records priorities of all tasks in the to-be-executed task queue in the first scheduler.
15 . The non-transitory computer readable storage medium according to claim 12 , wherein the obtaining of the priorities of the plurality of tasks to be executed by the target virtual machine further comprises:
determining the at least one top-priority task based on a task in a queue that is stored in the first scheduler and corresponds to a top priority, wherein the first scheduler stores the to-be-executed tasks by using a plurality of queues, and each of the plurality of queues corresponds to a different priority.