~ubuntu-branches/ubuntu/trusty/log4shib/trusty

« back to all changes in this revision

Viewing changes to tests/Clock.cpp

  • Committer: Package Import Robot
  • Author(s): Russ Allbery
  • Date: 2012-06-05 21:20:25 UTC
  • Revision ID: package-import@ubuntu.com-20120605212025-uyigtav7dqwvnf41
Tags: upstream-1.0.4
ImportĀ upstreamĀ versionĀ 1.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
static const char rcsid[] = "$Id: Clock.cpp,v 1.9 2002/09/15 22:40:50 bastiaan Exp $";
 
2
 
 
3
/* 
 
4
 * See the COPYING file for the terms of usage and distribution.
 
5
 */
 
6
 
 
7
#include <cstdlib>
 
8
#include <sys/time.h>                   // for struct timeval
 
9
#if defined(__linux__) && defined(__i386__)
 
10
#    define rdtscl(low) \
 
11
     __asm__ __volatile__("rdtsc" : "=a" (low) : : "edx")
 
12
#endif
 
13
#include <iostream>
 
14
 
 
15
#include "Clock.hh"
 
16
 
 
17
namespace 
 
18
{
 
19
    const usec_t UsecPerSec = INT64_CONSTANT(1000000);
 
20
}
 
21
 
 
22
bool Clock::UsingCPU  = std::getenv("CLOCK_USE_CPU") ? true : false;
 
23
 
 
24
// -----------------------------------------------------------------------------
 
25
usec_t Clock::time(void)
 
26
{
 
27
    if (UsingCPU) {
 
28
        static bool warn = true;
 
29
                
 
30
        if (warn) {
 
31
            std::cout << "Using CPU clock." << std::endl;
 
32
            warn = false;
 
33
        }
 
34
 
 
35
#if defined(__linux__) && defined(__i386__)
 
36
        {
 
37
            unsigned long tsc;
 
38
                        
 
39
            rdtscl(tsc);
 
40
            return (usec_t) tsc;
 
41
        }
 
42
#else
 
43
        {
 
44
            std::cerr << "CPU clock not implemented for this architecture" << std::endl;
 
45
            UsingCPU = false;
 
46
            return Clock::time();
 
47
        }
 
48
#endif  
 
49
    } else {
 
50
        struct timeval tv;
 
51
        
 
52
        gettimeofday(&tv, NULL);        
 
53
        return (usec_t) (tv.tv_sec * UsecPerSec + tv.tv_usec);
 
54
    }
 
55
}
 
56
 
 
57
// -----------------------------------------------------------------------------
 
58
Clock::Clock(void)
 
59
    : _start(0),
 
60
      _elapsed(0),
 
61
      _active(false) 
 
62
{
 
63
    start();
 
64
}
 
65
 
 
66
// -----------------------------------------------------------------------------
 
67
Clock::~Clock(void)
 
68
{
 
69
    ;
 
70
}
 
71
 
 
72
// -----------------------------------------------------------------------------
 
73
usec_t Clock::elapsed(void) const
 
74
{
 
75
    if (!active())
 
76
        return _elapsed;
 
77
 
 
78
    return time() - _start;
 
79
}
 
80
 
 
81
 
 
82
// -----------------------------------------------------------------------------
 
83
usec_t Clock::start(void)
 
84
{
 
85
    _active = true;
 
86
 
 
87
    return _start = time();
 
88
}
 
89
 
 
90
// -----------------------------------------------------------------------------
 
91
usec_t Clock::stop(void)
 
92
{
 
93
    _elapsed = elapsed();
 
94
    _active  = false;
 
95
 
 
96
    return _elapsed;
 
97
}
 
98