Attachment "perfcounter.c" to
ticket [3002022fff]
added by
hgiese
2010-05-15 14:25:38.
0000: 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e #include <stdio.
0010: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 77 69 6e h>.#include <win
0020: 64 6f 77 73 2e 68 3e 0a 0a 23 64 65 66 69 6e 65 dows.h>..#define
0030: 20 54 43 4c 5f 4f 4b 20 20 20 20 20 20 20 20 20 TCL_OK
0040: 20 30 0a 23 64 65 66 69 6e 65 20 54 43 4c 5f 45 0.#define TCL_E
0050: 52 52 4f 52 20 20 20 20 20 20 20 31 0a 0a 2f 2f RROR 1..//
0060: 0a 2f 2f 20 46 72 6f 6d 20 74 63 6c 57 69 6e 33 .// From tclWin3
0070: 32 44 6c 6c 2e 63 0a 2f 2f 0a 2f 2a 0a 20 2a 2d 2Dll.c.//./*. *-
0080: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
0090: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
00a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
00b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
00c0: 2d 2d 2d 2d 2d 2d 2d 0a 20 2a 0a 20 2a 20 54 63 -------. *. * Tc
00d0: 6c 57 69 6e 43 50 55 49 44 20 2d 2d 0a 20 2a 0a lWinCPUID --. *.
00e0: 20 2a 20 47 65 74 20 43 50 55 20 49 44 20 69 6e * Get CPU ID in
00f0: 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 61 6e 20 formation on an
0100: 49 6e 74 65 6c 20 62 6f 78 20 75 6e 64 65 72 20 Intel box under
0110: 57 69 6e 64 6f 77 73 0a 20 2a 0a 20 2a 20 52 65 Windows. *. * Re
0120: 73 75 6c 74 73 3a 0a 20 2a 20 52 65 74 75 72 6e sults:. * Return
0130: 73 20 54 43 4c 5f 4f 4b 20 69 66 20 73 75 63 63 s TCL_OK if succ
0140: 65 73 73 66 75 6c 2c 20 54 43 4c 5f 45 52 52 4f essful, TCL_ERRO
0150: 52 20 69 66 20 43 50 55 49 44 20 69 73 20 6e 6f R if CPUID is no
0160: 74 20 73 75 70 70 6f 72 74 65 64 20 6f 72 0a 20 t supported or.
0170: 2a 20 66 61 69 6c 73 2e 0a 20 2a 0a 20 2a 20 53 * fails.. *. * S
0180: 69 64 65 20 65 66 66 65 63 74 73 3a 0a 20 2a 20 ide effects:. *
0190: 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 If successful, s
01a0: 74 6f 72 65 73 20 45 41 58 2c 20 45 42 58 2c 20 tores EAX, EBX,
01b0: 45 43 58 20 61 6e 64 20 45 44 58 20 72 65 67 69 ECX and EDX regi
01c0: 73 74 65 72 73 20 61 66 74 65 72 20 74 68 65 20 sters after the
01d0: 43 50 55 49 44 0a 20 2a 20 69 6e 73 74 72 75 63 CPUID. * instruc
01e0: 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 6f 75 72 tion in the four
01f0: 20 69 6e 74 65 67 65 72 73 20 64 65 73 69 67 6e integers design
0200: 61 74 65 64 20 62 79 20 27 72 65 67 73 50 74 72 ated by 'regsPtr
0210: 27 0a 20 2a 0a 20 2a 2d 2d 2d 2d 2d 2d 2d 2d 2d '. *. *---------
0220: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
0230: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
0240: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d ----------------
0250: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 2a -------------. *
0260: 2f 0a 0a 69 6e 74 0a 54 63 6c 57 69 6e 43 50 55 /..int.TclWinCPU
0270: 49 44 28 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 ID(. unsigned
0280: 20 69 6e 74 20 69 6e 64 65 78 2c 20 20 20 20 2f int index, /
0290: 2a 20 57 68 69 63 68 20 43 50 55 49 44 20 76 61 * Which CPUID va
02a0: 6c 75 65 20 74 6f 20 72 65 74 72 69 65 76 65 2e lue to retrieve.
02b0: 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 */. unsigned
02c0: 20 69 6e 74 20 2a 72 65 67 73 50 74 72 29 20 2f int *regsPtr) /
02d0: 2a 20 52 65 67 69 73 74 65 72 73 20 61 66 74 65 * Registers afte
02e0: 72 20 74 68 65 20 43 50 55 49 44 2e 20 2a 2f 0a r the CPUID. */.
02f0: 7b 0a 23 69 66 64 65 66 20 48 41 56 45 5f 4e 4f {.#ifdef HAVE_NO
0300: 5f 53 45 48 0a 20 20 20 20 45 58 43 45 50 54 49 _SEH. EXCEPTI
0310: 4f 4e 5f 52 45 47 49 53 54 52 41 54 49 4f 4e 20 ON_REGISTRATION
0320: 72 65 67 69 73 74 72 61 74 69 6f 6e 3b 0a 23 65 registration;.#e
0330: 6e 64 69 66 0a 20 20 20 20 69 6e 74 20 73 74 61 ndif. int sta
0340: 74 75 73 20 3d 20 54 43 4c 5f 45 52 52 4f 52 3b tus = TCL_ERROR;
0350: 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f ..#if defined(__
0360: 47 4e 55 43 5f 5f 29 20 26 26 20 21 64 65 66 69 GNUC__) && !defi
0370: 6e 65 64 28 5f 57 49 4e 36 34 29 0a 20 20 20 20 ned(_WIN64).
0380: 2f 2a 0a 20 20 20 20 20 2a 20 45 78 65 63 75 74 /*. * Execut
0390: 65 20 74 68 65 20 43 50 55 49 44 20 69 6e 73 74 e the CPUID inst
03a0: 72 75 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 ruction with the
03b0: 20 67 69 76 65 6e 20 69 6e 64 65 78 2c 20 61 6e given index, an
03c0: 64 20 73 74 6f 72 65 20 72 65 73 75 6c 74 73 0a d store results.
03d0: 20 20 20 20 20 2a 20 6f 66 66 20 27 72 65 67 50 * off 'regP
03e0: 74 72 27 2e 0a 20 20 20 20 20 2a 2f 0a 0a 20 20 tr'.. */..
03f0: 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 __asm__ __vola
0400: 74 69 6c 65 5f 5f 28 0a 20 20 20 2f 2a 0a 20 20 tile__(. /*.
0410: 20 20 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 6e * Construct an
0420: 20 45 58 43 45 50 54 49 4f 4e 5f 52 45 47 49 53 EXCEPTION_REGIS
0430: 54 52 41 54 49 4f 4e 20 74 6f 20 70 72 6f 74 65 TRATION to prote
0440: 63 74 20 74 68 65 20 43 50 55 49 44 0a 20 20 20 ct the CPUID.
0450: 20 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 28 * instruction (
0460: 65 61 72 6c 79 20 34 38 36 27 73 20 64 6f 6e 27 early 486's don'
0470: 74 20 68 61 76 65 20 43 50 55 49 44 29 0a 20 20 t have CPUID).
0480: 20 20 2a 2f 0a 0a 20 20 20 22 6c 65 61 6c 20 25 */.. "leal %
0490: 5b 72 65 67 69 73 74 72 61 74 69 6f 6e 5d 2c 20 [registration],
04a0: 25 25 65 64 78 22 20 20 20 20 22 5c 6e 5c 74 22 %%edx" "\n\t"
04b0: 0a 20 20 20 22 6d 6f 76 6c 20 25 25 66 73 3a 30 . "movl %%fs:0
04c0: 2c 20 20 20 20 20 25 25 65 61 78 22 20 20 20 20 , %%eax"
04d0: 20 20 22 5c 6e 5c 74 22 0a 20 20 20 22 6d 6f 76 "\n\t". "mov
04e0: 6c 20 25 25 65 61 78 2c 20 20 20 20 20 20 30 78 l %%eax, 0x
04f0: 30 28 25 25 65 64 78 29 22 20 22 5c 6e 5c 74 22 0(%%edx)" "\n\t"
0500: 20 2f 2a 20 6c 69 6e 6b 20 2a 2f 0a 20 20 20 22 /* link */. "
0510: 6c 65 61 6c 20 31 66 2c 20 20 20 20 20 20 25 25 leal 1f, %%
0520: 65 61 78 22 20 20 20 20 20 20 22 5c 6e 5c 74 22 eax" "\n\t"
0530: 0a 20 20 20 22 6d 6f 76 6c 20 25 25 65 61 78 2c . "movl %%eax,
0540: 20 20 20 20 20 20 30 78 34 28 25 25 65 64 78 29 0x4(%%edx)
0550: 22 20 22 5c 6e 5c 74 22 20 2f 2a 20 68 61 6e 64 " "\n\t" /* hand
0560: 6c 65 72 20 2a 2f 0a 20 20 20 22 6d 6f 76 6c 20 ler */. "movl
0570: 25 25 65 62 70 2c 20 20 20 20 20 20 30 78 38 28 %%ebp, 0x8(
0580: 25 25 65 64 78 29 22 20 22 5c 6e 5c 74 22 20 2f %%edx)" "\n\t" /
0590: 2a 20 65 62 70 20 2a 2f 0a 20 20 20 22 6d 6f 76 * ebp */. "mov
05a0: 6c 20 25 25 65 73 70 2c 20 20 20 20 20 20 30 78 l %%esp, 0x
05b0: 63 28 25 25 65 64 78 29 22 20 22 5c 6e 5c 74 22 c(%%edx)" "\n\t"
05c0: 20 2f 2a 20 65 73 70 20 2a 2f 0a 20 20 20 22 6d /* esp */. "m
05d0: 6f 76 6c 20 25 5b 65 72 72 6f 72 5d 2c 20 20 20 ovl %[error],
05e0: 30 78 31 30 28 25 25 65 64 78 29 22 20 20 20 22 0x10(%%edx)" "
05f0: 5c 6e 5c 74 22 20 2f 2a 20 73 74 61 74 75 73 20 \n\t" /* status
0600: 2a 2f 0a 0a 20 20 20 2f 2a 0a 20 20 20 20 2a 20 */.. /*. *
0610: 4c 69 6e 6b 20 74 68 65 20 45 58 43 45 50 54 49 Link the EXCEPTI
0620: 4f 4e 5f 52 45 47 49 53 54 52 41 54 49 4f 4e 20 ON_REGISTRATION
0630: 6f 6e 20 74 68 65 20 63 68 61 69 6e 0a 20 20 20 on the chain.
0640: 20 2a 2f 0a 0a 20 20 20 22 6d 6f 76 6c 20 25 25 */.. "movl %%
0650: 65 64 78 2c 20 20 20 20 20 20 25 25 66 73 3a 30 edx, %%fs:0
0660: 22 20 20 20 20 20 22 5c 6e 5c 74 22 0a 0a 20 20 " "\n\t"..
0670: 20 2f 2a 0a 20 20 20 20 2a 20 44 6f 20 74 68 65 /*. * Do the
0680: 20 43 50 55 49 44 20 69 6e 73 74 72 75 63 74 69 CPUID instructi
0690: 6f 6e 2c 20 61 6e 64 20 73 61 76 65 20 74 68 65 on, and save the
06a0: 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 results in the
06b0: 27 72 65 67 73 50 74 72 27 0a 20 20 20 20 2a 20 'regsPtr'. *
06c0: 61 72 65 61 2e 0a 20 20 20 20 2a 2f 0a 0a 20 20 area.. */..
06d0: 20 22 6d 6f 76 6c 20 25 5b 72 70 74 72 5d 2c 20 "movl %[rptr],
06e0: 25 25 65 64 69 22 20 20 20 20 20 20 22 5c 6e 5c %%edi" "\n\
06f0: 74 22 0a 20 20 20 22 6d 6f 76 6c 20 25 5b 69 6e t". "movl %[in
0700: 64 65 78 5d 2c 20 20 20 25 25 65 61 78 22 20 20 dex], %%eax"
0710: 20 20 20 20 22 5c 6e 5c 74 22 0a 20 20 20 22 63 "\n\t". "c
0720: 70 75 69 64 22 20 20 20 20 20 20 20 20 20 20 20 puid"
0730: 20 20 20 22 5c 6e 5c 74 22 0a 20 20 20 22 6d 6f "\n\t". "mo
0740: 76 6c 20 25 25 65 61 78 2c 20 20 20 20 20 20 30 vl %%eax, 0
0750: 78 30 28 25 25 65 64 69 29 22 20 22 5c 6e 5c 74 x0(%%edi)" "\n\t
0760: 22 0a 20 20 20 22 6d 6f 76 6c 20 25 25 65 62 78 ". "movl %%ebx
0770: 2c 20 20 20 20 20 20 30 78 34 28 25 25 65 64 69 , 0x4(%%edi
0780: 29 22 20 22 5c 6e 5c 74 22 0a 20 20 20 22 6d 6f )" "\n\t". "mo
0790: 76 6c 20 25 25 65 63 78 2c 20 20 20 20 20 20 30 vl %%ecx, 0
07a0: 78 38 28 25 25 65 64 69 29 22 20 22 5c 6e 5c 74 x8(%%edi)" "\n\t
07b0: 22 0a 20 20 20 22 6d 6f 76 6c 20 25 25 65 64 78 ". "movl %%edx
07c0: 2c 20 20 20 20 20 20 30 78 63 28 25 25 65 64 69 , 0xc(%%edi
07d0: 29 22 20 22 5c 6e 5c 74 22 0a 0a 20 20 20 2f 2a )" "\n\t".. /*
07e0: 0a 20 20 20 20 2a 20 43 6f 6d 65 20 68 65 72 65 . * Come here
07f0: 20 6f 6e 20 61 20 6e 6f 72 6d 61 6c 20 65 78 69 on a normal exi
0800: 74 2e 20 52 65 63 6f 76 65 72 20 74 68 65 20 45 t. Recover the E
0810: 58 43 45 50 54 49 4f 4e 5f 52 45 47 49 53 54 52 XCEPTION_REGISTR
0820: 41 54 49 4f 4e 20 61 6e 64 0a 20 20 20 20 2a 20 ATION and. *
0830: 73 74 6f 72 65 20 61 20 54 43 4c 5f 4f 4b 20 73 store a TCL_OK s
0840: 74 61 74 75 73 2e 0a 20 20 20 20 2a 2f 0a 0a 20 tatus.. */..
0850: 20 20 22 6d 6f 76 6c 20 25 25 66 73 3a 30 2c 20 "movl %%fs:0,
0860: 20 20 20 20 25 25 65 64 78 22 20 20 20 20 20 20 %%edx"
0870: 22 5c 6e 5c 74 22 0a 20 20 20 22 6d 6f 76 6c 20 "\n\t". "movl
0880: 25 5b 6f 6b 5d 2c 20 20 20 20 20 20 25 25 65 61 %[ok], %%ea
0890: 78 22 20 20 20 20 20 20 22 5c 6e 5c 74 22 0a 20 x" "\n\t".
08a0: 20 20 22 6d 6f 76 6c 20 25 25 65 61 78 2c 20 20 "movl %%eax,
08b0: 20 20 20 20 30 78 31 30 28 25 25 65 64 78 29 22 0x10(%%edx)"
08c0: 20 20 20 22 5c 6e 5c 74 22 0a 20 20 20 22 6a 6d "\n\t". "jm
08d0: 70 20 20 32 66 22 20 20 20 20 20 20 20 20 20 20 p 2f"
08e0: 20 20 22 5c 6e 22 0a 0a 20 20 20 2f 2a 0a 20 20 "\n".. /*.
08f0: 20 20 2a 20 43 6f 6d 65 20 68 65 72 65 20 6f 6e * Come here on
0900: 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 20 47 an exception. G
0910: 65 74 20 74 68 65 20 45 58 43 45 50 54 49 4f 4e et the EXCEPTION
0920: 5f 52 45 47 49 53 54 52 41 54 49 4f 4e 20 74 68 _REGISTRATION th
0930: 61 74 20 77 65 0a 20 20 20 20 2a 20 70 72 65 76 at we. * prev
0940: 69 6f 75 73 6c 79 20 70 75 74 20 6f 6e 20 74 68 iously put on th
0950: 65 20 63 68 61 69 6e 2e 0a 20 20 20 20 2a 2f 0a e chain.. */.
0960: 0a 20 20 20 22 31 3a 22 20 20 20 20 20 20 20 20 . "1:"
0970: 20 20 20 20 20 20 22 5c 74 22 0a 20 20 20 22 6d "\t". "m
0980: 6f 76 6c 20 25 25 66 73 3a 30 2c 20 20 20 20 20 ovl %%fs:0,
0990: 25 25 65 64 78 22 20 20 20 20 20 20 22 5c 6e 5c %%edx" "\n\
09a0: 74 22 0a 20 20 20 22 6d 6f 76 6c 20 30 78 38 28 t". "movl 0x8(
09b0: 25 25 65 64 78 29 2c 20 25 25 65 64 78 22 20 20 %%edx), %%edx"
09c0: 20 20 20 20 22 5c 6e 5c 74 22 0a 0a 20 20 20 2f "\n\t".. /
09d0: 2a 0a 20 20 20 20 2a 20 43 6f 6d 65 20 68 65 72 *. * Come her
09e0: 65 20 68 6f 77 65 76 65 72 20 77 65 20 65 78 69 e however we exi
09f0: 74 65 64 2e 20 52 65 73 74 6f 72 65 20 63 6f 6e ted. Restore con
0a00: 74 65 78 74 20 66 72 6f 6d 20 74 68 65 0a 20 20 text from the.
0a10: 20 20 2a 20 45 58 43 45 50 54 49 4f 4e 5f 52 45 * EXCEPTION_RE
0a20: 47 49 53 54 52 41 54 49 4f 4e 20 69 6e 20 63 61 GISTRATION in ca
0a30: 73 65 20 74 68 65 20 73 74 61 63 6b 20 69 73 20 se the stack is
0a40: 75 6e 62 61 6c 61 6e 63 65 64 2e 0a 20 20 20 20 unbalanced..
0a50: 2a 2f 0a 0a 20 20 20 22 32 3a 22 20 20 20 20 20 */.. "2:"
0a60: 20 20 20 20 20 20 20 20 20 22 5c 74 22 0a 20 20 "\t".
0a70: 20 22 6d 6f 76 6c 20 30 78 63 28 25 25 65 64 78 "movl 0xc(%%edx
0a80: 29 2c 20 25 25 65 73 70 22 20 20 20 20 20 20 22 ), %%esp" "
0a90: 5c 6e 5c 74 22 0a 20 20 20 22 6d 6f 76 6c 20 30 \n\t". "movl 0
0aa0: 78 38 28 25 25 65 64 78 29 2c 20 25 25 65 62 70 x8(%%edx), %%ebp
0ab0: 22 20 20 20 20 20 20 22 5c 6e 5c 74 22 0a 20 20 " "\n\t".
0ac0: 20 22 6d 6f 76 6c 20 30 78 30 28 25 25 65 64 78 "movl 0x0(%%edx
0ad0: 29 2c 20 25 25 65 61 78 22 20 20 20 20 20 20 22 ), %%eax" "
0ae0: 5c 6e 5c 74 22 0a 20 20 20 22 6d 6f 76 6c 20 25 \n\t". "movl %
0af0: 25 65 61 78 2c 20 20 20 20 20 20 25 25 66 73 3a %eax, %%fs:
0b00: 30 22 20 20 20 20 20 22 5c 6e 5c 74 22 0a 0a 20 0" "\n\t"..
0b10: 20 20 3a 0a 20 20 20 2f 2a 20 4e 6f 20 6f 75 74 :. /* No out
0b20: 70 75 74 73 20 2a 2f 0a 20 20 20 3a 0a 20 20 20 puts */. :.
0b30: 5b 69 6e 64 65 78 5d 20 20 20 20 20 22 6d 22 20 [index] "m"
0b40: 20 20 28 69 6e 64 65 78 29 2c 0a 20 20 20 5b 72 (index),. [r
0b50: 70 74 72 5d 20 20 20 20 20 20 22 6d 22 20 20 20 ptr] "m"
0b60: 28 72 65 67 73 50 74 72 29 2c 0a 20 20 20 5b 72 (regsPtr),. [r
0b70: 65 67 69 73 74 72 61 74 69 6f 6e 5d 20 22 6d 22 egistration] "m"
0b80: 20 20 20 28 72 65 67 69 73 74 72 61 74 69 6f 6e (registration
0b90: 29 2c 0a 20 20 20 5b 6f 6b 5d 20 20 20 20 20 22 ),. [ok] "
0ba0: 69 22 20 20 20 28 54 43 4c 5f 4f 4b 29 2c 0a 20 i" (TCL_OK),.
0bb0: 20 20 5b 65 72 72 6f 72 5d 20 20 20 20 20 22 69 [error] "i
0bc0: 22 20 20 20 28 54 43 4c 5f 45 52 52 4f 52 29 0a " (TCL_ERROR).
0bd0: 20 20 20 3a 0a 20 20 20 22 25 65 61 78 22 2c 20 :. "%eax",
0be0: 22 25 65 62 78 22 2c 20 22 25 65 63 78 22 2c 20 "%ebx", "%ecx",
0bf0: 22 25 65 64 78 22 2c 20 22 25 65 73 69 22 2c 20 "%edx", "%esi",
0c00: 22 25 65 64 69 22 2c 20 22 6d 65 6d 6f 72 79 22 "%edi", "memory"
0c10: 29 3b 0a 20 20 20 20 73 74 61 74 75 73 20 3d 20 );. status =
0c20: 72 65 67 69 73 74 72 61 74 69 6f 6e 2e 73 74 61 registration.sta
0c30: 74 75 73 3b 0a 0a 23 65 6c 69 66 20 64 65 66 69 tus;..#elif defi
0c40: 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 20 26 26 ned(_MSC_VER) &&
0c50: 20 21 64 65 66 69 6e 65 64 28 5f 57 49 4e 36 34 !defined(_WIN64
0c60: 29 0a 20 20 20 20 2f 2a 0a 20 20 20 20 20 2a 20 ). /*. *
0c70: 44 65 66 69 6e 65 20 61 20 73 74 72 75 63 74 75 Define a structu
0c80: 72 65 20 69 6e 20 74 68 65 20 73 74 61 63 6b 20 re in the stack
0c90: 66 72 61 6d 65 20 74 6f 20 68 6f 6c 64 20 74 68 frame to hold th
0ca0: 65 20 72 65 67 69 73 74 65 72 73 2e 0a 20 20 20 e registers..
0cb0: 20 20 2a 2f 0a 20 20 20 20 5f 74 72 79 20 7b 0a */. _try {.
0cc0: 09 5f 61 73 6d 20 7b 0a 09 20 20 20 20 70 75 73 ._asm {.. pus
0cd0: 68 20 20 20 20 65 62 78 0a 09 20 20 20 20 70 75 h ebx.. pu
0ce0: 73 68 20 20 20 20 65 63 78 0a 09 20 20 20 20 70 sh ecx.. p
0cf0: 75 73 68 20 20 20 20 65 64 78 0a 09 20 20 20 20 ush edx..
0d00: 6d 6f 76 09 20 20 20 20 65 61 78 2c 20 72 65 67 mov. eax, reg
0d10: 73 2e 64 77 30 0a 09 20 20 20 20 63 70 75 69 64 s.dw0.. cpuid
0d20: 0a 09 20 20 20 20 6d 6f 76 09 20 20 20 20 72 65 .. mov. re
0d30: 67 73 2e 64 77 30 2c 20 65 61 78 0a 09 20 20 20 gs.dw0, eax..
0d40: 20 6d 6f 76 09 20 20 20 20 72 65 67 73 2e 64 77 mov. regs.dw
0d50: 31 2c 20 65 62 78 0a 09 20 20 20 20 6d 6f 76 09 1, ebx.. mov.
0d60: 20 20 20 20 72 65 67 73 2e 64 77 32 2c 20 65 63 regs.dw2, ec
0d70: 78 0a 09 20 20 20 20 6d 6f 76 09 20 20 20 20 72 x.. mov. r
0d80: 65 67 73 2e 64 77 33 2c 20 65 64 78 0a 09 20 20 egs.dw3, edx..
0d90: 20 20 70 6f 70 09 20 20 20 20 65 64 78 0a 09 20 pop. edx..
0da0: 20 20 20 70 6f 70 09 20 20 20 20 65 63 78 0a 09 pop. ecx..
0db0: 20 20 20 20 70 6f 70 09 20 20 20 20 65 62 78 0a pop. ebx.
0dc0: 09 7d 0a 0a 09 2f 2a 0a 09 20 2a 20 43 6f 70 79 .}.../*.. * Copy
0dd0: 20 72 65 67 73 20 62 61 63 6b 20 6f 75 74 20 74 regs back out t
0de0: 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 09 20 o the caller...
0df0: 2a 2f 0a 09 72 65 67 73 50 74 72 5b 30 5d 20 3d */..regsPtr[0] =
0e00: 20 72 65 67 73 2e 64 77 30 3b 0a 09 72 65 67 73 regs.dw0;..regs
0e10: 50 74 72 5b 31 5d 20 3d 20 72 65 67 73 2e 64 77 Ptr[1] = regs.dw
0e20: 31 3b 0a 09 72 65 67 73 50 74 72 5b 32 5d 20 3d 1;..regsPtr[2] =
0e30: 20 72 65 67 73 2e 64 77 32 3b 0a 09 72 65 67 73 regs.dw2;..regs
0e40: 50 74 72 5b 33 5d 20 3d 20 72 65 67 73 2e 64 77 Ptr[3] = regs.dw
0e50: 33 3b 0a 0a 09 73 74 61 74 75 73 20 3d 20 54 43 3;...status = TC
0e60: 4c 5f 4f 4b 3b 0a 20 20 20 20 7d 20 5f 5f 65 78 L_OK;. } __ex
0e70: 63 65 70 74 28 45 58 43 45 50 54 49 4f 4e 5f 45 cept(EXCEPTION_E
0e80: 58 45 43 55 54 45 5f 48 41 4e 44 4c 45 52 29 20 XECUTE_HANDLER)
0e90: 7b 0a 09 2f 2a 20 64 6f 20 6e 6f 74 68 69 6e 67 {../* do nothing
0ea0: 20 2a 2f 0a 20 20 20 20 7d 0a 0a 23 65 6c 69 66 */. }..#elif
0eb0: 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 defined(__BORLA
0ec0: 4e 44 43 5f 5f 29 0a 2f 2f 20 43 6f 64 65 20 66 NDC__).// Code f
0ed0: 72 6f 6d 0a 2f 2f 20 68 74 74 70 3a 2f 2f 67 72 rom.// http://gr
0ee0: 6f 75 70 73 2e 67 6f 6f 67 6c 65 2e 63 6f 6d 2f oups.google.com/
0ef0: 67 72 6f 75 70 2f 62 6f 72 6c 61 6e 64 2e 70 75 group/borland.pu
0f00: 62 6c 69 63 2e 63 70 70 62 75 69 6c 64 65 72 2e blic.cppbuilder.
0f10: 6c 61 6e 67 75 61 67 65 2f 62 72 6f 77 73 65 5f language/browse_
0f20: 74 68 72 65 61 64 2f 74 68 72 65 61 64 2f 62 34 thread/thread/b4
0f30: 39 37 63 30 35 32 66 35 35 35 34 64 39 36 2f 36 97c052f5554d96/6
0f40: 61 65 38 33 33 62 30 66 37 30 62 38 65 38 34 3f ae833b0f70b8e84?
0f50: 71 3d 63 70 75 69 64 2b 4f 52 2b 63 70 75 2d 69 q=cpuid+OR+cpu-i
0f60: 64 2b 67 72 6f 75 70 3a 62 6f 72 6c 61 6e 64 2e d+group:borland.
0f70: 70 75 62 6c 69 63 2e 63 70 70 62 75 69 6c 64 65 public.cppbuilde
0f80: 72 2a 23 36 61 65 38 33 33 62 30 66 37 30 62 38 r*#6ae833b0f70b8
0f90: 65 38 34 0a 20 20 20 20 5f 5f 61 73 6d 20 7b 0a e84. __asm {.
0fa0: 20 20 20 20 20 20 20 20 20 20 20 20 50 55 53 48 PUSH
0fb0: 20 20 20 20 45 42 58 20 20 20 20 20 20 20 20 20 EBX
0fc0: 20 20 20 20 2f 2f 20 53 61 76 65 20 61 66 66 65 // Save affe
0fd0: 63 74 65 64 20 72 65 67 69 73 74 65 72 0a 20 20 cted register.
0fe0: 20 20 20 20 20 20 20 20 20 20 50 55 53 48 20 20 PUSH
0ff0: 20 20 45 44 49 0a 20 20 20 20 20 20 20 20 20 20 EDI.
1000: 20 20 4d 4f 56 20 20 20 20 20 45 44 49 2c 72 65 MOV EDI,re
1010: 67 73 50 74 72 20 20 20 20 20 2f 2f 20 64 65 73 gsPtr // des
1020: 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20 20 20 20 tination.
1030: 20 20 20 20 20 4d 4f 56 20 20 20 20 20 45 41 58 MOV EAX
1040: 2c 69 6e 64 65 78 0a 20 20 20 20 20 20 20 20 20 ,index.
1050: 20 20 20 44 57 20 20 20 20 20 20 30 78 41 32 30 DW 0xA20
1060: 46 20 20 20 20 20 20 20 20 20 20 2f 2f 20 43 50 F // CP
1070: 55 49 44 20 43 6f 6d 6d 61 6e 64 7d 0a 20 20 20 UID Command}.
1080: 20 20 20 20 20 20 20 20 20 53 54 4f 53 44 20 20 STOSD
1090: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10a0: 20 2f 2f 20 43 50 55 49 44 5b 31 5d 0a 20 20 20 // CPUID[1].
10b0: 20 20 20 20 20 20 20 20 20 4d 4f 56 20 20 20 20 MOV
10c0: 20 45 41 58 2c 45 42 58 0a 20 20 20 20 20 20 20 EAX,EBX.
10d0: 20 20 20 20 20 53 54 4f 53 44 20 20 20 20 20 20 STOSD
10e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 //
10f0: 43 50 55 49 44 5b 32 5d 0a 20 20 20 20 20 20 20 CPUID[2].
1100: 20 20 20 20 20 4d 4f 56 20 20 20 20 20 45 41 58 MOV EAX
1110: 2c 45 43 58 0a 20 20 20 20 20 20 20 20 20 20 20 ,ECX.
1120: 20 53 54 4f 53 44 20 20 20 20 20 20 20 20 20 20 STOSD
1130: 20 20 20 20 20 20 20 20 20 2f 2f 20 43 50 55 49 // CPUI
1140: 44 5b 33 5d 0a 20 20 20 20 20 20 20 20 20 20 20 D[3].
1150: 20 4d 4f 56 20 20 20 20 20 45 41 58 2c 45 44 58 MOV EAX,EDX
1160: 0a 20 20 20 20 20 20 20 20 20 20 20 20 53 54 4f . STO
1170: 53 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 SD
1180: 20 20 20 20 20 2f 2f 20 43 50 55 49 44 5b 34 5d // CPUID[4]
1190: 0a 20 20 20 20 20 20 20 20 20 20 20 20 50 4f 50 . POP
11a0: 20 20 20 20 20 45 44 49 20 20 20 20 20 20 20 20 EDI
11b0: 20 20 20 20 20 2f 2f 20 52 65 73 74 6f 72 65 20 // Restore
11c0: 72 65 67 69 73 74 65 72 73 0a 20 20 20 20 20 20 registers.
11d0: 20 20 20 20 20 20 50 4f 50 20 20 20 20 20 45 42 POP EB
11e0: 58 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 73 X. }. s
11f0: 74 61 74 75 73 20 3d 20 54 43 4c 5f 4f 4b 3b 0a tatus = TCL_OK;.
1200: 23 65 6c 73 65 0a 20 20 20 20 2f 2a 0a 20 20 20 #else. /*.
1210: 20 20 2a 20 44 6f 6e 27 74 20 6b 6e 6f 77 20 68 * Don't know h
1220: 6f 77 20 74 6f 20 64 6f 20 61 73 73 65 6d 62 6c ow to do assembl
1230: 79 20 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20 y code for this
1240: 63 6f 6d 70 69 6c 65 72 20 61 6e 64 2f 6f 72 0a compiler and/or.
1250: 20 20 20 20 20 2a 20 61 72 63 68 69 74 65 63 74 * architect
1260: 75 72 65 2e 0a 20 20 20 20 20 2a 2f 0a 23 65 6e ure.. */.#en
1270: 64 69 66 0a 20 20 20 20 72 65 74 75 72 6e 20 73 dif. return s
1280: 74 61 74 75 73 3b 0a 7d 0a 0a 2f 2f 20 41 64 61 tatus;.}..// Ada
1290: 70 74 65 64 20 66 72 6f 6d 20 74 63 6c 57 69 6e pted from tclWin
12a0: 54 69 6d 65 2e 63 0a 69 6e 74 20 6d 61 69 6e 28 Time.c.int main(
12b0: 76 6f 69 64 29 20 7b 0a 20 20 20 75 6e 73 69 67 void) {. unsig
12c0: 6e 65 64 20 69 6e 74 20 72 65 67 73 5b 35 5d 2c ned int regs[5],
12d0: 20 74 6d 70 2c 20 72 65 73 3b 0a 20 20 20 4c 41 tmp, res;. LA
12e0: 52 47 45 5f 49 4e 54 45 47 45 52 20 66 72 65 71 RGE_INTEGER freq
12f0: 75 3b 0a 0a 20 20 20 2f 2f 20 53 68 6f 77 20 74 u;.. // Show t
1300: 68 65 20 63 6f 75 6e 74 65 72 27 73 20 66 72 65 he counter's fre
1310: 71 75 65 6e 63 79 0a 20 20 20 69 66 20 28 21 20 quency. if (!
1320: 51 75 65 72 79 50 65 72 66 6f 72 6d 61 6e 63 65 QueryPerformance
1330: 46 72 65 71 75 65 6e 63 79 28 26 66 72 65 71 75 Frequency(&frequ
1340: 29 29 20 7b 0a 20 20 20 20 20 20 70 72 69 6e 74 )) {. print
1350: 66 28 22 4e 6f 20 70 65 72 66 6f 72 6d 61 6e 63 f("No performanc
1360: 65 20 63 6f 75 6e 74 65 72 2e 5c 6e 22 29 3b 0a e counter.\n");.
1370: 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 } else {.
1380: 20 20 70 72 69 6e 74 66 28 22 50 65 72 66 6f 72 printf("Perfor
1390: 6d 61 6e 63 65 20 63 6f 75 6e 74 65 72 27 73 20 mance counter's
13a0: 66 72 65 71 75 65 6e 63 79 3a 20 25 6c 75 20 48 frequency: %lu H
13b0: 7a 2e 5c 6e 22 2c 20 66 72 65 71 75 2e 51 75 61 z.\n", frequ.Qua
13c0: 64 50 61 72 74 29 3b 0a 20 20 20 7d 0a 20 20 20 dPart);. }.
13d0: 2f 2f 20 49 6e 66 6f 20 66 6f 72 20 63 6f 64 65 // Info for code
13e0: 20 27 30 27 0a 20 20 20 69 66 20 28 54 63 6c 57 '0'. if (TclW
13f0: 69 6e 43 50 55 49 44 28 30 2c 20 72 65 67 73 29 inCPUID(0, regs)
1400: 20 21 3d 20 54 43 4c 5f 4f 4b 29 20 7b 0a 20 20 != TCL_OK) {.
1410: 20 20 20 20 70 72 69 6e 74 66 28 22 54 63 6c 57 printf("TclW
1420: 69 6e 43 50 55 49 44 20 77 69 74 68 20 63 6f 64 inCPUID with cod
1430: 65 20 27 30 27 20 66 61 69 6c 65 64 2e 5c 6e 22 e '0' failed.\n"
1440: 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 );. return
1450: 31 3b 0a 20 20 20 7d 0a 20 20 20 2f 2f 20 53 68 1;. }. // Sh
1460: 6f 77 20 69 6e 66 6f 3a 20 48 61 76 65 20 74 6f ow info: Have to
1470: 20 72 65 2d 61 72 72 61 6e 67 65 20 74 68 65 20 re-arrange the
1480: 63 6f 6e 74 65 6e 74 20 6f 66 20 27 72 65 67 73 content of 'regs
1490: 27 0a 20 20 20 72 65 67 73 5b 34 5d 20 3d 20 30 '. regs[4] = 0
14a0: 3b 20 20 20 20 20 20 20 20 20 2f 2f 20 64 65 6c ; // del
14b0: 69 6d 69 74 20 73 74 72 69 6e 67 0a 20 20 20 2f imit string. /
14c0: 2f 20 73 77 61 70 20 72 65 67 73 5b 32 5d 20 26 / swap regs[2] &
14d0: 20 72 65 67 73 5b 33 5d 0a 20 20 20 74 6d 70 20 regs[3]. tmp
14e0: 3d 20 72 65 67 73 5b 32 5d 3b 0a 20 20 20 72 65 = regs[2];. re
14f0: 67 73 5b 32 5d 20 3d 20 72 65 67 73 5b 33 5d 3b gs[2] = regs[3];
1500: 0a 20 20 20 72 65 67 73 5b 33 5d 20 3d 20 74 6d . regs[3] = tm
1510: 70 3b 0a 20 20 20 70 72 69 6e 74 66 28 22 52 65 p;. printf("Re
1520: 73 75 6c 74 73 20 6f 66 20 43 50 55 49 44 28 30 sults of CPUID(0
1530: 29 3a 20 25 73 5c 6e 22 2c 20 26 72 65 67 73 5b ): %s\n", ®s[
1540: 31 5d 29 3b 0a 20 20 20 72 65 73 20 3d 20 28 73 1]);. res = (s
1550: 74 72 63 6d 70 28 28 63 6f 6e 73 74 20 63 68 61 trcmp((const cha
1560: 72 20 2a 29 26 72 65 67 73 5b 31 5d 2c 20 22 47 r *)®s[1], "G
1570: 65 6e 75 69 6e 65 49 6e 74 65 6c 22 29 20 3d 3d enuineIntel") ==
1580: 20 30 29 3b 0a 20 20 20 70 72 69 6e 74 66 28 22 0);. printf("
1590: 27 70 65 72 66 6f 72 6d 61 6e 63 65 20 63 6f 75 'performance cou
15a0: 6e 74 65 72 20 61 76 61 69 6c 61 62 6c 65 27 20 nter available'
15b0: 28 74 65 73 74 20 70 61 72 74 20 31 29 20 28 31 (test part 1) (1
15c0: 20 3d 3d 20 6f 6b 29 3f 20 25 64 5c 6e 22 2c 20 == ok)? %d\n",
15d0: 72 65 73 29 3b 0a 0a 20 20 20 2f 2f 20 49 6e 66 res);.. // Inf
15e0: 6f 20 66 6f 72 20 63 6f 64 65 20 27 31 27 0a 20 o for code '1'.
15f0: 20 20 69 66 20 28 54 63 6c 57 69 6e 43 50 55 49 if (TclWinCPUI
1600: 44 28 31 2c 20 72 65 67 73 29 20 21 3d 20 54 43 D(1, regs) != TC
1610: 4c 5f 4f 4b 29 20 7b 0a 20 20 20 20 20 20 70 72 L_OK) {. pr
1620: 69 6e 74 66 28 22 54 63 6c 57 69 6e 43 50 55 49 intf("TclWinCPUI
1630: 44 20 77 69 74 68 20 63 6f 64 65 20 27 31 27 20 D with code '1'
1640: 66 61 69 6c 65 64 2e 5c 6e 22 29 3b 0a 20 20 20 failed.\n");.
1650: 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
1660: 7d 0a 20 20 20 2f 2f 20 41 70 70 6c 79 20 74 68 }. // Apply th
1670: 65 20 74 65 73 74 20 66 72 6f 6d 20 4e 61 74 69 e test from Nati
1680: 76 65 47 65 74 54 69 6d 65 20 28 32 6e 64 20 70 veGetTime (2nd p
1690: 61 72 74 29 0a 20 20 20 20 20 20 20 20 20 20 20 art).
16a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
16b0: 50 65 6e 74 69 75 6d 20 34 20 2a 2f 0a 20 20 20 Pentium 4 */.
16c0: 72 65 73 20 3d 20 28 72 65 67 73 5b 30 5d 26 30 res = (regs[0]&0
16d0: 78 30 30 30 30 30 46 30 30 29 20 3d 3d 20 30 78 x00000F00) == 0x
16e0: 30 30 30 30 30 46 30 30 0a 20 20 20 20 20 20 20 00000F00.
16f0: 20 20 20 20 20 20 20 2f 2a 20 45 78 74 65 6e 64 /* Extend
1700: 65 64 20 66 61 6d 69 6c 79 20 20 20 20 20 20 20 ed family
1710: 20 20 20 20 20 20 48 79 70 65 72 74 68 72 65 61 Hyperthrea
1720: 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 7c d */. |
1730: 7c 20 28 28 72 65 67 73 5b 30 5d 20 26 20 30 78 | ((regs[0] & 0x
1740: 30 30 46 30 30 30 30 30 29 20 26 26 20 28 72 65 00F00000) && (re
1750: 67 73 5b 33 5d 20 26 20 30 78 31 30 30 30 30 30 gs[3] & 0x100000
1760: 30 30 29 29 3b 0a 20 20 20 70 72 69 6e 74 66 28 00));. printf(
1770: 22 52 65 73 75 6c 74 73 20 6f 66 20 43 50 55 49 "Results of CPUI
1780: 44 28 31 29 5c 6e 22 29 3b 0a 20 20 20 70 72 69 D(1)\n");. pri
1790: 6e 74 66 28 22 27 70 65 72 66 6f 72 6d 61 6e 63 ntf("'performanc
17a0: 65 20 63 6f 75 6e 74 65 72 20 61 76 61 69 6c 61 e counter availa
17b0: 62 6c 65 27 20 28 74 65 73 74 20 70 61 72 74 20 ble' (test part
17c0: 32 29 20 28 31 20 3d 3d 20 6f 6b 29 3f 20 25 64 2) (1 == ok)? %d
17d0: 5c 6e 22 2c 20 72 65 73 29 3b 0a 20 20 20 70 72 \n", res);. pr
17e0: 69 6e 74 66 28 22 44 65 74 61 69 6c 65 64 20 72 intf("Detailed r
17f0: 65 73 75 6c 74 73 5c 6e 22 29 3b 0a 20 20 20 70 esults\n");. p
1800: 72 69 6e 74 66 28 22 72 65 67 73 5b 30 5d 20 3d rintf("regs[0] =
1810: 20 30 78 25 30 38 58 2c 20 72 65 67 73 5b 31 5d 0x%08X, regs[1]
1820: 20 3d 20 30 78 25 30 38 58 2c 20 72 65 67 73 5b = 0x%08X, regs[
1830: 33 5d 20 3d 20 30 78 25 30 38 58 5c 6e 22 2c 0a 3] = 0x%08X\n",.
1840: 20 20 20 20 20 20 20 20 20 20 20 72 65 67 73 5b regs[
1850: 30 5d 2c 20 72 65 67 73 5b 31 5d 2c 20 72 65 67 0], regs[1], reg
1860: 73 5b 33 5d 29 3b 0a 20 20 20 72 65 74 75 72 6e s[3]);. return
1870: 20 30 3b 0a 7d 0a 0;.}.