SlideShare a Scribd company logo
1 of 22
Time Measurement
Topics
 Time scales
 Interval counting
 Cycle counters
 K-best measurement scheme
time.ppt
CS 105
Tour of Black Holes of Computing
– 2 – CS 105
Computer Time Scales
Two Fundamental Time Scales
 Processor: ~10–9 sec.
 External events: ~10–2 sec.
 Keyboard input
 Disk seek
 Screen refresh
Implication
 Can execute many
instructions while waiting
for external event to occur
 Can alternate among
processes without anyone
noticing
Time Scale (1 Ghz Machine)
1.E-09 1.E-06 1.E-03 1.E+00
Time (seconds)
1 ns 1 ms 1 ms 1 s
Integer Add
FP Multiply
FP Divide
Keystroke
Interrupt
Handler
Disk Access
Screen Refresh
Keystroke
Microscopic Macroscopic
– 3 – CS 105
Measurement Challenge
How Much Time Does Program X Require?
 CPU time
 How many total seconds are used when executing X?
 Measure used for most applications
 Small dependence on other system activities
 Actual (“Wall”) Time
 How many seconds elapse between the start and the
completion of X?
 Depends on system load, I/O times, etc.
Confounding Factors
 How does time get measured?
 Many processes share computing resources
 Transient effects when switching from one process to another
 Suddenly, the effects of alternating among processes become
noticeable
– 4 – CS 105
“Time” on a Computer System
real (wall clock) time
= user time (time executing instructions in the user process)
+ = real (wall clock) time
We will use the word “time” to refer to user time.
= system time (time executing instructions in kernel on behalf
of user process)
+
= some other user’s time (time executing instructions in
different user’s process)
cumulative user time
– 5 – CS 105
Interval Counting
OS Measures Runtimes Using Interval Timer
 Maintain 2 counts per process
 User time
 System time
 Each time get timer interrupt, increment counter for
executing process
 User time if running in user mode
 System time if running in kernel mode
– 6 – CS 105
Interval Counting Example
Au Au Au As Bu Bs Bu Bu Bu Bu As Au Au Au Au Au Bs Bu Bu Bs Au Au Au As As
A 110u + 40s
B 70u + 30s
(a) Interval Timings
B B
A
A A
(b) Actual Times
B
A
A
B
A 120.0u + 33.3s
B 73.3u + 23.3s
0 10 20 30 40 50 60 70 80 90 100110120130140150160
A
Au Au Au As Bu Bs Bu Bu Bu Bu As Au Au Au Au Au Bs Bu Bu Bs Au Au Au As As
A 110u + 40s
B 70u + 30s
(a) Interval Timings
B B
A
A A
Au Au Au As Bu Bs Bu Bu Bu Bu As Au Au Au Au Au Bs Bu Bu Bs Au Au Au As As
A 110u + 40s
B 70u + 30s
(a) Interval Timings
B B
A
A A
(b) Actual Times
B
A
A
B
A 120.0u + 33.3s
B 73.3u + 23.3s
0 10 20 30 40 50 60 70 80 90 100110120130140150160
A
(b) Actual Times
B
A
A
B
A 120.0u + 33.3s
B 73.3u + 23.3s
0 10 20 30 40 50 60 70 80 90 100110120130140150160
A
– 7 – CS 105
Unix time Command
 0.82 seconds user time
 82 timer intervals
 0.30 seconds system time
 30 timer intervals
 1.32 seconds wall time
 84.8% of total was used running these processes
 (.82+0.3)/1.32 = .848
time make osevent
gcc -O2 -Wall -g -march=i486 -c clock.c
gcc -O2 -Wall -g -march=i486 -c options.c
gcc -O2 -Wall -g -march=i486 -c load.c
gcc -O2 -Wall -g -march=i486 -o osevent osevent.c . . .
0.820u 0.300s 0:01.32 84.8% 0+0k 0+0io 4049pf+0w
– 8 – CS 105
Accuracy of Interval Counting
Worst-Case Analysis
 Timer Interval = 
 Single process segment measurement can be off by 
 No bound on error for multiple segments
 Could consistently underestimate, or consistently overestimate
0 10 20 30 40 50 60 70 80
A
A
Minimum
Maximum
0 10 20 30 40 50 60 70 80
A
A
Minimum
Maximum
• Computed time = 70ms
• Min Actual = 60 + 
• Max Actual = 80 – 
– 9 – CS 105
Accuracy of Int. Cntg. (cont.)
Average-Case Analysis
 Over/underestimates tend to balance out
 As long as total run time is sufficiently large
 Min run time ~1 second
 100 timer intervals
 Consistently miss 4% overhead due to timer interrupts
0 10 20 30 40 50 60 70 80
A
A
Minimum
Maximum
0 10 20 30 40 50 60 70 80
A
A
Minimum
Maximum
• Computed time = 70ms
• Min Actual = 60 + 
• Max Actual = 80 – 
– 10 – CS 105
Cycle Counters -
Another Timer
 Most modern systems have built-in registers that are
incremented every clock cycle
 Very fine grained
 Maintained as part of process state
» In Linux, counts elapsed global time
 Special assembly code instruction to access
 On (recent model) Intel machines:
 64 bit counter.
 RDTSC instruction sets %edx to high order 32-bits, %eax to low
order 32-bits
– 11 – CS 105
Cycle Counter
Period
Wrap Around Times for 550 MHz machine
 Low order 32 bits wrap around every 232 / (550 * 106) = 7.8
seconds
 High order 64 bits wrap around every 264 / (550 * 106) =
33539534679 seconds
 1065 years
For 2 GHz machine
 Low order 32 bits every 2.1 seconds
 High order 64 bits every 293 years
– 12 – CS 105
Measuring with Cycle
Counter
Idea
 Get current value of cycle counter
 store as pair of unsigned’s cyc_hi and cyc_lo
 Compute something
 Get new value of cycle counter
 Perform double precision subtraction to get elapsed cycles
/* Keep track of most recent reading of cycle counter */
static unsigned cyc_hi = 0;
static unsigned cyc_lo = 0;
void start_counter()
{
/* Get current value of cycle counter */
access_counter(&cyc_hi, &cyc_lo);
}
– 13 – CS 105
Accessing the Cycle Cntr.
 GCC allows inline assembly code with mechanism for
matching registers with program variables
 Code only works on x86 machine compiling with GCC
 Emit assembly with rdtsc and two movl instructions
void access_counter(unsigned *hi, unsigned *lo)
{
/* Get cycle counter */
asm("rdtsc; movl %%edx,%0; movl %%eax,%1"
: "=r" (*hi), "=r" (*lo)
: /* No input */
: "%edx", "%eax");
}
– 14 – CS 105
Completing Measurement
 Get new value of cycle counter
 Perform double precision subtraction to get elapsed cycles
 Express as double to avoid overflow problems
double get_counter()
{
unsigned ncyc_hi, ncyc_lo
unsigned hi, lo, borrow;
/* Get cycle counter */
access_counter(&ncyc_hi, &ncyc_lo);
/* Do double precision subtraction */
lo = ncyc_lo - cyc_lo;
borrow = lo > ncyc_lo;
hi = ncyc_hi - cyc_hi - borrow;
return (double) hi * (1 << 30) * 4 + lo;
}
– 15 – CS 105
Timing With Cycle
Counter
Determine Clock Rate of Processor
 Count number of cycles required for some fixed number of
seconds
Time Function P
 First attempt: Simply count cycles for one execution of P
double tsecs;
start_counter();
P();
tsecs = get_counter() / (MHZ * 1e6);
double MHZ;
int sleep_time = 10;
start_counter();
sleep(sleep_time);
MHZ = get_counter()/(sleep_time * 1e6);
– 16 – CS 105
Measurement Pitfalls
Overhead
 Calling get_counter() incurs small amount of overhead
 Want to measure long enough code sequence to
compensate
Unexpected Cache Effects
 artificial hits or misses
 e.g., these measurements were taken with the Alpha cycle
counter:
foo1(array1, array2, array3); /* 68,829 cycles */
foo2(array1, array2, array3); /* 23,337 cycles */
vs.
foo2(array1, array2, array3); /* 70,513 cycles */
foo1(array1, array2, array3); /* 23,203 cycles */
– 17 – CS 105
Dealing with Overhead & Cache
Effects
 Always execute function once to “warm up” cache
 Keep doubling number of times execute P() until reach some
threshold
 Used CMIN = 50000
int cnt = 1;
double cmeas = 0;
double cycles;
do {
int c = cnt;
P(); /* Warm up cache */
get_counter();
while (c-- > 0)
P();
cmeas = get_counter();
cycles = cmeas / cnt;
cnt += cnt;
} while (cmeas < CMIN); /* Make sure have enough */
return cycles / (1e6 * MHZ);
– 18 – CS 105
Multitasking Effects
Cycle Counter Measures Elapsed Time
 Keeps accumulating during periods of inactivity
 System activity
 Running other processes
Key Observation
 Cycle counter never underestimates program run time
 Possibly overestimates by large amount
K-Best Measurement Scheme
 Perform up to N (e.g., 20) measurements of function
 See if fastest K (e.g., 3) within some relative factor  (e.g., 0.001)
K
– 19 – CS 105
K-Best
Validation
Very good accuracy for < 8ms
 Within one timer interval
 Even when heavily loaded
Less accurate for > 10ms
 Light load: ~4% error
 Interval clock interrupt
handling
 Heavy load: Very high error
Intel Pentium III, Linux
0.001
0.01
0.1
1
10
100
0 10 20 30 40 50
Expected CPU Time (ms)
Measured:Expected
Error
Load 1
Load 2
Load 11
K = 3,  = 0.001
– 20 – CS 105
Time of Day Clock
 Unix gettimeofday() function
 Return elapsed time since reference time (Jan 1, 1970)
 Implementation
 Uses interval counting on some machines
» Coarse grained
 Uses cycle counter on others
» Fine grained, but significant overhead and only 1
microsecond resolution
#include <sys/time.h>
#include <unistd.h>
struct timeval tstart, tfinish;
double tsecs;
gettimeofday(&tstart, NULL);
P();
gettimeofday(&tfinish, NULL);
tsecs = (tfinish.tv_sec - tstart.tv_sec) +
1e6 * (tfinish.tv_usec - tstart.tv_usec);
– 21 – CS 105
K-Best Using gettimeofday
Linux
 As good as using cycle
counter
 For times > 10 microseconds
Windows
 Implemented by interval
counting
 Too coarse-grained
Using gettimeofday
-0.5
-0.4
-0.3
-0.2
-0.1
0
0.1
0.2
0.3
0.4
0.5
0 50 100 150 200 250 300
Expected CPU Time (ms)
Measured:Expected
Error
Win-NT
Linux
Linux-comp
– 22 – CS 105
Measurement Summary
Timing is highly case and system dependent
 What is overall duration being measured?
 > 1 second: interval counting is OK
 << 1 second: must use cycle counters
 On what hardware / OS / OS version?
 Accessing counters
» How gettimeofday is implemented
 Timer interrupt overhead
 Scheduling policy
Devising a Measurement Method
 Long durations: use Unix timing functions
 Short durations
 If possible, use gettimeofday
 Otherwise must work with cycle counters
 K-best scheme most successful

More Related Content

Similar to class12_time.ppt

OS scheduling and The anatomy of a context switch
OS scheduling and The anatomy of a context switchOS scheduling and The anatomy of a context switch
OS scheduling and The anatomy of a context switchDaniel Ben-Zvi
 
Where the wild things are - Benchmarking and Micro-Optimisations
Where the wild things are - Benchmarking and Micro-OptimisationsWhere the wild things are - Benchmarking and Micro-Optimisations
Where the wild things are - Benchmarking and Micro-OptimisationsMatt Warren
 
Priority Scheduling.pptx
Priority Scheduling.pptxPriority Scheduling.pptx
Priority Scheduling.pptxSarupyaDatta1
 
Exploring Parallel Merging In GPU Based Systems Using CUDA C.
Exploring Parallel Merging In GPU Based Systems Using CUDA C.Exploring Parallel Merging In GPU Based Systems Using CUDA C.
Exploring Parallel Merging In GPU Based Systems Using CUDA C.Rakib Hossain
 
Clock Synchronization (Distributed computing)
Clock Synchronization (Distributed computing)Clock Synchronization (Distributed computing)
Clock Synchronization (Distributed computing)Sri Prasanna
 
Computer Performance Microscopy with SHIM
Computer Performance Microscopy with SHIMComputer Performance Microscopy with SHIM
Computer Performance Microscopy with SHIMhiyangxi
 
PID Control of Runaway Processes - Greg McMillan Deminar
PID Control of Runaway Processes - Greg McMillan DeminarPID Control of Runaway Processes - Greg McMillan Deminar
PID Control of Runaway Processes - Greg McMillan DeminarJim Cahill
 
AVR_Course_Day7 timers counters and interrupt programming
AVR_Course_Day7 timers counters and  interrupt programmingAVR_Course_Day7 timers counters and  interrupt programming
AVR_Course_Day7 timers counters and interrupt programmingMohamed Ali
 
10 Discrete Time Controller Design.pptx
10 Discrete Time Controller Design.pptx10 Discrete Time Controller Design.pptx
10 Discrete Time Controller Design.pptxSaadAzhar15
 
Parallel R in snow (english after 2nd slide)
Parallel R in snow (english after 2nd slide)Parallel R in snow (english after 2nd slide)
Parallel R in snow (english after 2nd slide)Cdiscount
 
Best-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiBest-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiUnmesh Baile
 
ppt-U3 - (Programming, Memory Interfacing).pptx
ppt-U3 - (Programming, Memory Interfacing).pptxppt-U3 - (Programming, Memory Interfacing).pptx
ppt-U3 - (Programming, Memory Interfacing).pptxRaviKiranVarma4
 
On the way to low latency
On the way to low latencyOn the way to low latency
On the way to low latencyArtem Orobets
 
Deep Dive on Amazon EC2 Instances (March 2017)
Deep Dive on Amazon EC2 Instances (March 2017)Deep Dive on Amazon EC2 Instances (March 2017)
Deep Dive on Amazon EC2 Instances (March 2017)Julien SIMON
 

Similar to class12_time.ppt (20)

Chapter 10
Chapter 10Chapter 10
Chapter 10
 
OS scheduling and The anatomy of a context switch
OS scheduling and The anatomy of a context switchOS scheduling and The anatomy of a context switch
OS scheduling and The anatomy of a context switch
 
Timers
TimersTimers
Timers
 
Timer
TimerTimer
Timer
 
2_2_Event_Mechanism_Chapter_2.pdf
2_2_Event_Mechanism_Chapter_2.pdf2_2_Event_Mechanism_Chapter_2.pdf
2_2_Event_Mechanism_Chapter_2.pdf
 
Where the wild things are - Benchmarking and Micro-Optimisations
Where the wild things are - Benchmarking and Micro-OptimisationsWhere the wild things are - Benchmarking and Micro-Optimisations
Where the wild things are - Benchmarking and Micro-Optimisations
 
Priority Scheduling.pptx
Priority Scheduling.pptxPriority Scheduling.pptx
Priority Scheduling.pptx
 
Exploring Parallel Merging In GPU Based Systems Using CUDA C.
Exploring Parallel Merging In GPU Based Systems Using CUDA C.Exploring Parallel Merging In GPU Based Systems Using CUDA C.
Exploring Parallel Merging In GPU Based Systems Using CUDA C.
 
Clock Synchronization (Distributed computing)
Clock Synchronization (Distributed computing)Clock Synchronization (Distributed computing)
Clock Synchronization (Distributed computing)
 
chapter 4
chapter 4chapter 4
chapter 4
 
Computer Performance Microscopy with SHIM
Computer Performance Microscopy with SHIMComputer Performance Microscopy with SHIM
Computer Performance Microscopy with SHIM
 
PID Control of Runaway Processes - Greg McMillan Deminar
PID Control of Runaway Processes - Greg McMillan DeminarPID Control of Runaway Processes - Greg McMillan Deminar
PID Control of Runaway Processes - Greg McMillan Deminar
 
AVR_Course_Day7 timers counters and interrupt programming
AVR_Course_Day7 timers counters and  interrupt programmingAVR_Course_Day7 timers counters and  interrupt programming
AVR_Course_Day7 timers counters and interrupt programming
 
10 Discrete Time Controller Design.pptx
10 Discrete Time Controller Design.pptx10 Discrete Time Controller Design.pptx
10 Discrete Time Controller Design.pptx
 
Parallel R in snow (english after 2nd slide)
Parallel R in snow (english after 2nd slide)Parallel R in snow (english after 2nd slide)
Parallel R in snow (english after 2nd slide)
 
Best-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiBest-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbai
 
ppt-U3 - (Programming, Memory Interfacing).pptx
ppt-U3 - (Programming, Memory Interfacing).pptxppt-U3 - (Programming, Memory Interfacing).pptx
ppt-U3 - (Programming, Memory Interfacing).pptx
 
On the way to low latency
On the way to low latencyOn the way to low latency
On the way to low latency
 
Static_Time_Analysis.pptx
Static_Time_Analysis.pptxStatic_Time_Analysis.pptx
Static_Time_Analysis.pptx
 
Deep Dive on Amazon EC2 Instances (March 2017)
Deep Dive on Amazon EC2 Instances (March 2017)Deep Dive on Amazon EC2 Instances (March 2017)
Deep Dive on Amazon EC2 Instances (March 2017)
 

Recently uploaded

Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 

Recently uploaded (20)

Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 

class12_time.ppt

  • 1. Time Measurement Topics  Time scales  Interval counting  Cycle counters  K-best measurement scheme time.ppt CS 105 Tour of Black Holes of Computing
  • 2. – 2 – CS 105 Computer Time Scales Two Fundamental Time Scales  Processor: ~10–9 sec.  External events: ~10–2 sec.  Keyboard input  Disk seek  Screen refresh Implication  Can execute many instructions while waiting for external event to occur  Can alternate among processes without anyone noticing Time Scale (1 Ghz Machine) 1.E-09 1.E-06 1.E-03 1.E+00 Time (seconds) 1 ns 1 ms 1 ms 1 s Integer Add FP Multiply FP Divide Keystroke Interrupt Handler Disk Access Screen Refresh Keystroke Microscopic Macroscopic
  • 3. – 3 – CS 105 Measurement Challenge How Much Time Does Program X Require?  CPU time  How many total seconds are used when executing X?  Measure used for most applications  Small dependence on other system activities  Actual (“Wall”) Time  How many seconds elapse between the start and the completion of X?  Depends on system load, I/O times, etc. Confounding Factors  How does time get measured?  Many processes share computing resources  Transient effects when switching from one process to another  Suddenly, the effects of alternating among processes become noticeable
  • 4. – 4 – CS 105 “Time” on a Computer System real (wall clock) time = user time (time executing instructions in the user process) + = real (wall clock) time We will use the word “time” to refer to user time. = system time (time executing instructions in kernel on behalf of user process) + = some other user’s time (time executing instructions in different user’s process) cumulative user time
  • 5. – 5 – CS 105 Interval Counting OS Measures Runtimes Using Interval Timer  Maintain 2 counts per process  User time  System time  Each time get timer interrupt, increment counter for executing process  User time if running in user mode  System time if running in kernel mode
  • 6. – 6 – CS 105 Interval Counting Example Au Au Au As Bu Bs Bu Bu Bu Bu As Au Au Au Au Au Bs Bu Bu Bs Au Au Au As As A 110u + 40s B 70u + 30s (a) Interval Timings B B A A A (b) Actual Times B A A B A 120.0u + 33.3s B 73.3u + 23.3s 0 10 20 30 40 50 60 70 80 90 100110120130140150160 A Au Au Au As Bu Bs Bu Bu Bu Bu As Au Au Au Au Au Bs Bu Bu Bs Au Au Au As As A 110u + 40s B 70u + 30s (a) Interval Timings B B A A A Au Au Au As Bu Bs Bu Bu Bu Bu As Au Au Au Au Au Bs Bu Bu Bs Au Au Au As As A 110u + 40s B 70u + 30s (a) Interval Timings B B A A A (b) Actual Times B A A B A 120.0u + 33.3s B 73.3u + 23.3s 0 10 20 30 40 50 60 70 80 90 100110120130140150160 A (b) Actual Times B A A B A 120.0u + 33.3s B 73.3u + 23.3s 0 10 20 30 40 50 60 70 80 90 100110120130140150160 A
  • 7. – 7 – CS 105 Unix time Command  0.82 seconds user time  82 timer intervals  0.30 seconds system time  30 timer intervals  1.32 seconds wall time  84.8% of total was used running these processes  (.82+0.3)/1.32 = .848 time make osevent gcc -O2 -Wall -g -march=i486 -c clock.c gcc -O2 -Wall -g -march=i486 -c options.c gcc -O2 -Wall -g -march=i486 -c load.c gcc -O2 -Wall -g -march=i486 -o osevent osevent.c . . . 0.820u 0.300s 0:01.32 84.8% 0+0k 0+0io 4049pf+0w
  • 8. – 8 – CS 105 Accuracy of Interval Counting Worst-Case Analysis  Timer Interval =   Single process segment measurement can be off by   No bound on error for multiple segments  Could consistently underestimate, or consistently overestimate 0 10 20 30 40 50 60 70 80 A A Minimum Maximum 0 10 20 30 40 50 60 70 80 A A Minimum Maximum • Computed time = 70ms • Min Actual = 60 +  • Max Actual = 80 – 
  • 9. – 9 – CS 105 Accuracy of Int. Cntg. (cont.) Average-Case Analysis  Over/underestimates tend to balance out  As long as total run time is sufficiently large  Min run time ~1 second  100 timer intervals  Consistently miss 4% overhead due to timer interrupts 0 10 20 30 40 50 60 70 80 A A Minimum Maximum 0 10 20 30 40 50 60 70 80 A A Minimum Maximum • Computed time = 70ms • Min Actual = 60 +  • Max Actual = 80 – 
  • 10. – 10 – CS 105 Cycle Counters - Another Timer  Most modern systems have built-in registers that are incremented every clock cycle  Very fine grained  Maintained as part of process state » In Linux, counts elapsed global time  Special assembly code instruction to access  On (recent model) Intel machines:  64 bit counter.  RDTSC instruction sets %edx to high order 32-bits, %eax to low order 32-bits
  • 11. – 11 – CS 105 Cycle Counter Period Wrap Around Times for 550 MHz machine  Low order 32 bits wrap around every 232 / (550 * 106) = 7.8 seconds  High order 64 bits wrap around every 264 / (550 * 106) = 33539534679 seconds  1065 years For 2 GHz machine  Low order 32 bits every 2.1 seconds  High order 64 bits every 293 years
  • 12. – 12 – CS 105 Measuring with Cycle Counter Idea  Get current value of cycle counter  store as pair of unsigned’s cyc_hi and cyc_lo  Compute something  Get new value of cycle counter  Perform double precision subtraction to get elapsed cycles /* Keep track of most recent reading of cycle counter */ static unsigned cyc_hi = 0; static unsigned cyc_lo = 0; void start_counter() { /* Get current value of cycle counter */ access_counter(&cyc_hi, &cyc_lo); }
  • 13. – 13 – CS 105 Accessing the Cycle Cntr.  GCC allows inline assembly code with mechanism for matching registers with program variables  Code only works on x86 machine compiling with GCC  Emit assembly with rdtsc and two movl instructions void access_counter(unsigned *hi, unsigned *lo) { /* Get cycle counter */ asm("rdtsc; movl %%edx,%0; movl %%eax,%1" : "=r" (*hi), "=r" (*lo) : /* No input */ : "%edx", "%eax"); }
  • 14. – 14 – CS 105 Completing Measurement  Get new value of cycle counter  Perform double precision subtraction to get elapsed cycles  Express as double to avoid overflow problems double get_counter() { unsigned ncyc_hi, ncyc_lo unsigned hi, lo, borrow; /* Get cycle counter */ access_counter(&ncyc_hi, &ncyc_lo); /* Do double precision subtraction */ lo = ncyc_lo - cyc_lo; borrow = lo > ncyc_lo; hi = ncyc_hi - cyc_hi - borrow; return (double) hi * (1 << 30) * 4 + lo; }
  • 15. – 15 – CS 105 Timing With Cycle Counter Determine Clock Rate of Processor  Count number of cycles required for some fixed number of seconds Time Function P  First attempt: Simply count cycles for one execution of P double tsecs; start_counter(); P(); tsecs = get_counter() / (MHZ * 1e6); double MHZ; int sleep_time = 10; start_counter(); sleep(sleep_time); MHZ = get_counter()/(sleep_time * 1e6);
  • 16. – 16 – CS 105 Measurement Pitfalls Overhead  Calling get_counter() incurs small amount of overhead  Want to measure long enough code sequence to compensate Unexpected Cache Effects  artificial hits or misses  e.g., these measurements were taken with the Alpha cycle counter: foo1(array1, array2, array3); /* 68,829 cycles */ foo2(array1, array2, array3); /* 23,337 cycles */ vs. foo2(array1, array2, array3); /* 70,513 cycles */ foo1(array1, array2, array3); /* 23,203 cycles */
  • 17. – 17 – CS 105 Dealing with Overhead & Cache Effects  Always execute function once to “warm up” cache  Keep doubling number of times execute P() until reach some threshold  Used CMIN = 50000 int cnt = 1; double cmeas = 0; double cycles; do { int c = cnt; P(); /* Warm up cache */ get_counter(); while (c-- > 0) P(); cmeas = get_counter(); cycles = cmeas / cnt; cnt += cnt; } while (cmeas < CMIN); /* Make sure have enough */ return cycles / (1e6 * MHZ);
  • 18. – 18 – CS 105 Multitasking Effects Cycle Counter Measures Elapsed Time  Keeps accumulating during periods of inactivity  System activity  Running other processes Key Observation  Cycle counter never underestimates program run time  Possibly overestimates by large amount K-Best Measurement Scheme  Perform up to N (e.g., 20) measurements of function  See if fastest K (e.g., 3) within some relative factor  (e.g., 0.001) K
  • 19. – 19 – CS 105 K-Best Validation Very good accuracy for < 8ms  Within one timer interval  Even when heavily loaded Less accurate for > 10ms  Light load: ~4% error  Interval clock interrupt handling  Heavy load: Very high error Intel Pentium III, Linux 0.001 0.01 0.1 1 10 100 0 10 20 30 40 50 Expected CPU Time (ms) Measured:Expected Error Load 1 Load 2 Load 11 K = 3,  = 0.001
  • 20. – 20 – CS 105 Time of Day Clock  Unix gettimeofday() function  Return elapsed time since reference time (Jan 1, 1970)  Implementation  Uses interval counting on some machines » Coarse grained  Uses cycle counter on others » Fine grained, but significant overhead and only 1 microsecond resolution #include <sys/time.h> #include <unistd.h> struct timeval tstart, tfinish; double tsecs; gettimeofday(&tstart, NULL); P(); gettimeofday(&tfinish, NULL); tsecs = (tfinish.tv_sec - tstart.tv_sec) + 1e6 * (tfinish.tv_usec - tstart.tv_usec);
  • 21. – 21 – CS 105 K-Best Using gettimeofday Linux  As good as using cycle counter  For times > 10 microseconds Windows  Implemented by interval counting  Too coarse-grained Using gettimeofday -0.5 -0.4 -0.3 -0.2 -0.1 0 0.1 0.2 0.3 0.4 0.5 0 50 100 150 200 250 300 Expected CPU Time (ms) Measured:Expected Error Win-NT Linux Linux-comp
  • 22. – 22 – CS 105 Measurement Summary Timing is highly case and system dependent  What is overall duration being measured?  > 1 second: interval counting is OK  << 1 second: must use cycle counters  On what hardware / OS / OS version?  Accessing counters » How gettimeofday is implemented  Timer interrupt overhead  Scheduling policy Devising a Measurement Method  Long durations: use Unix timing functions  Short durations  If possible, use gettimeofday  Otherwise must work with cycle counters  K-best scheme most successful