From de4ac2c223ab74efa66c0bec8bf4b7d2e846d242 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Wed, 8 Jul 2009 11:49:17 +0200
Subject: [PATCH] hspace: move instance count and score into CStats

Currently the instance count and cluster score are separated from the
other initial/final phase stats, even though they are very similar. This
patch moves computation of these two into totalResources/CStats and
removes special printing in hspace.
---
 Ganeti/HTools/Cluster.hs | 45 +++++++++++++++++++++++-----------------
 hspace.hs                | 21 ++++++++++++-------
 2 files changed, 39 insertions(+), 27 deletions(-)

diff --git a/Ganeti/HTools/Cluster.hs b/Ganeti/HTools/Cluster.hs
index 8d633bc5f..b1970b84d 100644
--- a/Ganeti/HTools/Cluster.hs
+++ b/Ganeti/HTools/Cluster.hs
@@ -106,6 +106,8 @@ data CStats = CStats { cs_fmem :: Int    -- ^ Cluster free mem
                      , cs_tcpu :: Double -- ^ Cluster total cpus
                      , cs_xmem :: Int    -- ^ Unnacounted for mem
                      , cs_nmem :: Int    -- ^ Node own memory
+                     , cs_score :: Score -- ^ The cluster score
+                     , cs_ninst :: Int   -- ^ The total number of instances
                      }
 
 -- * Utility functions
@@ -148,6 +150,8 @@ emptyCStats = CStats { cs_fmem = 0
                      , cs_tcpu = 0
                      , cs_xmem = 0
                      , cs_nmem = 0
+                     , cs_score = 0
+                     , cs_ninst = 0
                      }
 
 updateCStats :: CStats -> Node.Node -> CStats
@@ -157,7 +161,7 @@ updateCStats cs node =
                  cs_mmem = x_mmem, cs_mdsk = x_mdsk, cs_mcpu = x_mcpu,
                  cs_imem = x_imem, cs_idsk = x_idsk, cs_icpu = x_icpu,
                  cs_tmem = x_tmem, cs_tdsk = x_tdsk, cs_tcpu = x_tcpu,
-                 cs_xmem = x_xmem, cs_nmem = x_nmem
+                 cs_xmem = x_xmem, cs_nmem = x_nmem, cs_ninst = x_ninst
                }
             = cs
         inc_amem = Node.f_mem node - Node.r_mem node
@@ -168,27 +172,30 @@ updateCStats cs node =
         inc_icpu = Node.u_cpu node
         inc_idsk = truncate (Node.t_dsk node) - Node.f_dsk node
 
-    in CStats { cs_fmem = x_fmem + Node.f_mem node
-              , cs_fdsk = x_fdsk + Node.f_dsk node
-              , cs_amem = x_amem + inc_amem'
-              , cs_adsk = x_adsk + inc_adsk
-              , cs_acpu = x_acpu
-              , cs_mmem = max x_mmem inc_amem'
-              , cs_mdsk = max x_mdsk inc_adsk
-              , cs_mcpu = x_mcpu
-              , cs_imem = x_imem + inc_imem
-              , cs_idsk = x_idsk + inc_idsk
-              , cs_icpu = x_icpu + inc_icpu
-              , cs_tmem = x_tmem + Node.t_mem node
-              , cs_tdsk = x_tdsk + Node.t_dsk node
-              , cs_tcpu = x_tcpu + Node.t_cpu node
-              , cs_xmem = x_xmem + Node.x_mem node
-              , cs_nmem = x_nmem + Node.n_mem node
-              }
+    in cs { cs_fmem = x_fmem + Node.f_mem node
+          , cs_fdsk = x_fdsk + Node.f_dsk node
+          , cs_amem = x_amem + inc_amem'
+          , cs_adsk = x_adsk + inc_adsk
+          , cs_acpu = x_acpu
+          , cs_mmem = max x_mmem inc_amem'
+          , cs_mdsk = max x_mdsk inc_adsk
+          , cs_mcpu = x_mcpu
+          , cs_imem = x_imem + inc_imem
+          , cs_idsk = x_idsk + inc_idsk
+          , cs_icpu = x_icpu + inc_icpu
+          , cs_tmem = x_tmem + Node.t_mem node
+          , cs_tdsk = x_tdsk + Node.t_dsk node
+          , cs_tcpu = x_tcpu + Node.t_cpu node
+          , cs_xmem = x_xmem + Node.x_mem node
+          , cs_nmem = x_nmem + Node.n_mem node
+          , cs_ninst = x_ninst + length (Node.plist node)
+          }
 
 -- | Compute the total free disk and memory in the cluster.
 totalResources :: Node.List -> CStats
-totalResources = foldl' updateCStats emptyCStats . Container.elems
+totalResources nl =
+    let cs = foldl' updateCStats emptyCStats . Container.elems $ nl
+    in cs { cs_score = compCV nl }
 
 -- | Compute the mem and disk covariance.
 compDetailedCV :: Node.List -> (Double, Double, Double, Double, Double, Double)
diff --git a/hspace.hs b/hspace.hs
index 04ef1a1e2..4b1417894 100644
--- a/hspace.hs
+++ b/hspace.hs
@@ -205,6 +205,8 @@ iterateDepth nl il newinst nreq ixes =
 -- | Function to print stats for a given phase
 printStats :: String -> Cluster.CStats -> IO ()
 printStats kind cs = do
+  printf "%s score: %.8f\n" kind (Cluster.cs_score cs)
+  printf "%s instances: %d\n" kind (Cluster.cs_ninst cs)
   printf "%s free RAM: %d\n" kind (Cluster.cs_fmem cs)
   printf "%s allocatable RAM: %d\n" kind (Cluster.cs_amem cs)
   printf "%s reserved RAM: %d\n" kind (Cluster.cs_fmem cs -
@@ -232,8 +234,13 @@ printResults fin_nl num_instances allocs sreason = do
   let fin_stats = Cluster.totalResources fin_nl
       fin_instances = num_instances + allocs
 
-  printf "Final score: %.8f\n" (Cluster.compCV fin_nl)
-  printf "Final instances: %d\n" (num_instances + allocs)
+  when (num_instances + allocs /= Cluster.cs_ninst fin_stats) $
+       do
+         hPrintf stderr "ERROR: internal inconsistency, allocated (%d)\
+                        \ != counted (%d)\n" (num_instances + allocs)
+                                 (Cluster.cs_ninst fin_stats)
+         exitWith $ ExitFailure 1
+
   printStats "Final" fin_stats
   printf "Usage: %.5f\n" ((fromIntegral num_instances::Double) /
                           fromIntegral fin_instances)
@@ -299,15 +306,13 @@ main = do
   let ini_cv = Cluster.compCV nl
       ini_stats = Cluster.totalResources nl
 
-  (if verbose > 2 then
-       printf "Initial coefficients: overall %.8f, %s\n"
-       ini_cv (Cluster.printStats nl)
-   else
-       printf "Initial score: %.8f\n" ini_cv)
+  when (verbose > 2) $ do
+         printf "Initial coefficients: overall %.8f, %s\n"
+                ini_cv (Cluster.printStats nl)
+
   printf "Cluster RAM: %.0f\n" (Cluster.cs_tmem ini_stats)
   printf "Cluster disk: %.0f\n" (Cluster.cs_tdsk ini_stats)
   printf "Cluster cpus: %.0f\n" (Cluster.cs_tcpu ini_stats)
-  printf "Initial instances: %d\n" num_instances
   printStats "Initial" ini_stats
 
   let bad_nodes = fst $ Cluster.computeBadItems nl il
-- 
GitLab