Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Node.hs @ 82f19570

History | View | Annotate | Download (22.5 kB)

1
{-| Module describing a node.
2

    
3
    All updates are functional (copy-based) and return a new node with
4
    updated value.
5
-}
6

    
7
{-
8

    
9
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
10

    
11
This program is free software; you can redistribute it and/or modify
12
it under the terms of the GNU General Public License as published by
13
the Free Software Foundation; either version 2 of the License, or
14
(at your option) any later version.
15

    
16
This program is distributed in the hope that it will be useful, but
17
WITHOUT ANY WARRANTY; without even the implied warranty of
18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
General Public License for more details.
20

    
21
You should have received a copy of the GNU General Public License
22
along with this program; if not, write to the Free Software
23
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24
02110-1301, USA.
25

    
26
-}
27

    
28
module Ganeti.HTools.Node
29
  ( Node(..)
30
  , List
31
  -- * Constructor
32
  , create
33
  -- ** Finalization after data loading
34
  , buildPeers
35
  , setIdx
36
  , setAlias
37
  , setOffline
38
  , setXmem
39
  , setFmem
40
  , setPri
41
  , setSec
42
  , setMdsk
43
  , setMcpu
44
  , setPolicy
45
  -- * Tag maps
46
  , addTags
47
  , delTags
48
  , rejectAddTags
49
  -- * Instance (re)location
50
  , removePri
51
  , removeSec
52
  , addPri
53
  , addPriEx
54
  , addSec
55
  , addSecEx
56
  -- * Stats
57
  , availDisk
58
  , availMem
59
  , availCpu
60
  , iMem
61
  , iDsk
62
  , conflictingPrimaries
63
  -- * Formatting
64
  , defaultFields
65
  , showHeader
66
  , showField
67
  , list
68
  -- * Misc stuff
69
  , AssocList
70
  , AllocElement
71
  , noSecondary
72
  , computeGroups
73
  ) where
74

    
75
import Data.List hiding (group)
76
import qualified Data.Map as Map
77
import qualified Data.Foldable as Foldable
78
import Data.Ord (comparing)
79
import Text.Printf (printf)
80

    
81
import qualified Ganeti.HTools.Container as Container
82
import qualified Ganeti.HTools.Instance as Instance
83
import qualified Ganeti.HTools.PeerMap as P
84

    
85
import qualified Ganeti.HTools.Types as T
86

    
87
-- * Type declarations
88

    
89
-- | The tag map type.
90
type TagMap = Map.Map String Int
91

    
92
-- | The node type.
93
data Node = Node
94
  { name     :: String    -- ^ The node name
95
  , alias    :: String    -- ^ The shortened name (for display purposes)
96
  , tMem     :: Double    -- ^ Total memory (MiB)
97
  , nMem     :: Int       -- ^ Node memory (MiB)
98
  , fMem     :: Int       -- ^ Free memory (MiB)
99
  , xMem     :: Int       -- ^ Unaccounted memory (MiB)
100
  , tDsk     :: Double    -- ^ Total disk space (MiB)
101
  , fDsk     :: Int       -- ^ Free disk space (MiB)
102
  , tCpu     :: Double    -- ^ Total CPU count
103
  , uCpu     :: Int       -- ^ Used VCPU count
104
  , spindleCount :: Int   -- ^ Node spindles (spindle_count node parameter)
105
  , pList    :: [T.Idx]   -- ^ List of primary instance indices
106
  , sList    :: [T.Idx]   -- ^ List of secondary instance indices
107
  , idx      :: T.Ndx     -- ^ Internal index for book-keeping
108
  , peers    :: P.PeerMap -- ^ Pnode to instance mapping
109
  , failN1   :: Bool      -- ^ Whether the node has failed n1
110
  , rMem     :: Int       -- ^ Maximum memory needed for failover by
111
                          -- primaries of this node
112
  , pMem     :: Double    -- ^ Percent of free memory
113
  , pDsk     :: Double    -- ^ Percent of free disk
114
  , pRem     :: Double    -- ^ Percent of reserved memory
115
  , pCpu     :: Double    -- ^ Ratio of virtual to physical CPUs
116
  , mDsk     :: Double    -- ^ Minimum free disk ratio
117
  , loDsk    :: Int       -- ^ Autocomputed from mDsk low disk
118
                          -- threshold
119
  , hiCpu    :: Int       -- ^ Autocomputed from mCpu high cpu
120
                          -- threshold
121
  , hiSpindles :: Double  -- ^ Auto-computed from policy spindle_ratio
122
                          -- and the node spindle count
123
  , instSpindles :: Double -- ^ Spindles used by instances
124
  , offline  :: Bool      -- ^ Whether the node should not be used for
125
                          -- allocations and skipped from score
126
                          -- computations
127
  , utilPool :: T.DynUtil -- ^ Total utilisation capacity
128
  , utilLoad :: T.DynUtil -- ^ Sum of instance utilisation
129
  , pTags    :: TagMap    -- ^ Map of primary instance tags and their count
130
  , group    :: T.Gdx     -- ^ The node's group (index)
131
  , iPolicy  :: T.IPolicy -- ^ The instance policy (of the node's group)
132
  } deriving (Show, Read, Eq)
133

    
134
instance T.Element Node where
135
  nameOf = name
136
  idxOf = idx
137
  setAlias = setAlias
138
  setIdx = setIdx
139
  allNames n = [name n, alias n]
140

    
141
-- | A simple name for the int, node association list.
142
type AssocList = [(T.Ndx, Node)]
143

    
144
-- | A simple name for a node map.
145
type List = Container.Container Node
146

    
147
-- | A simple name for an allocation element (here just for logistic
148
-- reasons).
149
type AllocElement = (List, Instance.Instance, [Node], T.Score)
150

    
151
-- | Constant node index for a non-moveable instance.
152
noSecondary :: T.Ndx
153
noSecondary = -1
154

    
155
-- * Helper functions
156

    
157
-- | Add a tag to a tagmap.
158
addTag :: TagMap -> String -> TagMap
159
addTag t s = Map.insertWith (+) s 1 t
160

    
161
-- | Add multiple tags.
162
addTags :: TagMap -> [String] -> TagMap
163
addTags = foldl' addTag
164

    
165
-- | Adjust or delete a tag from a tagmap.
166
delTag :: TagMap -> String -> TagMap
167
delTag t s = Map.update (\v -> if v > 1
168
                                 then Just (v-1)
169
                                 else Nothing)
170
             s t
171

    
172
-- | Remove multiple tags.
173
delTags :: TagMap -> [String] -> TagMap
174
delTags = foldl' delTag
175

    
176
-- | Check if we can add a list of tags to a tagmap.
177
rejectAddTags :: TagMap -> [String] -> Bool
178
rejectAddTags t = any (`Map.member` t)
179

    
180
-- | Check how many primary instances have conflicting tags. The
181
-- algorithm to compute this is to sum the count of all tags, then
182
-- subtract the size of the tag map (since each tag has at least one,
183
-- non-conflicting instance); this is equivalent to summing the
184
-- values in the tag map minus one.
185
conflictingPrimaries :: Node -> Int
186
conflictingPrimaries (Node { pTags = t }) = Foldable.sum t - Map.size t
187

    
188
-- | Helper function to increment a base value depending on the passed
189
-- boolean argument.
190
incIf :: (Num a) => Bool -> a -> a -> a
191
incIf True  base delta = base + delta
192
incIf False base _     = base
193

    
194
-- | Helper function to decrement a base value depending on the passed
195
-- boolean argument.
196
decIf :: (Num a) => Bool -> a -> a -> a
197
decIf True  base delta = base - delta
198
decIf False base _     = base
199

    
200
-- * Initialization functions
201

    
202
-- | Create a new node.
203
--
204
-- The index and the peers maps are empty, and will be need to be
205
-- update later via the 'setIdx' and 'buildPeers' functions.
206
create :: String -> Double -> Int -> Int -> Double
207
       -> Int -> Double -> Bool -> Int -> T.Gdx -> Node
208
create name_init mem_t_init mem_n_init mem_f_init
209
       dsk_t_init dsk_f_init cpu_t_init offline_init spindles_init
210
       group_init =
211
  Node { name = name_init
212
       , alias = name_init
213
       , tMem = mem_t_init
214
       , nMem = mem_n_init
215
       , fMem = mem_f_init
216
       , tDsk = dsk_t_init
217
       , fDsk = dsk_f_init
218
       , tCpu = cpu_t_init
219
       , spindleCount = spindles_init
220
       , uCpu = 0
221
       , pList = []
222
       , sList = []
223
       , failN1 = True
224
       , idx = -1
225
       , peers = P.empty
226
       , rMem = 0
227
       , pMem = fromIntegral mem_f_init / mem_t_init
228
       , pDsk = fromIntegral dsk_f_init / dsk_t_init
229
       , pRem = 0
230
       , pCpu = 0
231
       , offline = offline_init
232
       , xMem = 0
233
       , mDsk = T.defReservedDiskRatio
234
       , loDsk = mDskToloDsk T.defReservedDiskRatio dsk_t_init
235
       , hiCpu = mCpuTohiCpu (T.iPolicyVcpuRatio T.defIPolicy) cpu_t_init
236
       , hiSpindles = computeHiSpindles (T.iPolicySpindleRatio T.defIPolicy)
237
                      spindles_init
238
       , instSpindles = 0
239
       , utilPool = T.baseUtil
240
       , utilLoad = T.zeroUtil
241
       , pTags = Map.empty
242
       , group = group_init
243
       , iPolicy = T.defIPolicy
244
       }
245

    
246
-- | Conversion formula from mDsk\/tDsk to loDsk.
247
mDskToloDsk :: Double -> Double -> Int
248
mDskToloDsk mval = floor . (mval *)
249

    
250
-- | Conversion formula from mCpu\/tCpu to hiCpu.
251
mCpuTohiCpu :: Double -> Double -> Int
252
mCpuTohiCpu mval = floor . (mval *)
253

    
254
-- | Conversiojn formula from spindles and spindle ratio to hiSpindles.
255
computeHiSpindles :: Double -> Int -> Double
256
computeHiSpindles spindle_ratio = (spindle_ratio *) . fromIntegral
257

    
258
-- | Changes the index.
259
--
260
-- This is used only during the building of the data structures.
261
setIdx :: Node -> T.Ndx -> Node
262
setIdx t i = t {idx = i}
263

    
264
-- | Changes the alias.
265
--
266
-- This is used only during the building of the data structures.
267
setAlias :: Node -> String -> Node
268
setAlias t s = t { alias = s }
269

    
270
-- | Sets the offline attribute.
271
setOffline :: Node -> Bool -> Node
272
setOffline t val = t { offline = val }
273

    
274
-- | Sets the unnaccounted memory.
275
setXmem :: Node -> Int -> Node
276
setXmem t val = t { xMem = val }
277

    
278
-- | Sets the max disk usage ratio.
279
setMdsk :: Node -> Double -> Node
280
setMdsk t val = t { mDsk = val, loDsk = mDskToloDsk val (tDsk t) }
281

    
282
-- | Sets the max cpu usage ratio. This will update the node's
283
-- ipolicy, losing sharing (but it should be a seldomly done operation).
284
setMcpu :: Node -> Double -> Node
285
setMcpu t val =
286
  let new_ipol = (iPolicy t) { T.iPolicyVcpuRatio = val }
287
  in t { hiCpu = mCpuTohiCpu val (tCpu t), iPolicy = new_ipol }
288

    
289
-- | Sets the policy.
290
setPolicy :: T.IPolicy -> Node -> Node
291
setPolicy pol node =
292
  node { iPolicy = pol
293
       , hiCpu = mCpuTohiCpu (T.iPolicyVcpuRatio pol) (tCpu node)
294
       , hiSpindles = computeHiSpindles (T.iPolicySpindleRatio pol)
295
                      (spindleCount node)
296
       }
297

    
298
-- | Computes the maximum reserved memory for peers from a peer map.
299
computeMaxRes :: P.PeerMap -> P.Elem
300
computeMaxRes = P.maxElem
301

    
302
-- | Builds the peer map for a given node.
303
buildPeers :: Node -> Instance.List -> Node
304
buildPeers t il =
305
  let mdata = map
306
              (\i_idx -> let inst = Container.find i_idx il
307
                             mem = if Instance.usesSecMem inst
308
                                     then Instance.mem inst
309
                                     else 0
310
                         in (Instance.pNode inst, mem))
311
              (sList t)
312
      pmap = P.accumArray (+) mdata
313
      new_rmem = computeMaxRes pmap
314
      new_failN1 = fMem t <= new_rmem
315
      new_prem = fromIntegral new_rmem / tMem t
316
  in t {peers=pmap, failN1 = new_failN1, rMem = new_rmem, pRem = new_prem}
317

    
318
-- | Assigns an instance to a node as primary and update the used VCPU
319
-- count, utilisation data and tags map.
320
setPri :: Node -> Instance.Instance -> Node
321
setPri t inst = t { pList = Instance.idx inst:pList t
322
                  , uCpu = new_count
323
                  , pCpu = fromIntegral new_count / tCpu t
324
                  , utilLoad = utilLoad t `T.addUtil` Instance.util inst
325
                  , pTags = addTags (pTags t) (Instance.tags inst)
326
                  , instSpindles = new_spindles
327
                  }
328
  where new_count = Instance.applyIfOnline inst (+ Instance.vcpus inst)
329
                    (uCpu t )
330
        new_spindles = instSpindles t + if Instance.usesLocalStorage inst
331
                                          then 1 else 0
332

    
333
-- | Assigns an instance to a node as secondary without other updates.
334
setSec :: Node -> Instance.Instance -> Node
335
setSec t inst = t { sList = Instance.idx inst:sList t
336
                  , utilLoad = old_load { T.dskWeight = T.dskWeight old_load +
337
                                          T.dskWeight (Instance.util inst) }
338
                  , instSpindles = new_spindles
339
                  }
340
  where old_load = utilLoad t
341
        new_spindles = instSpindles t + if Instance.usesLocalStorage inst
342
                                          then 1 else 0
343

    
344
-- * Update functions
345

    
346
-- | Sets the free memory.
347
setFmem :: Node -> Int -> Node
348
setFmem t new_mem =
349
  let new_n1 = new_mem <= rMem t
350
      new_mp = fromIntegral new_mem / tMem t
351
  in t { fMem = new_mem, failN1 = new_n1, pMem = new_mp }
352

    
353
-- | Removes a primary instance.
354
removePri :: Node -> Instance.Instance -> Node
355
removePri t inst =
356
  let iname = Instance.idx inst
357
      i_online = Instance.notOffline inst
358
      uses_disk = Instance.usesLocalStorage inst
359
      new_plist = delete iname (pList t)
360
      new_mem = incIf i_online (fMem t) (Instance.mem inst)
361
      new_dsk = incIf uses_disk (fDsk t) (Instance.dsk inst)
362
      new_spindles = decIf uses_disk (instSpindles t) 1
363
      new_mp = fromIntegral new_mem / tMem t
364
      new_dp = fromIntegral new_dsk / tDsk t
365
      new_failn1 = new_mem <= rMem t
366
      new_ucpu = decIf i_online (uCpu t) (Instance.vcpus inst)
367
      new_rcpu = fromIntegral new_ucpu / tCpu t
368
      new_load = utilLoad t `T.subUtil` Instance.util inst
369
  in t { pList = new_plist, fMem = new_mem, fDsk = new_dsk
370
       , failN1 = new_failn1, pMem = new_mp, pDsk = new_dp
371
       , uCpu = new_ucpu, pCpu = new_rcpu, utilLoad = new_load
372
       , pTags = delTags (pTags t) (Instance.tags inst)
373
       , instSpindles = new_spindles
374
       }
375

    
376
-- | Removes a secondary instance.
377
removeSec :: Node -> Instance.Instance -> Node
378
removeSec t inst =
379
  let iname = Instance.idx inst
380
      uses_disk = Instance.usesLocalStorage inst
381
      cur_dsk = fDsk t
382
      pnode = Instance.pNode inst
383
      new_slist = delete iname (sList t)
384
      new_dsk = incIf uses_disk cur_dsk (Instance.dsk inst)
385
      new_spindles = decIf uses_disk (instSpindles t) 1
386
      old_peers = peers t
387
      old_peem = P.find pnode old_peers
388
      new_peem = decIf (Instance.usesSecMem inst) old_peem (Instance.mem inst)
389
      new_peers = if new_peem > 0
390
                    then P.add pnode new_peem old_peers
391
                    else P.remove pnode old_peers
392
      old_rmem = rMem t
393
      new_rmem = if old_peem < old_rmem
394
                   then old_rmem
395
                   else computeMaxRes new_peers
396
      new_prem = fromIntegral new_rmem / tMem t
397
      new_failn1 = fMem t <= new_rmem
398
      new_dp = fromIntegral new_dsk / tDsk t
399
      old_load = utilLoad t
400
      new_load = old_load { T.dskWeight = T.dskWeight old_load -
401
                                          T.dskWeight (Instance.util inst) }
402
  in t { sList = new_slist, fDsk = new_dsk, peers = new_peers
403
       , failN1 = new_failn1, rMem = new_rmem, pDsk = new_dp
404
       , pRem = new_prem, utilLoad = new_load
405
       , instSpindles = new_spindles
406
       }
407

    
408
-- | Adds a primary instance (basic version).
409
addPri :: Node -> Instance.Instance -> T.OpResult Node
410
addPri = addPriEx False
411

    
412
-- | Adds a primary instance (extended version).
413
addPriEx :: Bool               -- ^ Whether to override the N+1 and
414
                               -- other /soft/ checks, useful if we
415
                               -- come from a worse status
416
                               -- (e.g. offline)
417
         -> Node               -- ^ The target node
418
         -> Instance.Instance  -- ^ The instance to add
419
         -> T.OpResult Node    -- ^ The result of the operation,
420
                               -- either the new version of the node
421
                               -- or a failure mode
422
addPriEx force t inst =
423
  let iname = Instance.idx inst
424
      i_online = Instance.notOffline inst
425
      uses_disk = Instance.usesLocalStorage inst
426
      cur_dsk = fDsk t
427
      new_mem = decIf i_online (fMem t) (Instance.mem inst)
428
      new_dsk = decIf uses_disk cur_dsk (Instance.dsk inst)
429
      new_spindles = incIf uses_disk (instSpindles t) 1
430
      new_failn1 = new_mem <= rMem t
431
      new_ucpu = incIf i_online (uCpu t) (Instance.vcpus inst)
432
      new_pcpu = fromIntegral new_ucpu / tCpu t
433
      new_dp = fromIntegral new_dsk / tDsk t
434
      l_cpu = T.iPolicyVcpuRatio $ iPolicy t
435
      new_load = utilLoad t `T.addUtil` Instance.util inst
436
      inst_tags = Instance.tags inst
437
      old_tags = pTags t
438
      strict = not force
439
  in case () of
440
       _ | new_mem <= 0 -> T.OpFail T.FailMem
441
         | uses_disk && new_dsk <= 0 -> T.OpFail T.FailDisk
442
         | uses_disk && mDsk t > new_dp && strict -> T.OpFail T.FailDisk
443
         | uses_disk && new_spindles > hiSpindles t
444
             && strict -> T.OpFail T.FailDisk
445
         | new_failn1 && not (failN1 t) && strict -> T.OpFail T.FailMem
446
         | l_cpu >= 0 && l_cpu < new_pcpu && strict -> T.OpFail T.FailCPU
447
         | rejectAddTags old_tags inst_tags -> T.OpFail T.FailTags
448
         | otherwise ->
449
           let new_plist = iname:pList t
450
               new_mp = fromIntegral new_mem / tMem t
451
               r = t { pList = new_plist, fMem = new_mem, fDsk = new_dsk
452
                     , failN1 = new_failn1, pMem = new_mp, pDsk = new_dp
453
                     , uCpu = new_ucpu, pCpu = new_pcpu
454
                     , utilLoad = new_load
455
                     , pTags = addTags old_tags inst_tags
456
                     , instSpindles = new_spindles
457
                     }
458
           in T.OpGood r
459

    
460
-- | Adds a secondary instance (basic version).
461
addSec :: Node -> Instance.Instance -> T.Ndx -> T.OpResult Node
462
addSec = addSecEx False
463

    
464
-- | Adds a secondary instance (extended version).
465
addSecEx :: Bool -> Node -> Instance.Instance -> T.Ndx -> T.OpResult Node
466
addSecEx force t inst pdx =
467
  let iname = Instance.idx inst
468
      old_peers = peers t
469
      old_mem = fMem t
470
      new_dsk = fDsk t - Instance.dsk inst
471
      new_spindles = instSpindles t + 1
472
      secondary_needed_mem = if Instance.usesSecMem inst
473
                               then Instance.mem inst
474
                               else 0
475
      new_peem = P.find pdx old_peers + secondary_needed_mem
476
      new_peers = P.add pdx new_peem old_peers
477
      new_rmem = max (rMem t) new_peem
478
      new_prem = fromIntegral new_rmem / tMem t
479
      new_failn1 = old_mem <= new_rmem
480
      new_dp = fromIntegral new_dsk / tDsk t
481
      old_load = utilLoad t
482
      new_load = old_load { T.dskWeight = T.dskWeight old_load +
483
                                          T.dskWeight (Instance.util inst) }
484
      strict = not force
485
  in case () of
486
       _ | not (Instance.hasSecondary inst) -> T.OpFail T.FailDisk
487
         | new_dsk <= 0 -> T.OpFail T.FailDisk
488
         | mDsk t > new_dp && strict -> T.OpFail T.FailDisk
489
         | new_spindles > hiSpindles t && strict -> T.OpFail T.FailDisk
490
         | secondary_needed_mem >= old_mem && strict -> T.OpFail T.FailMem
491
         | new_failn1 && not (failN1 t) && strict -> T.OpFail T.FailMem
492
         | otherwise ->
493
           let new_slist = iname:sList t
494
               r = t { sList = new_slist, fDsk = new_dsk
495
                     , peers = new_peers, failN1 = new_failn1
496
                     , rMem = new_rmem, pDsk = new_dp
497
                     , pRem = new_prem, utilLoad = new_load
498
                     , instSpindles = new_spindles
499
                     }
500
           in T.OpGood r
501

    
502
-- * Stats functions
503

    
504
-- | Computes the amount of available disk on a given node.
505
availDisk :: Node -> Int
506
availDisk t =
507
  let _f = fDsk t
508
      _l = loDsk t
509
  in if _f < _l
510
       then 0
511
       else _f - _l
512

    
513
-- | Computes the amount of used disk on a given node.
514
iDsk :: Node -> Int
515
iDsk t = truncate (tDsk t) - fDsk t
516

    
517
-- | Computes the amount of available memory on a given node.
518
availMem :: Node -> Int
519
availMem t =
520
  let _f = fMem t
521
      _l = rMem t
522
  in if _f < _l
523
       then 0
524
       else _f - _l
525

    
526
-- | Computes the amount of available memory on a given node.
527
availCpu :: Node -> Int
528
availCpu t =
529
  let _u = uCpu t
530
      _l = hiCpu t
531
  in if _l >= _u
532
       then _l - _u
533
       else 0
534

    
535
-- | The memory used by instances on a given node.
536
iMem :: Node -> Int
537
iMem t = truncate (tMem t) - nMem t - xMem t - fMem t
538

    
539
-- * Display functions
540

    
541
-- | Return a field for a given node.
542
showField :: Node   -- ^ Node which we're querying
543
          -> String -- ^ Field name
544
          -> String -- ^ Field value as string
545
showField t field =
546
  case field of
547
    "idx"  -> printf "%4d" $ idx t
548
    "name" -> alias t
549
    "fqdn" -> name t
550
    "status" -> case () of
551
                  _ | offline t -> "-"
552
                    | failN1 t -> "*"
553
                    | otherwise -> " "
554
    "tmem" -> printf "%5.0f" $ tMem t
555
    "nmem" -> printf "%5d" $ nMem t
556
    "xmem" -> printf "%5d" $ xMem t
557
    "fmem" -> printf "%5d" $ fMem t
558
    "imem" -> printf "%5d" $ iMem t
559
    "rmem" -> printf "%5d" $ rMem t
560
    "amem" -> printf "%5d" $ fMem t - rMem t
561
    "tdsk" -> printf "%5.0f" $ tDsk t / 1024
562
    "fdsk" -> printf "%5d" $ fDsk t `div` 1024
563
    "tcpu" -> printf "%4.0f" $ tCpu t
564
    "ucpu" -> printf "%4d" $ uCpu t
565
    "pcnt" -> printf "%3d" $ length (pList t)
566
    "scnt" -> printf "%3d" $ length (sList t)
567
    "plist" -> show $ pList t
568
    "slist" -> show $ sList t
569
    "pfmem" -> printf "%6.4f" $ pMem t
570
    "pfdsk" -> printf "%6.4f" $ pDsk t
571
    "rcpu"  -> printf "%5.2f" $ pCpu t
572
    "cload" -> printf "%5.3f" uC
573
    "mload" -> printf "%5.3f" uM
574
    "dload" -> printf "%5.3f" uD
575
    "nload" -> printf "%5.3f" uN
576
    "ptags" -> intercalate "," . map (uncurry (printf "%s=%d")) .
577
               Map.toList $ pTags t
578
    "peermap" -> show $ peers t
579
    "spindle_count" -> show $ spindleCount t
580
    "hi_spindles" -> show $ hiSpindles t
581
    "inst_spindles" -> show $ instSpindles t
582
    _ -> T.unknownField
583
  where
584
    T.DynUtil { T.cpuWeight = uC, T.memWeight = uM,
585
                T.dskWeight = uD, T.netWeight = uN } = utilLoad t
586

    
587
-- | Returns the header and numeric propery of a field.
588
showHeader :: String -> (String, Bool)
589
showHeader field =
590
  case field of
591
    "idx" -> ("Index", True)
592
    "name" -> ("Name", False)
593
    "fqdn" -> ("Name", False)
594
    "status" -> ("F", False)
595
    "tmem" -> ("t_mem", True)
596
    "nmem" -> ("n_mem", True)
597
    "xmem" -> ("x_mem", True)
598
    "fmem" -> ("f_mem", True)
599
    "imem" -> ("i_mem", True)
600
    "rmem" -> ("r_mem", True)
601
    "amem" -> ("a_mem", True)
602
    "tdsk" -> ("t_dsk", True)
603
    "fdsk" -> ("f_dsk", True)
604
    "tcpu" -> ("pcpu", True)
605
    "ucpu" -> ("vcpu", True)
606
    "pcnt" -> ("pcnt", True)
607
    "scnt" -> ("scnt", True)
608
    "plist" -> ("primaries", True)
609
    "slist" -> ("secondaries", True)
610
    "pfmem" -> ("p_fmem", True)
611
    "pfdsk" -> ("p_fdsk", True)
612
    "rcpu"  -> ("r_cpu", True)
613
    "cload" -> ("lCpu", True)
614
    "mload" -> ("lMem", True)
615
    "dload" -> ("lDsk", True)
616
    "nload" -> ("lNet", True)
617
    "ptags" -> ("PrimaryTags", False)
618
    "peermap" -> ("PeerMap", False)
619
    "spindle_count" -> ("NodeSpindles", True)
620
    "hi_spindles" -> ("MaxSpindles", True)
621
    "inst_spindles" -> ("InstSpindles", True)
622
    -- TODO: add node fields (group.uuid, group)
623
    _ -> (T.unknownField, False)
624

    
625
-- | String converter for the node list functionality.
626
list :: [String] -> Node -> [String]
627
list fields t = map (showField t) fields
628

    
629
-- | Constant holding the fields we're displaying by default.
630
defaultFields :: [String]
631
defaultFields =
632
  [ "status", "name", "tmem", "nmem", "imem", "xmem", "fmem"
633
  , "rmem", "tdsk", "fdsk", "tcpu", "ucpu", "pcnt", "scnt"
634
  , "pfmem", "pfdsk", "rcpu"
635
  , "cload", "mload", "dload", "nload" ]
636

    
637
-- | Split a list of nodes into a list of (node group UUID, list of
638
-- associated nodes).
639
computeGroups :: [Node] -> [(T.Gdx, [Node])]
640
computeGroups nodes =
641
  let nodes' = sortBy (comparing group) nodes
642
      nodes'' = groupBy (\a b -> group a == group b) nodes'
643
  in map (\nl -> (group (head nl), nl)) nodes''