GCC Code Coverage Report
Directory: cvmfs/ Exec Total Coverage
File: cvmfs/util/algorithm.cc Lines: 26 29 89.7 %
Date: 2019-02-03 02:48:13 Branches: 6 10 60.0 %

Line Branch Exec Source
1
/**
2
 * This file is part of the CernVM File System.
3
 *
4
 * Some common functions.
5
 */
6
7
#ifndef __STDC_FORMAT_MACROS
8
#define __STDC_FORMAT_MACROS
9
#endif
10
11
#include "cvmfs_config.h"
12
#include "util/algorithm.h"
13
14
using namespace std;  // NOLINT
15
16
#ifdef CVMFS_NAMESPACE_GUARD
17
namespace CVMFS_NAMESPACE_GUARD {
18
#endif
19
20
21
7
double DiffTimeSeconds(struct timeval start, struct timeval end) {
22
  // Time substraction, from GCC documentation
23
7
  if (end.tv_usec < start.tv_usec) {
24
1
    int nsec = (end.tv_usec - start.tv_usec) / 1000000 + 1;
25
1
    start.tv_usec -= 1000000 * nsec;
26
1
    start.tv_sec += nsec;
27
  }
28
7
  if (end.tv_usec - start.tv_usec > 1000000) {
29
    int nsec = (end.tv_usec - start.tv_usec) / 1000000;
30
    start.tv_usec += 1000000 * nsec;
31
    start.tv_sec -= nsec;
32
  }
33
34
  // Compute the time remaining to wait in microseconds.
35
  // tv_usec is certainly positive.
36
  uint64_t elapsed_usec = ((end.tv_sec - start.tv_sec)*1000000) +
37
7
  (end.tv_usec - start.tv_usec);
38
7
  return static_cast<double>(elapsed_usec)/1000000.0;
39
}
40
41
42
43
2
void StopWatch::Start() {
44
2
  assert(!running_);
45
46
2
  gettimeofday(&start_, NULL);
47
2
  running_ = true;
48
2
}
49
50
51
2
void StopWatch::Stop() {
52
2
  assert(running_);
53
54
2
  gettimeofday(&end_, NULL);
55
2
  running_ = false;
56
2
}
57
58
59
1
void StopWatch::Reset() {
60
1
  start_ = timeval();
61
1
  end_   = timeval();
62
1
  running_ = false;
63
1
}
64
65
66
4
double StopWatch::GetTime() const {
67
4
  assert(!running_);
68
69
4
  return DiffTimeSeconds(start_, end_);
70
}
71
72
73
#ifdef CVMFS_NAMESPACE_GUARD
74
}  // namespace CVMFS_NAMESPACE_GUARD
75
#endif