+ show_lat(io_u_lat_m, FIO_IO_U_LAT_M_NR, ranges, "msec", out);
+}
+
+static void show_latencies(struct thread_stat *ts, struct buf_output *out)
+{
+ double io_u_lat_u[FIO_IO_U_LAT_U_NR];
+ double io_u_lat_m[FIO_IO_U_LAT_M_NR];
+
+ stat_calc_lat_u(ts, io_u_lat_u);
+ stat_calc_lat_m(ts, io_u_lat_m);
+
+ show_lat_u(io_u_lat_u, out);
+ show_lat_m(io_u_lat_m, out);
+}
+
+static int block_state_category(int block_state)
+{
+ switch (block_state) {
+ case BLOCK_STATE_UNINIT:
+ return 0;
+ case BLOCK_STATE_TRIMMED:
+ case BLOCK_STATE_WRITTEN:
+ return 1;
+ case BLOCK_STATE_WRITE_FAILURE:
+ case BLOCK_STATE_TRIM_FAILURE:
+ return 2;
+ default:
+ /* Silence compile warning on some BSDs and have a return */
+ assert(0);
+ return -1;
+ }
+}
+
+static int compare_block_infos(const void *bs1, const void *bs2)
+{
+ uint32_t block1 = *(uint32_t *)bs1;
+ uint32_t block2 = *(uint32_t *)bs2;
+ int state1 = BLOCK_INFO_STATE(block1);
+ int state2 = BLOCK_INFO_STATE(block2);
+ int bscat1 = block_state_category(state1);
+ int bscat2 = block_state_category(state2);
+ int cycles1 = BLOCK_INFO_TRIMS(block1);
+ int cycles2 = BLOCK_INFO_TRIMS(block2);
+
+ if (bscat1 < bscat2)
+ return -1;
+ if (bscat1 > bscat2)
+ return 1;
+
+ if (cycles1 < cycles2)
+ return -1;
+ if (cycles1 > cycles2)
+ return 1;
+
+ if (state1 < state2)
+ return -1;
+ if (state1 > state2)
+ return 1;
+
+ assert(block1 == block2);
+ return 0;
+}
+
+static int calc_block_percentiles(int nr_block_infos, uint32_t *block_infos,
+ fio_fp64_t *plist, unsigned int **percentiles,
+ unsigned int *types)
+{
+ int len = 0;
+ int i, nr_uninit;
+
+ qsort(block_infos, nr_block_infos, sizeof(uint32_t), compare_block_infos);
+
+ while (len < FIO_IO_U_LIST_MAX_LEN && plist[len].u.f != 0.0)
+ len++;
+
+ if (!len)
+ return 0;
+
+ /*
+ * Sort the percentile list. Note that it may already be sorted if
+ * we are using the default values, but since it's a short list this
+ * isn't a worry. Also note that this does not work for NaN values.
+ */
+ if (len > 1)
+ qsort((void *)plist, len, sizeof(plist[0]), double_cmp);
+
+ nr_uninit = 0;
+ /* Start only after the uninit entries end */
+ for (nr_uninit = 0;
+ nr_uninit < nr_block_infos
+ && BLOCK_INFO_STATE(block_infos[nr_uninit]) == BLOCK_STATE_UNINIT;
+ nr_uninit ++)
+ ;
+
+ if (nr_uninit == nr_block_infos)
+ return 0;
+
+ *percentiles = calloc(len, sizeof(**percentiles));
+
+ for (i = 0; i < len; i++) {
+ int idx = (plist[i].u.f * (nr_block_infos - nr_uninit) / 100)
+ + nr_uninit;
+ (*percentiles)[i] = BLOCK_INFO_TRIMS(block_infos[idx]);
+ }
+
+ memset(types, 0, sizeof(*types) * BLOCK_STATE_COUNT);
+ for (i = 0; i < nr_block_infos; i++)
+ types[BLOCK_INFO_STATE(block_infos[i])]++;
+
+ return len;