【发布时间】:2019-05-16 17:33:25
【问题描述】:
在 Linux 中,每当一个进程被派生时,父进程的内存映射都会克隆到子进程中。实际上,出于性能原因,页面被设置为 copy-on-write —— 最初它们是共享的,如果两个进程之一在其中一个上写入,那么它们将被克隆 (MAP_PRIVATE)。
这是获取正在运行的程序状态快照的一种非常常见的机制——您执行一个 fork,这为您提供了该时间点进程内存的(一致)视图。
我做了一个简单的基准测试,其中包含两个组件:
- 具有线程池的父进程写入数组
- 具有线程池的子进程制作数组的快照并取消映射
在某些情况下(机器/架构/内存放置/线程数/...),我可以使复制比线程写入数组更早完成。
但是,当子进程退出时,在htop 中我仍然看到大部分 CPU 时间都花在了内核中,这与它用于处理 copy-on-write 每当父进程写入页面时。
在我的理解中,如果标记为copy-on-write的匿名页面是由单个进程映射的,则不应复制,而应直接使用。
我如何确定这确实是复制内存所花费的时间?
如果我是对的,我该如何避免这种开销?
基准测试的核心如下,现代 C++。
定义WITH_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
}
【问题讨论】:
-
没有“现代”C++ 这样的东西。如果您想具体一点,请谈谈您正在使用哪个版本进行编译,例如 C++14 或 C++17 等,以及如果您认为这是影响结果的一个因素,请说明使用什么编译器。
-
我正在使用
C++17,但我不确定这是否是最低要求标准:) 我只使用了gcc8(试用clang的好提示) -
在进一步阅读您的问题时,如果您在谈论内核如何处理页面,我认为这根本不是 C++ 的事情。这可能与您正在使用的内核有关,甚至可能与内核的编译方式有关。 Copy-on-Write 不是免费的,内核必须为此提供便利,因此您可能会看到与清理它相关的更高 CPU 使用率。 Redis 是一个著名的软件示例,它大量使用了 fork/dump 模型,这就是它制作定期快照的方式。
-
如果您更深入地研究可以从
/proc获得的统计信息,您可能能够衡量内核为管理您的页面所做的工作。那里有令人眼花缭乱的旋钮、按钮和仪表,所以在四处寻找之前,您需要找到一个很好的参考,但确实有很多。 -
对
How can I be sure that this is indeed time being spent copying the memory?的回答是运行内核分析器。
标签: c++ performance memory linux-kernel copy-on-write