Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Program / Hspace.hs @ 1f5635a9

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