From 13a0c1b05a4b175f13decf3b15567dc71ea043ac Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 10 Feb 2020 21:44:14 +0100 Subject: Cleanup statistics print out - Give a rounded input/output byte count. - Seperate groups by new lines. Signed-off-by: David Oberhollenzer --- include/common.h | 2 ++ lib/common/Makemodule.am | 1 + lib/common/print_size.c | 38 ++++++++++++++++++++++++++++++++++++++ lib/common/statistics.c | 21 ++++++++++++++++----- unpack/list_files.c | 25 ++++--------------------- 5 files changed, 61 insertions(+), 26 deletions(-) create mode 100644 lib/common/print_size.c diff --git a/include/common.h b/include/common.h index a3055dc..0fd349a 100644 --- a/include/common.h +++ b/include/common.h @@ -162,4 +162,6 @@ sqfs_compressor_t *lzo_compressor_create(const sqfs_compressor_config_t *cfg); int parse_size(const char *what, size_t *out, const char *str, size_t reference); +void print_size(sqfs_u64 size, char *buffer, bool round_to_int); + #endif /* COMMON_H */ diff --git a/lib/common/Makemodule.am b/lib/common/Makemodule.am index ac1a4e9..c59ab4c 100644 --- a/lib/common/Makemodule.am +++ b/lib/common/Makemodule.am @@ -6,6 +6,7 @@ libcommon_a_SOURCES += lib/common/data_writer.c include/common.h libcommon_a_SOURCES += lib/common/get_path.c lib/common/io_stdin.c libcommon_a_SOURCES += lib/common/writer.c lib/common/perror.c libcommon_a_SOURCES += lib/common/mkdir_p.c lib/common/parse_size.c +libcommon_a_SOURCES += lib/common/print_size.c libcommon_a_CFLAGS = $(AM_CFLAGS) $(LZO_CFLAGS) if WITH_LZO diff --git a/lib/common/print_size.c b/lib/common/print_size.c new file mode 100644 index 0000000..6e76805 --- /dev/null +++ b/lib/common/print_size.c @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * print_size.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" +#include "common.h" + +void print_size(sqfs_u64 size, char *buffer, bool round_to_int) +{ + static const char *fractions = "0112334456678899"; + static const char *suffices = "kMGTPEZY"; + unsigned int fraction; + int suffix = -1; + + while (size > 1024) { + ++suffix; + fraction = size % 1024; + size /= 1024; + } + + if (suffix >= 0) { + fraction /= 64; + + if (round_to_int) { + size = fraction >= 8 ? (size + 1) : size; + + sprintf(buffer, "%u%c", (unsigned int)size, + suffices[suffix]); + } else { + sprintf(buffer, "%u.%c%c", (unsigned int)size, + fractions[fraction], suffices[suffix]); + } + } else { + sprintf(buffer, "%u", (unsigned int)size); + } +} diff --git a/lib/common/statistics.c b/lib/common/statistics.c index eabe037..92d365c 100644 --- a/lib/common/statistics.c +++ b/lib/common/statistics.c @@ -14,6 +14,7 @@ void sqfs_print_statistics(const sqfs_super_t *super, { const sqfs_block_processor_stats_t *proc_stats; const sqfs_block_writer_stats_t *wr_stats; + char read_sz[32], written_sz[32]; size_t ratio; proc_stats = sqfs_block_processor_get_stats(blk); @@ -26,18 +27,28 @@ void sqfs_print_statistics(const sqfs_super_t *super, ratio = 100; } + print_size(proc_stats->input_bytes_read, read_sz, false); + print_size(wr_stats->bytes_written, written_sz, false); + fputs("---------------------------------------------------\n", stdout); - printf("Input files processed: %lu\n", proc_stats->input_bytes_read); - printf("Data blocks actually written: %lu\n", wr_stats->blocks_written); - printf("Fragment blocks written: %lu\n", proc_stats->frag_block_count); - printf("Duplicate data blocks omitted: %lu\n", + printf("Data bytes read: %s\n", read_sz); + printf("Data bytes written: %s\n", written_sz); + printf("Data compression ratio: " PRI_SZ "%%\n", ratio); + fputc('\n', stdout); + + printf("Data blocks written: %lu\n", wr_stats->blocks_written); + printf("Out of which where fragment blocks: %lu\n", + proc_stats->frag_block_count); + printf("Duplicate blocks omitted: %lu\n", wr_stats->blocks_submitted - wr_stats->blocks_written); printf("Sparse blocks omitted: %lu\n", proc_stats->sparse_block_count); + fputc('\n', stdout); + printf("Fragments actually written: %lu\n", proc_stats->actual_frag_count); printf("Duplicated fragments omitted: %lu\n", proc_stats->total_frag_count - proc_stats->actual_frag_count); printf("Total number of inodes: %u\n", super->inode_count); printf("Number of unique group/user IDs: %u\n", super->id_count); - printf("Data compression ratio: " PRI_SZ "%%\n", ratio); + fputc('\n', stdout); } diff --git a/unpack/list_files.c b/unpack/list_files.c index c08c2dd..238ffec 100644 --- a/unpack/list_files.c +++ b/unpack/list_files.c @@ -64,40 +64,23 @@ static int count_int_chars(unsigned int i) return count; } -static void print_size(sqfs_u64 size, char *buffer) -{ - static const char *suffices = "kMGTPEZY"; - int suffix = -1; - - while (size > 1024) { - ++suffix; - size /= 1024; - } - - if (suffix >= 0) { - sprintf(buffer, "%u%c", (unsigned int)size, suffices[suffix]); - } else { - sprintf(buffer, "%u", (unsigned int)size); - } -} - static void print_node_size(const sqfs_tree_node_t *n, char *buffer) { switch (n->inode->base.mode & S_IFMT) { case S_IFLNK: - print_size(strlen((const char *)n->inode->extra), buffer); + print_size(strlen((const char *)n->inode->extra), buffer, true); break; case S_IFREG: { sqfs_u64 size; sqfs_inode_get_file_size(n->inode, &size); - print_size(size, buffer); + print_size(size, buffer, true); break; } case S_IFDIR: if (n->inode->base.type == SQFS_INODE_EXT_DIR) { - print_size(n->inode->data.dir_ext.size, buffer); + print_size(n->inode->data.dir_ext.size, buffer, true); } else { - print_size(n->inode->data.dir.size, buffer); + print_size(n->inode->data.dir.size, buffer, true); } break; case S_IFBLK: -- cgit v1.2.3