From: Pierre Ossman Date: Tue, 3 Mar 2015 15:30:13 +0000 (+0100) Subject: Shorten stats from EncodeManager using SI/IEC prefixes X-Git-Tag: v1.4.90~33^2~23 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=64624342d98c62563c2a78b117ce3fcea9b61e68;p=tigervnc.git Shorten stats from EncodeManager using SI/IEC prefixes Also avoids %lld which isn't supported on Windows. --- diff --git a/common/rfb/EncodeManager.cxx b/common/rfb/EncodeManager.cxx index 954f29b1..9122cd75 100644 --- a/common/rfb/EncodeManager.cxx +++ b/common/rfb/EncodeManager.cxx @@ -163,6 +163,8 @@ void EncodeManager::logStats() double ratio; + char a[1024], b[1024]; + rects = 0; pixels = bytes = equivalent = 0; @@ -190,19 +192,23 @@ void EncodeManager::logStats() ratio = (double)stats[i][j].equivalent / stats[i][j].bytes; - vlog.info(" %s: %u rects, %llu pixels", - encoderTypeName((EncoderType)j), - stats[i][j].rects, stats[i][j].pixels); - vlog.info(" %*s %llu bytes (%g ratio)", - strlen(encoderTypeName((EncoderType)j)), "", - stats[i][j].bytes, ratio); + siPrefix(stats[i][j].rects, "rects", a, sizeof(a)); + siPrefix(stats[i][j].pixels, "pixels", b, sizeof(b)); + vlog.info(" %s: %s, %s", encoderTypeName((EncoderType)j), a, b); + iecPrefix(stats[i][j].bytes, "B", a, sizeof(a)); + vlog.info(" %*s %s (1:%g ratio)", + (int)strlen(encoderTypeName((EncoderType)j)), "", + a, ratio); } } ratio = (double)equivalent / bytes; - vlog.info(" Total: %u rects, %llu pixels", rects, pixels); - vlog.info(" %llu bytes (%g ratio)", bytes, ratio); + siPrefix(rects, "rects", a, sizeof(a)); + siPrefix(pixels, "pixels", b, sizeof(b)); + vlog.info(" Total: %s, %s", a, b); + iecPrefix(bytes, "B", a, sizeof(a)); + vlog.info(" %s (1:%g ratio)", a, ratio); } bool EncodeManager::supported(int encoding) diff --git a/common/rfb/util.cxx b/common/rfb/util.cxx index a41ad96a..2b3c9f40 100644 --- a/common/rfb/util.cxx +++ b/common/rfb/util.cxx @@ -34,6 +34,7 @@ #include #endif +#include #include #include @@ -110,4 +111,44 @@ namespace rfb { return diff; } + + static size_t doPrefix(long long value, const char *unit, + char *buffer, size_t maxlen, + unsigned divisor, const char **prefixes, + size_t prefixCount) { + double newValue; + size_t prefix, len; + + newValue = value; + prefix = 0; + while (newValue >= divisor) { + if (prefix >= prefixCount) + break; + newValue /= divisor; + prefix++; + } + + len = snprintf(buffer, maxlen, "%g %s%s", newValue, + (prefix == 0) ? "" : prefixes[prefix-1], unit); + buffer[maxlen-1] = '\0'; + + return len; + } + + static const char *siPrefixes[] = + { "k", "M", "G", "T", "P", "E", "Z", "Y" }; + static const char *iecPrefixes[] = + { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" }; + + size_t siPrefix(long long value, const char *unit, + char *buffer, size_t maxlen) { + return doPrefix(value, unit, buffer, maxlen, 1000, siPrefixes, + sizeof(siPrefixes)/sizeof(*siPrefixes)); + } + + size_t iecPrefix(long long value, const char *unit, + char *buffer, size_t maxlen) { + return doPrefix(value, unit, buffer, maxlen, 1024, iecPrefixes, + sizeof(iecPrefixes)/sizeof(*iecPrefixes)); + } }; diff --git a/common/rfb/util.h b/common/rfb/util.h index 13dbe68e..98ce6420 100644 --- a/common/rfb/util.h +++ b/common/rfb/util.h @@ -90,6 +90,11 @@ namespace rfb { // Returns time elapsed since given moment in milliseconds. unsigned msSince(const struct timeval *then); + + size_t siPrefix(long long value, const char *unit, + char *buffer, size_t maxlen); + size_t iecPrefix(long long value, const char *unit, + char *buffer, size_t maxlen); } // Some platforms (e.g. Windows) include max() and min() macros in their