chiark / gitweb /
build-sys: add a makefile target to run all tests through valgrind
[elogind.git] / src / test / test-boot-timestamps.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7   Copyright 2013 Kay Sievers
8
9   systemd is free software; you can redistribute it and/or modify it
10   under the terms of the GNU Lesser General Public License as published by
11   the Free Software Foundation; either version 2.1 of the License, or
12   (at your option) any later version.
13
14   systemd is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include "util.h"
24 #include "log.h"
25 #include "boot-timestamps.h"
26 #include "efivars.h"
27 #include "acpi-fpdt.h"
28
29 static int test_acpi_fpdt(void) {
30         usec_t loader_start;
31         usec_t loader_exit;
32         char ts_start[FORMAT_TIMESPAN_MAX];
33         char ts_exit[FORMAT_TIMESPAN_MAX];
34         char ts_span[FORMAT_TIMESPAN_MAX];
35         int r;
36
37         r = acpi_get_boot_usec(&loader_start, &loader_exit);
38         if (r < 0) {
39                 if (r != -ENOENT)
40                         log_error("Failed to read ACPI FPDT: %s", strerror(-r));
41                 return r;
42         }
43
44         log_info("ACPI FPDT: loader start=%s exit=%s duration=%s",
45                  format_timespan(ts_start, sizeof(ts_start), loader_start, USEC_PER_MSEC),
46                  format_timespan(ts_exit, sizeof(ts_exit), loader_exit, USEC_PER_MSEC),
47                  format_timespan(ts_span, sizeof(ts_span), loader_exit - loader_start, USEC_PER_MSEC));
48
49         return 0;
50 }
51
52 static int test_efi_loader(void) {
53         usec_t loader_start;
54         usec_t loader_exit;
55         char ts_start[FORMAT_TIMESPAN_MAX];
56         char ts_exit[FORMAT_TIMESPAN_MAX];
57         char ts_span[FORMAT_TIMESPAN_MAX];
58         int r;
59
60         r = efi_loader_get_boot_usec(&loader_start, &loader_exit);
61         if (r < 0) {
62                 if (r != -ENOENT)
63                         log_error("Failed to read EFI loader data: %s", strerror(-r));
64                 return r;
65         }
66
67         log_info("EFI Loader: start=%s exit=%s duration=%s",
68                  format_timespan(ts_start, sizeof(ts_start), loader_start, USEC_PER_MSEC),
69                  format_timespan(ts_exit, sizeof(ts_exit), loader_exit, USEC_PER_MSEC),
70                  format_timespan(ts_span, sizeof(ts_span), loader_exit - loader_start, USEC_PER_MSEC));
71
72         return 0;
73 }
74
75 int main(int argc, char* argv[]) {
76         char s[MAX(FORMAT_TIMESPAN_MAX, FORMAT_TIMESTAMP_MAX)];
77         int r;
78         dual_timestamp fw, l, k;
79
80         test_acpi_fpdt();
81         test_efi_loader();
82
83         dual_timestamp_from_monotonic(&k, 0);
84
85         r = boot_timestamps(NULL, &fw, &l);
86         if (r < 0) {
87                 log_error("Failed to read variables: %s", strerror(-r));
88                 return 1;
89         }
90
91         log_info("Firmware began %s before kernel.", format_timespan(s, sizeof(s), fw.monotonic, 0));
92         log_info("Loader began %s before kernel.", format_timespan(s, sizeof(s), l.monotonic, 0));
93         log_info("Firmware began %s.", format_timestamp(s, sizeof(s), fw.realtime));
94         log_info("Loader began %s.", format_timestamp(s, sizeof(s), l.realtime));
95         log_info("Kernel began %s.", format_timestamp(s, sizeof(s), k.realtime));
96
97         return 0;
98 }