Coverage Report

Created: 2023-09-25 07:15

/src/yara/libyara/stopwatch.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
Copyright (c) 2017. The YARA Authors. All Rights Reserved.
3
4
Redistribution and use in source and binary forms, with or without modification,
5
are permitted provided that the following conditions are met:
6
7
1. Redistributions of source code must retain the above copyright notice, this
8
list of conditions and the following disclaimer.
9
10
2. Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions and the following disclaimer in the documentation and/or
12
other materials provided with the distribution.
13
14
3. Neither the name of the copyright holder nor the names of its contributors
15
may be used to endorse or promote products derived from this software without
16
specific prior written permission.
17
18
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
#include <yara/stopwatch.h>
31
32
#if defined(_WIN32)
33
34
void yr_stopwatch_start(YR_STOPWATCH* sw)
35
{
36
  QueryPerformanceFrequency(&sw->frequency);
37
  QueryPerformanceCounter(&sw->start);
38
}
39
40
41
uint64_t yr_stopwatch_elapsed_ns(YR_STOPWATCH* sw)
42
{
43
  LARGE_INTEGER li;
44
45
  QueryPerformanceCounter(&li);
46
47
  return (li.QuadPart - sw->start.QuadPart) * 1000000000ULL /
48
         sw->frequency.QuadPart;
49
}
50
51
52
#elif defined(__APPLE__) && defined(__MACH__)
53
54
void yr_stopwatch_start(YR_STOPWATCH* sw)
55
{
56
  mach_timebase_info(&sw->timebase);
57
  sw->start = mach_absolute_time();
58
}
59
60
61
uint64_t yr_stopwatch_elapsed_ns(YR_STOPWATCH* sw)
62
{
63
  uint64_t now = mach_absolute_time();
64
  return ((now - sw->start) * sw->timebase.numer) / sw->timebase.denom;
65
}
66
67
68
#elif defined(HAVE_CLOCK_GETTIME)
69
70
#define timespecsub(tsp, usp, vsp)                    \
71
0
  do                                                  \
72
0
  {                                                   \
73
0
    (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;    \
74
0
    (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
75
0
    if ((vsp)->tv_nsec < 0)                           \
76
0
    {                                                 \
77
0
      (vsp)->tv_sec--;                                \
78
0
      (vsp)->tv_nsec += 1000000000L;                  \
79
0
    }                                                 \
80
0
  } while (0)
81
82
83
void yr_stopwatch_start(YR_STOPWATCH* stopwatch)
84
110
{
85
110
  clock_gettime(CLOCK_MONOTONIC, &stopwatch->ts_start);
86
110
}
87
88
89
uint64_t yr_stopwatch_elapsed_ns(YR_STOPWATCH* stopwatch)
90
0
{
91
0
  struct timespec ts_stop;
92
0
  struct timespec ts_elapsed;
93
94
0
  clock_gettime(CLOCK_MONOTONIC, &ts_stop);
95
0
  timespecsub(&ts_stop, &stopwatch->ts_start, &ts_elapsed);
96
0
  return ts_elapsed.tv_sec * 1000000000ULL + ts_elapsed.tv_nsec;
97
0
}
98
99
100
#else
101
102
#include <sys/time.h>
103
104
#define timevalsub(tvp, uvp, vvp)                     \
105
  do                                                  \
106
  {                                                   \
107
    (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;    \
108
    (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
109
    if ((vvp)->tv_usec < 0)                           \
110
    {                                                 \
111
      (vvp)->tv_sec--;                                \
112
      (vvp)->tv_usec += 1000000L;                     \
113
    }                                                 \
114
  } while (0)
115
116
117
void yr_stopwatch_start(YR_STOPWATCH* stopwatch)
118
{
119
  gettimeofday(&stopwatch->tv_start, NULL);
120
}
121
122
123
uint64_t yr_stopwatch_elapsed_ns(YR_STOPWATCH* stopwatch)
124
{
125
  struct timeval tv_stop;
126
  struct timeval tv_elapsed;
127
128
  gettimeofday(&tv_stop, NULL);
129
  timevalsub(&tv_stop, &stopwatch->tv_start, &tv_elapsed);
130
  return tv_elapsed.tv_sec * 1000000000ULL + tv_elapsed.tv_usec * 1000ULL;
131
}
132
133
#endif