Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Node.hs @ 1496f5f3

History | View | Annotate | Download (24 kB)

1 e4f08c46 Iustin Pop
{-| Module describing a node.
2 e4f08c46 Iustin Pop
3 e4f08c46 Iustin Pop
    All updates are functional (copy-based) and return a new node with
4 e4f08c46 Iustin Pop
    updated value.
5 e4f08c46 Iustin Pop
-}
6 e4f08c46 Iustin Pop
7 e2fa2baf Iustin Pop
{-
8 e2fa2baf Iustin Pop
9 d6eec019 Iustin Pop
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
10 e2fa2baf Iustin Pop
11 e2fa2baf Iustin Pop
This program is free software; you can redistribute it and/or modify
12 e2fa2baf Iustin Pop
it under the terms of the GNU General Public License as published by
13 e2fa2baf Iustin Pop
the Free Software Foundation; either version 2 of the License, or
14 e2fa2baf Iustin Pop
(at your option) any later version.
15 e2fa2baf Iustin Pop
16 e2fa2baf Iustin Pop
This program is distributed in the hope that it will be useful, but
17 e2fa2baf Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
18 e2fa2baf Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 e2fa2baf Iustin Pop
General Public License for more details.
20 e2fa2baf Iustin Pop
21 e2fa2baf Iustin Pop
You should have received a copy of the GNU General Public License
22 e2fa2baf Iustin Pop
along with this program; if not, write to the Free Software
23 e2fa2baf Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 e2fa2baf Iustin Pop
02110-1301, USA.
25 e2fa2baf Iustin Pop
26 e2fa2baf Iustin Pop
-}
27 e2fa2baf Iustin Pop
28 669d7e3d Iustin Pop
module Ganeti.HTools.Node
29 fd7a7c73 Iustin Pop
  ( Node(..)
30 fd7a7c73 Iustin Pop
  , List
31 fd7a7c73 Iustin Pop
  -- * Constructor
32 fd7a7c73 Iustin Pop
  , create
33 fd7a7c73 Iustin Pop
  -- ** Finalization after data loading
34 fd7a7c73 Iustin Pop
  , buildPeers
35 fd7a7c73 Iustin Pop
  , setIdx
36 fd7a7c73 Iustin Pop
  , setAlias
37 fd7a7c73 Iustin Pop
  , setOffline
38 fd7a7c73 Iustin Pop
  , setXmem
39 fd7a7c73 Iustin Pop
  , setFmem
40 fd7a7c73 Iustin Pop
  , setPri
41 fd7a7c73 Iustin Pop
  , setSec
42 fd7a7c73 Iustin Pop
  , setMdsk
43 fd7a7c73 Iustin Pop
  , setMcpu
44 d6eec019 Iustin Pop
  , setPolicy
45 fd7a7c73 Iustin Pop
  -- * Tag maps
46 fd7a7c73 Iustin Pop
  , addTags
47 fd7a7c73 Iustin Pop
  , delTags
48 fd7a7c73 Iustin Pop
  , rejectAddTags
49 fd7a7c73 Iustin Pop
  -- * Instance (re)location
50 fd7a7c73 Iustin Pop
  , removePri
51 fd7a7c73 Iustin Pop
  , removeSec
52 fd7a7c73 Iustin Pop
  , addPri
53 fd7a7c73 Iustin Pop
  , addPriEx
54 fd7a7c73 Iustin Pop
  , addSec
55 fd7a7c73 Iustin Pop
  , addSecEx
56 fd7a7c73 Iustin Pop
  -- * Stats
57 fd7a7c73 Iustin Pop
  , availDisk
58 fd7a7c73 Iustin Pop
  , availMem
59 fd7a7c73 Iustin Pop
  , availCpu
60 fd7a7c73 Iustin Pop
  , iMem
61 fd7a7c73 Iustin Pop
  , iDsk
62 fd7a7c73 Iustin Pop
  , conflictingPrimaries
63 fd7a7c73 Iustin Pop
  -- * Formatting
64 fd7a7c73 Iustin Pop
  , defaultFields
65 fd7a7c73 Iustin Pop
  , showHeader
66 fd7a7c73 Iustin Pop
  , showField
67 fd7a7c73 Iustin Pop
  , list
68 fd7a7c73 Iustin Pop
  -- * Misc stuff
69 fd7a7c73 Iustin Pop
  , AssocList
70 fd7a7c73 Iustin Pop
  , AllocElement
71 fd7a7c73 Iustin Pop
  , noSecondary
72 fd7a7c73 Iustin Pop
  , computeGroups
73 dae1f9cb Guido Trotter
  , mkNodeGraph
74 fd7a7c73 Iustin Pop
  ) where
75 e4f08c46 Iustin Pop
76 a68004b7 Iustin Pop
import Data.List hiding (group)
77 5f0b9579 Iustin Pop
import qualified Data.Map as Map
78 1e4b5230 Iustin Pop
import qualified Data.Foldable as Foldable
79 dae1f9cb Guido Trotter
import qualified Data.IntMap as IntMap
80 dae1f9cb Guido Trotter
import qualified Data.Graph as Graph
81 d8bcd0a8 Iustin Pop
import Data.Ord (comparing)
82 e4f08c46 Iustin Pop
import Text.Printf (printf)
83 dae1f9cb Guido Trotter
import Control.Monad (liftM, liftM2)
84 e4f08c46 Iustin Pop
85 669d7e3d Iustin Pop
import qualified Ganeti.HTools.Container as Container
86 669d7e3d Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
87 12e6776a Iustin Pop
import qualified Ganeti.HTools.PeerMap as P
88 e4f08c46 Iustin Pop
89 a8038349 Iustin Pop
import Ganeti.BasicTypes
90 262a08a2 Iustin Pop
import qualified Ganeti.HTools.Types as T
91 262a08a2 Iustin Pop
92 9188aeef Iustin Pop
-- * Type declarations
93 9188aeef Iustin Pop
94 525bfb36 Iustin Pop
-- | The tag map type.
95 5f0b9579 Iustin Pop
type TagMap = Map.Map String Int
96 5f0b9579 Iustin Pop
97 9188aeef Iustin Pop
-- | The node type.
98 12e6776a Iustin Pop
data Node = Node
99 fd7a7c73 Iustin Pop
  { name     :: String    -- ^ The node name
100 fd7a7c73 Iustin Pop
  , alias    :: String    -- ^ The shortened name (for display purposes)
101 fd7a7c73 Iustin Pop
  , tMem     :: Double    -- ^ Total memory (MiB)
102 fd7a7c73 Iustin Pop
  , nMem     :: Int       -- ^ Node memory (MiB)
103 fd7a7c73 Iustin Pop
  , fMem     :: Int       -- ^ Free memory (MiB)
104 fd7a7c73 Iustin Pop
  , xMem     :: Int       -- ^ Unaccounted memory (MiB)
105 fd7a7c73 Iustin Pop
  , tDsk     :: Double    -- ^ Total disk space (MiB)
106 fd7a7c73 Iustin Pop
  , fDsk     :: Int       -- ^ Free disk space (MiB)
107 fd7a7c73 Iustin Pop
  , tCpu     :: Double    -- ^ Total CPU count
108 fd7a7c73 Iustin Pop
  , uCpu     :: Int       -- ^ Used VCPU count
109 8bc34c7b Iustin Pop
  , spindleCount :: Int   -- ^ Node spindles (spindle_count node parameter)
110 fd7a7c73 Iustin Pop
  , pList    :: [T.Idx]   -- ^ List of primary instance indices
111 fd7a7c73 Iustin Pop
  , sList    :: [T.Idx]   -- ^ List of secondary instance indices
112 fd7a7c73 Iustin Pop
  , idx      :: T.Ndx     -- ^ Internal index for book-keeping
113 fd7a7c73 Iustin Pop
  , peers    :: P.PeerMap -- ^ Pnode to instance mapping
114 fd7a7c73 Iustin Pop
  , failN1   :: Bool      -- ^ Whether the node has failed n1
115 fd7a7c73 Iustin Pop
  , rMem     :: Int       -- ^ Maximum memory needed for failover by
116 fd7a7c73 Iustin Pop
                          -- primaries of this node
117 fd7a7c73 Iustin Pop
  , pMem     :: Double    -- ^ Percent of free memory
118 fd7a7c73 Iustin Pop
  , pDsk     :: Double    -- ^ Percent of free disk
119 fd7a7c73 Iustin Pop
  , pRem     :: Double    -- ^ Percent of reserved memory
120 fd7a7c73 Iustin Pop
  , pCpu     :: Double    -- ^ Ratio of virtual to physical CPUs
121 fd7a7c73 Iustin Pop
  , mDsk     :: Double    -- ^ Minimum free disk ratio
122 fd7a7c73 Iustin Pop
  , loDsk    :: Int       -- ^ Autocomputed from mDsk low disk
123 fd7a7c73 Iustin Pop
                          -- threshold
124 fd7a7c73 Iustin Pop
  , hiCpu    :: Int       -- ^ Autocomputed from mCpu high cpu
125 fd7a7c73 Iustin Pop
                          -- threshold
126 8bc34c7b Iustin Pop
  , hiSpindles :: Double  -- ^ Auto-computed from policy spindle_ratio
127 8bc34c7b Iustin Pop
                          -- and the node spindle count
128 8bc34c7b Iustin Pop
  , instSpindles :: Double -- ^ Spindles used by instances
129 fd7a7c73 Iustin Pop
  , offline  :: Bool      -- ^ Whether the node should not be used for
130 fd7a7c73 Iustin Pop
                          -- allocations and skipped from score
131 fd7a7c73 Iustin Pop
                          -- computations
132 fd7a7c73 Iustin Pop
  , utilPool :: T.DynUtil -- ^ Total utilisation capacity
133 fd7a7c73 Iustin Pop
  , utilLoad :: T.DynUtil -- ^ Sum of instance utilisation
134 2f907bad Dato Simó
  , pTags    :: TagMap    -- ^ Primary instance exclusion tags and their count
135 fd7a7c73 Iustin Pop
  , group    :: T.Gdx     -- ^ The node's group (index)
136 d6eec019 Iustin Pop
  , iPolicy  :: T.IPolicy -- ^ The instance policy (of the node's group)
137 139c0683 Iustin Pop
  } deriving (Show, Eq)
138 e4f08c46 Iustin Pop
139 262a08a2 Iustin Pop
instance T.Element Node where
140 fd7a7c73 Iustin Pop
  nameOf = name
141 fd7a7c73 Iustin Pop
  idxOf = idx
142 fd7a7c73 Iustin Pop
  setAlias = setAlias
143 fd7a7c73 Iustin Pop
  setIdx = setIdx
144 fd7a7c73 Iustin Pop
  allNames n = [name n, alias n]
145 262a08a2 Iustin Pop
146 9188aeef Iustin Pop
-- | A simple name for the int, node association list.
147 608efcce Iustin Pop
type AssocList = [(T.Ndx, Node)]
148 040afc35 Iustin Pop
149 9188aeef Iustin Pop
-- | A simple name for a node map.
150 262a08a2 Iustin Pop
type List = Container.Container Node
151 262a08a2 Iustin Pop
152 1fe81531 Iustin Pop
-- | A simple name for an allocation element (here just for logistic
153 525bfb36 Iustin Pop
-- reasons).
154 7d3f4253 Iustin Pop
type AllocElement = (List, Instance.Instance, [Node], T.Score)
155 1fe81531 Iustin Pop
156 9188aeef Iustin Pop
-- | Constant node index for a non-moveable instance.
157 608efcce Iustin Pop
noSecondary :: T.Ndx
158 040afc35 Iustin Pop
noSecondary = -1
159 040afc35 Iustin Pop
160 5f0b9579 Iustin Pop
-- * Helper functions
161 5f0b9579 Iustin Pop
162 525bfb36 Iustin Pop
-- | Add a tag to a tagmap.
163 5f0b9579 Iustin Pop
addTag :: TagMap -> String -> TagMap
164 5f0b9579 Iustin Pop
addTag t s = Map.insertWith (+) s 1 t
165 5f0b9579 Iustin Pop
166 525bfb36 Iustin Pop
-- | Add multiple tags.
167 5f0b9579 Iustin Pop
addTags :: TagMap -> [String] -> TagMap
168 5f0b9579 Iustin Pop
addTags = foldl' addTag
169 5f0b9579 Iustin Pop
170 525bfb36 Iustin Pop
-- | Adjust or delete a tag from a tagmap.
171 5f0b9579 Iustin Pop
delTag :: TagMap -> String -> TagMap
172 5f0b9579 Iustin Pop
delTag t s = Map.update (\v -> if v > 1
173 fd7a7c73 Iustin Pop
                                 then Just (v-1)
174 fd7a7c73 Iustin Pop
                                 else Nothing)
175 5f0b9579 Iustin Pop
             s t
176 5f0b9579 Iustin Pop
177 525bfb36 Iustin Pop
-- | Remove multiple tags.
178 5f0b9579 Iustin Pop
delTags :: TagMap -> [String] -> TagMap
179 5f0b9579 Iustin Pop
delTags = foldl' delTag
180 5f0b9579 Iustin Pop
181 525bfb36 Iustin Pop
-- | Check if we can add a list of tags to a tagmap.
182 5f0b9579 Iustin Pop
rejectAddTags :: TagMap -> [String] -> Bool
183 5182e970 Iustin Pop
rejectAddTags t = any (`Map.member` t)
184 5f0b9579 Iustin Pop
185 1e4b5230 Iustin Pop
-- | Check how many primary instances have conflicting tags. The
186 1e4b5230 Iustin Pop
-- algorithm to compute this is to sum the count of all tags, then
187 1e4b5230 Iustin Pop
-- subtract the size of the tag map (since each tag has at least one,
188 1e4b5230 Iustin Pop
-- non-conflicting instance); this is equivalent to summing the
189 1e4b5230 Iustin Pop
-- values in the tag map minus one.
190 1e4b5230 Iustin Pop
conflictingPrimaries :: Node -> Int
191 1e4b5230 Iustin Pop
conflictingPrimaries (Node { pTags = t }) = Foldable.sum t - Map.size t
192 1e4b5230 Iustin Pop
193 f87c9f5d Iustin Pop
-- | Helper function to increment a base value depending on the passed
194 f87c9f5d Iustin Pop
-- boolean argument.
195 f87c9f5d Iustin Pop
incIf :: (Num a) => Bool -> a -> a -> a
196 f87c9f5d Iustin Pop
incIf True  base delta = base + delta
197 f87c9f5d Iustin Pop
incIf False base _     = base
198 f87c9f5d Iustin Pop
199 f87c9f5d Iustin Pop
-- | Helper function to decrement a base value depending on the passed
200 f87c9f5d Iustin Pop
-- boolean argument.
201 f87c9f5d Iustin Pop
decIf :: (Num a) => Bool -> a -> a -> a
202 f87c9f5d Iustin Pop
decIf True  base delta = base - delta
203 f87c9f5d Iustin Pop
decIf False base _     = base
204 f87c9f5d Iustin Pop
205 9188aeef Iustin Pop
-- * Initialization functions
206 e4f08c46 Iustin Pop
207 9188aeef Iustin Pop
-- | Create a new node.
208 9188aeef Iustin Pop
--
209 9188aeef Iustin Pop
-- The index and the peers maps are empty, and will be need to be
210 9188aeef Iustin Pop
-- update later via the 'setIdx' and 'buildPeers' functions.
211 1a82215d Iustin Pop
create :: String -> Double -> Int -> Int -> Double
212 8bc34c7b Iustin Pop
       -> Int -> Double -> Bool -> Int -> T.Gdx -> Node
213 2727257a Iustin Pop
create name_init mem_t_init mem_n_init mem_f_init
214 8bc34c7b Iustin Pop
       dsk_t_init dsk_f_init cpu_t_init offline_init spindles_init
215 8bc34c7b Iustin Pop
       group_init =
216 fd7a7c73 Iustin Pop
  Node { name = name_init
217 fd7a7c73 Iustin Pop
       , alias = name_init
218 fd7a7c73 Iustin Pop
       , tMem = mem_t_init
219 fd7a7c73 Iustin Pop
       , nMem = mem_n_init
220 fd7a7c73 Iustin Pop
       , fMem = mem_f_init
221 fd7a7c73 Iustin Pop
       , tDsk = dsk_t_init
222 fd7a7c73 Iustin Pop
       , fDsk = dsk_f_init
223 fd7a7c73 Iustin Pop
       , tCpu = cpu_t_init
224 8bc34c7b Iustin Pop
       , spindleCount = spindles_init
225 fd7a7c73 Iustin Pop
       , uCpu = 0
226 fd7a7c73 Iustin Pop
       , pList = []
227 fd7a7c73 Iustin Pop
       , sList = []
228 fd7a7c73 Iustin Pop
       , failN1 = True
229 fd7a7c73 Iustin Pop
       , idx = -1
230 fd7a7c73 Iustin Pop
       , peers = P.empty
231 fd7a7c73 Iustin Pop
       , rMem = 0
232 fd7a7c73 Iustin Pop
       , pMem = fromIntegral mem_f_init / mem_t_init
233 b7743258 Iustin Pop
       , pDsk = computePDsk dsk_f_init dsk_t_init
234 fd7a7c73 Iustin Pop
       , pRem = 0
235 fd7a7c73 Iustin Pop
       , pCpu = 0
236 fd7a7c73 Iustin Pop
       , offline = offline_init
237 fd7a7c73 Iustin Pop
       , xMem = 0
238 fd7a7c73 Iustin Pop
       , mDsk = T.defReservedDiskRatio
239 fd7a7c73 Iustin Pop
       , loDsk = mDskToloDsk T.defReservedDiskRatio dsk_t_init
240 487e1962 Iustin Pop
       , hiCpu = mCpuTohiCpu (T.iPolicyVcpuRatio T.defIPolicy) cpu_t_init
241 8bc34c7b Iustin Pop
       , hiSpindles = computeHiSpindles (T.iPolicySpindleRatio T.defIPolicy)
242 8bc34c7b Iustin Pop
                      spindles_init
243 8bc34c7b Iustin Pop
       , instSpindles = 0
244 fd7a7c73 Iustin Pop
       , utilPool = T.baseUtil
245 fd7a7c73 Iustin Pop
       , utilLoad = T.zeroUtil
246 fd7a7c73 Iustin Pop
       , pTags = Map.empty
247 fd7a7c73 Iustin Pop
       , group = group_init
248 d6eec019 Iustin Pop
       , iPolicy = T.defIPolicy
249 fd7a7c73 Iustin Pop
       }
250 e4f08c46 Iustin Pop
251 525bfb36 Iustin Pop
-- | Conversion formula from mDsk\/tDsk to loDsk.
252 f4c0b8c5 Iustin Pop
mDskToloDsk :: Double -> Double -> Int
253 05ff7a00 Agata Murawska
mDskToloDsk mval = floor . (mval *)
254 f4c0b8c5 Iustin Pop
255 525bfb36 Iustin Pop
-- | Conversion formula from mCpu\/tCpu to hiCpu.
256 f4c0b8c5 Iustin Pop
mCpuTohiCpu :: Double -> Double -> Int
257 05ff7a00 Agata Murawska
mCpuTohiCpu mval = floor . (mval *)
258 f4c0b8c5 Iustin Pop
259 8bc34c7b Iustin Pop
-- | Conversiojn formula from spindles and spindle ratio to hiSpindles.
260 8bc34c7b Iustin Pop
computeHiSpindles :: Double -> Int -> Double
261 8bc34c7b Iustin Pop
computeHiSpindles spindle_ratio = (spindle_ratio *) . fromIntegral
262 8bc34c7b Iustin Pop
263 e4f08c46 Iustin Pop
-- | Changes the index.
264 9188aeef Iustin Pop
--
265 e4f08c46 Iustin Pop
-- This is used only during the building of the data structures.
266 608efcce Iustin Pop
setIdx :: Node -> T.Ndx -> Node
267 e4f08c46 Iustin Pop
setIdx t i = t {idx = i}
268 e4f08c46 Iustin Pop
269 8bcdde0c Iustin Pop
-- | Changes the alias.
270 8bcdde0c Iustin Pop
--
271 8bcdde0c Iustin Pop
-- This is used only during the building of the data structures.
272 8bcdde0c Iustin Pop
setAlias :: Node -> String -> Node
273 8bcdde0c Iustin Pop
setAlias t s = t { alias = s }
274 497e30a1 Iustin Pop
275 9188aeef Iustin Pop
-- | Sets the offline attribute.
276 c2c1ef0c Iustin Pop
setOffline :: Node -> Bool -> Node
277 c2c1ef0c Iustin Pop
setOffline t val = t { offline = val }
278 c2c1ef0c Iustin Pop
279 9188aeef Iustin Pop
-- | Sets the unnaccounted memory.
280 8c5b0a0d Iustin Pop
setXmem :: Node -> Int -> Node
281 2060348b Iustin Pop
setXmem t val = t { xMem = val }
282 8c5b0a0d Iustin Pop
283 525bfb36 Iustin Pop
-- | Sets the max disk usage ratio.
284 844eff86 Iustin Pop
setMdsk :: Node -> Double -> Node
285 f4c0b8c5 Iustin Pop
setMdsk t val = t { mDsk = val, loDsk = mDskToloDsk val (tDsk t) }
286 844eff86 Iustin Pop
287 487e1962 Iustin Pop
-- | Sets the max cpu usage ratio. This will update the node's
288 487e1962 Iustin Pop
-- ipolicy, losing sharing (but it should be a seldomly done operation).
289 844eff86 Iustin Pop
setMcpu :: Node -> Double -> Node
290 487e1962 Iustin Pop
setMcpu t val =
291 487e1962 Iustin Pop
  let new_ipol = (iPolicy t) { T.iPolicyVcpuRatio = val }
292 487e1962 Iustin Pop
  in t { hiCpu = mCpuTohiCpu val (tCpu t), iPolicy = new_ipol }
293 844eff86 Iustin Pop
294 d6eec019 Iustin Pop
-- | Sets the policy.
295 d6eec019 Iustin Pop
setPolicy :: T.IPolicy -> Node -> Node
296 487e1962 Iustin Pop
setPolicy pol node =
297 487e1962 Iustin Pop
  node { iPolicy = pol
298 8bc34c7b Iustin Pop
       , hiCpu = mCpuTohiCpu (T.iPolicyVcpuRatio pol) (tCpu node)
299 8bc34c7b Iustin Pop
       , hiSpindles = computeHiSpindles (T.iPolicySpindleRatio pol)
300 8bc34c7b Iustin Pop
                      (spindleCount node)
301 8bc34c7b Iustin Pop
       }
302 d6eec019 Iustin Pop
303 e4f08c46 Iustin Pop
-- | Computes the maximum reserved memory for peers from a peer map.
304 12e6776a Iustin Pop
computeMaxRes :: P.PeerMap -> P.Elem
305 12e6776a Iustin Pop
computeMaxRes = P.maxElem
306 e4f08c46 Iustin Pop
307 e4f08c46 Iustin Pop
-- | Builds the peer map for a given node.
308 9cf4267a Iustin Pop
buildPeers :: Node -> Instance.List -> Node
309 9cf4267a Iustin Pop
buildPeers t il =
310 fd7a7c73 Iustin Pop
  let mdata = map
311 fd7a7c73 Iustin Pop
              (\i_idx -> let inst = Container.find i_idx il
312 55bd1414 Iustin Pop
                             mem = if Instance.usesSecMem inst
313 929b60d8 Iustin Pop
                                     then Instance.mem inst
314 929b60d8 Iustin Pop
                                     else 0
315 fd7a7c73 Iustin Pop
                         in (Instance.pNode inst, mem))
316 fd7a7c73 Iustin Pop
              (sList t)
317 fd7a7c73 Iustin Pop
      pmap = P.accumArray (+) mdata
318 fd7a7c73 Iustin Pop
      new_rmem = computeMaxRes pmap
319 fd7a7c73 Iustin Pop
      new_failN1 = fMem t <= new_rmem
320 fd7a7c73 Iustin Pop
      new_prem = fromIntegral new_rmem / tMem t
321 fd7a7c73 Iustin Pop
  in t {peers=pmap, failN1 = new_failN1, rMem = new_rmem, pRem = new_prem}
322 e4f08c46 Iustin Pop
323 4c18f468 René Nussbaumer
-- | Calculate the new spindle usage
324 ec629280 René Nussbaumer
calcSpindleUse :: Node -> Instance.Instance -> Double
325 ec629280 René Nussbaumer
calcSpindleUse n i = incIf (Instance.usesLocalStorage i) (instSpindles n)
326 ec629280 René Nussbaumer
                       (fromIntegral $ Instance.spindleUse i)
327 4c18f468 René Nussbaumer
328 a488a217 Iustin Pop
-- | Assigns an instance to a node as primary and update the used VCPU
329 5f0b9579 Iustin Pop
-- count, utilisation data and tags map.
330 a488a217 Iustin Pop
setPri :: Node -> Instance.Instance -> Node
331 aa8d2e71 Iustin Pop
setPri t inst = t { pList = Instance.idx inst:pList t
332 a488a217 Iustin Pop
                  , uCpu = new_count
333 aa8d2e71 Iustin Pop
                  , pCpu = fromIntegral new_count / tCpu t
334 aa8d2e71 Iustin Pop
                  , utilLoad = utilLoad t `T.addUtil` Instance.util inst
335 2f907bad Dato Simó
                  , pTags = addTags (pTags t) (Instance.exclTags inst)
336 ec629280 René Nussbaumer
                  , instSpindles = calcSpindleUse t inst
337 aa8d2e71 Iustin Pop
                  }
338 55bd1414 Iustin Pop
  where new_count = Instance.applyIfOnline inst (+ Instance.vcpus inst)
339 55bd1414 Iustin Pop
                    (uCpu t )
340 9188aeef Iustin Pop
341 9188aeef Iustin Pop
-- | Assigns an instance to a node as secondary without other updates.
342 a488a217 Iustin Pop
setSec :: Node -> Instance.Instance -> Node
343 aa8d2e71 Iustin Pop
setSec t inst = t { sList = Instance.idx inst:sList t
344 aa8d2e71 Iustin Pop
                  , utilLoad = old_load { T.dskWeight = T.dskWeight old_load +
345 aa8d2e71 Iustin Pop
                                          T.dskWeight (Instance.util inst) }
346 ec629280 René Nussbaumer
                  , instSpindles = calcSpindleUse t inst
347 aa8d2e71 Iustin Pop
                  }
348 fd7a7c73 Iustin Pop
  where old_load = utilLoad t
349 1a82215d Iustin Pop
350 b7743258 Iustin Pop
-- | Computes the new 'pDsk' value, handling nodes without local disk
351 b7743258 Iustin Pop
-- storage (we consider all their disk used).
352 b7743258 Iustin Pop
computePDsk :: Int -> Double -> Double
353 b7743258 Iustin Pop
computePDsk _    0     = 1
354 b7743258 Iustin Pop
computePDsk used total = fromIntegral used / total
355 b7743258 Iustin Pop
356 9188aeef Iustin Pop
-- * Update functions
357 9188aeef Iustin Pop
358 9188aeef Iustin Pop
-- | Sets the free memory.
359 9188aeef Iustin Pop
setFmem :: Node -> Int -> Node
360 9188aeef Iustin Pop
setFmem t new_mem =
361 77ffd663 Helga Velroyen
  let new_n1 = new_mem < rMem t
362 fd7a7c73 Iustin Pop
      new_mp = fromIntegral new_mem / tMem t
363 fd7a7c73 Iustin Pop
  in t { fMem = new_mem, failN1 = new_n1, pMem = new_mp }
364 9188aeef Iustin Pop
365 e4f08c46 Iustin Pop
-- | Removes a primary instance.
366 e4f08c46 Iustin Pop
removePri :: Node -> Instance.Instance -> Node
367 e4f08c46 Iustin Pop
removePri t inst =
368 fd7a7c73 Iustin Pop
  let iname = Instance.idx inst
369 7959cbb9 Iustin Pop
      i_online = Instance.notOffline inst
370 f87c9f5d Iustin Pop
      uses_disk = Instance.usesLocalStorage inst
371 fd7a7c73 Iustin Pop
      new_plist = delete iname (pList t)
372 f87c9f5d Iustin Pop
      new_mem = incIf i_online (fMem t) (Instance.mem inst)
373 f87c9f5d Iustin Pop
      new_dsk = incIf uses_disk (fDsk t) (Instance.dsk inst)
374 82f19570 Iustin Pop
      new_spindles = decIf uses_disk (instSpindles t) 1
375 fd7a7c73 Iustin Pop
      new_mp = fromIntegral new_mem / tMem t
376 b7743258 Iustin Pop
      new_dp = computePDsk new_dsk (tDsk t)
377 fd7a7c73 Iustin Pop
      new_failn1 = new_mem <= rMem t
378 f87c9f5d Iustin Pop
      new_ucpu = decIf i_online (uCpu t) (Instance.vcpus inst)
379 fd7a7c73 Iustin Pop
      new_rcpu = fromIntegral new_ucpu / tCpu t
380 fd7a7c73 Iustin Pop
      new_load = utilLoad t `T.subUtil` Instance.util inst
381 fd7a7c73 Iustin Pop
  in t { pList = new_plist, fMem = new_mem, fDsk = new_dsk
382 fd7a7c73 Iustin Pop
       , failN1 = new_failn1, pMem = new_mp, pDsk = new_dp
383 fd7a7c73 Iustin Pop
       , uCpu = new_ucpu, pCpu = new_rcpu, utilLoad = new_load
384 2f907bad Dato Simó
       , pTags = delTags (pTags t) (Instance.exclTags inst)
385 82f19570 Iustin Pop
       , instSpindles = new_spindles
386 82f19570 Iustin Pop
       }
387 e4f08c46 Iustin Pop
388 e4f08c46 Iustin Pop
-- | Removes a secondary instance.
389 e4f08c46 Iustin Pop
removeSec :: Node -> Instance.Instance -> Node
390 e4f08c46 Iustin Pop
removeSec t inst =
391 fd7a7c73 Iustin Pop
  let iname = Instance.idx inst
392 f87c9f5d Iustin Pop
      uses_disk = Instance.usesLocalStorage inst
393 fd7a7c73 Iustin Pop
      cur_dsk = fDsk t
394 fd7a7c73 Iustin Pop
      pnode = Instance.pNode inst
395 fd7a7c73 Iustin Pop
      new_slist = delete iname (sList t)
396 f87c9f5d Iustin Pop
      new_dsk = incIf uses_disk cur_dsk (Instance.dsk inst)
397 82f19570 Iustin Pop
      new_spindles = decIf uses_disk (instSpindles t) 1
398 fd7a7c73 Iustin Pop
      old_peers = peers t
399 fd7a7c73 Iustin Pop
      old_peem = P.find pnode old_peers
400 f87c9f5d Iustin Pop
      new_peem = decIf (Instance.usesSecMem inst) old_peem (Instance.mem inst)
401 fd7a7c73 Iustin Pop
      new_peers = if new_peem > 0
402 124b7cd7 Iustin Pop
                    then P.add pnode new_peem old_peers
403 124b7cd7 Iustin Pop
                    else P.remove pnode old_peers
404 fd7a7c73 Iustin Pop
      old_rmem = rMem t
405 fd7a7c73 Iustin Pop
      new_rmem = if old_peem < old_rmem
406 bbd8efd2 Iustin Pop
                   then old_rmem
407 bbd8efd2 Iustin Pop
                   else computeMaxRes new_peers
408 fd7a7c73 Iustin Pop
      new_prem = fromIntegral new_rmem / tMem t
409 fd7a7c73 Iustin Pop
      new_failn1 = fMem t <= new_rmem
410 b7743258 Iustin Pop
      new_dp = computePDsk new_dsk (tDsk t)
411 fd7a7c73 Iustin Pop
      old_load = utilLoad t
412 fd7a7c73 Iustin Pop
      new_load = old_load { T.dskWeight = T.dskWeight old_load -
413 fd7a7c73 Iustin Pop
                                          T.dskWeight (Instance.util inst) }
414 fd7a7c73 Iustin Pop
  in t { sList = new_slist, fDsk = new_dsk, peers = new_peers
415 fd7a7c73 Iustin Pop
       , failN1 = new_failn1, rMem = new_rmem, pDsk = new_dp
416 82f19570 Iustin Pop
       , pRem = new_prem, utilLoad = new_load
417 82f19570 Iustin Pop
       , instSpindles = new_spindles
418 82f19570 Iustin Pop
       }
419 e4f08c46 Iustin Pop
420 3e3c9393 Iustin Pop
-- | Adds a primary instance (basic version).
421 f2280553 Iustin Pop
addPri :: Node -> Instance.Instance -> T.OpResult Node
422 3e3c9393 Iustin Pop
addPri = addPriEx False
423 3e3c9393 Iustin Pop
424 3e3c9393 Iustin Pop
-- | Adds a primary instance (extended version).
425 3e3c9393 Iustin Pop
addPriEx :: Bool               -- ^ Whether to override the N+1 and
426 3e3c9393 Iustin Pop
                               -- other /soft/ checks, useful if we
427 3e3c9393 Iustin Pop
                               -- come from a worse status
428 3e3c9393 Iustin Pop
                               -- (e.g. offline)
429 3e3c9393 Iustin Pop
         -> Node               -- ^ The target node
430 3e3c9393 Iustin Pop
         -> Instance.Instance  -- ^ The instance to add
431 3e3c9393 Iustin Pop
         -> T.OpResult Node    -- ^ The result of the operation,
432 3e3c9393 Iustin Pop
                               -- either the new version of the node
433 3e3c9393 Iustin Pop
                               -- or a failure mode
434 3e3c9393 Iustin Pop
addPriEx force t inst =
435 fd7a7c73 Iustin Pop
  let iname = Instance.idx inst
436 7959cbb9 Iustin Pop
      i_online = Instance.notOffline inst
437 fd7a7c73 Iustin Pop
      uses_disk = Instance.usesLocalStorage inst
438 fd7a7c73 Iustin Pop
      cur_dsk = fDsk t
439 f87c9f5d Iustin Pop
      new_mem = decIf i_online (fMem t) (Instance.mem inst)
440 f87c9f5d Iustin Pop
      new_dsk = decIf uses_disk cur_dsk (Instance.dsk inst)
441 82f19570 Iustin Pop
      new_spindles = incIf uses_disk (instSpindles t) 1
442 fd7a7c73 Iustin Pop
      new_failn1 = new_mem <= rMem t
443 f87c9f5d Iustin Pop
      new_ucpu = incIf i_online (uCpu t) (Instance.vcpus inst)
444 fd7a7c73 Iustin Pop
      new_pcpu = fromIntegral new_ucpu / tCpu t
445 b7743258 Iustin Pop
      new_dp = computePDsk new_dsk (tDsk t)
446 487e1962 Iustin Pop
      l_cpu = T.iPolicyVcpuRatio $ iPolicy t
447 fd7a7c73 Iustin Pop
      new_load = utilLoad t `T.addUtil` Instance.util inst
448 2f907bad Dato Simó
      inst_tags = Instance.exclTags inst
449 fd7a7c73 Iustin Pop
      old_tags = pTags t
450 fd7a7c73 Iustin Pop
      strict = not force
451 fd7a7c73 Iustin Pop
  in case () of
452 a8038349 Iustin Pop
       _ | new_mem <= 0 -> Bad T.FailMem
453 a8038349 Iustin Pop
         | uses_disk && new_dsk <= 0 -> Bad T.FailDisk
454 a8038349 Iustin Pop
         | uses_disk && mDsk t > new_dp && strict -> Bad T.FailDisk
455 82f19570 Iustin Pop
         | uses_disk && new_spindles > hiSpindles t
456 a8038349 Iustin Pop
             && strict -> Bad T.FailDisk
457 a8038349 Iustin Pop
         | new_failn1 && not (failN1 t) && strict -> Bad T.FailMem
458 a8038349 Iustin Pop
         | l_cpu >= 0 && l_cpu < new_pcpu && strict -> Bad T.FailCPU
459 a8038349 Iustin Pop
         | rejectAddTags old_tags inst_tags -> Bad T.FailTags
460 fd7a7c73 Iustin Pop
         | otherwise ->
461 fd7a7c73 Iustin Pop
           let new_plist = iname:pList t
462 fd7a7c73 Iustin Pop
               new_mp = fromIntegral new_mem / tMem t
463 fd7a7c73 Iustin Pop
               r = t { pList = new_plist, fMem = new_mem, fDsk = new_dsk
464 fd7a7c73 Iustin Pop
                     , failN1 = new_failn1, pMem = new_mp, pDsk = new_dp
465 fd7a7c73 Iustin Pop
                     , uCpu = new_ucpu, pCpu = new_pcpu
466 fd7a7c73 Iustin Pop
                     , utilLoad = new_load
467 82f19570 Iustin Pop
                     , pTags = addTags old_tags inst_tags
468 82f19570 Iustin Pop
                     , instSpindles = new_spindles
469 82f19570 Iustin Pop
                     }
470 a8038349 Iustin Pop
           in Ok r
471 e4f08c46 Iustin Pop
472 3e3c9393 Iustin Pop
-- | Adds a secondary instance (basic version).
473 f2280553 Iustin Pop
addSec :: Node -> Instance.Instance -> T.Ndx -> T.OpResult Node
474 3e3c9393 Iustin Pop
addSec = addSecEx False
475 3e3c9393 Iustin Pop
476 3e3c9393 Iustin Pop
-- | Adds a secondary instance (extended version).
477 3e3c9393 Iustin Pop
addSecEx :: Bool -> Node -> Instance.Instance -> T.Ndx -> T.OpResult Node
478 3e3c9393 Iustin Pop
addSecEx force t inst pdx =
479 fd7a7c73 Iustin Pop
  let iname = Instance.idx inst
480 fd7a7c73 Iustin Pop
      old_peers = peers t
481 fd7a7c73 Iustin Pop
      old_mem = fMem t
482 fd7a7c73 Iustin Pop
      new_dsk = fDsk t - Instance.dsk inst
483 82f19570 Iustin Pop
      new_spindles = instSpindles t + 1
484 55bd1414 Iustin Pop
      secondary_needed_mem = if Instance.usesSecMem inst
485 929b60d8 Iustin Pop
                               then Instance.mem inst
486 929b60d8 Iustin Pop
                               else 0
487 fd7a7c73 Iustin Pop
      new_peem = P.find pdx old_peers + secondary_needed_mem
488 fd7a7c73 Iustin Pop
      new_peers = P.add pdx new_peem old_peers
489 fd7a7c73 Iustin Pop
      new_rmem = max (rMem t) new_peem
490 fd7a7c73 Iustin Pop
      new_prem = fromIntegral new_rmem / tMem t
491 fd7a7c73 Iustin Pop
      new_failn1 = old_mem <= new_rmem
492 b7743258 Iustin Pop
      new_dp = computePDsk new_dsk (tDsk t)
493 fd7a7c73 Iustin Pop
      old_load = utilLoad t
494 fd7a7c73 Iustin Pop
      new_load = old_load { T.dskWeight = T.dskWeight old_load +
495 fd7a7c73 Iustin Pop
                                          T.dskWeight (Instance.util inst) }
496 fd7a7c73 Iustin Pop
      strict = not force
497 fd7a7c73 Iustin Pop
  in case () of
498 a8038349 Iustin Pop
       _ | not (Instance.hasSecondary inst) -> Bad T.FailDisk
499 a8038349 Iustin Pop
         | new_dsk <= 0 -> Bad T.FailDisk
500 a8038349 Iustin Pop
         | mDsk t > new_dp && strict -> Bad T.FailDisk
501 a8038349 Iustin Pop
         | new_spindles > hiSpindles t && strict -> Bad T.FailDisk
502 a8038349 Iustin Pop
         | secondary_needed_mem >= old_mem && strict -> Bad T.FailMem
503 a8038349 Iustin Pop
         | new_failn1 && not (failN1 t) && strict -> Bad T.FailMem
504 fd7a7c73 Iustin Pop
         | otherwise ->
505 fd7a7c73 Iustin Pop
           let new_slist = iname:sList t
506 fd7a7c73 Iustin Pop
               r = t { sList = new_slist, fDsk = new_dsk
507 fd7a7c73 Iustin Pop
                     , peers = new_peers, failN1 = new_failn1
508 fd7a7c73 Iustin Pop
                     , rMem = new_rmem, pDsk = new_dp
509 82f19570 Iustin Pop
                     , pRem = new_prem, utilLoad = new_load
510 82f19570 Iustin Pop
                     , instSpindles = new_spindles
511 82f19570 Iustin Pop
                     }
512 a8038349 Iustin Pop
           in Ok r
513 e4f08c46 Iustin Pop
514 fe3d6f02 Iustin Pop
-- * Stats functions
515 fe3d6f02 Iustin Pop
516 525bfb36 Iustin Pop
-- | Computes the amount of available disk on a given node.
517 fe3d6f02 Iustin Pop
availDisk :: Node -> Int
518 fe3d6f02 Iustin Pop
availDisk t =
519 fd7a7c73 Iustin Pop
  let _f = fDsk t
520 fd7a7c73 Iustin Pop
      _l = loDsk t
521 fd7a7c73 Iustin Pop
  in if _f < _l
522 f4c0b8c5 Iustin Pop
       then 0
523 f4c0b8c5 Iustin Pop
       else _f - _l
524 fe3d6f02 Iustin Pop
525 525bfb36 Iustin Pop
-- | Computes the amount of used disk on a given node.
526 55da339e Iustin Pop
iDsk :: Node -> Int
527 55da339e Iustin Pop
iDsk t = truncate (tDsk t) - fDsk t
528 55da339e Iustin Pop
529 525bfb36 Iustin Pop
-- | Computes the amount of available memory on a given node.
530 1e3dccc8 Iustin Pop
availMem :: Node -> Int
531 1e3dccc8 Iustin Pop
availMem t =
532 fd7a7c73 Iustin Pop
  let _f = fMem t
533 fd7a7c73 Iustin Pop
      _l = rMem t
534 fd7a7c73 Iustin Pop
  in if _f < _l
535 1e3dccc8 Iustin Pop
       then 0
536 1e3dccc8 Iustin Pop
       else _f - _l
537 1e3dccc8 Iustin Pop
538 525bfb36 Iustin Pop
-- | Computes the amount of available memory on a given node.
539 1e3dccc8 Iustin Pop
availCpu :: Node -> Int
540 1e3dccc8 Iustin Pop
availCpu t =
541 fd7a7c73 Iustin Pop
  let _u = uCpu t
542 fd7a7c73 Iustin Pop
      _l = hiCpu t
543 fd7a7c73 Iustin Pop
  in if _l >= _u
544 1e3dccc8 Iustin Pop
       then _l - _u
545 1e3dccc8 Iustin Pop
       else 0
546 1e3dccc8 Iustin Pop
547 425af248 Iustin Pop
-- | The memory used by instances on a given node.
548 425af248 Iustin Pop
iMem :: Node -> Int
549 425af248 Iustin Pop
iMem t = truncate (tMem t) - nMem t - xMem t - fMem t
550 425af248 Iustin Pop
551 dae1f9cb Guido Trotter
-- * Node graph functions
552 dae1f9cb Guido Trotter
-- These functions do the transformations needed so that nodes can be
553 dae1f9cb Guido Trotter
-- represented as a graph connected by the instances that are replicated
554 dae1f9cb Guido Trotter
-- on them.
555 dae1f9cb Guido Trotter
556 dae1f9cb Guido Trotter
-- * Making of a Graph from a node/instance list
557 dae1f9cb Guido Trotter
558 dae1f9cb Guido Trotter
-- | Transform an instance into a list of edges on the node graph
559 dae1f9cb Guido Trotter
instanceToEdges :: Instance.Instance -> [Graph.Edge]
560 dae1f9cb Guido Trotter
instanceToEdges i
561 dae1f9cb Guido Trotter
  | Instance.hasSecondary i = [(pnode,snode), (snode,pnode)]
562 dae1f9cb Guido Trotter
  | otherwise = []
563 dae1f9cb Guido Trotter
    where pnode = Instance.pNode i
564 dae1f9cb Guido Trotter
          snode = Instance.sNode i
565 dae1f9cb Guido Trotter
566 dae1f9cb Guido Trotter
-- | Transform the list of instances into list of destination edges
567 dae1f9cb Guido Trotter
instancesToEdges :: Instance.List -> [Graph.Edge]
568 dae1f9cb Guido Trotter
instancesToEdges = concatMap instanceToEdges . Container.elems
569 dae1f9cb Guido Trotter
570 dae1f9cb Guido Trotter
-- | Transform the list of nodes into vertices bounds.
571 dae1f9cb Guido Trotter
-- Returns Nothing is the list is empty.
572 dae1f9cb Guido Trotter
nodesToBounds :: List -> Maybe Graph.Bounds
573 dae1f9cb Guido Trotter
nodesToBounds nl = liftM2 (,) nmin nmax
574 dae1f9cb Guido Trotter
    where nmin = fmap (fst . fst) (IntMap.minViewWithKey nl)
575 dae1f9cb Guido Trotter
          nmax = fmap (fst . fst) (IntMap.maxViewWithKey nl)
576 dae1f9cb Guido Trotter
577 dae1f9cb Guido Trotter
-- | Transform a Node + Instance list into a NodeGraph type.
578 dae1f9cb Guido Trotter
-- Returns Nothing if the node list is empty.
579 dae1f9cb Guido Trotter
mkNodeGraph :: List -> Instance.List -> Maybe Graph.Graph
580 dae1f9cb Guido Trotter
mkNodeGraph nl il =
581 dae1f9cb Guido Trotter
  liftM (`Graph.buildG` instancesToEdges il) (nodesToBounds nl)
582 dae1f9cb Guido Trotter
583 9188aeef Iustin Pop
-- * Display functions
584 01f6a5d2 Iustin Pop
585 525bfb36 Iustin Pop
-- | Return a field for a given node.
586 525bfb36 Iustin Pop
showField :: Node   -- ^ Node which we're querying
587 525bfb36 Iustin Pop
          -> String -- ^ Field name
588 525bfb36 Iustin Pop
          -> String -- ^ Field value as string
589 c5f7412e Iustin Pop
showField t field =
590 fd7a7c73 Iustin Pop
  case field of
591 fd7a7c73 Iustin Pop
    "idx"  -> printf "%4d" $ idx t
592 fd7a7c73 Iustin Pop
    "name" -> alias t
593 fd7a7c73 Iustin Pop
    "fqdn" -> name t
594 fd7a7c73 Iustin Pop
    "status" -> case () of
595 fd7a7c73 Iustin Pop
                  _ | offline t -> "-"
596 fd7a7c73 Iustin Pop
                    | failN1 t -> "*"
597 fd7a7c73 Iustin Pop
                    | otherwise -> " "
598 fd7a7c73 Iustin Pop
    "tmem" -> printf "%5.0f" $ tMem t
599 fd7a7c73 Iustin Pop
    "nmem" -> printf "%5d" $ nMem t
600 fd7a7c73 Iustin Pop
    "xmem" -> printf "%5d" $ xMem t
601 fd7a7c73 Iustin Pop
    "fmem" -> printf "%5d" $ fMem t
602 fd7a7c73 Iustin Pop
    "imem" -> printf "%5d" $ iMem t
603 fd7a7c73 Iustin Pop
    "rmem" -> printf "%5d" $ rMem t
604 fd7a7c73 Iustin Pop
    "amem" -> printf "%5d" $ fMem t - rMem t
605 fd7a7c73 Iustin Pop
    "tdsk" -> printf "%5.0f" $ tDsk t / 1024
606 fd7a7c73 Iustin Pop
    "fdsk" -> printf "%5d" $ fDsk t `div` 1024
607 fd7a7c73 Iustin Pop
    "tcpu" -> printf "%4.0f" $ tCpu t
608 fd7a7c73 Iustin Pop
    "ucpu" -> printf "%4d" $ uCpu t
609 fd7a7c73 Iustin Pop
    "pcnt" -> printf "%3d" $ length (pList t)
610 fd7a7c73 Iustin Pop
    "scnt" -> printf "%3d" $ length (sList t)
611 fd7a7c73 Iustin Pop
    "plist" -> show $ pList t
612 fd7a7c73 Iustin Pop
    "slist" -> show $ sList t
613 fd7a7c73 Iustin Pop
    "pfmem" -> printf "%6.4f" $ pMem t
614 fd7a7c73 Iustin Pop
    "pfdsk" -> printf "%6.4f" $ pDsk t
615 fd7a7c73 Iustin Pop
    "rcpu"  -> printf "%5.2f" $ pCpu t
616 fd7a7c73 Iustin Pop
    "cload" -> printf "%5.3f" uC
617 fd7a7c73 Iustin Pop
    "mload" -> printf "%5.3f" uM
618 fd7a7c73 Iustin Pop
    "dload" -> printf "%5.3f" uD
619 fd7a7c73 Iustin Pop
    "nload" -> printf "%5.3f" uN
620 fd7a7c73 Iustin Pop
    "ptags" -> intercalate "," . map (uncurry (printf "%s=%d")) .
621 fd7a7c73 Iustin Pop
               Map.toList $ pTags t
622 fd7a7c73 Iustin Pop
    "peermap" -> show $ peers t
623 34ace266 Iustin Pop
    "spindle_count" -> show $ spindleCount t
624 34ace266 Iustin Pop
    "hi_spindles" -> show $ hiSpindles t
625 34ace266 Iustin Pop
    "inst_spindles" -> show $ instSpindles t
626 fd7a7c73 Iustin Pop
    _ -> T.unknownField
627 fd7a7c73 Iustin Pop
  where
628 fd7a7c73 Iustin Pop
    T.DynUtil { T.cpuWeight = uC, T.memWeight = uM,
629 fd7a7c73 Iustin Pop
                T.dskWeight = uD, T.netWeight = uN } = utilLoad t
630 c5f7412e Iustin Pop
631 525bfb36 Iustin Pop
-- | Returns the header and numeric propery of a field.
632 76354e11 Iustin Pop
showHeader :: String -> (String, Bool)
633 76354e11 Iustin Pop
showHeader field =
634 fd7a7c73 Iustin Pop
  case field of
635 fd7a7c73 Iustin Pop
    "idx" -> ("Index", True)
636 fd7a7c73 Iustin Pop
    "name" -> ("Name", False)
637 fd7a7c73 Iustin Pop
    "fqdn" -> ("Name", False)
638 fd7a7c73 Iustin Pop
    "status" -> ("F", False)
639 fd7a7c73 Iustin Pop
    "tmem" -> ("t_mem", True)
640 fd7a7c73 Iustin Pop
    "nmem" -> ("n_mem", True)
641 fd7a7c73 Iustin Pop
    "xmem" -> ("x_mem", True)
642 fd7a7c73 Iustin Pop
    "fmem" -> ("f_mem", True)
643 fd7a7c73 Iustin Pop
    "imem" -> ("i_mem", True)
644 fd7a7c73 Iustin Pop
    "rmem" -> ("r_mem", True)
645 fd7a7c73 Iustin Pop
    "amem" -> ("a_mem", True)
646 fd7a7c73 Iustin Pop
    "tdsk" -> ("t_dsk", True)
647 fd7a7c73 Iustin Pop
    "fdsk" -> ("f_dsk", True)
648 fd7a7c73 Iustin Pop
    "tcpu" -> ("pcpu", True)
649 fd7a7c73 Iustin Pop
    "ucpu" -> ("vcpu", True)
650 fd7a7c73 Iustin Pop
    "pcnt" -> ("pcnt", True)
651 fd7a7c73 Iustin Pop
    "scnt" -> ("scnt", True)
652 fd7a7c73 Iustin Pop
    "plist" -> ("primaries", True)
653 fd7a7c73 Iustin Pop
    "slist" -> ("secondaries", True)
654 fd7a7c73 Iustin Pop
    "pfmem" -> ("p_fmem", True)
655 fd7a7c73 Iustin Pop
    "pfdsk" -> ("p_fdsk", True)
656 fd7a7c73 Iustin Pop
    "rcpu"  -> ("r_cpu", True)
657 fd7a7c73 Iustin Pop
    "cload" -> ("lCpu", True)
658 fd7a7c73 Iustin Pop
    "mload" -> ("lMem", True)
659 fd7a7c73 Iustin Pop
    "dload" -> ("lDsk", True)
660 fd7a7c73 Iustin Pop
    "nload" -> ("lNet", True)
661 fd7a7c73 Iustin Pop
    "ptags" -> ("PrimaryTags", False)
662 fd7a7c73 Iustin Pop
    "peermap" -> ("PeerMap", False)
663 34ace266 Iustin Pop
    "spindle_count" -> ("NodeSpindles", True)
664 34ace266 Iustin Pop
    "hi_spindles" -> ("MaxSpindles", True)
665 34ace266 Iustin Pop
    "inst_spindles" -> ("InstSpindles", True)
666 fd7a7c73 Iustin Pop
    -- TODO: add node fields (group.uuid, group)
667 fd7a7c73 Iustin Pop
    _ -> (T.unknownField, False)
668 c5f7412e Iustin Pop
669 e4f08c46 Iustin Pop
-- | String converter for the node list functionality.
670 76354e11 Iustin Pop
list :: [String] -> Node -> [String]
671 76354e11 Iustin Pop
list fields t = map (showField t) fields
672 76354e11 Iustin Pop
673 525bfb36 Iustin Pop
-- | Constant holding the fields we're displaying by default.
674 76354e11 Iustin Pop
defaultFields :: [String]
675 76354e11 Iustin Pop
defaultFields =
676 fd7a7c73 Iustin Pop
  [ "status", "name", "tmem", "nmem", "imem", "xmem", "fmem"
677 fd7a7c73 Iustin Pop
  , "rmem", "tdsk", "fdsk", "tcpu", "ucpu", "pcnt", "scnt"
678 fd7a7c73 Iustin Pop
  , "pfmem", "pfdsk", "rcpu"
679 fd7a7c73 Iustin Pop
  , "cload", "mload", "dload", "nload" ]
680 d8bcd0a8 Iustin Pop
681 d8bcd0a8 Iustin Pop
-- | Split a list of nodes into a list of (node group UUID, list of
682 525bfb36 Iustin Pop
-- associated nodes).
683 10ef6b4e Iustin Pop
computeGroups :: [Node] -> [(T.Gdx, [Node])]
684 d8bcd0a8 Iustin Pop
computeGroups nodes =
685 d8bcd0a8 Iustin Pop
  let nodes' = sortBy (comparing group) nodes
686 d8bcd0a8 Iustin Pop
      nodes'' = groupBy (\a b -> group a == group b) nodes'
687 d8bcd0a8 Iustin Pop
  in map (\nl -> (group (head nl), nl)) nodes''