linux c/c++最高效的计时方法

每个cpu的core在执行指令(代码)的时候,身边有约30大悍将(寄存器)在辅做,比如最典型的EBP、ESP,当要发生运行空间切换(syscall、中断),这30个寄存器里的数据是不是都要存起来以待再切回来时恢复继续接着干;

几个问题:

什么时候存?

谁存?

存在哪?

什么时候恢复?

最主要这个过程耗时多久?本文syscal揭开耗时的面纱。

程序的大致要干的事;

对64MB 循环访问一把;

连续20次调用同一个syscall函数,并分别记录每次的耗时;

printf 20次的耗时;

循环以上三步骤;

main_syscall.c

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <unistd.h>

#include <sys/types.h>

#include "common.h"

#define LOOPCNT 20

#define BLOCKLEN 1000000

int g_aiData[BLOCKLEN][16];

int main(int argc, char ** argv)

{

int i;

long long t1, t2;

struct timespec stTP;

复制代码
long long aldiff[LOOPCNT] = { 0 };
int iTemp = 0;

while(1)
{
    for (i = 0; i < BLOCKLEN; i++) /* 64MB的内存先访问一把,把L1 L2 L3尽力刷一把 */
        g_aiData[i][0] = i;

    aldiff[0] = 0;
     
    for (i = 0; i < LOOPCNT; i++)
    {
        t1 = rte_rdtsc();  /* 直接从rdtsc寄存器取,注:这个是x86_64上的 */
        getnstime(&stTP);  /* 这个函数会走syscall进内核 */
        t2 = rte_rdtsc();
        aldiff[i] = t2 - t1;
    }
     
    printf("----------------------------\n");
    for (i = 0; i < LOOPCNT; i++)
    {
        printf("%d:%lld, ", i, aldiff[i]);
    }
    printf("\n");
}
 
return 0;

}

common.c

#include <sys/time.h>

#include <time.h>

#include <stdlib.h>

#include <stdio.h>

#include "common.h"

//typedef unsigned long long uint64_t;

long long getustime(void)

{

struct timeval tv;

long long ust;

复制代码
gettimeofday(&tv, NULL);
ust = ((long)tv.tv_sec)*1000000;
ust += tv.tv_usec;
return ust;

}

void getnstime(struct timespec pstTP)
{
clock_gettime(CLOCK_MONOTONIC_RAW, pstTP); /
这里带RAW的是会syscall进内核的 */

return;

}

long diff_nstime(struct timespec *pstTP1, struct timespec *pstTP2)

{

return (pstTP2->tv_sec - pstTP1->tv_sec)*1000000000 + pstTP2->tv_nsec - pstTP1->tv_nsec;

}

inline long long rte_rdtsc(void)

{

union {

long long tsc_64;

struct {

unsigned int lo_32;

unsigned int hi_32;

};

} tsc;

复制代码
asm volatile("rdtsc" :
         "=a" (tsc.lo_32),
         "=d" (tsc.hi_32));
return tsc.tsc_64;

}

common.h

#ifndef COMMON_H_

#define COMMON_H_

#define ERROR_SUCCESS 0

#define ERROR_FAIL 1

#define IN

#define OUT

#define INOUT

#define CACHE_LINE 64

//#define HASHSIZE 1*1024

typedef unsigned long ulong_t;

long long getustime(void);

void getnstime(struct timespec *pstTP);

long diff_nstime(struct timespec *pstTP1, struct timespec *pstTP2);

inline long long rte_rdtsc(void);

#endif

可以看出,每轮都是第1次耗时最长,约3000(计时单位是时钟周期)这个量级,后面接着的都是300,相差了近10倍;

为什么会是这样?

我们平常调的系统调用都是每轮第1次这个样子么?

相关推荐
尘似鹤25 分钟前
linux驱动学习---有些节点不会生成platform_device,怎么访问它们
linux
iCxhust27 分钟前
windows环境下在Bochs中运行Linux0.12系统
linux·运维·服务器·windows·minix
九河云2 小时前
数字化转型中的网络安全风险与零信任架构实践
运维·科技·安全·web安全·架构
七七七七074 小时前
【计算机网络】深入理解ARP协议:工作原理、报文格式与安全防护
linux·服务器·网络·计算机网络·安全
守城小轩4 小时前
轻量级HTTP&Socks代理GOST: Linux编译安装
运维·网络·网络协议
qq_5470261794 小时前
Flowable 工作流引擎
java·服务器·前端
奋斗的蛋黄5 小时前
网络卡顿运维排查方案:从客户端到服务器的全链路处理
运维·服务器·网络
wanhengidc6 小时前
云手机搬砖 尤弥尔传奇自动化操作
运维·服务器·arm开发·安全·智能手机·自动化
图图图图爱睡觉7 小时前
主机跟虚拟机ip一直Ping不通,并且虚拟机使用ifconfig命令时,ens33没有ipv4地址,只有ipv6地址
服务器·网络·tcp/ip
deephub7 小时前
FastMCP 入门:用 Python 快速搭建 MCP 服务器接入 LLM
服务器·人工智能·python·大语言模型·mcp