/src/barebox/commands/time.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | |
3 | | #include <common.h> |
4 | | #include <command.h> |
5 | | #include <clock.h> |
6 | | #include <linux/math64.h> |
7 | | #include <malloc.h> |
8 | | #include <getopt.h> |
9 | | |
10 | | static int do_time(int argc, char *argv[]) |
11 | 0 | { |
12 | 0 | int opt; |
13 | 0 | unsigned char *buf; |
14 | 0 | u64 start, end, diff64; |
15 | 0 | bool nanoseconds = false; |
16 | |
|
17 | 0 | while ((opt = getopt(argc, argv, "+n")) > 0) { |
18 | 0 | switch (opt) { |
19 | 0 | case 'n': |
20 | 0 | nanoseconds = true; |
21 | 0 | break; |
22 | 0 | default: |
23 | 0 | return COMMAND_ERROR_USAGE; |
24 | 0 | } |
25 | 0 | } |
26 | | |
27 | 0 | argv += optind; |
28 | 0 | argc -= optind; |
29 | |
|
30 | 0 | if (argc < 1) |
31 | 0 | return COMMAND_ERROR_USAGE; |
32 | | |
33 | 0 | buf = strjoin(" ", argv, argc); |
34 | |
|
35 | 0 | start = get_time_ns(); |
36 | |
|
37 | 0 | run_command("%s", buf); |
38 | |
|
39 | 0 | end = get_time_ns(); |
40 | |
|
41 | 0 | diff64 = end - start; |
42 | |
|
43 | 0 | if (!nanoseconds) |
44 | 0 | do_div(diff64, 1000000); |
45 | |
|
46 | 0 | printf("time: %llu%cs\n", diff64, nanoseconds ? 'n' : 'm'); |
47 | |
|
48 | 0 | free(buf); |
49 | |
|
50 | 0 | return 0; |
51 | 0 | } |
52 | | |
53 | | BAREBOX_CMD_HELP_START(time) |
54 | | BAREBOX_CMD_HELP_TEXT("Note: This command depends on COMMAND being interruptible,") |
55 | | BAREBOX_CMD_HELP_TEXT("otherwise the timer may overrun resulting in incorrect results") |
56 | | BAREBOX_CMD_HELP_TEXT("Options:") |
57 | | BAREBOX_CMD_HELP_OPT ("-n", "output elapsed time in nanoseconds") |
58 | | BAREBOX_CMD_HELP_END |
59 | | |
60 | | BAREBOX_CMD_START(time) |
61 | | .cmd = do_time, |
62 | | BAREBOX_CMD_DESC("measure execution duration of a command") |
63 | | BAREBOX_CMD_OPTS("[-n] COMMAND") |
64 | | BAREBOX_CMD_GROUP(CMD_GRP_MISC) |
65 | | BAREBOX_CMD_HELP(cmd_time_help) |
66 | | BAREBOX_CMD_END |