HelenOS sources
This source file includes following definitions.
- tmon_test_main
- format_unit
- tmon_format_size
- tmon_format_duration
#include <stdio.h>
#include <macros.h>
#include <devman.h>
#include <str_error.h>
#include <usbdiag_iface.h>
#include "resolve.h"
#include "tf.h"
#define NAME "tmon"
#define MAX_PATH_LENGTH 1024
int tmon_test_main(int argc, char *argv[], const tmon_test_ops_t *ops)
{
devman_handle_t fun = -1;
if (argc >= 2 && *argv[1] != '-') {
if (tmon_resolve_named(argv[1], &fun))
return 1;
} else {
if (tmon_resolve_default(&fun))
return 1;
}
errno_t rc;
int ec;
char path[MAX_PATH_LENGTH];
if ((rc = devman_fun_get_path(fun, path, sizeof(path)))) {
printf(NAME ": Error resolving path of device with handle "
"%" PRIun ". %s\n", fun, str_error(rc));
return 1;
}
printf("Device: %s\n", path);
void *params = NULL;
if ((rc = ops->read_params(argc, argv, ¶ms))) {
printf(NAME ": Reading test parameters failed. %s\n", str_error(rc));
return 1;
}
if ((rc = ops->pre_run(params))) {
printf(NAME ": Pre-run hook failed. %s\n", str_error(rc));
return 1;
}
async_sess_t *sess = usbdiag_connect(fun);
if (!sess) {
printf(NAME ": Could not connect to the device.\n");
return 1;
}
async_exch_t *exch = async_exchange_begin(sess);
if (!exch) {
printf(NAME ": Could not start exchange with the device.\n");
ec = 1;
goto err_sess;
}
ec = ops->run(exch, params);
async_exchange_end(exch);
err_sess:
usbdiag_disconnect(sess);
free(params);
return ec;
}
typedef struct tmon_unit {
const char *unit;
double factor;
} tmon_unit_t;
static char *format_unit(double val, const char *fmt, const tmon_unit_t *units, size_t len)
{
unsigned i;
for (i = 0; i < len; ++i) {
if (units[i].factor <= val)
break;
}
if (i == len)
--i;
const char *unit = units[i].unit;
double factor = units[i].factor;
const double div_size = val / factor;
char *out = NULL;
asprintf(&out, fmt, div_size, unit);
return out;
}
static const tmon_unit_t size_units[] = {
{ .unit = "EB", .factor = 1ULL << 60 },
{ .unit = "PB", .factor = 1ULL << 50 },
{ .unit = "TB", .factor = 1ULL << 40 },
{ .unit = "GB", .factor = 1ULL << 30 },
{ .unit = "MB", .factor = 1ULL << 20 },
{ .unit = "kB", .factor = 1ULL << 10 },
{ .unit = "B", .factor = 1ULL },
};
char *tmon_format_size(double val, const char *fmt)
{
return format_unit(val, fmt, size_units, ARRAY_SIZE(size_units));
}
static const tmon_unit_t dur_units[] = {
{ .unit = "d", .factor = 60 * 60 * 24 },
{ .unit = "h", .factor = 60 * 60 },
{ .unit = "min", .factor = 60 },
{ .unit = "s", .factor = 1 },
{ .unit = "ms", .factor = 1e-3 },
{ .unit = "us", .factor = 1e-6 },
{ .unit = "ns", .factor = 1e-9 },
{ .unit = "ps", .factor = 1e-12 },
};
char *tmon_format_duration(usbdiag_dur_t val, const char *fmt)
{
return format_unit(val / 1000.0, fmt, dur_units, ARRAY_SIZE(dur_units));
}
HelenOS homepage, sources at GitHub