Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

tsc.c

Go to the documentation of this file.
00001 
00002 //#define _TSC  // do this in the project settings!
00003 
00004 /****************************************************************************************/
00005 /*  TSC                                                                                 */
00006 /*                                                                                      */
00007 /*  Author: Charles Bloom                                                               */
00008 /*  Description: tsc accessors                                                          */
00009 /*                                                                                      */
00010 /*  The contents of this file are subject to the Genesis3D Public License               */
00011 /*  Version 1.01 (the "License"); you may not use this file except in                   */
00012 /*  compliance with the License. You may obtain a copy of the License at                */
00013 /*  http://www.genesis3d.com                                                            */
00014 /*                                                                                      */
00015 /*  Software distributed under the License is distributed on an "AS IS"                 */
00016 /*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See                */
00017 /*  the License for the specific language governing rights and limitations              */
00018 /*  under the License.                                                                  */
00019 /*                                                                                      */
00020 /*  The Original Code is Genesis3D, released March 25, 1999.                            */
00021 /*Genesis3D Version 1.1 released November 15, 1999                            */
00022 /*  Copyright (C) 1999 WildTangent, Inc. All Rights Reserved           */
00023 /*                                                                                      */
00024 /****************************************************************************************/
00025 
00026 typedef unsigned long ulong;
00027 
00028 // {
00029 #ifdef _TSC
00030 
00031 #pragma message("TSC on")
00032 
00033 #include "ram.h"
00034 #include "log.h"
00035 #include <stdio.h>      //sprintf
00036 #include <windows.h>    //outputdebug
00037 
00038 #define MemAlloc(size)  geRam_Allocate(size)
00039 #define MemFree(mem)    geRam_Free(mem)
00040 
00041 #undef  new
00042 #define new(type)               MemAlloc(sizeof(type))
00043 #undef  destroy
00044 #define destroy(mem)    MemFree(mem)
00045 
00046 #include "tsc.h"
00047 
00048 typedef struct tscNode tscNode;
00049 struct tscNode 
00050 {
00051         tscNode *next;
00052         ulong tsc[2];
00053 };
00054 tscNode * head = NULL;
00055 
00056 void pushTSC(void)
00057 {
00058 tscNode *tn;
00059         tn = new(tscNode);
00060         if ( !tn ) return;
00061         tn->next = head;
00062         head = tn;
00063         readTSC(tn->tsc);
00064 }
00065 
00066 double popTSC(void)
00067 {
00068 tscNode *tn;
00069 ulong tsc[2];
00070         readTSC(tsc);
00071         if ( ! head ) return 0.0;
00072         tn = head;
00073         head = head->next;
00074 return diffTSC(tn->tsc,tsc);
00075 }
00076 
00077 void showPopTSC(const char *tag)
00078 {
00079 double time;
00080         time = popTSC();
00081         Log_Printf("%s : %f seconds\n",tag,time);
00082 }
00083 
00084 void showPopTSCper(const char *tag,int items,const char *itemTag)
00085 {
00086 double time,per;
00087         time = popTSC();
00088         per = (time/(double)items);
00089         if ( per < 0.000001 ) 
00090                 Log_Printf("%s : %f = %e per %s\n",tag,time,per,itemTag);
00091         else
00092                 Log_Printf("%s : %f = %f per %s\n",tag,time,per,itemTag);
00093 }
00094 
00095 void readTSC(ulong *hi)
00096 {
00097 ulong *lo = hi + 1;
00098         __asm 
00099         {
00100                 _emit 0x0F;
00101                 _emit 0x31;     // rdtsc
00102                 mov EBX,hi
00103                 mov DWORD PTR [EBX],EDX;
00104                 mov EBX,lo
00105                 mov DWORD PTR [EBX],EAX;
00106         }
00107         return;
00108 }
00109 
00110 #define CPU_HZ (_TSC_CPU_MHZ*1000000.0)
00111 #define msw_scale (4294967296.0/CPU_HZ)
00112 #define lsw_scale (1.0/CPU_HZ)
00113 
00114 double diffTSC(ulong *tsc1,ulong *tsc2)
00115 {
00116 
00117  return (tsc2[1] - tsc1[1])*lsw_scale +
00118                 (tsc2[0] - tsc1[0])*msw_scale;
00119 
00120 }
00121 
00122 #else // _TSC
00123 // }{
00124 
00125 #pragma message("TSC off")
00126 
00127 void pushTSC(void) {}
00128 double popTSC(void) { return 0.0; }
00129 void showPopTSC(const char *tag) { }
00130 void showPopTSCper(const char *tag,int items,const char *itemTag) { }
00131 void readTSC(ulong *hi) {}
00132 double diffTSC(ulong *tsc1,ulong *tsc2) { return 0; }
00133 
00134 #endif // _TSC
00135 // }

Generated on Tue Sep 30 12:36:35 2003 for GTestAndEngine by doxygen 1.3.2