0

我正在尝试将某些流程优先于其他流程。这是我正在使用的主要脚本,它模拟了一个 CPU 密集型进程:

simple_app.py

import os
from multiprocessing import Pool, cpu_count

def f(x):
    while True:
        x*x

if __name__ == '__main__':

    cpu = cpu_count()
    pid = os.getpid()

    print('-' * 20)
    print('pid: {}'.format(pid))
    print('Utilizing {} cores'.format(cpu))
    print('Current niceness: {}'.format(os.nice(0)))
    print('-' * 20)

    pool = Pool(cpu)
    pool.map(f, range(cpu))

我的下一步是生成大量(具体而言,在本例中为 9 个)运行此代码的进程:

simple_runner.sh

# Start with lowest priority
nice -19 python3 simple_app.py &
# Much higher priority
nice -0 python3 simple_app.py &
# Lower priority spawned
nice -10 python3 simple_app.py &
# Higher priority again
nice -7 python3 simple_app.py &
# Highest priority yet
nice -1 python3 simple_app.py &
# Highest priority yet
nice -0 python3 simple_app.py &
# Highest priority yet
nice -0 python3 simple_app.py &
# Highest priority yet
nice -0 python3 simple_app.py &
# Highest priority yet
nice -0 python3 simple_app.py

然后我监视每个进程,报告子 CPU 利用率,这里:

process_reporting_server.py

import os
import time
import argparse
import pprint
from multiprocessing import Pool, cpu_count

import psutil

def most_recent_process_info(pid, interval=0.5):
    while True:
        proc = psutil.Process(pid)
        children_cpu_percent = [child.cpu_percent(interval) for child in proc.children()]
        children_cpu_percent_mean = sum(children_cpu_percent) / len(children_cpu_percent) if children_cpu_percent else -1.
        print('Time: {}, PID: {}, niceness: {}, average child CPU percent: {:.2f}'.format(
            time.ctime(),
            pid,
            proc.nice(),
            children_cpu_percent_mean)
        )

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument('-p', '--pids', type=str, help='Whitespace-delimited string containing PIDs', dest='pids')
    parser.add_argument('-s', '--seconds', type=int, help='Seconds to sleep', default=10, dest='seconds')
    args = parser.parse_args()

    pids = list(map(int, args.pids.split()))

    pool = Pool(len(pids))
    pool.map(most_recent_process_info, pids)

我想看看被赋予较低 niceness 值的进程是否实际上被优先考虑。所以这就是我所做的:

运行simple_app_runner.sh

$ ./simple_app_runner.sh 
--------------------
pid: 45036
Utilizing 8 cores
Current niceness: 0
--------------------
--------------------
pid: 45030
Utilizing 8 cores
Current niceness: 19
--------------------
--------------------
pid: 45034
Utilizing 8 cores
Current niceness: 1
--------------------
--------------------
pid: 45032
Utilizing 8 cores
Current niceness: 10
--------------------
--------------------
pid: 45033
Utilizing 8 cores
Current niceness: 7
--------------------
--------------------
pid: 45037
Utilizing 8 cores
Current niceness: 0
--------------------
--------------------
pid: 45038
Utilizing 8 cores
Current niceness: 0
--------------------
--------------------
pid: 45031
Utilizing 8 cores
Current niceness: 0
--------------------
--------------------
pid: 45035
Utilizing 8 cores
Current niceness: 0
--------------------

然后,这是报告:

$ python3 process_reporting_server.py -p '45036 45030 45034 45032 45033 45037 45038 45031 45035'

稍微清理一下并使用 pandas 进行分析,我们看到在五分钟的时间间隔内,指定的好坏似乎并不重要

>>> df.groupby('nice')['mean_child_cpu'].max()
nice
0.0     10.50
1.0      9.75
7.0      8.28
10.0     8.50
19.0    21.97

我在这里完全错过了什么吗?为什么我指定的 niceness 似乎不会影响 CPU 资源的优先级?

4

1 回答 1

1

我不认为你缺少任何东西。我的经验是,最重要的过程是第一优先,其他人都在为剩下的东西而战。如果仅将一个进程调整为 -1 并将其余进程保留为 -0,您可能会得到相同的结果(对于像这样的纯 cpu 绑定进程)。

而且,这样做的原因是因为人们通常真的不希望优先排序像我们有时期望的那样硬核。就像现在我发布这个,而我的平均负载超过 200,并且有一堆经过优化的(更高优先级)进程正在运行。如果所有这些过程都是真正的猪,那么它就不会“很好”。我喜欢我仍然可以在所有 cpu 负载持续的情况下使用我的浏览器。

在某一时刻,我的印象是您可以更改优先级队列,至少在某些 unix 上是这样。我隐约记得我的一些客户要求我们这样做,我们(系统管理员团队)说“不是一个好主意”,客户要求我们这样做,我们这样做“,然后客户要求我们撤消它。调度是棘手的业务。

这是对幕后发生的事情的介绍:http ://www.cs.montana.edu/~chandrima.sarkar/AdvancedOS/SchedulingLinux/index.html 特别注意底部 - “算法不能很好地扩展”,这与我的第一段密切相关。

于 2018-11-28T17:31:21.977 回答