C++ 子进程退出后内核复制CoW页
在Linux中,每当进程分叉时,父进程的内存映射都会克隆到子进程中。实际上,出于性能原因,页面被设置为写时复制——最初它们是共享的,如果两个进程中的一个进程在其中一个进程上写入,那么它们将被克隆(C++ 子进程退出后内核复制CoW页,c++,performance,memory,linux-kernel,copy-on-write,C++,Performance,Memory,Linux Kernel,Copy On Write,在Linux中,每当进程分叉时,父进程的内存映射都会克隆到子进程中。实际上,出于性能原因,页面被设置为写时复制——最初它们是共享的,如果两个进程中的一个进程在其中一个进程上写入,那么它们将被克隆(MAP\u PRIVATE) 这是获取正在运行的程序状态快照的一种非常常见的机制——您可以执行fork,这将为您提供该时间点上进程内存的(一致的)视图 我做了一个简单的基准测试,其中我有两个组件: 具有写入数组的线程池的父进程 一个子进程,它有一个线程池,对数组进行快照并取消映射 在某些情况下(机器
MAP\u PRIVATE
)
这是获取正在运行的程序状态快照的一种非常常见的机制——您可以执行fork,这将为您提供该时间点上进程内存的(一致的)视图
我做了一个简单的基准测试,其中我有两个组件:
- 具有写入数组的线程池的父进程
- 一个子进程,它有一个线程池,对数组进行快照并取消映射
htop
中,我仍然看到大部分CPU时间都花在内核中,这与每当父进程写入页面时,它被用来处理写时拷贝是一致的
在我的理解中,如果标记为“写时复制”的匿名页面由单个进程映射,则不应复制它,而应直接使用它
我如何确定这确实是复制内存所花费的时间?
如果我是对的,我如何避免这种开销?
核心的基准是在现代C++中。 使用_FORK定义
,以启用快照;保留未定义以禁用子进程
#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
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义数组大小1073741824//1GB
#定义NUM_WORKERS 28
#定义NUM_检查点4
#定义批量大小2097152//2MB
使用inttype=uint64\u t;
使用timepoint=std::chrono::time\u point;
constexpr uint64_t NUM_ELEMS(){
返回数组\u SIZE/sizeof(inttype);
}
int main(){
//分配数组
std::array*arrayptr=new std::array();
std::array&array=*arrayptr;
//分配检查点空间
std::array*cpptr=new std::array();
std::array&cp=*cpptr;
//初始化数组
std::fill(array.begin(),array.end(),123);
#带_叉的ifdef
//生成检查点线程
int-pid=fork();
如果(pid==-1){
佩罗尔(“福克”);
出口(-1);
}
//子进程--执行检查点
如果(pid==0){
std::数组cpthreads;
对于(大小tid=0;tid
阵列大小为1GB,约为250K页,其中每页大小为4KB。对于该程序,可以轻松估计由于写入CoW页面而发生的页面错误数。它还可以使用Linuxperf
工具进行测量。new
操作符将数组初始化为零。因此,以下代码行:
std::array<inttype, NUM_ELEMS()> *arrayptr = new std::array<inttype, NUM_ELEMS()>();
默认情况下,这将统计整个流程树的次要和主要故障。-r3
选项告诉perf
重复实验三次,并报告平均值和标准偏差
我还注意到线程总数是28+4。最佳线程数大约等于系统上联机逻辑内核的总数。如果线程的数量远远大于或小于此数量,那么由于创建太多线程并在它们之间切换的开销,性能将降低
以下循环中可能存在另一个潜在问题:
for (it = array.begin() + tid; it < array.end(); it += NUM_WORKERS) {
*it = tid;
}
for(it=array.begin()+tid;it
不同的线程可能会尝试在同一时间多次写入同一缓存线,从而导致错误共享。这可能不是一个重要的问题,这取决于处理器缓存线的大小、线程的数量以及所有内核是否以相同的频率运行,因此很难说没有m
perf stat -r 3 -e minor-faults,major-faults ./binary
for (it = array.begin() + tid; it < array.end(); it += NUM_WORKERS) {
*it = tid;
}