- 注册时间
- 2007-12-28
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 12785
- 在线时间
- 小时
|
发表于 2008-4-30 16:58:37
|
显示全部楼层
幸运的是,我去年曾经写过获取CPU信息的代码,这里一并贡献出来,下面的代码仅列出获取CPU Cache size 的代码,通过getCacheSize 函数可得到Intel或者AMD Cpu L2 cache2的大小。- typedef struct _cpuReg
- {
- DWORD eax;
- DWORD ebx;
- DWORD ecx;
- DWORD edx;
- }CPU_REG;
-
-
- typedef unsigned long DWORD;
- typedef unsigned char BYTE;
-
- typedef struct _cpuInfo
- {
- DWORD version;
- DWORD featureID;
- DWORD extFeatureID;
- DWORD L1_inst_cache;
- DWORD L1_data_cache;
- DWORD L2_cache;
- DWORD brandIdx;
-
- char vendor[16];
- char brand[64];
- char name[64];
-
- char type;
- char family;
- char model;
- char stepping;
-
- char support_MMX;
- char support_SSE;
- char support_SSE2;
- char support_SSE3;
- char no[32];
- }CPU_INFO;
-
- void getIntelCPUCacheSize(CPU_INFO *p)
- {
- CPU_REG cpuReg;
- BYTE cacheDesTab[32];
- BYTE *pt;
- int i,idx;
- //---------------------
- idx=0;
- CPUID(2,&cpuReg);
- for (i=0;i<4;i++)
- {
- switch(i)
- {
- case 0: pt=(BYTE *)(&(cpuReg.eax)); break;
- case 1: pt=(BYTE *)(&(cpuReg.ebx)); break;
- case 2: pt=(BYTE *)(&(cpuReg.ecx)); break;
- case 3: pt=(BYTE *)(&(cpuReg.edx)); break;
- }
-
- if ( (pt[3] & 128) ==0)
- {
- if (pt[0] !=0)
- cacheDesTab[idx++]=pt[0];
- if (pt[1] !=0)
- cacheDesTab[idx++]=pt[1];
- if (pt[2] !=0)
- cacheDesTab[idx++]=pt[2];
- if (pt[3] !=0)
- cacheDesTab[idx++]=pt[3];
- }
-
- }
-
- for (i=0;i<idx;i++)
- {
- BYTE flag;
- flag= cacheDesTab[i];
- switch (flag)
- {
- case 6: p->L1_inst_cache=8; break;
- case 8: p->L1_inst_cache=16; break;
- case 0x0a: p->L1_data_cache=8; break;
- case 0x0c: p->L1_data_cache=16; break;
- case 0x2c: p->L1_data_cache=32; break;
- case 0x30: p->L1_inst_cache=32; break;
- case 0x41: p->L2_cache =128; break;
- case 0x42: p->L2_cache =256; break;
- case 0x43: p->L2_cache =512; break;
- case 0x44: p->L2_cache =1024; break;
- case 0x45: p->L2_cache =2048; break;
- case 0x66: p->L1_data_cache=8; break;
- case 0x67: p->L1_data_cache=16; break;
- case 0x68: p->L1_data_cache=32; break;
-
- case 0x70:
- if ( p->L1_inst_cache==0 ) //have not been set
- p->L1_inst_cache=12;
- break;
-
- case 0x71:
- if ( p->L1_inst_cache==0 ) //have not been set
- p->L1_inst_cache=16;
- break;
-
- case 0x72:
- if ( p->L1_inst_cache==0 ) //have not been set
- p->L1_inst_cache=32;
- break;
-
- case 0x78: p->L2_cache=1024; break;
- case 0x79: p->L2_cache=128; break;
- case 0x7A: p->L2_cache=256; break;
- case 0x7B: p->L2_cache=512; break;
- case 0x7C: p->L2_cache=1024; break;
- case 0x7D: p->L2_cache=2048; break;
- case 0x82: p->L2_cache=256; break;
- case 0x83: p->L2_cache=512; break;
- case 0x84: p->L2_cache=1024; break;
- case 0x85: p->L2_cache=2048; break;
- case 0x86: p->L2_cache=512; break;
- case 0x87: p->L2_cache=1024; break;
- }
- }
- }
-
-
- void getAMDCPUCacheSize(CPU_INFO *p)
- {
- DWORD maxCPUFunction;
- CPU_REG cpuReg;
- //----------------------------
- CPUID(0x80000000,&cpuReg);
- maxCPUFunction=cpuReg.eax;
-
- if (maxCPUFunction>=0x80000005)
- {
- CPUID(0x80000005,&cpuReg);
- /*
- ECX 31:24 L1DcSize. L1 data cache size in KB.
- EDX 31:24 L1IcSize. L1 instruction cache size KB.
- */
- p->L1_data_cache= (cpuReg.ecx >> 24);
- p->L1_inst_cache= (cpuReg.edx >> 24);
- }
-
- if (maxCPUFunction>=0x80000006)
- {
- CPUID(0x80000006,&cpuReg);
- //ECX 31:16 L2Size. L2 cache size in KB.
- p->L2_cache = (cpuReg.ecx >> 16);
- }
- }
-
-
- void getCacheSize(CPU_INFO *p)
- {
- CPU_REG cpuReg;
- BYTE cacheDesTab[32];
- BYTE *pt;
- int i,idx;
-
- memset(cacheDesTab,0,sizeof(cacheDesTab));
- p->L1_data_cache=0;
- p->L1_inst_cache=0;
- p->L2_cache=0;
-
- if (strcmp(p->vendor,"GenuineIntel")==0)
- {
- getIntelCPUCacheSize(p);
- }
- else if ( strcmp(p->vendor,"AuthenticAMD")==0 )
- {
- getAMDCPUCacheSize(p);
- }
- else //for other CPU
- {
-
- }
- }
复制代码 |
|