Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Program / Hspace.hs @ 01e52493

History | View | Annotate | Download (16.6 kB)

1 e10be8f2 Iustin Pop
{-| Cluster space sizing
2 e10be8f2 Iustin Pop
3 e10be8f2 Iustin Pop
-}
4 e10be8f2 Iustin Pop
5 e10be8f2 Iustin Pop
{-
6 e10be8f2 Iustin Pop
7 1f5635a9 Iustin Pop
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
8 e10be8f2 Iustin Pop
9 e10be8f2 Iustin Pop
This program is free software; you can redistribute it and/or modify
10 e10be8f2 Iustin Pop
it under the terms of the GNU General Public License as published by
11 e10be8f2 Iustin Pop
the Free Software Foundation; either version 2 of the License, or
12 e10be8f2 Iustin Pop
(at your option) any later version.
13 e10be8f2 Iustin Pop
14 e10be8f2 Iustin Pop
This program is distributed in the hope that it will be useful, but
15 e10be8f2 Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
16 e10be8f2 Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 e10be8f2 Iustin Pop
General Public License for more details.
18 e10be8f2 Iustin Pop
19 e10be8f2 Iustin Pop
You should have received a copy of the GNU General Public License
20 e10be8f2 Iustin Pop
along with this program; if not, write to the Free Software
21 e10be8f2 Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 e10be8f2 Iustin Pop
02110-1301, USA.
23 e10be8f2 Iustin Pop
24 e10be8f2 Iustin Pop
-}
25 e10be8f2 Iustin Pop
26 22278fa7 Iustin Pop
module Ganeti.HTools.Program.Hspace
27 22278fa7 Iustin Pop
  (main
28 22278fa7 Iustin Pop
  , options
29 22278fa7 Iustin Pop
  , arguments
30 22278fa7 Iustin Pop
  ) where
31 e10be8f2 Iustin Pop
32 cc532bdd Iustin Pop
import Control.Monad
33 79eef90b Agata Murawska
import Data.Char (toUpper, toLower)
34 756df409 Iustin Pop
import Data.Function (on)
35 e10be8f2 Iustin Pop
import Data.List
36 be468da0 Iustin Pop
import Data.Maybe (fromMaybe)
37 5182e970 Iustin Pop
import Data.Ord (comparing)
38 e10be8f2 Iustin Pop
import System.IO
39 e10be8f2 Iustin Pop
40 2795466b Iustin Pop
import Text.Printf (printf, hPrintf)
41 e10be8f2 Iustin Pop
42 e10be8f2 Iustin Pop
import qualified Ganeti.HTools.Container as Container
43 e10be8f2 Iustin Pop
import qualified Ganeti.HTools.Cluster as Cluster
44 e10be8f2 Iustin Pop
import qualified Ganeti.HTools.Node as Node
45 e10be8f2 Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
46 e10be8f2 Iustin Pop
47 01e52493 Iustin Pop
import Ganeti.BasicTypes
48 22278fa7 Iustin Pop
import Ganeti.Common
49 f2280553 Iustin Pop
import Ganeti.HTools.Types
50 0427285d Iustin Pop
import Ganeti.HTools.CLI
51 e8f89bb6 Iustin Pop
import Ganeti.HTools.ExtLoader
52 4938fa30 Guido Trotter
import Ganeti.HTools.Loader
53 26d62e4c Iustin Pop
import Ganeti.Utils
54 e10be8f2 Iustin Pop
55 179c0828 Iustin Pop
-- | Options list and functions.
56 0427285d Iustin Pop
options :: [OptType]
57 e10be8f2 Iustin Pop
options =
58 3c3690aa Iustin Pop
  [ oPrintNodes
59 3c3690aa Iustin Pop
  , oDataFile
60 3c3690aa Iustin Pop
  , oDiskTemplate
61 f0753837 René Nussbaumer
  , oSpindleUse
62 3c3690aa Iustin Pop
  , oNodeSim
63 3c3690aa Iustin Pop
  , oRapiMaster
64 3c3690aa Iustin Pop
  , oLuxiSocket
65 0547d608 René Nussbaumer
  , oIAllocSrc
66 3c3690aa Iustin Pop
  , oVerbose
67 3c3690aa Iustin Pop
  , oQuiet
68 3c3690aa Iustin Pop
  , oOfflineNode
69 3c3690aa Iustin Pop
  , oMachineReadable
70 3c3690aa Iustin Pop
  , oMaxCpu
71 3c3690aa Iustin Pop
  , oMaxSolLength
72 3c3690aa Iustin Pop
  , oMinDisk
73 294bb337 Iustin Pop
  , oStdSpec
74 3c3690aa Iustin Pop
  , oTieredSpec
75 3c3690aa Iustin Pop
  , oSaveCluster
76 3c3690aa Iustin Pop
  ]
77 e10be8f2 Iustin Pop
78 22278fa7 Iustin Pop
-- | The list of arguments supported by the program.
79 22278fa7 Iustin Pop
arguments :: [ArgCompletion]
80 22278fa7 Iustin Pop
arguments = []
81 22278fa7 Iustin Pop
82 fcebc9db Iustin Pop
-- | The allocation phase we're in (initial, after tiered allocs, or
83 fcebc9db Iustin Pop
-- after regular allocation).
84 fcebc9db Iustin Pop
data Phase = PInitial
85 fcebc9db Iustin Pop
           | PFinal
86 fcebc9db Iustin Pop
           | PTiered
87 2bbf77cc Iustin Pop
88 375969eb Iustin Pop
-- | The kind of instance spec we print.
89 375969eb Iustin Pop
data SpecType = SpecNormal
90 375969eb Iustin Pop
              | SpecTiered
91 375969eb Iustin Pop
92 79eef90b Agata Murawska
-- | Prefix for machine readable names
93 79eef90b Agata Murawska
htsPrefix :: String
94 79eef90b Agata Murawska
htsPrefix = "HTS"
95 79eef90b Agata Murawska
96 375969eb Iustin Pop
-- | What we prefix a spec with.
97 375969eb Iustin Pop
specPrefix :: SpecType -> String
98 375969eb Iustin Pop
specPrefix SpecNormal = "SPEC"
99 375969eb Iustin Pop
specPrefix SpecTiered = "TSPEC_INI"
100 375969eb Iustin Pop
101 375969eb Iustin Pop
-- | The description of a spec.
102 375969eb Iustin Pop
specDescription :: SpecType -> String
103 e86f7f65 Iustin Pop
specDescription SpecNormal = "Standard (fixed-size)"
104 375969eb Iustin Pop
specDescription SpecTiered = "Tiered (initial size)"
105 375969eb Iustin Pop
106 375969eb Iustin Pop
-- | Efficiency generic function.
107 375969eb Iustin Pop
effFn :: (Cluster.CStats -> Integer)
108 375969eb Iustin Pop
      -> (Cluster.CStats -> Double)
109 1b0a6356 Iustin Pop
      -> Cluster.CStats -> Double
110 375969eb Iustin Pop
effFn fi ft cs = fromIntegral (fi cs) / ft cs
111 375969eb Iustin Pop
112 375969eb Iustin Pop
-- | Memory efficiency.
113 375969eb Iustin Pop
memEff :: Cluster.CStats -> Double
114 375969eb Iustin Pop
memEff = effFn Cluster.csImem Cluster.csTmem
115 375969eb Iustin Pop
116 375969eb Iustin Pop
-- | Disk efficiency.
117 375969eb Iustin Pop
dskEff :: Cluster.CStats -> Double
118 375969eb Iustin Pop
dskEff = effFn Cluster.csIdsk Cluster.csTdsk
119 375969eb Iustin Pop
120 375969eb Iustin Pop
-- | Cpu efficiency.
121 375969eb Iustin Pop
cpuEff :: Cluster.CStats -> Double
122 375969eb Iustin Pop
cpuEff = effFn Cluster.csIcpu (fromIntegral . Cluster.csVcpu)
123 375969eb Iustin Pop
124 179c0828 Iustin Pop
-- | Holds data for converting a 'Cluster.CStats' structure into
125 179c0828 Iustin Pop
-- detailed statictics.
126 2bbf77cc Iustin Pop
statsData :: [(String, Cluster.CStats -> String)]
127 f5b553da Iustin Pop
statsData = [ ("SCORE", printf "%.8f" . Cluster.csScore)
128 f5b553da Iustin Pop
            , ("INST_CNT", printf "%d" . Cluster.csNinst)
129 f5b553da Iustin Pop
            , ("MEM_FREE", printf "%d" . Cluster.csFmem)
130 f5b553da Iustin Pop
            , ("MEM_AVAIL", printf "%d" . Cluster.csAmem)
131 2bbf77cc Iustin Pop
            , ("MEM_RESVD",
132 f5b553da Iustin Pop
               \cs -> printf "%d" (Cluster.csFmem cs - Cluster.csAmem cs))
133 f5b553da Iustin Pop
            , ("MEM_INST", printf "%d" . Cluster.csImem)
134 2bbf77cc Iustin Pop
            , ("MEM_OVERHEAD",
135 f5b553da Iustin Pop
               \cs -> printf "%d" (Cluster.csXmem cs + Cluster.csNmem cs))
136 375969eb Iustin Pop
            , ("MEM_EFF", printf "%.8f" . memEff)
137 f5b553da Iustin Pop
            , ("DSK_FREE", printf "%d" . Cluster.csFdsk)
138 9739b6b8 Iustin Pop
            , ("DSK_AVAIL", printf "%d". Cluster.csAdsk)
139 2bbf77cc Iustin Pop
            , ("DSK_RESVD",
140 f5b553da Iustin Pop
               \cs -> printf "%d" (Cluster.csFdsk cs - Cluster.csAdsk cs))
141 f5b553da Iustin Pop
            , ("DSK_INST", printf "%d" . Cluster.csIdsk)
142 375969eb Iustin Pop
            , ("DSK_EFF", printf "%.8f" . dskEff)
143 f5b553da Iustin Pop
            , ("CPU_INST", printf "%d" . Cluster.csIcpu)
144 375969eb Iustin Pop
            , ("CPU_EFF", printf "%.8f" . cpuEff)
145 f5b553da Iustin Pop
            , ("MNODE_MEM_AVAIL", printf "%d" . Cluster.csMmem)
146 f5b553da Iustin Pop
            , ("MNODE_DSK_AVAIL", printf "%d" . Cluster.csMdsk)
147 2bbf77cc Iustin Pop
            ]
148 2bbf77cc Iustin Pop
149 179c0828 Iustin Pop
-- | List holding 'RSpec' formatting information.
150 1f9066c0 Iustin Pop
specData :: [(String, RSpec -> String)]
151 1f9066c0 Iustin Pop
specData = [ ("MEM", printf "%d" . rspecMem)
152 1f9066c0 Iustin Pop
           , ("DSK", printf "%d" . rspecDsk)
153 1f9066c0 Iustin Pop
           , ("CPU", printf "%d" . rspecCpu)
154 2bbf77cc Iustin Pop
           ]
155 2bbf77cc Iustin Pop
156 179c0828 Iustin Pop
-- | List holding 'Cluster.CStats' formatting information.
157 2bbf77cc Iustin Pop
clusterData :: [(String, Cluster.CStats -> String)]
158 f5b553da Iustin Pop
clusterData = [ ("MEM", printf "%.0f" . Cluster.csTmem)
159 f5b553da Iustin Pop
              , ("DSK", printf "%.0f" . Cluster.csTdsk)
160 f5b553da Iustin Pop
              , ("CPU", printf "%.0f" . Cluster.csTcpu)
161 bd3286e9 Iustin Pop
              , ("VCPU", printf "%d" . Cluster.csVcpu)
162 2bbf77cc Iustin Pop
              ]
163 2bbf77cc Iustin Pop
164 179c0828 Iustin Pop
-- | Function to print stats for a given phase.
165 2bbf77cc Iustin Pop
printStats :: Phase -> Cluster.CStats -> [(String, String)]
166 2bbf77cc Iustin Pop
printStats ph cs =
167 2bbf77cc Iustin Pop
  map (\(s, fn) -> (printf "%s_%s" kind s, fn cs)) statsData
168 2bbf77cc Iustin Pop
  where kind = case ph of
169 2bbf77cc Iustin Pop
                 PInitial -> "INI"
170 2bbf77cc Iustin Pop
                 PFinal -> "FIN"
171 fcebc9db Iustin Pop
                 PTiered -> "TRL"
172 e10be8f2 Iustin Pop
173 2f9198be Iustin Pop
-- | Print failure reason and scores
174 2f9198be Iustin Pop
printFRScores :: Node.List -> Node.List -> [(FailMode, Int)] -> IO ()
175 2f9198be Iustin Pop
printFRScores ini_nl fin_nl sreason = do
176 2f9198be Iustin Pop
  printf "  - most likely failure reason: %s\n" $ failureReason sreason::IO ()
177 2f9198be Iustin Pop
  printClusterScores ini_nl fin_nl
178 2f9198be Iustin Pop
  printClusterEff (Cluster.totalResources fin_nl)
179 2f9198be Iustin Pop
180 375969eb Iustin Pop
-- | Print final stats and related metrics.
181 375969eb Iustin Pop
printResults :: Bool -> Node.List -> Node.List -> Int -> Int
182 375969eb Iustin Pop
             -> [(FailMode, Int)] -> IO ()
183 375969eb Iustin Pop
printResults True _ fin_nl num_instances allocs sreason = do
184 dca7f396 Iustin Pop
  let fin_stats = Cluster.totalResources fin_nl
185 dca7f396 Iustin Pop
      fin_instances = num_instances + allocs
186 dca7f396 Iustin Pop
187 88a10df5 Iustin Pop
  exitWhen (num_instances + allocs /= Cluster.csNinst fin_stats) $
188 88a10df5 Iustin Pop
           printf "internal inconsistency, allocated (%d)\
189 88a10df5 Iustin Pop
                  \ != counted (%d)\n" (num_instances + allocs)
190 88a10df5 Iustin Pop
           (Cluster.csNinst fin_stats)
191 de4ac2c2 Iustin Pop
192 79eef90b Agata Murawska
  printKeysHTS $ printStats PFinal fin_stats
193 79eef90b Agata Murawska
  printKeysHTS [ ("ALLOC_USAGE", printf "%.8f"
194 e60fa4af Agata Murawska
                                   ((fromIntegral num_instances::Double) /
195 e60fa4af Agata Murawska
                                   fromIntegral fin_instances))
196 e60fa4af Agata Murawska
               , ("ALLOC_INSTANCES", printf "%d" allocs)
197 e60fa4af Agata Murawska
               , ("ALLOC_FAIL_REASON", map toUpper . show . fst $ head sreason)
198 e60fa4af Agata Murawska
               ]
199 79eef90b Agata Murawska
  printKeysHTS $ map (\(x, y) -> (printf "ALLOC_%s_CNT" (show x),
200 e60fa4af Agata Murawska
                                  printf "%d" y)) sreason
201 375969eb Iustin Pop
202 375969eb Iustin Pop
printResults False ini_nl fin_nl _ allocs sreason = do
203 375969eb Iustin Pop
  putStrLn "Normal (fixed-size) allocation results:"
204 375969eb Iustin Pop
  printf "  - %3d instances allocated\n" allocs :: IO ()
205 2f9198be Iustin Pop
  printFRScores ini_nl fin_nl sreason
206 375969eb Iustin Pop
207 375969eb Iustin Pop
-- | Prints the final @OK@ marker in machine readable output.
208 79eef90b Agata Murawska
printFinalHTS :: Bool -> IO ()
209 79eef90b Agata Murawska
printFinalHTS = printFinal htsPrefix
210 375969eb Iustin Pop
211 756df409 Iustin Pop
-- | Compute the tiered spec counts from a list of allocated
212 756df409 Iustin Pop
-- instances.
213 756df409 Iustin Pop
tieredSpecMap :: [Instance.Instance]
214 756df409 Iustin Pop
              -> [(RSpec, Int)]
215 756df409 Iustin Pop
tieredSpecMap trl_ixes =
216 3c3690aa Iustin Pop
  let fin_trl_ixes = reverse trl_ixes
217 3c3690aa Iustin Pop
      ix_byspec = groupBy ((==) `on` Instance.specOf) fin_trl_ixes
218 3c3690aa Iustin Pop
      spec_map = map (\ixs -> (Instance.specOf $ head ixs, length ixs))
219 3c3690aa Iustin Pop
                 ix_byspec
220 3c3690aa Iustin Pop
  in spec_map
221 756df409 Iustin Pop
222 756df409 Iustin Pop
-- | Formats a spec map to strings.
223 756df409 Iustin Pop
formatSpecMap :: [(RSpec, Int)] -> [String]
224 756df409 Iustin Pop
formatSpecMap =
225 3c3690aa Iustin Pop
  map (\(spec, cnt) -> printf "%d,%d,%d=%d" (rspecMem spec)
226 3c3690aa Iustin Pop
                       (rspecDsk spec) (rspecCpu spec) cnt)
227 756df409 Iustin Pop
228 179c0828 Iustin Pop
-- | Formats \"key-metrics\" values.
229 80d7d8a1 Iustin Pop
formatRSpec :: String -> AllocInfo -> [(String, String)]
230 80d7d8a1 Iustin Pop
formatRSpec s r =
231 80d7d8a1 Iustin Pop
  [ ("KM_" ++ s ++ "_CPU", show $ allocInfoVCpus r)
232 80d7d8a1 Iustin Pop
  , ("KM_" ++ s ++ "_NPU", show $ allocInfoNCpus r)
233 80d7d8a1 Iustin Pop
  , ("KM_" ++ s ++ "_MEM", show $ allocInfoMem r)
234 80d7d8a1 Iustin Pop
  , ("KM_" ++ s ++ "_DSK", show $ allocInfoDisk r)
235 3c3690aa Iustin Pop
  ]
236 bd3286e9 Iustin Pop
237 179c0828 Iustin Pop
-- | Shows allocations stats.
238 5f3b040a Iustin Pop
printAllocationStats :: Node.List -> Node.List -> IO ()
239 5f3b040a Iustin Pop
printAllocationStats ini_nl fin_nl = do
240 bd3286e9 Iustin Pop
  let ini_stats = Cluster.totalResources ini_nl
241 bd3286e9 Iustin Pop
      fin_stats = Cluster.totalResources fin_nl
242 bd3286e9 Iustin Pop
      (rini, ralo, runa) = Cluster.computeAllocationDelta ini_stats fin_stats
243 79eef90b Agata Murawska
  printKeysHTS $ formatRSpec "USED" rini
244 79eef90b Agata Murawska
  printKeysHTS $ formatRSpec "POOL" ralo
245 79eef90b Agata Murawska
  printKeysHTS $ formatRSpec "UNAV" runa
246 9739b6b8 Iustin Pop
247 179c0828 Iustin Pop
-- | Format a list of key\/values as a shell fragment.
248 79eef90b Agata Murawska
printKeysHTS :: [(String, String)] -> IO ()
249 79eef90b Agata Murawska
printKeysHTS = printKeys htsPrefix
250 dca7f396 Iustin Pop
251 179c0828 Iustin Pop
-- | Converts instance data to a list of strings.
252 366a7c89 Iustin Pop
printInstance :: Node.List -> Instance.Instance -> [String]
253 366a7c89 Iustin Pop
printInstance nl i = [ Instance.name i
254 5182e970 Iustin Pop
                     , Container.nameOf nl $ Instance.pNode i
255 5182e970 Iustin Pop
                     , let sdx = Instance.sNode i
256 5182e970 Iustin Pop
                       in if sdx == Node.noSecondary then ""
257 5182e970 Iustin Pop
                          else Container.nameOf nl sdx
258 366a7c89 Iustin Pop
                     , show (Instance.mem i)
259 366a7c89 Iustin Pop
                     , show (Instance.dsk i)
260 366a7c89 Iustin Pop
                     , show (Instance.vcpus i)
261 366a7c89 Iustin Pop
                     ]
262 366a7c89 Iustin Pop
263 179c0828 Iustin Pop
-- | Optionally print the allocation map.
264 6eaa7bb8 Iustin Pop
printAllocationMap :: Int -> String
265 6eaa7bb8 Iustin Pop
                   -> Node.List -> [Instance.Instance] -> IO ()
266 6eaa7bb8 Iustin Pop
printAllocationMap verbose msg nl ixes =
267 6eaa7bb8 Iustin Pop
  when (verbose > 1) $ do
268 e86f7f65 Iustin Pop
    hPutStrLn stderr (msg ++ " map")
269 3603605a Iustin Pop
    hPutStr stderr . unlines . map ((:) ' ' .  unwords) $
270 6eaa7bb8 Iustin Pop
            formatTable (map (printInstance nl) (reverse ixes))
271 6eaa7bb8 Iustin Pop
                        -- This is the numberic-or-not field
272 6eaa7bb8 Iustin Pop
                        -- specification; the first three fields are
273 6eaa7bb8 Iustin Pop
                        -- strings, whereas the rest are numeric
274 6eaa7bb8 Iustin Pop
                       [False, False, False, True, True, True]
275 6eaa7bb8 Iustin Pop
276 375969eb Iustin Pop
-- | Formats nicely a list of resources.
277 1b0a6356 Iustin Pop
formatResources :: a -> [(String, a->String)] -> String
278 375969eb Iustin Pop
formatResources res =
279 375969eb Iustin Pop
    intercalate ", " . map (\(a, fn) -> a ++ " " ++ fn res)
280 375969eb Iustin Pop
281 375969eb Iustin Pop
-- | Print the cluster resources.
282 375969eb Iustin Pop
printCluster :: Bool -> Cluster.CStats -> Int -> IO ()
283 375969eb Iustin Pop
printCluster True ini_stats node_count = do
284 79eef90b Agata Murawska
  printKeysHTS $ map (\(a, fn) -> ("CLUSTER_" ++ a, fn ini_stats)) clusterData
285 79eef90b Agata Murawska
  printKeysHTS [("CLUSTER_NODES", printf "%d" node_count)]
286 79eef90b Agata Murawska
  printKeysHTS $ printStats PInitial ini_stats
287 375969eb Iustin Pop
288 375969eb Iustin Pop
printCluster False ini_stats node_count = do
289 375969eb Iustin Pop
  printf "The cluster has %d nodes and the following resources:\n  %s.\n"
290 375969eb Iustin Pop
         node_count (formatResources ini_stats clusterData)::IO ()
291 375969eb Iustin Pop
  printf "There are %s initial instances on the cluster.\n"
292 375969eb Iustin Pop
             (if inst_count > 0 then show inst_count else "no" )
293 375969eb Iustin Pop
      where inst_count = Cluster.csNinst ini_stats
294 375969eb Iustin Pop
295 375969eb Iustin Pop
-- | Prints the normal instance spec.
296 375969eb Iustin Pop
printISpec :: Bool -> RSpec -> SpecType -> DiskTemplate -> IO ()
297 375969eb Iustin Pop
printISpec True ispec spec disk_template = do
298 79eef90b Agata Murawska
  printKeysHTS $ map (\(a, fn) -> (prefix ++ "_" ++ a, fn ispec)) specData
299 79eef90b Agata Murawska
  printKeysHTS [ (prefix ++ "_RQN", printf "%d" req_nodes) ]
300 79eef90b Agata Murawska
  printKeysHTS [ (prefix ++ "_DISK_TEMPLATE",
301 e60fa4af Agata Murawska
                  diskTemplateToRaw disk_template) ]
302 375969eb Iustin Pop
      where req_nodes = Instance.requiredNodes disk_template
303 375969eb Iustin Pop
            prefix = specPrefix spec
304 375969eb Iustin Pop
305 1b0a6356 Iustin Pop
printISpec False ispec spec disk_template =
306 375969eb Iustin Pop
  printf "%s instance spec is:\n  %s, using disk\
307 375969eb Iustin Pop
         \ template '%s'.\n"
308 375969eb Iustin Pop
         (specDescription spec)
309 5f828ce4 Agata Murawska
         (formatResources ispec specData) (diskTemplateToRaw disk_template)
310 375969eb Iustin Pop
311 375969eb Iustin Pop
-- | Prints the tiered results.
312 5f3b040a Iustin Pop
printTiered :: Bool -> [(RSpec, Int)]
313 375969eb Iustin Pop
            -> Node.List -> Node.List -> [(FailMode, Int)] -> IO ()
314 5f3b040a Iustin Pop
printTiered True spec_map nl trl_nl _ = do
315 79eef90b Agata Murawska
  printKeysHTS $ printStats PTiered (Cluster.totalResources trl_nl)
316 79eef90b Agata Murawska
  printKeysHTS [("TSPEC", unwords (formatSpecMap spec_map))]
317 5f3b040a Iustin Pop
  printAllocationStats nl trl_nl
318 375969eb Iustin Pop
319 5f3b040a Iustin Pop
printTiered False spec_map ini_nl fin_nl sreason = do
320 375969eb Iustin Pop
  _ <- printf "Tiered allocation results:\n"
321 1f5635a9 Iustin Pop
  if null spec_map
322 1f5635a9 Iustin Pop
    then putStrLn "  - no instances allocated"
323 1f5635a9 Iustin Pop
    else mapM_ (\(ispec, cnt) ->
324 1f5635a9 Iustin Pop
                  printf "  - %3d instances of spec %s\n" cnt
325 1f5635a9 Iustin Pop
                           (formatResources ispec specData)) spec_map
326 2f9198be Iustin Pop
  printFRScores ini_nl fin_nl sreason
327 375969eb Iustin Pop
328 179c0828 Iustin Pop
-- | Displays the initial/final cluster scores.
329 375969eb Iustin Pop
printClusterScores :: Node.List -> Node.List -> IO ()
330 375969eb Iustin Pop
printClusterScores ini_nl fin_nl = do
331 375969eb Iustin Pop
  printf "  - initial cluster score: %.8f\n" $ Cluster.compCV ini_nl::IO ()
332 375969eb Iustin Pop
  printf "  -   final cluster score: %.8f\n" $ Cluster.compCV fin_nl
333 375969eb Iustin Pop
334 179c0828 Iustin Pop
-- | Displays the cluster efficiency.
335 375969eb Iustin Pop
printClusterEff :: Cluster.CStats -> IO ()
336 375969eb Iustin Pop
printClusterEff cs =
337 3c3690aa Iustin Pop
  mapM_ (\(s, fn) ->
338 3c3690aa Iustin Pop
           printf "  - %s usage efficiency: %5.2f%%\n" s (fn cs * 100))
339 375969eb Iustin Pop
          [("memory", memEff),
340 375969eb Iustin Pop
           ("  disk", dskEff),
341 375969eb Iustin Pop
           ("  vcpu", cpuEff)]
342 375969eb Iustin Pop
343 375969eb Iustin Pop
-- | Computes the most likely failure reason.
344 375969eb Iustin Pop
failureReason :: [(FailMode, Int)] -> String
345 375969eb Iustin Pop
failureReason = show . fst . head
346 375969eb Iustin Pop
347 375969eb Iustin Pop
-- | Sorts the failure reasons.
348 375969eb Iustin Pop
sortReasons :: [(FailMode, Int)] -> [(FailMode, Int)]
349 375969eb Iustin Pop
sortReasons = reverse . sortBy (comparing snd)
350 375969eb Iustin Pop
351 e86f7f65 Iustin Pop
-- | Runs an allocation algorithm and saves cluster state.
352 e86f7f65 Iustin Pop
runAllocation :: ClusterData                -- ^ Cluster data
353 e86f7f65 Iustin Pop
              -> Maybe Cluster.AllocResult  -- ^ Optional stop-allocation
354 e86f7f65 Iustin Pop
              -> Result Cluster.AllocResult -- ^ Allocation result
355 e86f7f65 Iustin Pop
              -> RSpec                      -- ^ Requested instance spec
356 9fdd3d0f Iustin Pop
              -> DiskTemplate               -- ^ Requested disk template
357 e86f7f65 Iustin Pop
              -> SpecType                   -- ^ Allocation type
358 e86f7f65 Iustin Pop
              -> Options                    -- ^ CLI options
359 e86f7f65 Iustin Pop
              -> IO (FailStats, Node.List, Int, [(RSpec, Int)])
360 9fdd3d0f Iustin Pop
runAllocation cdata stop_allocation actual_result spec dt mode opts = do
361 e86f7f65 Iustin Pop
  (reasons, new_nl, new_il, new_ixes, _) <-
362 e86f7f65 Iustin Pop
      case stop_allocation of
363 e86f7f65 Iustin Pop
        Just result_noalloc -> return result_noalloc
364 88a10df5 Iustin Pop
        Nothing -> exitIfBad "failure during allocation" actual_result
365 e86f7f65 Iustin Pop
366 e86f7f65 Iustin Pop
  let name = head . words . specDescription $ mode
367 e86f7f65 Iustin Pop
      descr = name ++ " allocation"
368 e86f7f65 Iustin Pop
      ldescr = "after " ++ map toLower descr
369 e86f7f65 Iustin Pop
370 9fdd3d0f Iustin Pop
  printISpec (optMachineReadable opts) spec mode dt
371 e86f7f65 Iustin Pop
372 e86f7f65 Iustin Pop
  printAllocationMap (optVerbose opts) descr new_nl new_ixes
373 e86f7f65 Iustin Pop
374 e86f7f65 Iustin Pop
  maybePrintNodes (optShowNodes opts) descr (Cluster.printNodes new_nl)
375 e86f7f65 Iustin Pop
376 e86f7f65 Iustin Pop
  maybeSaveData (optSaveCluster opts) (map toLower name) ldescr
377 e86f7f65 Iustin Pop
                    (cdata { cdNodes = new_nl, cdInstances = new_il})
378 e86f7f65 Iustin Pop
379 e86f7f65 Iustin Pop
  return (sortReasons reasons, new_nl, length new_ixes, tieredSpecMap new_ixes)
380 e86f7f65 Iustin Pop
381 8564fb47 Iustin Pop
-- | Create an instance from a given spec.
382 f0753837 René Nussbaumer
instFromSpec :: RSpec -> DiskTemplate -> Int -> Instance.Instance
383 5b11f8db Iustin Pop
instFromSpec spx =
384 8564fb47 Iustin Pop
  Instance.create "new" (rspecMem spx) (rspecDsk spx)
385 5b11f8db Iustin Pop
    (rspecCpu spx) Running [] True (-1) (-1)
386 8564fb47 Iustin Pop
387 e10be8f2 Iustin Pop
-- | Main function.
388 21839f47 Iustin Pop
main :: Options -> [String] -> IO ()
389 21839f47 Iustin Pop
main opts args = do
390 88a10df5 Iustin Pop
  exitUnless (null args) "this program doesn't take any arguments"
391 e10be8f2 Iustin Pop
392 2795466b Iustin Pop
  let verbose = optVerbose opts
393 375969eb Iustin Pop
      machine_r = optMachineReadable opts
394 2795466b Iustin Pop
395 be468da0 Iustin Pop
  orig_cdata@(ClusterData gl fixed_nl il _ ipol) <- loadExternalData opts
396 e86f7f65 Iustin Pop
  nl <- setNodeStatus opts fixed_nl
397 2795466b Iustin Pop
398 9fdd3d0f Iustin Pop
  cluster_disk_template <-
399 9fdd3d0f Iustin Pop
    case iPolicyDiskTemplates ipol of
400 9fdd3d0f Iustin Pop
      first_templ:_ -> return first_templ
401 88a10df5 Iustin Pop
      _ -> exitErr "null list of disk templates received from cluster"
402 9fdd3d0f Iustin Pop
403 5296ee23 Iustin Pop
  let num_instances = Container.size il
404 e10be8f2 Iustin Pop
      all_nodes = Container.elems fixed_nl
405 71375ef7 Iustin Pop
      cdata = orig_cdata { cdNodes = fixed_nl }
406 9fdd3d0f Iustin Pop
      disk_template = fromMaybe cluster_disk_template (optDiskTemplate opts)
407 9fdd3d0f Iustin Pop
      req_nodes = Instance.requiredNodes disk_template
408 3e4480e0 Iustin Pop
      csf = commonSuffix fixed_nl il
409 f0753837 René Nussbaumer
      su = fromMaybe (iSpecSpindleUse $ iPolicyStdSpec ipol)
410 f0753837 René Nussbaumer
                     (optSpindleUse opts)
411 e10be8f2 Iustin Pop
412 5296ee23 Iustin Pop
  when (not (null csf) && verbose > 1) $
413 2bbf77cc Iustin Pop
       hPrintf stderr "Note: Stripping common suffix of '%s' from names\n" csf
414 e10be8f2 Iustin Pop
415 e86f7f65 Iustin Pop
  maybePrintNodes (optShowNodes opts) "Initial cluster" (Cluster.printNodes nl)
416 e10be8f2 Iustin Pop
417 2485487d Iustin Pop
  when (verbose > 2) $
418 2922d2c5 René Nussbaumer
         hPrintf stderr "Initial coefficients: overall %.8f\n%s"
419 2922d2c5 René Nussbaumer
                 (Cluster.compCV nl) (Cluster.printStats "  " nl)
420 de4ac2c2 Iustin Pop
421 e86f7f65 Iustin Pop
  printCluster machine_r (Cluster.totalResources nl) (length all_nodes)
422 375969eb Iustin Pop
423 e86f7f65 Iustin Pop
  let stop_allocation = case Cluster.computeBadItems nl il of
424 e86f7f65 Iustin Pop
                          ([], _) -> Nothing
425 e86f7f65 Iustin Pop
                          _ -> Just ([(FailN1, 1)]::FailStats, nl, il, [], [])
426 e86f7f65 Iustin Pop
      alloclimit = if optMaxLength opts == -1
427 e86f7f65 Iustin Pop
                   then Nothing
428 e86f7f65 Iustin Pop
                   else Just (optMaxLength opts)
429 dca7f396 Iustin Pop
430 88a10df5 Iustin Pop
  allocnodes <- exitIfBad "failure during allocation" $
431 88a10df5 Iustin Pop
                Cluster.genAllocNodes gl nl req_nodes True
432 41b5c85a Iustin Pop
433 be468da0 Iustin Pop
  -- Run the tiered allocation
434 fcebc9db Iustin Pop
435 be468da0 Iustin Pop
  let tspec = fromMaybe (rspecFromISpec (iPolicyMaxSpec ipol))
436 be468da0 Iustin Pop
              (optTieredSpec opts)
437 fcebc9db Iustin Pop
438 be468da0 Iustin Pop
  (treason, trl_nl, _, spec_map) <-
439 be468da0 Iustin Pop
    runAllocation cdata stop_allocation
440 be468da0 Iustin Pop
       (Cluster.tieredAlloc nl il alloclimit
441 f0753837 René Nussbaumer
        (instFromSpec tspec disk_template su) allocnodes [] [])
442 9fdd3d0f Iustin Pop
       tspec disk_template SpecTiered opts
443 be468da0 Iustin Pop
444 5f3b040a Iustin Pop
  printTiered machine_r spec_map nl trl_nl treason
445 fcebc9db Iustin Pop
446 fcebc9db Iustin Pop
  -- Run the standard (avg-mode) allocation
447 e10be8f2 Iustin Pop
448 be468da0 Iustin Pop
  let ispec = fromMaybe (rspecFromISpec (iPolicyStdSpec ipol))
449 be468da0 Iustin Pop
              (optStdSpec opts)
450 be468da0 Iustin Pop
451 e86f7f65 Iustin Pop
  (sreason, fin_nl, allocs, _) <-
452 e86f7f65 Iustin Pop
      runAllocation cdata stop_allocation
453 8564fb47 Iustin Pop
            (Cluster.iterateAlloc nl il alloclimit
454 f0753837 René Nussbaumer
             (instFromSpec ispec disk_template su) allocnodes [] [])
455 9fdd3d0f Iustin Pop
            ispec disk_template SpecNormal opts
456 3e9501d0 Iustin Pop
457 375969eb Iustin Pop
  printResults machine_r nl fin_nl num_instances allocs sreason
458 375969eb Iustin Pop
459 be468da0 Iustin Pop
  -- Print final result
460 be468da0 Iustin Pop
461 79eef90b Agata Murawska
  printFinalHTS machine_r