www.digitalmars.com         C & C++   DMDScript  

c++ - Counting Clock cycles

reply "jim p" <x y.com> writes:
I hope this isn't too much of a stupid question.

I've just developed a silly little real time network routing simulator.

I need to determine the minimum processor speed that will perform all the
necessary calculations to keep it operating in real time.

Forgetting memory access times, to do this I need to know how much CPU work
the application is doing.

Is there any way of counting CPU clock cycles for example, for a particular
function call ???

Jim
Oct 07 2003
parent reply "Walter" <walter digitalmars.com> writes:
The Intel x86 RDTSC instruction is what you're looking for.

"jim p" <x y.com> wrote in message news:blvp83$22tm$1 digitaldaemon.com...
 I hope this isn't too much of a stupid question.

 I've just developed a silly little real time network routing simulator.

 I need to determine the minimum processor speed that will perform all the
 necessary calculations to keep it operating in real time.

 Forgetting memory access times, to do this I need to know how much CPU
work
 the application is doing.

 Is there any way of counting CPU clock cycles for example, for a
particular
 function call ???

 Jim
Oct 08 2003
next sibling parent reply "jim p" <x y.com> writes:
Any pointers to some documentation on the x86 RDTSC instruction ??




"Walter" <walter digitalmars.com> wrote in message
news:bm0cve$2tkb$1 digitaldaemon.com...
 The Intel x86 RDTSC instruction is what you're looking for.

 "jim p" <x y.com> wrote in message news:blvp83$22tm$1 digitaldaemon.com...
 I hope this isn't too much of a stupid question.

 I've just developed a silly little real time network routing simulator.

 I need to determine the minimum processor speed that will perform all
the
 necessary calculations to keep it operating in real time.

 Forgetting memory access times, to do this I need to know how much CPU
work
 the application is doing.

 Is there any way of counting CPU clock cycles for example, for a
particular
 function call ???

 Jim
Oct 08 2003
parent reply "Walter" <walter digitalmars.com> writes:
"jim p" <x y.com> wrote in message news:bm0md1$9sk$1 digitaldaemon.com...
 Any pointers to some documentation on the x86 RDTSC instruction ??
long long rdtsc() { __asm { RDTSC } } should do it.
Oct 08 2003
parent reply "jim p" <x y.com> writes:
Thanks.

Another question for you Walter.

Is it possible to determine the number of CPU instructions used by a
function ?
If so, with help from the RDTSC code below, I could the time taken per
instruction.
And then.....would it be possible to say that a 1700 MHz processor processes
this function in X amount of time, therefore a 1200 MHz processor will take
Y amount time to perform the same function.

Am I talking bollocks ??



"Walter" <walter digitalmars.com> wrote in message
news:bm1ul0$iag$1 digitaldaemon.com...
 "jim p" <x y.com> wrote in message news:bm0md1$9sk$1 digitaldaemon.com...
 Any pointers to some documentation on the x86 RDTSC instruction ??
long long rdtsc() { __asm { RDTSC } } should do it.
Oct 08 2003
next sibling parent "Walter" <walter digitalmars.com> writes:
"jim p" <x y.com> wrote in message news:bm24t5$sk1$1 digitaldaemon.com...
 Is it possible to determine the number of CPU instructions used by a
 function ?
 If so, with help from the RDTSC code below, I could the time taken per
 instruction.
 And then.....would it be possible to say that a 1700 MHz processor
processes
 this function in X amount of time, therefore a 1200 MHz processor will
take
 Y amount time to perform the same function.

 Am I talking bollocks ??
Each instruction takes widely varying lengths of time, so you won't be getting a very meaningful result. You're also reaching a level where it's time to download the Intel CPU processor manuals; I suggest that www.intel.com should be your next stop!
Oct 08 2003
prev sibling parent "_" <mjoyl lineone.netX> writes:
"jim p" <x y.com> wrote in message news:bm24t5$sk1$1 digitaldaemon.com...
 Thanks.

 Another question for you Walter.

 Is it possible to determine the number of CPU instructions used by a
 function ?
 If so, with help from the RDTSC code below, I could the time taken per
 instruction.
 And then.....would it be possible to say that a 1700 MHz processor
processes
 this function in X amount of time, therefore a 1200 MHz processor will
take
 Y amount time to perform the same function.

 Am I talking bollocks ??
As I read it, you are thinking single threaded single tasked CPU loading and that is just not the case in real world installations, while feasibility studies on whether C++ could possibly produce fast enough executable code is a valid proposition, recommended minimum and ideal systems really should be determined by real world trials as different manufacturers processors and their individual renditions of their processor types all perform differently per CPU core clock cycle, not to mention real-time processes hogging the CPU time and all the myriad of threads running on even a home PC let alone a networked router. If you have a functional prototype that works on all the machines you have.. maybe it's time to put it out for limited trials to people with alpha/beta test experience and various system configurations. If there are performance problems, determine the bottlenecks and maybe try re-writing those bottlenecks in inline assembler if the product is to be specific to x86 class processors or #ifdef inline assembler to replace the C functions for x86 platforms... that's just my opinion and I've yet to create anything meaningful with DMC++ yet but I've been debugging/hacking/hand optimising, alpha/beta testing software for several years. But if you must benchtest your functions... why not just use them in a simple benchtest program that reads the current time to highest resolution then calls an empty function say a million times or what ever's needed and reads time after the sequence is done to determine benchtests overheads then re-run again using each of your functions with simulated data and and from that determine whets eating processing time and go from there... i.e. produce calibrated test environment for the functions of the network routing simulator ;-) HTH :-)
Dec 17 2003
prev sibling next sibling parent reply "jim p" <x y.com> writes:
OK its an assembler instruction.
But, I've never used assembler.
Has anyone written a C function to perform this task ??



"Walter" <walter digitalmars.com> wrote in message
news:bm0cve$2tkb$1 digitaldaemon.com...
 The Intel x86 RDTSC instruction is what you're looking for.

 "jim p" <x y.com> wrote in message news:blvp83$22tm$1 digitaldaemon.com...
 I hope this isn't too much of a stupid question.

 I've just developed a silly little real time network routing simulator.

 I need to determine the minimum processor speed that will perform all
the
 necessary calculations to keep it operating in real time.

 Forgetting memory access times, to do this I need to know how much CPU
work
 the application is doing.

 Is there any way of counting CPU clock cycles for example, for a
particular
 function call ???

 Jim
Oct 08 2003
parent roland <--rv ronetech.com> writes:
jim p a écrit :
 OK its an assembler instruction.
 But, I've never used assembler.
 Has anyone written a C function to perform this task ??
 
#if (1==0) ------------------------------------------------------------------------------------------------------ ------------------- Written by Roland VARICHON for RONE Technologies - 254 rue de Pressense 69100 Villeurbane - France - www.ronetech.com //does not hurt Free use //of course if you make a nuclear plan blow because of this code we are not responsible ------------------------------------------------------------------------------------------------------ ------------------- #endif #define _TICK_CTR_PORT 0x43 //read only #define _TICK_PORT 0x40 long getcpufreq(); long long rdtsc_get(); long cputick2ns(const long cputick); long ns2cputick(const long ns); static long _bus_freq = 1193180ul; //bus clock static int _gettick(); static long _cpu_freq = getcpufreq(); long getcpufreq() { int dummy[1]; dummy[0] = 0; //force stack frame, may be not necessary, just a bad habit long long rdtsci,rdtscf; unsigned ticki,tickf; asm { pushf //save if flag stat cli //lock interrupt //cpuid is faster after it had been executed 3 time ??? cpuid cpuid cpuid //warm the cache mov DWORD PTR rdtsci,eax mov DWORD PTR rdtsci[4],edx mov ticki,eax mov tickf,eax //loop until timer >= 0x110 lp0: call _gettick cmp eax,110h jb lp0 //now timer >= 0x110 //now going to loop until timer decrements one for precision dec eax mov ticki,eax sub eax,100h mov tickf,eax lp1: call _gettick cmp eax,ticki ja lp1 cpuid //flush pipeline rdtsc mov DWORD PTR rdtsci,eax mov DWORD PTR rdtsci[4],edx //ok we are now synchronized, initial rdtsc value saved //now wait until timer decrements 0x100 lp2: call _gettick cmp eax,tickf ja lp2 cpuid rdtsc mov DWORD PTR rdtscf,eax mov DWORD PTR rdtscf[4],edx //restaure if stat popf } return ((rdtscf-rdtsci)*_bus_freq)/0x100; //bus_T = 1/bus_Freq //cpu_T = 1/cpu_Freq //0x100*bus_T = delta_rdtsc*cpu_T //=> cpu_Freq = bus_Freq*delta_rdtsc/0x100 } long long rdtsc_get() { //return current value of rdtsc (in cpu tick) long long ret; asm cpuid; asm rdtsc; // asm mov DWORD PTR ret,eax; // asm mov DWORD PTR ret[4],eax; return; } long cputick2ns(const long cputick) { //cpu tick to nanosecond //convert cpu ticks in nanosecond //excess convertion return ( (((long long)cputick) * 1000000000LL) + ((long long)(cpufreq()-1)) ) / ((long long)cpufreq()); } long ns2cputick(const long ns) { //nanosecond to cpu tisk //convert nanoseconds in cpu ticks //excess convertion return ( (((long long)ns) * ((long long)cpufreq())) + (1000000000LL-1) ) / 1000000000LL; } //------------------------------------------------------------------------ static int _gettick() { //return the current value of the 8254's timer 0 #if (_INTSIZE==4) asm { xor eax,eax out _TICK_CTR_PORT,al //counter 0 latch in al,_TICK_PORT //lo cp0 mov ah,al in al,_TICK_PORT //hi cp0 xchg ah,al } return _EAX; #else int ret = 0; outp(_TICK_CTR_PORT,0); ret |= inp(_TICK_PORT); ret |= (((unsigned)inp(_TICK_PORT))<<8); return ret; #endif }
Oct 16 2003
prev sibling parent reply "jim p" <x y.com> writes:
No problem, I found a c++ class to do the job.

CCPUTicker v1.22
Written by J. M.McGuiness
source code can be downloaded from
http://www.codeproject.com/datetime/ccputicker.asp?print=true if anyone is
interested



"Walter" <walter digitalmars.com> wrote in message
news:bm0cve$2tkb$1 digitaldaemon.com...
 The Intel x86 RDTSC instruction is what you're looking for.

 "jim p" <x y.com> wrote in message news:blvp83$22tm$1 digitaldaemon.com...
 I hope this isn't too much of a stupid question.

 I've just developed a silly little real time network routing simulator.

 I need to determine the minimum processor speed that will perform all
the
 necessary calculations to keep it operating in real time.

 Forgetting memory access times, to do this I need to know how much CPU
work
 the application is doing.

 Is there any way of counting CPU clock cycles for example, for a
particular
 function call ???

 Jim
Oct 08 2003
next sibling parent reply "Charles Sanders" <sanders-consulting comcast.net> writes:
Very cool, what are you doing all this profiling for ?  Id like to see some
results of whatever your testing.

C
"jim p" <x y.com> wrote in message news:bm1a7i$1e2d$1 digitaldaemon.com...
 No problem, I found a c++ class to do the job.

 CCPUTicker v1.22
 Written by J. M.McGuiness
 source code can be downloaded from
 http://www.codeproject.com/datetime/ccputicker.asp?print=true if anyone is
 interested



 "Walter" <walter digitalmars.com> wrote in message
 news:bm0cve$2tkb$1 digitaldaemon.com...
 The Intel x86 RDTSC instruction is what you're looking for.

 "jim p" <x y.com> wrote in message
news:blvp83$22tm$1 digitaldaemon.com...
 I hope this isn't too much of a stupid question.

 I've just developed a silly little real time network routing
simulator.
 I need to determine the minimum processor speed that will perform all
the
 necessary calculations to keep it operating in real time.

 Forgetting memory access times, to do this I need to know how much CPU
work
 the application is doing.

 Is there any way of counting CPU clock cycles for example, for a
particular
 function call ???

 Jim
Oct 08 2003
parent "Christian Kaiser" <chk online.de> writes:
Please remember that you might be in a multitasking environment, so a
function can take a picosecond or a second, depends on the system load,
process priority etc.

Christian

"Charles Sanders" <sanders-consulting comcast.net> wrote in message
news:bm2g79$1c9o$1 digitaldaemon.com...
 Very cool, what are you doing all this profiling for ?  Id like to see
some
 results of whatever your testing.

 C
 "jim p" <x y.com> wrote in message news:bm1a7i$1e2d$1 digitaldaemon.com...
 No problem, I found a c++ class to do the job.

 CCPUTicker v1.22
 Written by J. M.McGuiness
 source code can be downloaded from
 http://www.codeproject.com/datetime/ccputicker.asp?print=true if anyone
is
 interested



 "Walter" <walter digitalmars.com> wrote in message
 news:bm0cve$2tkb$1 digitaldaemon.com...
 The Intel x86 RDTSC instruction is what you're looking for.

 "jim p" <x y.com> wrote in message
news:blvp83$22tm$1 digitaldaemon.com...
 I hope this isn't too much of a stupid question.

 I've just developed a silly little real time network routing
simulator.
 I need to determine the minimum processor speed that will perform
all
 the
 necessary calculations to keep it operating in real time.

 Forgetting memory access times, to do this I need to know how much
CPU
 work
 the application is doing.

 Is there any way of counting CPU clock cycles for example, for a
particular
 function call ???

 Jim
Oct 09 2003
prev sibling parent "jim p" <x y.com> writes:
I was trying to gather some meaningful results for my masters dissertation
that has to be handed in tomorrow.
I've run out of time and haven't been able to get the exact results I
wanted.

But hey, not to worry.
There goes my distinction!!

I'm finally free from this damned course!!!


"jim p" <x y.com> wrote in message news:bm1a7i$1e2d$1 digitaldaemon.com...
 No problem, I found a c++ class to do the job.

 CCPUTicker v1.22
 Written by J. M.McGuiness
 source code can be downloaded from
 http://www.codeproject.com/datetime/ccputicker.asp?print=true if anyone is
 interested



 "Walter" <walter digitalmars.com> wrote in message
 news:bm0cve$2tkb$1 digitaldaemon.com...
 The Intel x86 RDTSC instruction is what you're looking for.

 "jim p" <x y.com> wrote in message
news:blvp83$22tm$1 digitaldaemon.com...
 I hope this isn't too much of a stupid question.

 I've just developed a silly little real time network routing
simulator.
 I need to determine the minimum processor speed that will perform all
the
 necessary calculations to keep it operating in real time.

 Forgetting memory access times, to do this I need to know how much CPU
work
 the application is doing.

 Is there any way of counting CPU clock cycles for example, for a
particular
 function call ???

 Jim
Oct 09 2003