spec.go 2.5 KB
Newer Older
Ryan Goodfellow's avatar
Ryan Goodfellow committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
package hw

type Procs struct {
	Count int
	Spec  Proc
}

type Dimms struct {
	Count int
	Spec  Dimm
}

type Nics struct {
	Count int
	Spec  Nic
}

type Disks struct {
	Count int
	Spec  Disk
}

// DeviceSpec contains a keyed set of components. The semantics behind this data
// structure are that they key uniquely identifies the object, and that when a
// new object is added with the same key the count is simply incremented. This
// is uesful for building device specifications that contain many of the same
// parts. For example consider a computer with 8 identical ram sticks, or 10
// identical drives. This data structure contains the spec for just one and
// increments a counter.
type DeviceSpec struct {
	Base
	Procs  map[string]*Procs
	Memory map[string]*Dimms
	Nics   map[string]*Nics
	Disks  map[string]*Disks
}

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
func (s DeviceSpec) CoreCount() int {

	c := 0
	for _, p := range s.Procs {
		c += p.Count * int(p.Spec.Cores)
	}

	return c

}

func (s DeviceSpec) MemoryCapacity() uint64 {

	c := uint64(0)
	for _, m := range s.Memory {
		c += uint64(m.Count) * m.Spec.Capacity
	}

	return c

}

func (s DeviceSpec) DiskCapacity() uint64 {

	c := uint64(0)
	for _, m := range s.Disks {
		c += uint64(m.Count) * m.Spec.Capacity
	}

	return c

}

func (s DeviceSpec) Bandwidth() uint64 {

	c := uint64(0)
	for _, n := range s.Nics {
		for _, p := range n.Spec.Ports {
			c += uint64(n.Count) * p.Capacity
		}
	}

	return c

}

Ryan Goodfellow's avatar
Ryan Goodfellow committed
84 85
func (s *DeviceSpec) AddProc(x Proc) {

86
	_, ok := s.Procs[x.SKU]
Ryan Goodfellow's avatar
Ryan Goodfellow committed
87
	if ok {
88
		s.Procs[x.SKU].Count += 1
Ryan Goodfellow's avatar
Ryan Goodfellow committed
89
	} else {
90
		s.Procs[x.SKU] = &Procs{
Ryan Goodfellow's avatar
Ryan Goodfellow committed
91 92 93 94 95 96 97 98 99
			Count: 1,
			Spec:  x,
		}
	}

}

func (s *DeviceSpec) AddMemory(x Dimm) {

100
	_, ok := s.Memory[x.SKU]
Ryan Goodfellow's avatar
Ryan Goodfellow committed
101
	if ok {
102
		s.Memory[x.SKU].Count += 1
Ryan Goodfellow's avatar
Ryan Goodfellow committed
103
	} else {
104
		s.Memory[x.SKU] = &Dimms{
Ryan Goodfellow's avatar
Ryan Goodfellow committed
105 106 107 108 109 110 111 112 113
			Count: 1,
			Spec:  x,
		}
	}

}

func (s *DeviceSpec) AddNic(x Nic) {

114
	_, ok := s.Nics[x.SKU]
Ryan Goodfellow's avatar
Ryan Goodfellow committed
115
	if ok {
116
		s.Nics[x.SKU].Count += 1
Ryan Goodfellow's avatar
Ryan Goodfellow committed
117
	} else {
118
		s.Nics[x.SKU] = &Nics{
Ryan Goodfellow's avatar
Ryan Goodfellow committed
119 120 121 122 123 124 125 126 127
			Count: 1,
			Spec:  x,
		}
	}

}

func (s *DeviceSpec) AddDisk(x Disk) {

128
	_, ok := s.Disks[x.SKU]
Ryan Goodfellow's avatar
Ryan Goodfellow committed
129
	if ok {
130
		s.Disks[x.SKU].Count += 1
Ryan Goodfellow's avatar
Ryan Goodfellow committed
131
	} else {
132
		s.Disks[x.SKU] = &Disks{
Ryan Goodfellow's avatar
Ryan Goodfellow committed
133 134 135 136 137 138 139 140 141 142
			Count: 1,
			Spec:  x,
		}
	}

}

func NewSpec(d *Device) *DeviceSpec {

	s := &DeviceSpec{
Ryan Goodfellow's avatar
Ryan Goodfellow committed
143
		Base:   d.Base,
Ryan Goodfellow's avatar
Ryan Goodfellow committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
		Procs:  make(map[string]*Procs),
		Memory: make(map[string]*Dimms),
		Nics:   make(map[string]*Nics),
		Disks:  make(map[string]*Disks),
	}

	for _, x := range d.Procs {
		s.AddProc(x)
	}

	for _, x := range d.Memory {
		s.AddMemory(x)
	}

	for _, x := range d.Nics {
		s.AddNic(x)
	}

	for _, x := range d.Disks {
		s.AddDisk(x)
	}

	return s
}