]> xenbits.xensource.com Git - libvirt.git/commitdiff
conf: Format cache banks in capabilities with virFormatIntPretty
authorMartin Kletzander <mkletzan@redhat.com>
Fri, 10 Nov 2017 12:19:53 +0000 (13:19 +0100)
committerMartin Kletzander <mkletzan@redhat.com>
Sat, 18 Nov 2017 09:45:10 +0000 (10:45 +0100)
Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
Reviewed-by: John Ferlan <jferlan@redhat.com>
src/conf/capabilities.c
tests/vircaps2xmldata/vircaps-x86_64-caches.xml
tests/vircaps2xmldata/vircaps-x86_64-resctrl-cdp.xml
tests/vircaps2xmldata/vircaps-x86_64-resctrl-skx.xml
tests/vircaps2xmldata/vircaps-x86_64-resctrl.xml

index 1f7d8cdb31475dc4515a049b7385fe788c15ad72..798c9bdaeaf3e485f76467492d9fe5b124560e0f 100644 (file)
@@ -883,7 +883,8 @@ virCapabilitiesFormatCaches(virBufferPtr buf,
     for (i = 0; i < ncaches; i++) {
         virCapsHostCacheBankPtr bank = caches[i];
         char *cpus_str = virBitmapFormat(bank->cpus);
-        bool kilos = !(bank->size % 1024);
+        const char *unit = NULL;
+        unsigned long long short_size = virFormatIntPretty(bank->size, &unit);
 
         if (!cpus_str)
             return -1;
@@ -897,34 +898,47 @@ virCapabilitiesFormatCaches(virBufferPtr buf,
                           "size='%llu' unit='%s' cpus='%s'",
                           bank->id, bank->level,
                           virCacheTypeToString(bank->type),
-                          bank->size >> (kilos * 10),
-                          kilos ? "KiB" : "B",
-                          cpus_str);
+                          short_size, unit, cpus_str);
         VIR_FREE(cpus_str);
 
         virBufferSetChildIndent(&controlBuf, buf);
         for (j = 0; j < bank->ncontrols; j++) {
-            bool min_kilos = !(bank->controls[j]->granularity % 1024);
-
-            /* Only use KiB if both values are divisible */
-            if (bank->controls[j]->min)
-                min_kilos = min_kilos && !(bank->controls[j]->min % 1024);
+            const char *min_unit;
+            virResctrlInfoPtr controls = bank->controls[j];
+            unsigned long long gran_short_size = controls->granularity;
+            unsigned long long min_short_size = controls->min;
+
+            gran_short_size = virFormatIntPretty(gran_short_size, &unit);
+            min_short_size = virFormatIntPretty(min_short_size, &min_unit);
+
+            /* Only use the smaller unit if they are different */
+            if (min_short_size) {
+                unsigned long long gran_div;
+                unsigned long long min_div;
+
+                gran_div = controls->granularity / gran_short_size;
+                min_div = controls->min / min_short_size;
+
+                if (min_div > gran_div) {
+                    min_short_size *= min_div / gran_div;
+                } else if (min_div < gran_div) {
+                    unit = min_unit;
+                    gran_short_size *= gran_div / min_div;
+                }
+            }
 
             virBufferAsprintf(&controlBuf,
                               "<control granularity='%llu'",
-                              bank->controls[j]->granularity >> (min_kilos * 10));
+                              gran_short_size);
 
-            if (bank->controls[j]->min) {
-                virBufferAsprintf(&controlBuf,
-                                  " min='%llu'",
-                                  bank->controls[j]->min >> (min_kilos * 10));
-            }
+            if (min_short_size)
+                virBufferAsprintf(&controlBuf, " min='%llu'", min_short_size);
 
             virBufferAsprintf(&controlBuf,
                               " unit='%s' type='%s' maxAllocs='%u'/>\n",
-                              min_kilos ? "KiB" : "B",
-                              virCacheTypeToString(bank->controls[j]->scope),
-                              bank->controls[j]->max_allocation);
+                              unit,
+                              virCacheTypeToString(controls->scope),
+                              controls->max_allocation);
         }
 
         if (virBufferCheckError(&controlBuf) < 0)
index fe0be6d08fa7cdcf66ccef830560a4825dbb2ed4..0c6f3769a2a761a8c95accd4a762bb702f4d47f7 100644 (file)
@@ -29,7 +29,7 @@
       </cells>
     </topology>
     <cache>
-      <bank id='0' level='3' type='both' size='8192' unit='KiB' cpus='0-7'/>
+      <bank id='0' level='3' type='both' size='8' unit='MiB' cpus='0-7'/>
     </cache>
   </host>
 
index 7361537bfb56307d2d5fb8975225808c8f27e9bd..443917c62d69f0f1fe2f2ed91e8b941306406ea1 100644 (file)
       </cells>
     </topology>
     <cache>
-      <bank id='0' level='3' type='both' size='15360' unit='KiB' cpus='0-5'>
+      <bank id='0' level='3' type='both' size='15' unit='MiB' cpus='0-5'>
         <control granularity='768' unit='KiB' type='code' maxAllocs='8'/>
         <control granularity='768' unit='KiB' type='data' maxAllocs='8'/>
       </bank>
-      <bank id='1' level='3' type='both' size='15360' unit='KiB' cpus='6-11'>
+      <bank id='1' level='3' type='both' size='15' unit='MiB' cpus='6-11'>
         <control granularity='768' unit='KiB' type='code' maxAllocs='8'/>
         <control granularity='768' unit='KiB' type='data' maxAllocs='8'/>
       </bank>
index 4e91c87de3b10534a82137204321b081f8c85994..0cd25e59a9e0a5cd72503453fc73617fcb94c575 100644 (file)
@@ -22,8 +22,8 @@
       </cells>
     </topology>
     <cache>
-      <bank id='0' level='3' type='both' size='33792' unit='KiB' cpus='0'>
-        <control granularity='3072' unit='KiB' type='both' maxAllocs='16'/>
+      <bank id='0' level='3' type='both' size='33' unit='MiB' cpus='0'>
+        <control granularity='3' unit='MiB' type='both' maxAllocs='16'/>
       </bank>
     </cache>
   </host>
index eb02ad3322a26d39249e55a758984daa1e2078b4..7629259294d69b66f383201bd7da7895aed495d9 100644 (file)
       </cells>
     </topology>
     <cache>
-      <bank id='0' level='3' type='both' size='15360' unit='KiB' cpus='0-5'>
+      <bank id='0' level='3' type='both' size='15' unit='MiB' cpus='0-5'>
         <control granularity='768' min='1536' unit='KiB' type='both' maxAllocs='4'/>
       </bank>
-      <bank id='1' level='3' type='both' size='15360' unit='KiB' cpus='6-11'>
+      <bank id='1' level='3' type='both' size='15' unit='MiB' cpus='6-11'>
         <control granularity='768' min='1536' unit='KiB' type='both' maxAllocs='4'/>
       </bank>
     </cache>