我们最近购买了一些新服务器,但 memcpy()
性能不佳。与我们的笔记本电脑相比,服务器上的 memcpy()
性能要慢 3 倍。
服务器规格
- 机箱和主板:SUPER MICRO 1027GR-TRF
- CPU:2x Intel Xeon E5-2680 @ 2.70 Ghz
- 内存:8x 16GB DDR3 1600MHz
我还在另一台规格稍高的服务器上进行测试,并看到与上述服务器相同的结果。
服务器 2 规格
- 机箱和主板:SUPER MICRO 10227GR-TRFT
- CPU:2x Intel Xeon E5-2650 v2 @ 2.6 Ghz
- 内存:8x 16GB DDR3 1866MHz
笔记本电脑规格
- 机箱:联想W530
- CPU:1x Intel Core i7 i7-3720QM @ 2.6Ghz
- 内存:4x 4GB DDR3 1600MHz
操作系统
$ cat /etc/redhat-release
Scientific Linux release 6.5 (Carbon)
$ uname -a
Linux r113 2.6.32-431.1.2.el6.x86_64 #1 SMP Thu Dec 12 13:59:19 CST 2013 x86_64 x86_64 x86_64 GNU/Linux
编译器(在所有系统上)
$ gcc --version
gcc (GCC) 4.6.1
还根据@stefan 的建议使用 gcc 4.8.2 进行了测试。编译器之间没有性能差异。
测试代码
下面的测试代码是一个固定测试,用于复制我在生产代码中看到的问题。我知道这个基准很简单,但它能够利用和识别我们的问题。该代码在它们之间创建了两个 1GB 缓冲区和 memcpy,对 memcpy 调用进行计时。您可以使用以下命令在命令行上指定备用缓冲区大小: ./big_memcpy_test [SIZE_BYTES]
#include <chrono>
#include <cstring>
#include <iostream>
#include <cstdint>
class Timer
{
public:
Timer()
: mStart(),
mStop()
{
update();
}
void update()
{
mStart = std::chrono::high_resolution_clock::now();
mStop = mStart;
}
double elapsedMs()
{
mStop = std::chrono::high_resolution_clock::now();
std::chrono::milliseconds elapsed_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(mStop - mStart);
return elapsed_ms.count();
}
private:
std::chrono::high_resolution_clock::time_point mStart;
std::chrono::high_resolution_clock::time_point mStop;
};
std::string formatBytes(std::uint64_t bytes)
{
static const int num_suffix = 5;
static const char* suffix[num_suffix] = { "B", "KB", "MB", "GB", "TB" };
double dbl_s_byte = bytes;
int i = 0;
for (; (int)(bytes / 1024.) > 0 && i < num_suffix;
++i, bytes /= 1024.)
{
dbl_s_byte = bytes / 1024.0;
}
const int buf_len = 64;
char buf[buf_len];
// use snprintf so there is no buffer overrun
int res = snprintf(buf, buf_len,"%0.2f%s", dbl_s_byte, suffix[i]);
// snprintf returns number of characters that would have been written if n had
// been sufficiently large, not counting the terminating null character.
// if an encoding error occurs, a negative number is returned.
if (res >= 0)
{
return std::string(buf);
}
return std::string();
}
void doMemmove(void* pDest, const void* pSource, std::size_t sizeBytes)
{
memmove(pDest, pSource, sizeBytes);
}
int main(int argc, char* argv[])
{
std::uint64_t SIZE_BYTES = 1073741824; // 1GB
if (argc > 1)
{
SIZE_BYTES = std::stoull(argv[1]);
std::cout << "Using buffer size from command line: " << formatBytes(SIZE_BYTES)
<< std::endl;
}
else
{
std::cout << "To specify a custom buffer size: big_memcpy_test [SIZE_BYTES] \n"
<< "Using built in buffer size: " << formatBytes(SIZE_BYTES)
<< std::endl;
}
// big array to use for testing
char* p_big_array = NULL;
////////////
// malloc
{
Timer timer;
p_big_array = (char*)malloc(SIZE_BYTES * sizeof(char));
if (p_big_array == NULL)
{
std::cerr << "ERROR: malloc of " << SIZE_BYTES << " returned NULL!"
<< std::endl;
return 1;
}
std::cout << "malloc for " << formatBytes(SIZE_BYTES) << " took "
<< timer.elapsedMs() << "ms"
<< std::endl;
}
////////////
// memset
{
Timer timer;
// set all data in p_big_array to 0
memset(p_big_array, 0xF, SIZE_BYTES * sizeof(char));
double elapsed_ms = timer.elapsedMs();
std::cout << "memset for " << formatBytes(SIZE_BYTES) << " took "
<< elapsed_ms << "ms "
<< "(" << formatBytes(SIZE_BYTES / (elapsed_ms / 1.0e3)) << " bytes/sec)"
<< std::endl;
}
////////////
// memcpy
{
char* p_dest_array = (char*)malloc(SIZE_BYTES);
if (p_dest_array == NULL)
{
std::cerr << "ERROR: malloc of " << SIZE_BYTES << " for memcpy test"
<< " returned NULL!"
<< std::endl;
return 1;
}
memset(p_dest_array, 0xF, SIZE_BYTES * sizeof(char));
// time only the memcpy FROM p_big_array TO p_dest_array
Timer timer;
memcpy(p_dest_array, p_big_array, SIZE_BYTES * sizeof(char));
double elapsed_ms = timer.elapsedMs();
std::cout << "memcpy for " << formatBytes(SIZE_BYTES) << " took "
<< elapsed_ms << "ms "
<< "(" << formatBytes(SIZE_BYTES / (elapsed_ms / 1.0e3)) << " bytes/sec)"
<< std::endl;
// cleanup p_dest_array
free(p_dest_array);
p_dest_array = NULL;
}
////////////
// memmove
{
char* p_dest_array = (char*)malloc(SIZE_BYTES);
if (p_dest_array == NULL)
{
std::cerr << "ERROR: malloc of " << SIZE_BYTES << " for memmove test"
<< " returned NULL!"
<< std::endl;
return 1;
}
memset(p_dest_array, 0xF, SIZE_BYTES * sizeof(char));
// time only the memmove FROM p_big_array TO p_dest_array
Timer timer;
// memmove(p_dest_array, p_big_array, SIZE_BYTES * sizeof(char));
doMemmove(p_dest_array, p_big_array, SIZE_BYTES * sizeof(char));
double elapsed_ms = timer.elapsedMs();
std::cout << "memmove for " << formatBytes(SIZE_BYTES) << " took "
<< elapsed_ms << "ms "
<< "(" << formatBytes(SIZE_BYTES / (elapsed_ms / 1.0e3)) << " bytes/sec)"
<< std::endl;
// cleanup p_dest_array
free(p_dest_array);
p_dest_array = NULL;
}
// cleanup
free(p_big_array);
p_big_array = NULL;
return 0;
}
要构建的 CMake 文件
project(big_memcpy_test)
cmake_minimum_required(VERSION 2.4.0)
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# create verbose makefiles that show each command line as it is issued
set( CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "Verbose" FORCE )
# release mode
set( CMAKE_BUILD_TYPE Release )
# grab in CXXFLAGS environment variable and append C++11 and -Wall options
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -march=native -mtune=native" )
message( INFO "CMAKE_CXX_FLAGS = ${CMAKE_CXX_FLAGS}" )
# sources to build
set(big_memcpy_test_SRCS
main.cpp
)
# create an executable file named "big_memcpy_test" from
# the source files in the variable "big_memcpy_test_SRCS".
add_executable(big_memcpy_test ${big_memcpy_test_SRCS})
试验结果
Buffer Size: 1GB | malloc (ms) | memset (ms) | memcpy (ms) | NUMA nodes (numactl --hardware)
---------------------------------------------------------------------------------------------
Laptop 1 | 0 | 127 | 113 | 1
Laptop 2 | 0 | 180 | 120 | 1
Server 1 | 0 | 306 | 301 | 2
Server 2 | 0 | 352 | 325 | 2
如您所见,我们服务器上的 memcpy()
和 memset()
比我们的笔记本电脑慢得多。
不同的缓冲区大小
我尝试了从 100MB 到 5GB 的缓冲区,结果都相似(服务器比笔记本电脑慢)。
NUMA 亲和力
我读到有人在使用 NUMA 时遇到性能问题,所以我尝试使用 numactl
设置 CPU 和内存关联,但结果保持不变。
服务器 NUMA 硬件:
$ numactl --hardware
available: 2 nodes (0-1)
node 0 cpus: 0 1 2 3 4 5 6 7 16 17 18 19 20 21 22 23
node 0 size: 65501 MB
node 0 free: 62608 MB
node 1 cpus: 8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31
node 1 size: 65536 MB
node 1 free: 63837 MB
node distances:
node 0 1
0: 10 21
1: 21 10
笔记本电脑 NUMA 硬件:
$ numactl --hardware
available: 1 nodes (0)
node 0 cpus: 0 1 2 3 4 5 6 7
node 0 size: 16018 MB
node 0 free: 6622 MB
node distances:
node 0
0: 10
设置 NUMA 亲和性:
$ numactl --cpunodebind=0 --membind=0 ./big_memcpy_test
非常感谢任何解决此问题的帮助。
编辑:GCC 选项
我尝试使用不同的 GCC 选项进行编译:
编译 -march
和 -mtune
设置为本机
g++ -std=c++0x -Wall -march=native -mtune=native -O3 -DNDEBUG -o big_memcpy_test main.cpp
结果:完全相同的性能
用 -O2
而不是 -O3
编译
g++ -std=c++0x -Wall -march=native -mtune=native -O2 -DNDEBUG -o big_memcpy_test main.cpp
结果:完全相同的性能
编辑:更改 memset()
写入 0xF 而不是 0 以避免 NULL 页
结果:完全相同的性能
编辑:Cachebench 结果
为了排除我的测试程序过于简单,我下载了一个真正的基准测试程序 LLCCacheBench 。
我分别在每台机器上构建了基准测试以避免架构问题。下面是我的结果。
请注意,非常大的差异是较大缓冲区大小的性能。最后测试的大小 (16777216) 在笔记本电脑上以 18849.29 MB/秒的速度运行,在服务器上以 6710.40 的速度运行。这大约是性能差异的 3 倍。您还可以注意到,服务器的性能下降比笔记本电脑要严重得多。
编辑: memmove()
比服务器上的 memcpy()
2 倍
根据一些实验,我尝试在我的测试用例中使用 memmove()
而不是 memcpy()
并且发现服务器的性能提高了 2 倍。 memmove()
在笔记本电脑上的运行速度比 memcpy()
慢,但奇怪的是运行速度与服务器上的 memmove()
相同。这就引出了一个问题,为什么 memcpy()
这么慢?
更新代码以测试 memmove()
以及 memcpy()
。我不得不将 memmove()
包装在一个函数中,因为如果我将其保留为内联 GCC 会对其进行优化并执行与 memcpy()
完全相同的操作(我假设 GCC 将其优化为 memcpy()
因为它知道这些位置没有重叠)。
更新结果:
Buffer Size: 1GB | malloc (ms) | memset (ms) | memcpy (ms) | memmove() | NUMA nodes (numactl --hardware)
---------------------------------------------------------------------------------------------------------
Laptop 1 | 0 | 127 | 113 | 161 | 1
Laptop 2 | 0 | 180 | 120 | 160 | 1
Server 1 | 0 | 306 | 301 | 159 | 2
Server 2 | 0 | 352 | 325 | 159 | 2
编辑:天真 memcpy()
我已经实现了自己的 memcpy()
的幼稚版本并对其进行了测试:
naiveMemcpy()
来源
void naiveMemcpy(void* pDest, const void* pSource, std::size_t sizeBytes)
{
char* p_dest = (char*)pDest;
const char* p_source = (const char*)pSource;
for (std::size_t i = 0; i < sizeBytes; ++i)
{
*p_dest++ = *p_source++;
}
}
naiveMemcpy()
结果与 memcpy()
相比:
Buffer Size: 1GB | memcpy (ms) | memmove(ms) | naiveMemcpy()
------------------------------------------------------------
Laptop 1 | 113 | 161 | 160
Server 1 | 301 | 159 | 159
Server 2 | 325 | 159 | 159
编辑:装配输出
简单 memcpy()
来源:
#include <cstring>
#include <cstdlib>
int main(int argc, char* argv[])
{
size_t SIZE_BYTES = 1073741824; // 1GB
char* p_big_array = (char*)malloc(SIZE_BYTES * sizeof(char));
char* p_dest_array = (char*)malloc(SIZE_BYTES * sizeof(char));
memset(p_big_array, 0xA, SIZE_BYTES * sizeof(char));
memset(p_dest_array, 0xF, SIZE_BYTES * sizeof(char));
memcpy(p_dest_array, p_big_array, SIZE_BYTES * sizeof(char));
free(p_dest_array);
free(p_big_array);
return 0;
}
组装输出:这在服务器和笔记本电脑上完全相同。
.file "main_memcpy.cpp"
.section .text.startup,"ax",@progbits
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB25:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movl $1073741824, %edi
pushq %rbx
.cfi_def_cfa_offset 24
.cfi_offset 3, -24
subq $8, %rsp
.cfi_def_cfa_offset 32
call malloc
movl $1073741824, %edi
movq %rax, %rbx
call malloc
movl $1073741824, %edx
movq %rax, %rbp
movl $10, %esi
movq %rbx, %rdi
call memset
movl $1073741824, %edx
movl $15, %esi
movq %rbp, %rdi
call memset
movl $1073741824, %edx
movq %rbx, %rsi
movq %rbp, %rdi
call memcpy
movq %rbp, %rdi
call free
movq %rbx, %rdi
call free
addq $8, %rsp
.cfi_def_cfa_offset 24
xorl %eax, %eax
popq %rbx
.cfi_def_cfa_offset 16
popq %rbp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE25:
.size main, .-main
.ident "GCC: (GNU) 4.6.1"
.section .note.GNU-stack,"",@progbits
进步!!!! asmlib
根据@tbenson 的建议,我尝试使用 memcpy()
的 asmlib 版本运行。我的结果最初很差,但是在将 SetMemcpyCacheLimit()
更改为 1GB(我的缓冲区大小)之后,我的运行速度与我幼稚的 for 循环相当!
坏消息是 memmove()
的 asmlib 版本比 glibc 版本慢,它现在以 300 毫秒的速度运行(与 memcpy()
的 glibc 版本相当)。奇怪的是,当我在笔记本电脑上大量使用 SetMemcpyCacheLimit()
时,它会损害性能……
在下面的结果中,标有 SetCache 的行将 SetMemcpyCacheLimit 设置为 1073741824。没有 SetCache 的结果不会调用 SetMemcpyCacheLimit()
。
使用 asmlib 中的函数的结果:
Buffer Size: 1GB | memcpy (ms) | memmove(ms) | naiveMemcpy()
------------------------------------------------------------
Laptop | 136 | 132 | 161
Laptop SetCache | 182 | 137 | 161
Server 1 | 305 | 302 | 164
Server 1 SetCache | 162 | 303 | 164
Server 2 | 300 | 299 | 166
Server 2 SetCache | 166 | 301 | 166
开始倾向于缓存问题,但这会导致什么?
原文由 nick 发布,翻译遵循 CC BY-SA 4.0 许可协议
[我会发表评论,但没有足够的声誉这样做。]
我有一个类似的系统并看到类似的结果,但可以添加一些数据点:
memcpy
的方向(即转换为*p_dest-- = *p_src--
),那么你可能会得到比正向更差的性能(对我来说约为 637 毫秒)。 glibc 2.12 中的memcpy()
发生了变化,这暴露了几个在重叠缓冲区( http://lwn.net/Articles/414467/ )上调用memcpy
的错误,我相信这个问题是由切换到向后运行的memcpy
版本引起的。因此,向后与向前拷贝可以解释memcpy()
/memmove()
差异。memcpy()
实现切换到非临时存储(不缓存)用于大缓冲区(即大于最后一级缓存)。我测试了 Agner Fog 的 memcpy 版本( http://www.agner.org/optimize/#asmlib ),发现它的速度与glibc
中的版本大致相同。但是,asmlib
有一个功能(SetMemcpyCacheLimit
),允许设置使用非临时存储的阈值。将该限制设置为 8GiB(或仅大于 1 GiB 缓冲区)以避免非临时存储在我的情况下使性能翻倍(时间降至 176 毫秒)。当然,这仅与正向的幼稚表现相匹配,因此并不出色。memcpy
(104ms) 的速度为 9.6 GB/s。 Haswell 系统上的 RAM 为 DDR3-1600(与其他系统相同)。更新
/proc/cpuinfo
,内核的时钟频率为 3 GHz。然而,这奇怪地降低了大约 10% 的内存性能。