测试是否正确应用了精确性

2024-09-27 09:26:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图优先考虑某些过程。下面是我正在使用的模拟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)

我想看看给出较低准确度值的过程是否真的被优先考虑。所以我是这么做的:

运行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'

整理一下东西,用熊猫分析一下,我们发现在五分钟的时间间隔内,特定的精细度似乎并不重要:

>>> 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

我是不是完全错过了什么?为什么我指定的精确性似乎不会影响CPU资源的优先级?你知道吗


Tags: pyimportappcpucurrentsimplepidcores
1条回答
网友
1楼 · 发布于 2024-09-27 09:26:42

我不认为你遗漏了什么。我的经验是,最重要的过程得到优先考虑,其他人都在为剩下的东西而斗争。如果只将一个进程重新设置为-1,而将其余进程保留为-0,则可能会得到相同的结果(对于这样一个纯cpu绑定的进程)。你知道吗

而且,这是因为人们通常真的不希望优先顺序像我们有时所期望的那样。像现在这样,我发布这篇文章的时候,我的平均负载超过了200,并且有一堆优先级更高的进程在运行。如果所有这些进程都是真正的猪那么它就不会是“好的”。我喜欢我仍然可以使用我的浏览器与所有的cpu负载进行。你知道吗

有一次我觉得你可以改变优先级队列,至少在一些unix上是这样。我隐约记得我的一些客户要求我们这样做,我们(系统管理团队)说“不是一个好主意”,客户要求我们这样做,我们这样做”,然后客户要求我们撤销它。日程安排是件棘手的事。你知道吗

下面是关于封面下发生的事情的介绍:http://www.cs.montana.edu/~chandrima.sarkar/AdvancedOS/SchedulingLinux/index.html 请特别注意下面的部分-“算法的伸缩性不好”,这与我的第一段是并行不悖的。你知道吗

相关问题 更多 >

    热门问题