0% found this document useful (0 votes)
9 views22 pages

17 Clocks

Uploaded by

manjunathsg85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views22 pages

17 Clocks

Uploaded by

manjunathsg85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Time and Clocks

CS 241 Lecture 17
R: Ch 9, pp 300-307+man pages
for gprof

Roy Campbell

08/13/24 CS241 © 2005 Roy Campbell, All R 1


ights Reserved
Contents
 Types of Clocks
 GPROF
 WALLCLOCK
 Measuring a Function’s Performance

08/13/24 CS241 © 2005 Roy Campbell, All R 2


ights Reserved
Timers and Clocks
 Wall time
 Elapsed time
 Real time
 System Clock

08/13/24 CS241 © 2005 Roy Campbell, All R 3


ights Reserved
Measuring Performance
 gprof
 Time
 PAPI

08/13/24 CS241 © 2005 Roy Campbell, All R 4


ights Reserved
gprof
To 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 -pg

08/13/24 CS241 © 2005 Roy Campbell, All R 5


ights Reserved
gprof 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 exit

08/13/24 CS241 © 2005 Roy Campbell, All R 6


ights Reserved
gprof analysis
 gprof options [executable-file [profile-
data-files...]] [> outfile]
 Options include time spent in
function, call graph, …

08/13/24 CS241 © 2005 Roy Campbell, All R 7


ights Reserved
Flat 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 main

08/13/24 CS241 © 2005 Roy Campbell, All R 8


ights Reserved
Call Graph
index % time self children called name <spontaneous>
[1] 100.0 0.00 0.05 start [1]
0.00 0.05 1/1 main [2]

08/13/24 CS241 © 2005 Roy Campbell, All R 9


ights Reserved
gprof
 Profiling 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'.

08/13/24 CS241 © 2005 Roy Campbell, All R 10


ights Reserved
gprof
 Number-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.

08/13/24 CS241 © 2005 Roy Campbell, All R 11


ights Reserved
Time.h
(page R:Ch9 pp302-320)
#include <time.h>
time_t time(time_t *calptr);

Epoch: 00:00 (midnight), Jan 1, 1970 GMT


Day is 86,400 seconds
time_t is usually a long
If the long is 32 bits, time overflows in 2038

Extensions
POSIX:XSI microseconds
POSIX:TMR nanoseconds

08/13/24 CS241 © 2005 Roy Campbell, All R 12


ights Reserved
Timing 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);
}

08/13/24 CS241 © 2005 Roy Campbell, All R 13


ights Reserved
Time (P156)
struct tm *localtime(const time_t *timer);
Takes time since epoch, returns date
struct tm *gmtime(const time_t *timer);
Takes time since epoch, returns UTC
char *ctime(const time_t *clock);
26 byte date string in ascii
char *asctime(const struct tm *timeptr);
26 byte date string in ascii

08/13/24 CS241 © 2005 Roy Campbell, All R 14


ights Reserved
Time struct tm
int 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;

08/13/24 CS241 © 2005 Roy Campbell, All R 15


ights Reserved
POSIX XSI
struct timeval
time_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, historical

08/13/24 CS241 © 2005 Roy Campbell, All R 16


ights Reserved
P307 Measure running time using
gettimeofday
#include <stdio.h>
#include <sys/time.h>
#define MILLION 1000000L

void 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;
}

08/13/24 CS241 © 2005 Roy Campbell, All R 17


ights Reserved
Measure running time using
gettimeofday
function_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;
}

08/13/24 CS241 © 2005 Roy Campbell, All R 18


ights Reserved
Gettimeofday limitations
 Resolution small number of microsecs
 Many consecutive calls of get.. Will
return same value

08/13/24 CS241 © 2005 Roy Campbell, All R 19


ights Reserved
P308 A program to test the
resolution of gettimeofday
#include <stdio.h>
#include <sys/time.h>
#define MILLION 1000000L
#define NUMDIF 20

int main(void) {
int i;
int numcalls = 1;
int numdone = 0;
long sum = 0;
long timedif[NUMDIF];
struct timeval tlast;
struct timeval tthis;
if (gettimeofday(&tlast, NULL)) {
fprintf(stderr, “Failed to get gettimeofday\n”);
return 1;
}
08/13/24 CS241 © 2005 Roy Campbell, All R 20
ights Reserved
A program to test the resolution of
gettimeofday
while (numdone < NUMDIF) {
numcalls++;
if (gettimeofday(&tthis, NULL)) {
fprintf(stderr, “Failed to get a later gettimeofday.\n”);
return 1;
}
timedif[numdone] = MILLION*(tthis.tv_sec – tlast.tv_sec) +
tthis.tv_usec –tlast.tv_usec;
if (timedif[numdone] != 0 {
numdone++;
tlast=this;
}
}

………………….

08/13/24 CS241 © 2005 Roy Campbell, All R 21


ights Reserved
Summary
 Gprof
 Types of Clocks
 Wall Clock

08/13/24 CS241 © 2005 Roy Campbell, All R 22


ights Reserved

You might also like