Time and ClocksContentsTimers and ClocksMeasuring Performancegprofgprof datagprof analysisFlat profileCall GraphSlide 10Slide 11Time.h (page R:Ch9 pp302-320)Timing a functionTime (P156)Time struct tmPOSIX XSIP307 Measure running time using gettimeofdayMeasure running time using gettimeofdayGettimeofday limitationsP308 A program to test the resolution of gettimeofdayA program to test the resolution of gettimeofdaySummary01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved1Time and ClocksCS 241 Lecture 17R: Ch 9, pp 300-307+man pages for gprofRoy Campbell01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved2ContentsTypes of ClocksGPROFWALLCLOCKMeasuring a Function’s Performance01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved3Timers and ClocksWall timeElapsed timeReal timeSystem Clock01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved4Measuring PerformancegprofTime PAPI01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved5gprofTo compile a source file for profiling, specify the `-pg' option when you run the compiler. (This is in addition to the options you normally use.) To link the program for profiling, if you use a compiler such as cc to do the linking, simply specify `-pg' in addition to your usual options. The same option, `-pg', alters either compilation or linking to do what is necessary for profiling. Here are examples: cc -g -c myprog.c utils.c -pg cc -o myprog myprog.o utils.o -pg The `-pg' option also works with a command that both compiles and links: cc -o myprog myprog.c utils.c -g -pg01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved6gprof data`gmon.out' file is written in the program's current working directory at the time it exits your program must exit normally: by returning from main or by calling exit01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved7gprof analysisgprof options [executable-file [profile-data-files...]] [> outfile] Options include time spent in function, call graph, …01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved8Flat profile Flat profile: Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls ms/call ms/call name 33.34 0.02 0.02 7208 0 0 open 16.67 0.03 0.01 244 0.04 0.12 offtime 16.67 0.04 0.01 8 1.25 1.25 memccpy 16.67 0.05 0.01 7 1.43 1.43 write 16.67 0.06 0.01 mcount 0.00 0.06 0.00 1 0.00 50.00 main01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved9Call Graphindex % time self children called name <spontaneous> [1] 100.0 0.00 0.05 start [1] 0.00 0.05 1/1 main [2]01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved10gprofProfiling also involves watching your program as it runs, and keeping a histogram of where the program counter happens to be every now and then. Typically the program counter is looked at around 100 times per second of run time, but the exact frequency may vary from system to system. A special startup routine allocates memory for the histogram and sets up a clock signal handler to make entries in it. Use of this special startup routine is one of the effects of using `gcc ... -pg' to link. The startup file also includes an `exit' function which is responsible for writing the file `gmon.out'.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved11gprofNumber-of-calls information for library routines is collected by using a special version of the C library. The programs in it are the same as in the usual C library, but they were compiled with `-pg'. If you link your program with `gcc ... -pg', it automatically uses the profiling version of the library. The output from gprof gives no indication of parts of your program that are limited by I/O or swapping bandwidth.01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved12Time.h (page R:Ch9 pp302-320)#include <time.h>time_t time(time_t *calptr);Epoch: 00:00 (midnight), Jan 1, 1970 GMTDay is 86,400 secondstime_t is usually a longIf the long is 32 bits, time overflows in 2038ExtensionsPOSIX:XSI microsecondsPOSIX:TMR nanoseconds01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved13Timing a function#include <stdio.h>#include <time.h>void function_to_time(void);int main(void) { time_t tstart; tstart = time(NULL); function_to_time(); printf(“function_to_time took %f seconds of elapsed time\n”, difftime(time(NULL), tstart)); return(0);}01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved14Time (P156)struct tm *localtime(const time_t *timer);Takes time since epoch, returns datestruct tm *gmtime(const time_t *timer);Takes time since epoch, returns UTCchar *ctime(const time_t *clock); 26 byte date string in asciichar *asctime(const struct tm *timeptr); 26 byte date string in ascii01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved15Time struct tmint tm_sec;int tm_min;int tm_hour;int tm_mday;int tm_mon;int tm_year;int tm_wday;int tm_yday;int tm_isdst;01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved16POSIX XSIstruct timevaltime_t tv_sec; /* seconds since the Epoch*/time_t tv_usec /* and microsoeconds*/#include <sys/time.h>int gettimeofday(struct timeval *restrict tp, void *restrict tzp);tzp is null, historical01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved17P307 Measure running time using gettimeofday#include <stdio.h>#include <sys/time.h>#define MILLION 1000000Lvoid function_to_time(void);int main(void) {long timedif;struct timeval tpend;struct timeval tpstart;if (gettimeofday(&tpstart, NULL)) { fprintf(stderr, “Failed to get start time\n”); return 1; }01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved18Measure running time using gettimeofdayfunction_to_time(); /* timed code goes here */if (gettimeofday(&tppend, NULL)) { fprintf(stderr, “Failed to get end time\n”); return 1; }timedif = MILLION*(tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec – tpstart.tv_usec;printf(“The function_to_time took %ld microseconds\n”, timedif);return 0;}01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved19Gettimeofday limitationsResolution small number of microsecsMany consecutive calls of get.. Will return same value01/14/19 CS241 © 2005 Roy Campbell, All Rights Reserved20P308 A program to test the resolution of gettimeofday#include <stdio.h>#include <sys/time.h>#define MILLION
View Full Document