source: mainline/uspace/app/tmon/tf.c

Last change on this file was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 5.2 KB
RevLine 
[2bd04b2]1/*
2 * Copyright (c) 2017 Petr Manek
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup tmon
30 * @{
31 */
32/**
33 * @file
[dd36558]34 * Testing framework.
[2bd04b2]35 */
36
37#include <stdio.h>
[3b5a5e3]38#include <macros.h>
[2bd04b2]39#include <devman.h>
40#include <str_error.h>
[41ebc36]41#include <usbdiag_iface.h>
[ad2a8b1]42#include "resolve.h"
[2986763]43#include "tf.h"
[2bd04b2]44
45#define NAME "tmon"
[e9d600c2]46#define MAX_PATH_LENGTH 1024
47
[8e16454]48/** Common command handler for all test commands.
49 * @param[in] argc Number of arguments.
50 * @param[in] argv Argument values. Must point to exactly `argc` strings.
51 *
52 * @return Exit code
53 */
54int tmon_test_main(int argc, char *argv[], const tmon_test_ops_t *ops)
55{
56 // Resolve device function.
[2bd04b2]57 devman_handle_t fun = -1;
58
[119a794]59 if (argc >= 2 && *argv[1] != '-') {
60 // Assume that the first argument is device path.
61 if (tmon_resolve_named(argv[1], &fun))
[2bd04b2]62 return 1;
63 } else {
[119a794]64 // The first argument is either an option or not present.
65 if (tmon_resolve_default(&fun))
66 return 1;
[2bd04b2]67 }
68
[98b1d30]69 errno_t rc;
70 int ec;
[e9d600c2]71 char path[MAX_PATH_LENGTH];
72 if ((rc = devman_fun_get_path(fun, path, sizeof(path)))) {
[80f7c54]73 printf(NAME ": Error resolving path of device with handle "
74 "%" PRIun ". %s\n", fun, str_error(rc));
[e9d600c2]75 return 1;
76 }
77
[3b5a5e3]78 printf("Device: %s\n", path);
[e9d600c2]79
[8e16454]80 // Read test parameters from options.
[3b5a5e3]81 void *params = NULL;
[dd36558]82 if ((rc = ops->read_params(argc, argv, &params))) {
[5302493]83 printf(NAME ": Reading test parameters failed. %s\n", str_error(rc));
[dd36558]84 return 1;
85 }
[e9d600c2]86
[3b5a5e3]87 if ((rc = ops->pre_run(params))) {
88 printf(NAME ": Pre-run hook failed. %s\n", str_error(rc));
89 return 1;
90 }
91
[8e16454]92 // Run the test body.
[e9d600c2]93 async_sess_t *sess = usbdiag_connect(fun);
[d37dd3b9]94 if (!sess) {
95 printf(NAME ": Could not connect to the device.\n");
96 return 1;
97 }
[e9d600c2]98
[d37dd3b9]99 async_exch_t *exch = async_exchange_begin(sess);
100 if (!exch) {
101 printf(NAME ": Could not start exchange with the device.\n");
[e9d600c2]102 ec = 1;
[d37dd3b9]103 goto err_sess;
[e9d600c2]104 }
105
[dd36558]106 ec = ops->run(exch, params);
[e9d600c2]107 async_exchange_end(exch);
[d37dd3b9]108
109err_sess:
[e9d600c2]110 usbdiag_disconnect(sess);
[dd36558]111 free(params);
[e9d600c2]112 return ec;
[2bd04b2]113}
114
[3b5a5e3]115/** Unit of quantity used for pretty formatting. */
116typedef struct tmon_unit {
117 /** Prefix letter, which is printed before the actual unit. */
118 const char *unit;
119 /** Factor of the unit. */
120 double factor;
121} tmon_unit_t;
122
123/** Format a value for human reading.
124 * @param[in] val The value to format.
125 * @param[in] fmt Format string. Must include one double and char.
126 *
127 * @return Heap-allocated string if successful (caller becomes its owner), NULL otherwise.
128 */
129static char *format_unit(double val, const char *fmt, const tmon_unit_t *units, size_t len)
130{
131 // Figure out the "tightest" unit.
132 unsigned i;
133 for (i = 0; i < len; ++i) {
134 if (units[i].factor <= val)
135 break;
136 }
137
[1433ecda]138 if (i == len)
139 --i;
[3b5a5e3]140 const char *unit = units[i].unit;
141 double factor = units[i].factor;
142
143 // Format the size.
144 const double div_size = val / factor;
145
146 char *out = NULL;
147 asprintf(&out, fmt, div_size, unit);
148
149 return out;
150}
151
152/** Static array of size units with decreasing factors. */
153static const tmon_unit_t size_units[] = {
154 { .unit = "EB", .factor = 1ULL << 60 },
155 { .unit = "PB", .factor = 1ULL << 50 },
156 { .unit = "TB", .factor = 1ULL << 40 },
157 { .unit = "GB", .factor = 1ULL << 30 },
158 { .unit = "MB", .factor = 1ULL << 20 },
159 { .unit = "kB", .factor = 1ULL << 10 },
160 { .unit = "B", .factor = 1ULL },
161};
162
163char *tmon_format_size(double val, const char *fmt)
164{
165 return format_unit(val, fmt, size_units, ARRAY_SIZE(size_units));
166}
167
168/** Static array of duration units with decreasing factors. */
169static const tmon_unit_t dur_units[] = {
170 { .unit = "d", .factor = 60 * 60 * 24 },
171 { .unit = "h", .factor = 60 * 60 },
172 { .unit = "min", .factor = 60 },
173 { .unit = "s", .factor = 1 },
174 { .unit = "ms", .factor = 1e-3 },
175 { .unit = "us", .factor = 1e-6 },
176 { .unit = "ns", .factor = 1e-9 },
177 { .unit = "ps", .factor = 1e-12 },
178};
179
180char *tmon_format_duration(usbdiag_dur_t val, const char *fmt)
181{
182 return format_unit(val / 1000.0, fmt, dur_units, ARRAY_SIZE(dur_units));
183}
184
[2bd04b2]185/** @}
186 */
Note: See TracBrowser for help on using the repository browser.