0

我试图使用多线程 vorbis 编码,但似乎使用带有 virtualbox 的仿真 ubuntu 时,当尝试使用 2 个线程而不是 1 个线程时,多线程测试实际上更慢。我采用了 theora 编码器的svn theora-multithread 版本,如所述在这个问题中。我的硬件是带有 2 个内核的 intel i7 haswell,并且我已经为 2 个 CPU 配置了 VirtualBox。为什么结果不如预期?我希望多线程编码速度更快,但速度要慢得多。

developer@developer-VirtualBox:~/theora-multithread/examples$ lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                2
On-line CPU(s) list:   0,1
Thread(s) per core:    1
Core(s) per socket:    2
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 69
Stepping:              1
CPU MHz:               0.000
BogoMIPS:              1687.55
L1d cache:             32K
L1d cache:             32K
L2d cache:             6144K
NUMA node0 CPU(s):     0,1
developer@developer-VirtualBox:~/theora-multithread/examples$ time ./encoder_example --number-of-threads 1 wavesound.wav tmp.yuv -o TEST-1-thread.ogg
File wavesound.wav is 16 bit 2 channel 44100 Hz RIFF WAV audio.
File tmp.yuv is 48x48 25.00 fps YUV12 video.
Number of Threads: 1
Compressing....
      0:46:32.08 audio: 66kbps video: 3kbps                 
done.


real    0m23.907s
user    0m12.319s
sys 0m1.623s
developer@developer-VirtualBox:~/theora-multithread/examples$ time ./encoder_example --number-of-threads 2 wavesound.wav tmp.yuv -o TEST-2-thread.ogg
File wavesound.wav is 16 bit 2 channel 44100 Hz RIFF WAV audio.
File tmp.yuv is 48x48 25.00 fps YUV12 video.
Number of Threads: 2
Compressing....
      0:46:32.08 audio: 66kbps video: 3kbps                 
done.


real    1m7.882s
user    0m22.370s
sys 0m33.304s
developer@developer-VirtualBox:~/theora-multithread/examples$ 

主机操作系统 (Win 8.1) 中的 CPU-Z 报告有关硬件的以下内容。

Processor 1         ID = 0
    Number of cores     2 (max 8)
    Number of threads   4 (max 16)
    Name            Intel Core i3/i5/i7 4xxx
    Codename        Haswell ULT
    Specification       Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
    Package (platform ID)   Socket 1168 BGA (0x6)
    CPUID           6.5.1
    Extended CPUID      6.45
    Core Stepping       C0
    Technology      22 nm
    TDP Limit       28 Watts
    Tjmax           100.0 °C
    Core Speed      798.4 MHz
    Multiplier x Bus Speed  8.0 x 99.8 MHz
    Stock frequency     2800 MHz
    Instructions sets   MMX, SSE, SSE2, SSE3, SSSE3, SSE4.1, SSE4.2, EM64T, VT-x, AES, AVX, AVX2, FMA3
    L1 Data cache       2 x 32 KBytes, 8-way set associative, 64-byte line size
    L1 Instruction cache    2 x 32 KBytes, 8-way set associative, 64-byte line size
    L2 cache        2 x 256 KBytes, 8-way set associative, 64-byte line size
    L3 cache        4 MBytes, 16-way set associative, 64-byte line size
    FID/VID Control     yes

测试 2

使用更大的音频文件进行测试(视频只是从静态 png 创建的虚拟视频),那么差异不是那么大(?)。

developer@developer-VirtualBox:~/theora-multithread/examples$ time ./encoder_example --number-of-threads 1 140715MIX.wav tmp.yuv -o OGGTEST-1-threadv1.ogg
File 140715MIX.wav is 16 bit 2 channel 44100 Hz RIFF WAV audio.
File tmp.yuv is 48x48 25.00 fps YUV12 video.
Number of Threads: 1
Compressing....
      0:53:30.76 audio: 73kbps video: 3kbps                 
done.


real    5m20.807s
user    3m21.943s
sys 0m6.727s
developer@developer-VirtualBox:~/theora-multithread/examples$ time ./encoder_example --number-of-threads 2 140715MIX.wav tmp.yuv -o OGGTEST-2-threadv2.ogg
File 140715MIX.wav is 16 bit 2 channel 44100 Hz RIFF WAV audio.
File tmp.yuv is 48x48 25.00 fps YUV12 video.
Number of Threads: 2
Compressing....
      0:53:30.76 audio: 73kbps video: 3kbps                 
done.


real    6m8.159s
user    3m45.750s
sys 0m27.579s

测试 2(仅视频)

测试视频只有这样我才能使用线程数重现加速:

developer@developer-VirtualBox:~/theora-multithread$ time ./examples/encoder_example -v 1 -a 1 --number-of-threads 1 stream.yuv > theora_testfile_1.ogg
File stream.yuv is 320x240 15.00 fps YUV12 video.
Number of Threads: 1
Compressing....
      0:00:07.60 audio: 0kbps video: 138kbps                 
done.


real    0m2.136s
user    0m1.920s
sys 0m0.083s
developer@developer-VirtualBox:~/theora-multithread$ time ./examples/encoder_example -v 1 -a 1 --number-of-threads 2 stream.yuv > theora_testfile_2.ogg
File stream.yuv is 320x240 15.00 fps YUV12 video.
Number of Threads: 2
Compressing....
      0:00:07.60 audio: 0kbps video: 139kbps                 
done.


real    0m2.043s
user    0m1.994s
sys 0m0.175s
4

1 回答 1

2

如果我不得不猜测,那是因为创建线程和上下文切换的开销比进程本身更昂贵。

请记住,内核线程比用户线程要昂贵得多。如果可以,请避免内核级线程。

为了获得更好的性能,请尝试并发执行更大的任务并避免触发上下文切换的操作(例如等待资源或阻塞)。

此外,重用线程资源。为每个任务创建一个新线程会影响应用程序的性能。池化线程可以帮助避免创建它们的开销。

于 2014-08-13T00:05:24.977 回答