Changeset 3b5a5e3 in mainline for uspace/app/tmon/tf.c
- Timestamp:
- 2018-02-01T16:10:12Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 3038d51
- Parents:
- 8393c73b
- git-author:
- Petr Manek <petr.manek@…> (2018-02-01 16:09:43)
- git-committer:
- Petr Manek <petr.manek@…> (2018-02-01 16:10:12)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/tmon/tf.c
r8393c73b r3b5a5e3 36 36 37 37 #include <stdio.h> 38 #include <macros.h> 38 39 #include <devman.h> 39 40 #include <str_error.h> … … 74 75 } 75 76 76 printf(" Using device: %s\n", path);77 printf("Device: %s\n", path); 77 78 78 79 // Read test parameters from options. 79 tmon_test_params_t*params = NULL;80 void *params = NULL; 80 81 if ((rc = ops->read_params(argc, argv, ¶ms))) { 81 82 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)); 82 88 return 1; 83 89 } … … 106 112 } 107 113 114 /** Unit of quantity used for pretty formatting. */ 115 typedef 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 */ 128 static 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. */ 151 static 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 161 char *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. */ 167 static 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 178 char *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 108 183 /** @} 109 184 */
Note:
See TracChangeset
for help on using the changeset viewer.