syslog-ng source
stopwatch.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 László Várady <laszlo.varady93@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * As an additional exemption you are allowed to compile & link against the
19  * OpenSSL libraries as published by the OpenSSL project. See the file
20  * COPYING for details.
21  *
22  */
23 
24 #ifndef STOPWATCH_H
25 #define STOPWATCH_H
26 
27 #include "syslog-ng.h"
28 #include "compat/time.h"
29 
30 typedef struct _Stopwatch
31 {
32  struct timespec start_time;
33 } Stopwatch;
34 
35 static inline void
36 stopwatch_start(Stopwatch *self)
37 {
38  clock_gettime(CLOCK_MONOTONIC, &self->start_time);
39 }
40 
41 static inline guint64
42 stopwatch_get_elapsed_nsec(Stopwatch *self)
43 {
44  struct timespec stop_time;
45  clock_gettime(CLOCK_MONOTONIC, &stop_time);
46 
47  return (stop_time.tv_sec - self->start_time.tv_sec) * 1000000000
48  + (stop_time.tv_nsec - self->start_time.tv_nsec);
49 }
50 
51 #endif
#define self
Definition: rcptid.c:38
Definition: stopwatch.h:31
#define CLOCK_MONOTONIC
Definition: time.h:44