Joã*_*eto 5 c++ memory performance copy-on-write linux-kernel
在Linux中,只要分支了一个进程,父进程的内存映射就会被克隆到子进程中。实际上,出于性能原因,这些页面被设置为写时复制 -最初它们是共享的,并且如果两个进程之一在其中之一上进行写操作,则将其克隆(MAP_PRIVATE
)。
这是获取正在运行的程序状态的快照的一种非常常见的机制-您进行了分叉,这使您可以在该时间点(一致)查看进程的内存。
我做了一个简单的基准测试,其中包含两个部分:
在某些情况下(机器/体系结构/内存位置/线程数/ ...),我能够比线程写入数组早得多地完成复制。
但是,当子进程退出时,htop
我仍然可以看到大部分CPU时间都花在了内核上,这与它在父进程写入页面时用于处理写时复制是一致的。
以我的理解,如果标记为写时复制的匿名页面是由单个进程映射的,则不应复制该页面,而应直接使用它。
我如何确定这确实是在复制内存上花费的时间?
如果我是对的,如何避免这种开销?
在现代 C ++ 中,基准测试的核心如下。
定义WITH_FORK
以启用快照;保留undefined可禁用子进程。
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <numaif.h>
#include <numa.h>
#include <algorithm>
#include <cassert>
#include <condition_variable>
#include <mutex>
#include <iomanip>
#include <iostream>
#include <cmath>
#include <numeric>
#include <thread>
#include <vector>
#define ARRAY_SIZE 1073741824 // 1GB
#define NUM_WORKERS 28
#define NUM_CHECKPOINTERS 4
#define BATCH_SIZE 2097152 // 2MB
using inttype = uint64_t;
using timepoint = std::chrono::time_point<std::chrono::high_resolution_clock>;
constexpr uint64_t NUM_ELEMS() {
return ARRAY_SIZE / sizeof(inttype);
}
int main() {
// allocate array
std::array<inttype, NUM_ELEMS()> *arrayptr = new std::array<inttype, NUM_ELEMS()>();
std::array<inttype, NUM_ELEMS()> & array = *arrayptr;
// allocate checkpoint space
std::array<inttype, NUM_ELEMS()> *cpptr = new std::array<inttype, NUM_ELEMS()>();
std::array<inttype, NUM_ELEMS()> & cp = *cpptr;
// initialize array
std::fill(array.begin(), array.end(), 123);
#ifdef WITH_FORK
// spawn checkpointer threads
int pid = fork();
if (pid == -1) {
perror("fork");
exit(-1);
}
// child process -- do checkpoint
if (pid == 0) {
std::array<std::thread, NUM_CHECKPOINTERS> cpthreads;
for (size_t tid = 0; tid < NUM_CHECKPOINTERS; tid++) {
cpthreads[tid] = std::thread([&, tid] {
// copy array
const size_t numBatches = ARRAY_SIZE / BATCH_SIZE;
for (size_t i = tid; i < numBatches; i += NUM_CHECKPOINTERS) {
void *src = reinterpret_cast<void*>(
reinterpret_cast<intptr_t>(array.data()) + i * BATCH_SIZE);
void *dst = reinterpret_cast<void*>(
reinterpret_cast<intptr_t>(cp.data()) + i * BATCH_SIZE);
memcpy(dst, src, BATCH_SIZE);
munmap(src, BATCH_SIZE);
}
});
}
for (std::thread& thread : cpthreads) {
thread.join();
}
printf("CP finished successfully! Child exiting.\n");
exit(0);
}
#endif // #ifdef WITH_FORK
// spawn worker threads
std::array<std::thread, NUM_WORKERS> threads;
for (size_t tid = 0; tid < NUM_WORKERS; tid++) {
threads[tid] = std::thread([&, tid] {
// write to array
std::array<inttype, NUM_ELEMS()>::iterator it;
for (it = array.begin() + tid; it < array.end(); it += NUM_WORKERS) {
*it = tid;
}
});
}
timepoint tStart = std::chrono::high_resolution_clock::now();
#ifdef WITH_FORK
// allow reaping child process while workers work
std::thread childWaitThread = std::thread([&] {
if (waitpid(pid, nullptr, 0)) {
perror("waitpid");
}
timepoint tChild = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> durationChild = tChild - tStart;
printf("reunited with child after (s): %lf\n", durationChild.count());
});
#endif
// wait for workers to finish
for (std::thread& thread : threads) {
thread.join();
}
timepoint tEnd = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = tEnd - tStart;
printf("duration (s): %lf\n", duration.count());
#ifdef WITH_FORK
childWaitThread.join();
#endif
}
Run Code Online (Sandbox Code Playgroud)
数组的大小为1GB,大约有250K页,其中每页大小为4KB。对于该程序,可以轻松估计由于写入 CoW 页而发生的页错误数量。也可以使用Linuxperf
工具进行测量。该new
运算符将数组初始化为零。所以下面这行代码:
std::array<inttype, NUM_ELEMS()> *arrayptr = new std::array<inttype, NUM_ELEMS()>();
Run Code Online (Sandbox Code Playgroud)
将导致大约 250K 的页面错误。同样,下面这行代码:
std::array<inttype, NUM_ELEMS()> *cpptr = new std::array<inttype, NUM_ELEMS()>();
Run Code Online (Sandbox Code Playgroud)
将导致另外 250K 页面错误。所有这些页面错误都很轻微,即无需访问磁盘驱动器即可处理它们。对于物理内存多得多的系统来说,分配两个1GB阵列不会造成任何重大故障。
此时,已经发生了大约500K的页错误(当然,还会有由程序的其他内存访问引起的其他页错误,但可以忽略不计)。执行std::fill
不会引起任何小错误,但阵列的虚拟页已经映射到专用物理页。
然后程序的执行继续分叉子进程并创建父进程的工作线程。子进程的创建本身就足以制作数组的快照,因此实际上不需要在子进程中执行任何操作。事实上,当子进程被分叉时,两个数组的虚拟页都被标记为写时复制。子进程读取arrayptr
和写入cpptr
,这会导致额外的 250K 小错误。父进程也会写入arrayptr
,这也会导致额外的 250K 小错误。因此,在子进程中创建副本并取消映射页面并不会提高性能。相反,页面错误的数量会增加一倍,性能会显着下降。
您可以使用以下命令测量次要和主要故障的数量:
perf stat -r 3 -e minor-faults,major-faults ./binary
Run Code Online (Sandbox Code Playgroud)
默认情况下,这将计算整个进程树的次要和主要故障。该-r 3
选项指示perf
重复实验三次并报告平均值和标准偏差。
我还注意到线程总数为 28 + 4。最佳线程数大约等于系统上在线逻辑核心的总数。如果线程数量远大于或远小于该数量,则由于创建过多线程并在线程之间切换的开销,性能将会下降。
以下循环中可能存在另一个潜在问题:
for (it = array.begin() + tid; it < array.end(); it += NUM_WORKERS) {
*it = tid;
}
Run Code Online (Sandbox Code Playgroud)
不同的线程可能会尝试同时多次写入同一缓存行,从而导致错误共享。这可能不是一个重要问题,具体取决于处理器缓存行的大小、线程数量以及所有核心是否以相同频率运行,因此如果不进行测量就很难说。更好的循环形状是让每个线程的元素在数组中是连续的。
归档时间: |
|
查看次数: |
132 次 |
最近记录: |