CPU压力测试(Win Dev-C++)
2026-11-07 21:47:28
发布于:上海
Ad: CPU/GPU天梯(跑分参考)
注意:在Dev-C编译时,在工具->编译选项中,将“编译时加入以下命令”中,填入“-std=c11 -lurlmon”,如下图:

不执行此操作,会导致编译失败!
代码:
// 若链接报 pthread 错误,在连接器命令行加 -pthread
#define _WIN32_WINNT 0x0601
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <atomic>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <thread>
#include <vector>
#include <chrono>
#if defined(_WIN32)
#include <windows.h>
#elif defined(__linux__)
#include <pthread.h>
#include <sched.h>
#endif
// ---------- 全局控制 ----------
std::atomic<bool> g_stop(false);
volatile double g_sink = 0.0; // 防止计算被优化
// 压力开关(由命令行参数设置)
std::atomic<bool> g_run_cpu(true);
std::atomic<bool> g_run_fpu(true);
std::atomic<bool> g_run_cache(true);
void handle_signal(int) { g_stop.store(true, std::memory_order_relaxed); }
// ---------- 绑核(保持之前的跨平台实现) ----------
bool bind_current_thread_to_cpu(int cpu_id) {
#if defined(_WIN32)
WORD group = static_cast<WORD>(cpu_id / 64);
KAFFINITY mask = static_cast<KAFFINITY>(1) << (cpu_id % 64);
GROUP_AFFINITY ga;
ga.Mask = mask; ga.Group = group;
ga.Reserved[0] = 0; ga.Reserved[1] = 0; ga.Reserved[2] = 0;
return SetThreadGroupAffinity(GetCurrentThread(), &ga, NULL) != 0;
#elif defined(__linux__)
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpu_id, &cpuset);
return pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset) == 0;
#else
(void)cpu_id; return false;
#endif
}
std::vector<int> get_available_cpus() {
std::vector<int> cpus;
#if defined(_WIN32)
WORD group_count = GetActiveProcessorGroupCount();
for (WORD g = 0; g < group_count; ++g) {
DWORD count = GetActiveProcessorCount(g);
for (DWORD i = 0; i < count; ++i) cpus.push_back((int)g * 64 + (int)i);
}
#elif defined(__linux__)
cpu_set_t set; CPU_ZERO(&set);
if (sched_getaffinity(0, sizeof(set), &set) == 0) {
for (int i = 0; i < CPU_SETSIZE; ++i)
if (CPU_ISSET(i, &set)) cpus.push_back(i);
}
#endif
return cpus;
}
// ---------- 1. CPU 整数负载 (模拟 ALU 压力) ----------
void stress_cpu_integer(unsigned seed) {
unsigned x = seed * 2654435761u + 1;
while (!g_stop.load(std::memory_order_relaxed)) {
for (int i = 0; i < 2000000; ++i) {
// 典型的整数混合运算:乘法、移位、异或、加法
x ^= (x << 13);
x ^= (x >> 17);
x ^= (x << 5);
x = x * 1664525u + 1013904223u;
}
g_sink = (double)x; // 强制写回
}
}
// ---------- 2. FPU 浮点负载 (模拟 AVX/FPU 高功耗压力) ----------
// 使用双精度浮点密集运算链。如果编译器支持 AVX2,可替换为 intrinsics 获得更高强度。
void stress_fpu_float(unsigned seed) {
double a = 1.0 + seed * 0.001;
double b = 0.999999 + seed * 0.00001;
double c = 0.6180339887;
while (!g_stop.load(std::memory_order_relaxed)) {
for (int i = 0; i < 500000; ++i) {
// 构造无法被向量化/折叠的依赖链,模拟 FPU 高密度运算
a = a * b + c;
b = b * 0.9999999 + 0.0000001;
a = a / 1.0000001; // 除法进一步增加延迟和功耗
if (a > 1e100) { a = 1.0; b = 0.999999; }
}
g_sink = a + b;
}
}
// ---------- 3. Cache 负载 (模拟 L1/L2/L3 遍历压力) ----------
// 通过不同大小的步长遍历数组,制造缓存命中与未命中。
void stress_cache(unsigned seed) {
const size_t L1_SIZE = 32 * 1024 / sizeof(int); // 32KB
const size_t L2_SIZE = 256 * 1024 / sizeof(int); // 256KB
const size_t L3_SIZE = 8 * 1024 * 1024 / sizeof(int); // 8MB (典型值)
std::vector<int> buffer(L3_SIZE, 1);
unsigned idx = seed;
while (!g_stop.load(std::memory_order_relaxed)) {
// 大跨步访问 L3 区域,同时穿插小区域访问以冲击 L1/L2
for (size_t i = 0; i < L3_SIZE; i += 64) { // 64 步长对应缓存行
idx = (idx * 1103515245u + 12345u) & 0x7FFFFFFF;
buffer[idx % L3_SIZE] += 1;
// 周期性回扫 L1/L2 区域
if ((i & 0xFFF) == 0) {
for (size_t j = 0; j < L1_SIZE; j += 8) {
buffer[j % L2_SIZE] ^= idx;
}
}
}
g_sink = (double)buffer[0];
}
}
// ---------- 工作线程入口 ----------
void burn_worker(int cpu_id) {
bind_current_thread_to_cpu(cpu_id);
unsigned seed = (unsigned)(cpu_id + 1);
// 模拟 AIDA64 的“同时勾选”:在一个大循环里轮转执行被激活的压力
while (!g_stop.load(std::memory_order_relaxed)) {
if (g_run_cpu.load(std::memory_order_relaxed)) stress_cpu_integer(seed);
if (g_run_fpu.load(std::memory_order_relaxed)) stress_fpu_float(seed);
if (g_run_cache.load(std::memory_order_relaxed)) stress_cache(seed);
// 如果全关,避免空转烧核
if (!g_run_cpu && !g_run_fpu && !g_run_cache) break;
}
}
// ---------- 参数解析 ----------
void parse_mode(const char* arg) {
// 默认全开;可通过 --cpu-only, --fpu-only 等调整
std::string mode(arg);
if (mode == "--cpu-only") { g_run_cpu = true; g_run_fpu = false; g_run_cache = false; }
else if (mode == "--fpu-only") { g_run_cpu = false; g_run_fpu = true; g_run_cache = false; }
else if (mode == "--cache-only") { g_run_cpu = false; g_run_fpu = false; g_run_cache = true; }
else if (mode == "--all") { g_run_cpu = true; g_run_fpu = true; g_run_cache = true; }
}
int main(int argc, char** argv) {
std::signal(SIGINT, handle_signal);
std::signal(SIGTERM, handle_signal);
// 解析模式
if (argc > 1) parse_mode(argv[1]);
auto cpus = get_available_cpus();
if (cpus.empty()) { std::printf("未找到可用 CPU。\n"); return 1; }
std::printf("=== AIDA64 模拟烤机 ===\n");
std::printf("激活的负载: CPU(整数)=%s, FPU(浮点)=%s, Cache=%s\n",
g_run_cpu ? "开" : "关", g_run_fpu ? "开" : "关", g_run_cache ? "开" : "关");
std::printf("将在 %d 个逻辑 CPU 上启动绑定线程...\n", (int)cpus.size());
std::vector<std::thread> pool;
for (int cpu : cpus) pool.push_back(std::thread(burn_worker, cpu));
std::printf("运行中,Ctrl+C 退出。\n");
for (size_t i = 0; i < pool.size(); ++i) pool[i].join();
std::printf("已退出。\n");
return 0;
}
这里空空如也

















有帮助,赞一个