Overview
| Artifact ID: | c8c6d9714cc588fbdff96b9a2f7f7f880dbdfdb4 |
|---|---|
| Ticket: | 3002022fffffffffffffffffffffffffffffffff |
| Date: | 2010-05-15 14:25:38 |
| User: | hgiese |
| Artifact Attached: | f0a3ef0401b896a7e2511aae7465157b1479956b |
| Filename: | perfcounter.c |
| Description: | Utility to get the relevant information |
Content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224#include <stdio.h> #include <windows.h> #define TCL_OK 0 #define TCL_ERROR 1 // // From tclWin32Dll.c // /* *------------------------------------------------------------------------ * * TclWinCPUID -- * * Get CPU ID information on an Intel box under Windows * * Results: * Returns TCL_OK if successful, TCL_ERROR if CPUID is not supported or * fails. * * Side effects: * If successful, stores EAX, EBX, ECX and EDX registers after the CPUID * instruction in the four integers designated by 'regsPtr' * *---------------------------------------------------------------------- */ int TclWinCPUID( unsigned int index, /* Which CPUID value to retrieve. */ unsigned int *regsPtr) /* Registers after the CPUID. */ { #ifdef HAVE_NO_SEH EXCEPTION_REGISTRATION registration; #endif int status = TCL_ERROR; #if defined(__GNUC__) && !defined(_WIN64) /* * Execute the CPUID instruction with the given index, and store results * off 'regPtr'. */ __asm__ __volatile__( /* * Construct an EXCEPTION_REGISTRATION to protect the CPUID * instruction (early 486's don't have CPUID) */ "leal %[registration], %%edx" "\n\t" "movl %%fs:0, %%eax" "\n\t" "movl %%eax, 0x0(%%edx)" "\n\t" /* link */ "leal 1f, %%eax" "\n\t" "movl %%eax, 0x4(%%edx)" "\n\t" /* handler */ "movl %%ebp, 0x8(%%edx)" "\n\t" /* ebp */ "movl %%esp, 0xc(%%edx)" "\n\t" /* esp */ "movl %[error], 0x10(%%edx)" "\n\t" /* status */ /* * Link the EXCEPTION_REGISTRATION on the chain */ "movl %%edx, %%fs:0" "\n\t" /* * Do the CPUID instruction, and save the results in the 'regsPtr' * area. */ "movl %[rptr], %%edi" "\n\t" "movl %[index], %%eax" "\n\t" "cpuid" "\n\t" "movl %%eax, 0x0(%%edi)" "\n\t" "movl %%ebx, 0x4(%%edi)" "\n\t" "movl %%ecx, 0x8(%%edi)" "\n\t" "movl %%edx, 0xc(%%edi)" "\n\t" /* * Come here on a normal exit. Recover the EXCEPTION_REGISTRATION and * store a TCL_OK status. */ "movl %%fs:0, %%edx" "\n\t" "movl %[ok], %%eax" "\n\t" "movl %%eax, 0x10(%%edx)" "\n\t" "jmp 2f" "\n" /* * Come here on an exception. Get the EXCEPTION_REGISTRATION that we * previously put on the chain. */ "1:" "\t" "movl %%fs:0, %%edx" "\n\t" "movl 0x8(%%edx), %%edx" "\n\t" /* * Come here however we exited. Restore context from the * EXCEPTION_REGISTRATION in case the stack is unbalanced. */ "2:" "\t" "movl 0xc(%%edx), %%esp" "\n\t" "movl 0x8(%%edx), %%ebp" "\n\t" "movl 0x0(%%edx), %%eax" "\n\t" "movl %%eax, %%fs:0" "\n\t" : /* No outputs */ : [index] "m" (index), [rptr] "m" (regsPtr), [registration] "m" (registration), [ok] "i" (TCL_OK), [error] "i" (TCL_ERROR) : "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); status = registration.status; #elif defined(_MSC_VER) && !defined(_WIN64) /* * Define a structure in the stack frame to hold the registers. */ _try { _asm { push ebx push ecx push edx mov eax, regs.dw0 cpuid mov regs.dw0, eax mov regs.dw1, ebx mov regs.dw2, ecx mov regs.dw3, edx pop edx pop ecx pop ebx } /* * Copy regs back out to the caller. */ regsPtr[0] = regs.dw0; regsPtr[1] = regs.dw1; regsPtr[2] = regs.dw2; regsPtr[3] = regs.dw3; status = TCL_OK; } __except(EXCEPTION_EXECUTE_HANDLER) { /* do nothing */ } #elif defined(__BORLANDC__) // Code from // http://groups.google.com/group/borland.public.cppbuilder.language/browse_thread/thread/b497c052f5554d96/6ae833b0f70b8e84?q=cpuid+OR+cpu-id+group:borland.public.cppbuilder*#6ae833b0f70b8e84 __asm { PUSH EBX // Save affected register PUSH EDI MOV EDI,regsPtr // destination MOV EAX,index DW 0xA20F // CPUID Command} STOSD // CPUID[1] MOV EAX,EBX STOSD // CPUID[2] MOV EAX,ECX STOSD // CPUID[3] MOV EAX,EDX STOSD // CPUID[4] POP EDI // Restore registers POP EBX } status = TCL_OK; #else /* * Don't know how to do assembly code for this compiler and/or * architecture. */ #endif return status; } // Adapted from tclWinTime.c int main(void) { unsigned int regs[5], tmp, res; LARGE_INTEGER frequ; // Show the counter's frequency if (! QueryPerformanceFrequency(&frequ)) { printf("No performance counter.\n"); } else { printf("Performance counter's frequency: %lu Hz.\n", frequ.QuadPart); } // Info for code '0' if (TclWinCPUID(0, regs) != TCL_OK) { printf("TclWinCPUID with code '0' failed.\n"); return 1; } // Show info: Have to re-arrange the content of 'regs' regs[4] = 0; // delimit string // swap regs[2] & regs[3] tmp = regs[2]; regs[2] = regs[3]; regs[3] = tmp; printf("Results of CPUID(0): %s\n", ®s[1]); res = (strcmp((const char *)®s[1], "GenuineIntel") == 0); printf("'performance counter available' (test part 1) (1 == ok)? %d\n", res); // Info for code '1' if (TclWinCPUID(1, regs) != TCL_OK) { printf("TclWinCPUID with code '1' failed.\n"); return 1; } // Apply the test from NativeGetTime (2nd part) /* Pentium 4 */ res = (regs[0]&0x00000F00) == 0x00000F00 /* Extended family Hyperthread */ || ((regs[0] & 0x00F00000) && (regs[3] & 0x10000000)); printf("Results of CPUID(1)\n"); printf("'performance counter available' (test part 2) (1 == ok)? %d\n", res); printf("Detailed results\n"); printf("regs[0] = 0x%08X, regs[1] = 0x%08X, regs[3] = 0x%08X\n", regs[0], regs[1], regs[3]); return 0; }
