代码拉取完成,页面将自动刷新
// multiplex_bench.cpp —— select / poll / epoll 在"被监视 fd 数"增长时的真实代价
// 编译:g++ -std=c++17 -O2 multiplex_bench.cpp -o multiplex_bench
// 用法:./multiplex_bench <fd数量> <活跃比例分子> 例:./multiplex_bench 1000 1
// 活跃比例 = 1/N,这里用"每轮只有第 0 个 fd 活跃"最简单也最能说明问题
#include <sys/select.h>
#include <poll.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <cerrno>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <csignal>
#include <iostream>
#include <vector>
static const int kRounds = 2000;
int main(int argc, char **argv) {
int nfd = argc > 1 ? std::atoi(argv[1]) : 100;
int active_n = argc > 2 ? std::atoi(argv[2]) : 1; // 前 active_n 个 fd 保持活跃
// 用 socketpair 造出 nfd 个"可读/可写"的 fd
signal(SIGPIPE, SIG_IGN);
std::vector<int> fds, peers; // peers 必须保持打开,否则对端关闭会 SIGPIPE
for (int i = 0; i < nfd; ++i) {
int sv[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) != 0) { perror("socketpair"); return 1; }
fds.push_back(sv[0]);
peers.push_back(sv[1]);
if (i < active_n) (void)!write(sv[1], "x", 1); // 让它真的有数据可读
}
auto now = [] { return std::chrono::steady_clock::now(); };
auto ms = [](auto a, auto b) {
return std::chrono::duration<double, std::milli>(b - a).count();
};
// ---------- select ----------
// 注意:fd_set 是固定大小的位图,只能表示 < FD_SETSIZE(1024) 的 fd。
// fd 编号超过 1024 时 FD_SET 会越界写 —— 这正是 select 的硬上限。
int maxfd_num = 0;
for (int f : fds) if (f > maxfd_num) maxfd_num = f;
bool select_ok = (maxfd_num < FD_SETSIZE);
double t_select = 0;
if (select_ok) {
auto t0 = now();
for (int r = 0; r < kRounds; ++r) {
fd_set rfds; FD_ZERO(&rfds);
int maxfd = 0;
for (int f : fds) { FD_SET(f, &rfds); if (f > maxfd) maxfd = f; }
timeval tv{0, 0}; // 轮询,不等
select(maxfd + 1, &rfds, nullptr, nullptr, &tv);
for (int f : fds) if (FD_ISSET(f, &rfds)) { char c; (void)!read(f, &c, 1); (void)!write(f, "x", 1); }
}
t_select = ms(t0, now());
}
// ---------- poll ----------
double t_poll = 0;
{
std::vector<pollfd> pfds(nfd);
auto t0 = now();
for (int r = 0; r < kRounds; ++r) {
for (int i = 0; i < nfd; ++i) { pfds[i].fd = fds[i]; pfds[i].events = POLLIN; pfds[i].revents = 0; }
poll(pfds.data(), nfd, 0);
for (int i = 0; i < nfd; ++i)
if (pfds[i].revents & POLLIN) { char c; (void)!read(pfds[i].fd, &c, 1); (void)!write(pfds[i].fd, "x", 1); }
}
t_poll = ms(t0, now());
}
// ---------- epoll ----------
double t_epoll = 0;
{
int ep = epoll_create1(0);
std::vector<epoll_event> evs(nfd);
for (int i = 0; i < nfd; ++i) {
epoll_event e{}; e.events = EPOLLIN; e.data.fd = fds[i];
epoll_ctl(ep, EPOLL_CTL_ADD, fds[i], &e);
}
auto t0 = now();
for (int r = 0; r < kRounds; ++r) {
int n = epoll_wait(ep, evs.data(), nfd, 0);
for (int i = 0; i < n; ++i) {
char c; (void)!read(evs[i].data.fd, &c, 1); (void)!write(evs[i].data.fd, "x", 1);
}
}
t_epoll = ms(t0, now());
::close(ep);
}
std::cout << "被监视 fd 数 = " << nfd << ", 活跃 = " << active_n
<< ", 轮询 " << kRounds << " 轮 (最大 fd 编号 = " << maxfd_num << ")\n"
<< " select : " << (select_ok ? std::to_string(t_select) + " ms"
: std::string("不可用 —— fd 编号已达 ") +
std::to_string(maxfd_num) +
" >= FD_SETSIZE(1024)") << "\n"
<< " poll : " << t_poll << " ms\n"
<< " epoll : " << t_epoll << " ms\n";
for (int f : fds) ::close(f);
for (int f : peers) ::close(f);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。