diff mbox

[v2,13/30] xen: introduce a new format specifier to print sizes in human-readable form

Message ID 1474991845-27962-14-git-send-email-roger.pau@citrix.com (mailing list archive)
State New, archived
Headers show

Commit Message

Roger Pau Monné Sept. 27, 2016, 3:57 p.m. UTC
Introduce a new %pB format specifier to print sizes (in bytes) in a
human-readable form.

Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
---
Cc: Andrew Cooper <andrew.cooper3@citrix.com>
Cc: George Dunlap <George.Dunlap@eu.citrix.com>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Jan Beulich <jbeulich@suse.com>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Stefano Stabellini <sstabellini@kernel.org>
Cc: Tim Deegan <tim@xen.org>
Cc: Wei Liu <wei.liu2@citrix.com>
---
 docs/misc/printk-formats.txt |  5 +++++
 xen/common/vsprintf.c        | 15 +++++++++++++++
 2 files changed, 20 insertions(+)

Comments

Jürgen Groß Sept. 28, 2016, 8:24 a.m. UTC | #1
On 27/09/16 17:57, Roger Pau Monne wrote:
> Introduce a new %pB format specifier to print sizes (in bytes) in a

Code suggests it is pZ.

> human-readable form.
> 
> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> ---
> Cc: Andrew Cooper <andrew.cooper3@citrix.com>
> Cc: George Dunlap <George.Dunlap@eu.citrix.com>
> Cc: Ian Jackson <ian.jackson@eu.citrix.com>
> Cc: Jan Beulich <jbeulich@suse.com>
> Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
> Cc: Stefano Stabellini <sstabellini@kernel.org>
> Cc: Tim Deegan <tim@xen.org>
> Cc: Wei Liu <wei.liu2@citrix.com>
> ---
>  docs/misc/printk-formats.txt |  5 +++++
>  xen/common/vsprintf.c        | 15 +++++++++++++++
>  2 files changed, 20 insertions(+)
> 
> diff --git a/docs/misc/printk-formats.txt b/docs/misc/printk-formats.txt
> index 525108f..0ee3504 100644
> --- a/docs/misc/printk-formats.txt
> +++ b/docs/misc/printk-formats.txt
> @@ -30,3 +30,8 @@ Domain and vCPU information:
>  
>         %pv     Domain and vCPU ID from a 'struct vcpu *' (printed as
>                 "d<domid>v<vcpuid>")
> +
> +Size in human readable form:
> +
> +       %pZ     Size in human-readable form (input size must be in bytes).
> +                 e.g.  24MB
> diff --git a/xen/common/vsprintf.c b/xen/common/vsprintf.c
> index f92fb67..2dadaec 100644
> --- a/xen/common/vsprintf.c
> +++ b/xen/common/vsprintf.c
> @@ -386,6 +386,21 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
>              *str = 'v';
>          return number(str + 1, end, v->vcpu_id, 10, -1, -1, 0);
>      }
> +    case 'Z':
> +    {
> +        static const char units[][3] = {"B", "KB", "MB", "GB", "TB"};
> +        size_t size = (size_t)arg;
> +        int i = 0;
> +
> +        /* Advance parents fmt string, as we have consumed 'B' */
> +        ++*fmt_ptr;
> +
> +        while ( ++i < sizeof(units) && size >= 1024 )

sizeof(units) is 15. That's not what you want to check here.


Juergen

> +            size >>= 10; /* size /= 1024 */
> +
> +        str = number(str, end, size, 10, -1, -1, 0);
> +        return string(str, end, units[i-1], -1, -1, 0);
> +    }
>      }
>  
>      if ( field_width == -1 )
>
Roger Pau Monné Sept. 28, 2016, 11:56 a.m. UTC | #2
On Wed, Sep 28, 2016 at 10:24:07AM +0200, Juergen Gross wrote:
> On 27/09/16 17:57, Roger Pau Monne wrote:
> > Introduce a new %pB format specifier to print sizes (in bytes) in a
> 
> Code suggests it is pZ.

Right, first implementation used pB, but then I realized this was already 
used by Linux to print something else.
 
> > human-readable form.
> > 
> > Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
> > ---
> > Cc: Andrew Cooper <andrew.cooper3@citrix.com>
> > Cc: George Dunlap <George.Dunlap@eu.citrix.com>
> > Cc: Ian Jackson <ian.jackson@eu.citrix.com>
> > Cc: Jan Beulich <jbeulich@suse.com>
> > Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
> > Cc: Stefano Stabellini <sstabellini@kernel.org>
> > Cc: Tim Deegan <tim@xen.org>
> > Cc: Wei Liu <wei.liu2@citrix.com>
> > ---
> >  docs/misc/printk-formats.txt |  5 +++++
> >  xen/common/vsprintf.c        | 15 +++++++++++++++
> >  2 files changed, 20 insertions(+)
> > 
> > diff --git a/docs/misc/printk-formats.txt b/docs/misc/printk-formats.txt
> > index 525108f..0ee3504 100644
> > --- a/docs/misc/printk-formats.txt
> > +++ b/docs/misc/printk-formats.txt
> > @@ -30,3 +30,8 @@ Domain and vCPU information:
> >  
> >         %pv     Domain and vCPU ID from a 'struct vcpu *' (printed as
> >                 "d<domid>v<vcpuid>")
> > +
> > +Size in human readable form:
> > +
> > +       %pZ     Size in human-readable form (input size must be in bytes).
> > +                 e.g.  24MB
> > diff --git a/xen/common/vsprintf.c b/xen/common/vsprintf.c
> > index f92fb67..2dadaec 100644
> > --- a/xen/common/vsprintf.c
> > +++ b/xen/common/vsprintf.c
> > @@ -386,6 +386,21 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
> >              *str = 'v';
> >          return number(str + 1, end, v->vcpu_id, 10, -1, -1, 0);
> >      }
> > +    case 'Z':
> > +    {
> > +        static const char units[][3] = {"B", "KB", "MB", "GB", "TB"};
> > +        size_t size = (size_t)arg;
> > +        int i = 0;
> > +
> > +        /* Advance parents fmt string, as we have consumed 'B' */
> > +        ++*fmt_ptr;
> > +
> > +        while ( ++i < sizeof(units) && size >= 1024 )
> 
> sizeof(units) is 15. That's not what you want to check here.

Indeed, it's ARRAY_SIZE, thanks for noticing!

Roger.
Andrew Cooper Sept. 28, 2016, 12:01 p.m. UTC | #3
On 28/09/16 12:56, Roger Pau Monne wrote:
> On Wed, Sep 28, 2016 at 10:24:07AM +0200, Juergen Gross wrote:
>> On 27/09/16 17:57, Roger Pau Monne wrote:
>>> Introduce a new %pB format specifier to print sizes (in bytes) in a
>> Code suggests it is pZ.
> Right, first implementation used pB, but then I realized this was already 
> used by Linux to print something else.
>  
>>> diff --git a/xen/common/vsprintf.c b/xen/common/vsprintf.c
>>> index f92fb67..2dadaec 100644
>>> --- a/xen/common/vsprintf.c
>>> +++ b/xen/common/vsprintf.c
>>> @@ -386,6 +386,21 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
>>>              *str = 'v';
>>>          return number(str + 1, end, v->vcpu_id, 10, -1, -1, 0);
>>>      }
>>> +    case 'Z':
>>> +    {
>>> +        static const char units[][3] = {"B", "KB", "MB", "GB", "TB"};
>>> +        size_t size = (size_t)arg;
>>> +        int i = 0;
>>> +
>>> +        /* Advance parents fmt string, as we have consumed 'B' */

And this comment.

~Andrew
Paul Durrant Oct. 3, 2016, 8:36 a.m. UTC | #4
> -----Original Message-----

> From: Xen-devel [mailto:xen-devel-bounces@lists.xen.org] On Behalf Of

> Roger Pau Monne

> Sent: 27 September 2016 16:57

> To: xen-devel@lists.xenproject.org

> Cc: Stefano Stabellini <sstabellini@kernel.org>; Wei Liu

> <wei.liu2@citrix.com>; George Dunlap <George.Dunlap@citrix.com>;

> Andrew Cooper <Andrew.Cooper3@citrix.com>; Ian Jackson

> <Ian.Jackson@citrix.com>; Tim (Xen.org) <tim@xen.org>; Jan Beulich

> <jbeulich@suse.com>; boris.ostrovsky@oracle.com; Roger Pau Monne

> <roger.pau@citrix.com>

> Subject: [Xen-devel] [PATCH v2 13/30] xen: introduce a new format specifier

> to print sizes in human-readable form

> 

> Introduce a new %pB format specifier to print sizes (in bytes) in a human-

> readable form.

> 


This comment does not seem to match the code. You use 'Z' below...

  Paul

> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>

> ---

> Cc: Andrew Cooper <andrew.cooper3@citrix.com>

> Cc: George Dunlap <George.Dunlap@eu.citrix.com>

> Cc: Ian Jackson <ian.jackson@eu.citrix.com>

> Cc: Jan Beulich <jbeulich@suse.com>

> Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>

> Cc: Stefano Stabellini <sstabellini@kernel.org>

> Cc: Tim Deegan <tim@xen.org>

> Cc: Wei Liu <wei.liu2@citrix.com>

> ---

>  docs/misc/printk-formats.txt |  5 +++++

>  xen/common/vsprintf.c        | 15 +++++++++++++++

>  2 files changed, 20 insertions(+)

> 

> diff --git a/docs/misc/printk-formats.txt b/docs/misc/printk-formats.txt

> index 525108f..0ee3504 100644

> --- a/docs/misc/printk-formats.txt

> +++ b/docs/misc/printk-formats.txt

> @@ -30,3 +30,8 @@ Domain and vCPU information:

> 

>         %pv     Domain and vCPU ID from a 'struct vcpu *' (printed as

>                 "d<domid>v<vcpuid>")

> +

> +Size in human readable form:

> +

> +       %pZ     Size in human-readable form (input size must be in bytes).

> +                 e.g.  24MB

> diff --git a/xen/common/vsprintf.c b/xen/common/vsprintf.c index

> f92fb67..2dadaec 100644

> --- a/xen/common/vsprintf.c

> +++ b/xen/common/vsprintf.c

> @@ -386,6 +386,21 @@ static char *pointer(char *str, char *end, const char

> **fmt_ptr,

>              *str = 'v';

>          return number(str + 1, end, v->vcpu_id, 10, -1, -1, 0);

>      }

> +    case 'Z':

> +    {

> +        static const char units[][3] = {"B", "KB", "MB", "GB", "TB"};

> +        size_t size = (size_t)arg;

> +        int i = 0;

> +

> +        /* Advance parents fmt string, as we have consumed 'B' */

> +        ++*fmt_ptr;

> +

> +        while ( ++i < sizeof(units) && size >= 1024 )

> +            size >>= 10; /* size /= 1024 */

> +

> +        str = number(str, end, size, 10, -1, -1, 0);

> +        return string(str, end, units[i-1], -1, -1, 0);

> +    }

>      }

> 

>      if ( field_width == -1 )

> --

> 2.7.4 (Apple Git-66)

> 

> 

> _______________________________________________

> Xen-devel mailing list

> Xen-devel@lists.xen.org

> https://lists.xen.org/xen-devel
Roger Pau Monné Oct. 11, 2016, 10:27 a.m. UTC | #5
On Tue, Sep 27, 2016 at 05:57:08PM +0200, Roger Pau Monne wrote:
> Introduce a new %pB format specifier to print sizes (in bytes) in a
> human-readable form.

Since this was intended to be used to print the sizes of the memory 
allocations, and it has been requested to remove this information, I will 
also remove this patch. If someone is later interested in using this, feel 
free to pick up this patch.

Roger.
diff mbox

Patch

diff --git a/docs/misc/printk-formats.txt b/docs/misc/printk-formats.txt
index 525108f..0ee3504 100644
--- a/docs/misc/printk-formats.txt
+++ b/docs/misc/printk-formats.txt
@@ -30,3 +30,8 @@  Domain and vCPU information:
 
        %pv     Domain and vCPU ID from a 'struct vcpu *' (printed as
                "d<domid>v<vcpuid>")
+
+Size in human readable form:
+
+       %pZ     Size in human-readable form (input size must be in bytes).
+                 e.g.  24MB
diff --git a/xen/common/vsprintf.c b/xen/common/vsprintf.c
index f92fb67..2dadaec 100644
--- a/xen/common/vsprintf.c
+++ b/xen/common/vsprintf.c
@@ -386,6 +386,21 @@  static char *pointer(char *str, char *end, const char **fmt_ptr,
             *str = 'v';
         return number(str + 1, end, v->vcpu_id, 10, -1, -1, 0);
     }
+    case 'Z':
+    {
+        static const char units[][3] = {"B", "KB", "MB", "GB", "TB"};
+        size_t size = (size_t)arg;
+        int i = 0;
+
+        /* Advance parents fmt string, as we have consumed 'B' */
+        ++*fmt_ptr;
+
+        while ( ++i < sizeof(units) && size >= 1024 )
+            size >>= 10; /* size /= 1024 */
+
+        str = number(str, end, size, 10, -1, -1, 0);
+        return string(str, end, units[i-1], -1, -1, 0);
+    }
     }
 
     if ( field_width == -1 )