日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux CPU cache

發布時間:2025/3/15 linux 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux CPU cache 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

CPU cache

目前的CPU都是有三級緩存,一級二級是每個CPU獨有的,三級緩存是所有CPU共有的,其中L1的緩存分為數據緩存和指令緩存,如下圖所示:

虛擬化: VT-x L1d 緩存: 128 KiB // L1數據緩存 L1i 緩存: 128 KiB // L1 指令緩存 L2 緩存: 1 MiB L3 緩存: 8 MiB NUMA 節點0 CPU: 0-7

程序的數據最好能放到L1里面,大數據要進行拆分

緩存命中測試,保證測試的數據量大于L1+L2,系統會將內存放到L3緩存中,因此如下代碼測試時輸入的可以為 0K --> 8000 保證內存從L1 到L3的存取過程都能看到

// // Created by andrew on 2021/9/20. // #include <iostream> #include <vector> #include <sched.h> #include <ctime> #include <fstream>using namespace std;#define CACHE_LINE_SIZE 64struct Node {Node *next;char paddings[CACHE_LINE_SIZE - sizeof(void *)]; };// 內存掃描,使用鏈表的方式每次調用指針去踩內存 #define N1(node) node=node->next; #define N2(node) N1(node);N1(node); #define N4(node) N2(node);N2(node); #define N8(node) N4(node);N4(node); #define N16(node) N8(node);N8(node); #define N32(node) N16(node);N16(node); #define N64(node) N32(node);N32(node); #define N128(node) N64(node);N64(node); #define N256(node) N128(node);N128(node); #define N512(node) N256(node);N256(node); #define N1024(node) N512(node);N512(node);const long long sec2non = 1000000000;const Node *Test(int T, const Node *c, std::size_t size, ofstream & outFile) {// 提前定義,防止在時間打點的時候將變量申請的時間計算在內struct timespec start{};struct timespec end{};const Node* node = nullptr;//< startclock_gettime(CLOCK_MONOTONIC, &start);// 保證測試的次數,也就是踩內存的次數和T一樣const int M = static_cast<int>(T / size);for (int i = 0; i < M; i ++) {node = c;const int s = static_cast<int>(size / 64);for (int j = 0; j < s; ++ j) {// 踩緩存N64(node);}}clock_gettime(CLOCK_MONOTONIC, &end);//< end// 計算總耗時auto elapsed = (end.tv_sec - start.tv_sec) * sec2non + end.tv_nsec - start.tv_nsec;// 總耗時 除緩存命中次數,計算緩存命中耗時std::cout << "elapsed milliseconds per elements : " << elapsed / M << "ns" << std::endl;outFile << elapsed / M << endl;return node; }// 代碼地址 https://github.com/zzu-andrew/optimized_cplusplus.git /** @brief 系統緩存信息* L1d 緩存: 128 KiB* L1i 緩存: 128 KiB* L2 緩存: 1 MiB* L3 緩存: 8 MiB* *///< rdmsr: version msr-tools-1.3 rdmsr 版本信息 //< rdmsr - tool for reading CPU machine specific registers (MSR) int main(int argc, char*argv[]) {if (argc < 3) {std::cout << "argc : " << argc << std::endl;return -1;}// 清空CPU 參數,避免影響緩存命中cpu_set_t set;CPU_ZERO(&set);CPU_SET(0, &set);sched_setaffinity(0, sizeof(set), &set);// 16M,保證內存數量大于一二級緩存的量,才能保證系統會將緩存放到三級緩存里面// 如果一級 二級緩存夠使用 系統將不會將內存放到三級緩存里面const int N = 1024 * 1024 * 16; // * (64)// 使用vector搞一個環形bufferstd::vector<Node> va;va.reserve(N);for (int i = 0; i < N - 1; i ++) {va[i].next = &(va[i + 1]);}va[N - 1].next = &va[0];const int M = 1000000;char *endPtr = nullptr;// 轉換成10進制數const int beginKb = static_cast<int>(strtol(argv[1], &endPtr, 10));const int endKb = static_cast<int>(strtol(argv[2], &endPtr, 10));const Node* node = nullptr;// 耗時統計// 耗時統計ofstream outfile("./ou.csv", ios::trunc);for (int i = beginKb; i < endKb; i += 4) {std::cout << "test Kb : " << i << std::endl;std::cerr << i << '\t';node = Test(M, &va[0], i * 1024 / sizeof(Node), outfile);}outfile.close();std::cout << node << '\t' << &va[0] << std::endl;return 0; }

總結

以上是生活随笔為你收集整理的Linux CPU cache的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。