From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36130) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W8so3-0000dG-Vx for qemu-devel@nongnu.org; Thu, 30 Jan 2014 09:41:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W8snz-0002Zy-3K for qemu-devel@nongnu.org; Thu, 30 Jan 2014 09:41:27 -0500 Received: from mx1.redhat.com ([209.132.183.28]:18724) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W8sny-0002Zr-RH for qemu-devel@nongnu.org; Thu, 30 Jan 2014 09:41:23 -0500 Message-ID: <52EA5D37.4070209@redhat.com> Date: Thu, 30 Jan 2014 07:09:59 -0700 From: Eric Blake MIME-Version: 1.0 References: <1391087394-17914-1-git-send-email-pbonzini@redhat.com> <1391087394-17914-8-git-send-email-pbonzini@redhat.com> In-Reply-To: <1391087394-17914-8-git-send-email-pbonzini@redhat.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="3aAHd17Fu6S2ruLP5LdQuEuSBPkamfrX7" Subject: Re: [Qemu-devel] [PATCH 07/12] qapi: add human mode to StringOutputVisitor List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini , qemu-devel@nongnu.org Cc: armbru@redhat.com, afaerber@suse.de This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --3aAHd17Fu6S2ruLP5LdQuEuSBPkamfrX7 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 01/30/2014 06:09 AM, Paolo Bonzini wrote: > This will be used by "info qtree". For numbers it prints both the > decimal and hex values. For sizes it rounds to the nearest power > of 2^10. For strings, it puts quotes around the string and separates > NULL and empty string. Nice idea! But needs a v2. >=20 > Signed-off-by: Paolo Bonzini > --- > include/qapi/string-output-visitor.h | 2 +- > include/qom/object.h | 3 +- > qapi/string-output-visitor.c | 55 ++++++++++++++++++++++++++++= ++++++-- > qdev-monitor.c | 2 +- > qom/object.c | 4 +-- > tests/test-string-output-visitor.c | 2 +- > tests/test-visitor-serialization.c | 2 +- > 7 files changed, 60 insertions(+), 10 deletions(-) >=20 > +static void print_type_size(Visitor *v, uint64_t *obj, const char *nam= e, > + Error **errp) > +{ > + StringOutputVisitor *sov =3D DO_UPCAST(StringOutputVisitor, visito= r, v); > + static const char suffixes[] =3D { 'B', 'K', 'M', 'G', 'T' }; Any reason you don't include 'P' and 'E' at the end of this array? We parse them just fine (at least in some contexts, going by the fact that qemu-common.h has a STRTOSZ_DEFSUFFIX_ value for them), so we also ought to output them. > + uint64_t div, val; > + char *out; > + int i; > + > + if (!sov->human) { > + out =3D g_strdup_printf("%llu", (long long) *obj); (unsigned long long) would look better for the cast. > + string_output_set(sov, out); > + return; > + } > + > + val =3D *obj; > + > + /* Compute floor(log2(val)). */ > + i =3D 64 - clz64(val); > + > + /* Find the power of 1024 that we'll display as the units. */ > + i /=3D 10; > + if (i >=3D ARRAY_SIZE(suffixes)) { > + i =3D ARRAY_SIZE(suffixes) - 1; > + } > + div =3D 1ULL << (i * 10); > + > + out =3D g_strdup_printf("%0.03f%c", (double)val/div, suffixes[i]);= If val < 1024, this gives dumb output: 1.000B. Should you special case bytes? Also, I like how your int printout was both decimal and hex; but here you are throwing away information (and the bigger the number, the more we lose as to how much got rounded away). I'd rather see this as: "%0.03f%c (%llu)" so that we also have access to the unrounded exact amount. --=20 Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org --3aAHd17Fu6S2ruLP5LdQuEuSBPkamfrX7 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 Comment: Public key at http://people.redhat.com/eblake/eblake.gpg Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBCAAGBQJS6l03AAoJEKeha0olJ0Nqbx0H/1fW8Xdo4OojTD8YuIj4X/g/ K/7L39gLo55WFCCf5OM1hMBIpkc/TLQhOabiITjwFyIeKKgL+QsFAePXtP4TrD2U Ib2UuitWfJdpjeKTVGi+oISjuScvRkmjNM+KNaQ5gSabwy74o+ee4mwFS9/7+ZZG Uzh5zKh9vL10VfLL0PgqsZVTTs1Hp+EtkmWd2PRpNfYEPHq+IayhTZSX6ds3kXlS 3TV/nscw00ur3ySgWwrOmC8fuZrXUc1qG8A2HGEKjL0l4we9uQ95y1loqacMf0SM x+5Z70QxsWxOy/Xhki8FdhUJmP0tRYxm4aExo0doq1VOrpUXNJddS3bR2AMWuRs= =SqEb -----END PGP SIGNATURE----- --3aAHd17Fu6S2ruLP5LdQuEuSBPkamfrX7--