Changeset 64ce0c1 in mainline for uspace/app/tmon/tf.c


Ignore:
Timestamp:
2018-02-02T10:13:55Z (6 years ago)
Author:
Jenda <jenda.jzqk73@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
961a5ee
Parents:
e67c50a (diff), 290338b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge commit '290338bf7224f502808b23e82d98306208962b97' into forwardport

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/tmon/tf.c

    re67c50a r64ce0c1  
    3636
    3737#include <stdio.h>
     38#include <macros.h>
    3839#include <devman.h>
    3940#include <str_error.h>
     
    7475        }
    7576
    76         printf("Using device: %s\n", path);
     77        printf("Device: %s\n", path);
    7778
    7879        // Read test parameters from options.
    79         tmon_test_params_t *params = NULL;
     80        void *params = NULL;
    8081        if ((rc = ops->read_params(argc, argv, &params))) {
    8182                printf(NAME ": Reading test parameters failed. %s\n", str_error(rc));
     83                return 1;
     84        }
     85
     86        if ((rc = ops->pre_run(params))) {
     87                printf(NAME ": Pre-run hook failed. %s\n", str_error(rc));
    8288                return 1;
    8389        }
     
    106112}
    107113
     114/** Unit of quantity used for pretty formatting. */
     115typedef struct tmon_unit {
     116        /** Prefix letter, which is printed before the actual unit. */
     117        const char *unit;
     118        /** Factor of the unit. */
     119        double factor;
     120} tmon_unit_t;
     121
     122/** Format a value for human reading.
     123 * @param[in] val The value to format.
     124 * @param[in] fmt Format string. Must include one double and char.
     125 *
     126 * @return Heap-allocated string if successful (caller becomes its owner), NULL otherwise.
     127 */
     128static char *format_unit(double val, const char *fmt, const tmon_unit_t *units, size_t len)
     129{
     130        // Figure out the "tightest" unit.
     131        unsigned i;
     132        for (i = 0; i < len; ++i) {
     133                if (units[i].factor <= val)
     134                        break;
     135        }
     136
     137        if (i == len) --i;
     138        const char *unit = units[i].unit;
     139        double factor = units[i].factor;
     140
     141        // Format the size.
     142        const double div_size = val / factor;
     143
     144        char *out = NULL;
     145        asprintf(&out, fmt, div_size, unit);
     146
     147        return out;
     148}
     149
     150/** Static array of size units with decreasing factors. */
     151static const tmon_unit_t size_units[] = {
     152        { .unit = "EB", .factor = 1ULL << 60 },
     153        { .unit = "PB", .factor = 1ULL << 50 },
     154        { .unit = "TB", .factor = 1ULL << 40 },
     155        { .unit = "GB", .factor = 1ULL << 30 },
     156        { .unit = "MB", .factor = 1ULL << 20 },
     157        { .unit = "kB", .factor = 1ULL << 10 },
     158        { .unit = "B",  .factor = 1ULL },
     159};
     160
     161char *tmon_format_size(double val, const char *fmt)
     162{
     163        return format_unit(val, fmt, size_units, ARRAY_SIZE(size_units));
     164}
     165
     166/** Static array of duration units with decreasing factors. */
     167static const tmon_unit_t dur_units[] = {
     168        { .unit = "d",   .factor = 60 * 60 * 24 },
     169        { .unit = "h",   .factor = 60 * 60 },
     170        { .unit = "min", .factor = 60 },
     171        { .unit = "s",   .factor = 1 },
     172        { .unit = "ms",  .factor = 1e-3 },
     173        { .unit = "us",  .factor = 1e-6 },
     174        { .unit = "ns",  .factor = 1e-9 },
     175        { .unit = "ps",  .factor = 1e-12 },
     176};
     177
     178char *tmon_format_duration(usbdiag_dur_t val, const char *fmt)
     179{
     180        return format_unit(val / 1000.0, fmt, dur_units, ARRAY_SIZE(dur_units));
     181}
     182
    108183/** @}
    109184 */
Note: See TracChangeset for help on using the changeset viewer.