【发布时间】:2018-05-12 05:01:21
【问题描述】:
大家好,我有以下 Python 程序,用于对多处理应用程序的性能进行基准测试。
#
# Date : 09/May/2018
# Platform : Linux
#
import os
import sys
import ctypes
import signal
import multiprocessing as mp
ncpu = 4
counter = 0
child_index = 0
process_list = []
shared_array = None
def HandleSignal(signum, frame) :
total = 0
print("Parent timeout hence terminate child")
[hProc.terminate() for hProc in process_list]
[hProc.join() for hProc in process_list]
for each_count in shared_array :
total += each_count
print("{:,}".format(total))
def ChildHandleSignal(signum, frame) :
# print("{} - {} : {:,}".format(child_index, os.getpid(), counter))
shared_array[child_index] = counter
sys.exit(0)
def entry_point(index, sarr) :
global counter
global child_index
global shared_array
child_index = index
shared_array = sarr
signal.signal(signal.SIGTERM, ChildHandleSignal)
while True : counter += 1
return
ncpu = int(sys.argv[1])
maxcpu = os.cpu_count()
if ncpu > maxcpu :
print("Number of CPU greater than maximum CPU")
print("Setting number of CPU to maximum")
ncpu = maxcpu
shared_array = mp.Array(ctypes.c_int64, range(ncpu))
signal.signal(signal.SIGALRM, HandleSignal)
signal.alarm(5)
for I in range(ncpu) :
p1 = mp.Process(target=entry_point, args=(I, shared_array, ))
process_list.append(p1)
p1.start()
# I tried both with and with-out the below
# statement. The outputs are much similar
os.sched_setaffinity(p1.pid, {I})
我已经在两台不同的机器上运行了这个程序
- 在 8 个 VCPU 英特尔处理器上运行 Cent OS 7.x 的 Google 云虚拟机
- Cent OS 7.X Linux 机器,48 核 Intel 处理器
输出与使用的核心数量的关系图如下所示。我从中观察到输出增加,直到进程数达到核心数/ 2,然后下降。有人可以解释这种行为吗?
【问题讨论】:
-
疯狂猜测 - 超线程
-
但超线程应该会提高性能,但这里的性能下降了
标签: linux python-3.x concurrency multiprocessing centos7