dpkg 1.21.11
perf.h
Go to the documentation of this file.
1/*
2 * libdpkg - Debian packaging suite library routines
3 * perf.h - performance testing support
4 *
5 * Copyright © 2009-2019 Guillem Jover <guillem@debian.org>
6 *
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
11 *
12 * This is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef LIBDPKG_PERF_H
22#define LIBDPKG_PERF_H
23
24#include <config.h>
25#include <compat.h>
26
27#include <time.h>
28#include <stdio.h>
29
30#define TEST_OMIT_VARIABLES
31#include <dpkg/test.h>
32
34
35struct perf_slot {
36 struct timespec t_ini, t_end;
37};
38
39static inline void
40perf_ts_sub(struct timespec *a, struct timespec *b, struct timespec *res)
41{
42 res->tv_sec = a->tv_sec - b->tv_sec;
43 res->tv_nsec = a->tv_nsec - b->tv_nsec;
44 if (res->tv_nsec < 0) {
45 res->tv_sec--;
46 res->tv_nsec += 1000000000;
47 }
48}
49
50static void
51perf_ts_mark_print(const char *str)
52{
53 struct timespec ts;
54
55 clock_gettime(CLOCK_MONOTONIC, &ts);
56
57 printf("%lu.%.9lu: %s\n", ts.tv_sec, ts.tv_nsec, str);
58}
59
60static void
61perf_ts_slot_print(struct perf_slot *ps, const char *str)
62{
63 struct timespec t_res;
64
65 perf_ts_sub(&ps->t_end, &ps->t_ini, &t_res);
66
67 printf("%lu.%.9lu: %s (%lu.%.9lu sec)\n",
68 ps->t_end.tv_sec, ps->t_end.tv_nsec,
69 str, t_res.tv_sec, t_res.tv_nsec);
70}
71
72#define perf_ts_slot_start(ps) clock_gettime(CLOCK_MONOTONIC, &((ps)->t_ini))
73#define perf_ts_slot_stop(ps) clock_gettime(CLOCK_MONOTONIC, &((ps)->t_end))
74
76
77#endif
#define DPKG_BEGIN_DECLS
Definition: macros.h:86
#define DPKG_END_DECLS
Definition: macros.h:87
Definition: perf.h:35
struct timespec t_ini t_end
Definition: perf.h:36