Statistics
| Branch: | Tag: | Revision:

root / xseg / peers / user / timer.c @ 48a66f3b

History | View | Annotate | Download (4.1 kB)

1
/*
2
 * Copyright 2012 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *   2. Redistributions in binary form must reproduce the above
12
 *      copyright notice, this list of conditions and the following
13
 *      disclaimer in the documentation and/or other materials
14
 *      provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
 * POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * The views and conclusions contained in the software and
30
 * documentation are those of the authors and should not be
31
 * interpreted as representing official policies, either expressed
32
 * or implied, of GRNET S.A.
33
 */
34

    
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <unistd.h>
38
#include <sys/syscall.h>
39
#include <sys/types.h>
40
#include <pthread.h>
41
#include <xseg/xseg.h>
42
#include <peer.h>
43
#include <time.h>
44
#include <sys/util.h>
45
#include <signal.h>
46
#include <bench-xseg.h>
47
#include <limits.h>
48

    
49
#define SEC 1000000000 //1sec = 10^9 nsec
50
#define SEC2 (uint64_t) SEC*SEC //1sec*1sec = 10^18 nsec^2
51

    
52
void timer_start(struct timer *timer)
53
{
54
        //We need a low-latency way to get current time in nanoseconds.
55
        //Is this way the best way?
56
        //RAW means that we trust the system's oscilator isn't screwed up
57
        clock_gettime(CLOCK_MONOTONIC_RAW, &timer->start_time);
58
}
59

    
60
void timer_stop(struct timer *timer)
61
{
62
        struct timespec end_time;
63
        struct timespec start_time = timer->start_time;
64
        volatile struct timespec elapsed_time;
65
        struct timespec2 elapsed_time_sq;
66

    
67
        clock_gettime(CLOCK_MONOTONIC_RAW, &end_time);
68

    
69
        //Get elapsed time by subtracting start time from end time.
70
        //Subtraction can result to a negative value, so we check for both cases
71
        if (start_time.tv_nsec > end_time.tv_nsec) {
72
                elapsed_time.tv_nsec = SEC - start_time.tv_nsec + end_time.tv_nsec;
73
                elapsed_time.tv_sec = end_time.tv_sec - start_time.tv_sec - 1;
74
        } else {
75
                elapsed_time.tv_nsec = end_time.tv_nsec - start_time.tv_nsec;
76
                elapsed_time.tv_sec = end_time.tv_sec - start_time.tv_sec;
77
        }
78

    
79
        //Add the elapsed time to the current sum for this timer.
80
        //For accuracy, nanoseconds' sum has to be always less that 10^9
81
        if (elapsed_time.tv_nsec + timer->sum.tv_nsec > SEC){
82
                timer->sum.tv_nsec += elapsed_time.tv_nsec;
83
                timer->sum.tv_sec += elapsed_time.tv_sec + 1;
84
        } else {
85
                timer->sum.tv_nsec += elapsed_time.tv_nsec;
86
                timer->sum.tv_sec += elapsed_time.tv_sec;
87
        }
88

    
89
        //Add elapsed_time^2 to the current sum of squares for this timer
90
        //This is needed to calculate standard deviation.
91
        //As above, the sum of square of nanoseconds has to be less than 10^18
92
        elapsed_time_sq.tv_sec2 = elapsed_time.tv_sec*elapsed_time.tv_sec;
93
        elapsed_time_sq.tv_nsec2 = elapsed_time.tv_nsec*elapsed_time.tv_nsec;
94
        if (elapsed_time_sq.tv_nsec2 + timer->sum_sq.tv_nsec2 > SEC2) {
95
                timer->sum_sq.tv_nsec2 =
96
                        (timer->sum_sq.tv_nsec2 + elapsed_time_sq.tv_nsec2) % SEC2;
97
                timer->sum_sq.tv_sec2 += elapsed_time_sq.tv_sec2 + 1;
98
        } else {
99
                timer->sum_sq.tv_nsec2 += elapsed_time_sq.tv_nsec2;
100
                timer->sum_sq.tv_sec2 += elapsed_time_sq.tv_sec2;
101
        }
102

    
103
        //TODO: check if we need to make it volatile
104
        timer->completed++;
105

    
106
        printf("Start: %lu s %lu ns\n", start_time.tv_sec, start_time.tv_nsec);
107
        printf("Elpsd: %lu s %lu ns\n", elapsed_time.tv_sec, elapsed_time.tv_nsec);
108
        printf("End:   %lu s %lu ns\n", end_time.tv_sec, end_time.tv_nsec);
109
}
110

    
111

    
112

    
113