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
18 * License along with dpkg; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #ifndef LIBDPKG_PERF_H
23 #define LIBDPKG_PERF_H
24
25 #include <config.h>
26 #include <compat.h>
27
28 #include <time.h>
29 #include <stdio.h>
30
31 struct perf_slot {
32 struct timespec t_ini, t_end;
33 };
34
35 static inline void
perf_ts_sub(struct timespec * a,struct timespec * b,struct timespec * res)36 perf_ts_sub(struct timespec *a, struct timespec *b, struct timespec *res)
37 {
38 res->tv_sec = a->tv_sec - b->tv_sec;
39 res->tv_nsec = a->tv_nsec - b->tv_nsec;
40 if (res->tv_nsec < 0) {
41 res->tv_sec--;
42 res->tv_nsec += 1000000000;
43 }
44 }
45
46 static void
perf_ts_mark_print(const char * str)47 perf_ts_mark_print(const char *str)
48 {
49 struct timespec ts;
50
51 clock_gettime(CLOCK_MONOTONIC, &ts);
52
53 printf("%lu.%.9lu: %s\n", ts.tv_sec, ts.tv_nsec, str);
54 }
55
56 static void
perf_ts_slot_print(struct perf_slot * ps,const char * str)57 perf_ts_slot_print(struct perf_slot *ps, const char *str)
58 {
59 struct timespec t_res;
60
61 perf_ts_sub(&ps->t_end, &ps->t_ini, &t_res);
62
63 printf("%lu.%.9lu: %s (%lu.%.9lu sec)\n",
64 ps->t_end.tv_sec, ps->t_end.tv_nsec,
65 str, t_res.tv_sec, t_res.tv_nsec);
66 }
67
68 #define perf_ts_slot_start(ps) clock_gettime(CLOCK_MONOTONIC, &((ps)->t_ini))
69 #define perf_ts_slot_stop(ps) clock_gettime(CLOCK_MONOTONIC, &((ps)->t_end))
70
71 #endif
72