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 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
// Package mmvdump implements a go port of the C mmvdump utility included in PCP Core
//
// https://github.com/performancecopilot/pcp/blob/master/src/pmdas/mmv/mmvdump.c
//
// It has been written for maximum portability with the C equivalent, without having to use cgo or any other ninja stuff
//
// the main difference is that the reader is separate from the cli with the reading primarily implemented in mmvdump.go while the cli is implemented in cmd/mmvdump
//
// the cli application is completely go gettable and outputs the same things, in mostly the same way as the C cli app, to try it out,
//
// ```
// go get github.com/performancecopilot/speed/mmvdump/cmd/mmvdump
// ```
package mmvdump
import (
"math"
"sync"
"unsafe"
"github.com/pkg/errors"
)
func readHeader(data []byte) (*Header, error) {
if uint64(len(data)) < HeaderLength {
return nil, errors.New("file too small to contain a valid Header")
}
header := (*Header)(unsafe.Pointer(&data[0]))
if m := header.Magic[:3]; string(m) != "MMV" {
return nil, errors.Errorf("Bad Magic: %v", string(m))
}
if header.G1 != header.G2 {
return nil, errors.Errorf("Mismatched version numbers, %v and %v", header.G1, header.G2)
}
return header, nil
}
func readToc(data []byte, offset uint64) (*Toc, error) {
if uint64(len(data)) < offset+TocLength {
return nil, errors.New("Incomplete/Partially Written TOC")
}
return (*Toc)(unsafe.Pointer(&data[offset])), nil
}
type itemReaderFunc func([]byte, uint64, int32) (interface{}, error)
func readInstance(data []byte, offset uint64, version int32) (interface{}, error) {
var InstanceLength = Instance1Length
if version == 2 {
InstanceLength = Instance2Length
}
if uint64(len(data)) < offset+InstanceLength {
return nil, errors.New("Incomplete/Partially Written Instance")
}
if version == 1 {
return (*Instance1)(unsafe.Pointer(&data[offset])), nil
}
return (*Instance2)(unsafe.Pointer(&data[offset])), nil
}
func readInstanceDomain(data []byte, offset uint64, version int32) (interface{}, error) {
if uint64(len(data)) < offset+InstanceDomainLength {
return nil, errors.New("Incomplete/Partially Written InstanceDomain")
}
return (*InstanceDomain)(unsafe.Pointer(&data[offset])), nil
}
func readMetric(data []byte, offset uint64, version int32) (interface{}, error) {
var MetricLength = Metric1Length
if version == 2 {
MetricLength = Metric2Length
}
if uint64(len(data)) < offset+MetricLength {
return nil, errors.New("Incomplete/Partially Written Metric")
}
if version == 1 {
return (*Metric1)(unsafe.Pointer(&data[offset])), nil
}
return (*Metric2)(unsafe.Pointer(&data[offset])), nil
}
func readValue(data []byte, offset uint64, version int32) (interface{}, error) {
if uint64(len(data)) < offset+ValueLength {
return nil, errors.New("Incomplete/Partially Written Value")
}
return (*Value)(unsafe.Pointer(&data[offset])), nil
}
func readString(data []byte, offset uint64, version int32) (interface{}, error) {
if uint64(len(data)) < offset+StringLength {
return nil, errors.New("Incomplete/Partially Written String")
}
return (*String)(unsafe.Pointer(&data[offset])), nil
}
func readTocs(data []byte, count int32) ([]*Toc, error) {
tocs := make([]*Toc, count)
for i := int32(0); i < count; i++ {
t, err := readToc(data, HeaderLength+uint64(i)*TocLength)
if err != nil {
return nil, err
}
tocs[i] = t
}
return tocs, nil
}
func readItems(data []byte, offset uint64, count int32, itemlength uint64, readItem itemReaderFunc, version int32) (map[uint64]interface{}, error) {
var wg sync.WaitGroup
wg.Add(int(count))
items := make(map[uint64]interface{})
var (
err error
m sync.Mutex
)
for i := int32(0); i < count; i, offset = i+1, offset+itemlength {
go func(offset uint64) {
if err == nil {
item, ierr := readItem(data, offset, version)
if ierr == nil {
m.Lock()
items[offset] = item
m.Unlock()
} else {
err = ierr
}
}
wg.Done()
}(offset)
}
wg.Wait()
if err != nil {
return nil, err
}
return items, nil
}
func readInstances(data []byte, offset uint64, count int32, version int32) (map[uint64]Instance, error) {
InstanceLength := Instance1Length
if version == 2 {
InstanceLength = Instance2Length
}
i, err := readItems(data, offset, count, InstanceLength, readInstance, version)
if err != nil {
return nil, err
}
instances := make(map[uint64]Instance)
for off, val := range i {
instances[off] = val.(Instance)
}
return instances, nil
}
func readInstanceDomains(data []byte, offset uint64, count int32, version int32) (map[uint64]*InstanceDomain, error) {
i, err := readItems(data, offset, count, InstanceDomainLength, readInstanceDomain, version)
if err != nil {
return nil, err
}
indoms := make(map[uint64]*InstanceDomain)
for off, val := range i {
indoms[off] = val.(*InstanceDomain)
}
return indoms, nil
}
func readMetrics(data []byte, offset uint64, count int32, version int32) (map[uint64]Metric, error) {
var MetricLength = Metric1Length
if version == 2 {
MetricLength = Metric2Length
}
m, err := readItems(data, offset, count, MetricLength, readMetric, version)
if err != nil {
return nil, err
}
metrics := make(map[uint64]Metric)
for off, val := range m {
metrics[off] = val.(Metric)
}
return metrics, nil
}
func readValues(data []byte, offset uint64, count int32, version int32) (map[uint64]*Value, error) {
v, err := readItems(data, offset, count, ValueLength, readValue, version)
if err != nil {
return nil, err
}
values := make(map[uint64]*Value)
for off, val := range v {
values[off] = val.(*Value)
}
return values, nil
}
func readStrings(data []byte, offset uint64, count int32, version int32) (map[uint64]*String, error) {
s, err := readItems(data, offset, count, StringLength, readString, version)
if err != nil {
return nil, err
}
strings := make(map[uint64]*String)
for off, val := range s {
strings[off] = val.(*String)
}
return strings, nil
}
func readComponents(data []byte, tocs []*Toc, version int32) (
metrics map[uint64]Metric,
values map[uint64]*Value,
instances map[uint64]Instance,
indoms map[uint64]*InstanceDomain,
strings map[uint64]*String,
ierr, inerr, merr, verr, serr error,
) {
var wg sync.WaitGroup
wg.Add(len(tocs))
for _, toc := range tocs {
switch toc.Type {
case TocInstances:
go func(offset uint64, count int32) {
instances, ierr = readInstances(data, offset, count, version)
wg.Done()
}(toc.Offset, toc.Count)
case TocIndoms:
go func(offset uint64, count int32) {
indoms, inerr = readInstanceDomains(data, offset, count, version)
wg.Done()
}(toc.Offset, toc.Count)
case TocMetrics:
go func(offset uint64, count int32) {
metrics, merr = readMetrics(data, offset, count, version)
wg.Done()
}(toc.Offset, toc.Count)
case TocValues:
go func(offset uint64, count int32) {
values, verr = readValues(data, offset, count, version)
wg.Done()
}(toc.Offset, toc.Count)
case TocStrings:
go func(offset uint64, count int32) {
strings, serr = readStrings(data, offset, count, version)
wg.Done()
}(toc.Offset, toc.Count)
}
}
wg.Wait()
return
}
// Dump creates a data dump from the passed data
func Dump(data []byte) (
h *Header,
tocs []*Toc,
metrics map[uint64]Metric,
values map[uint64]*Value,
instances map[uint64]Instance,
indoms map[uint64]*InstanceDomain,
strings map[uint64]*String,
err error,
) {
h, err = readHeader(data)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, err
}
tocs, err = readTocs(data, h.Toc)
if err != nil {
return nil, nil, nil, nil, nil, nil, nil, err
}
var ierr, inerr, merr, verr, serr error
metrics, values, instances, indoms, strings, ierr, inerr, merr, verr, serr = readComponents(data, tocs, h.Version)
switch {
case ierr != nil:
return nil, nil, nil, nil, nil, nil, nil, ierr
case inerr != nil:
return nil, nil, nil, nil, nil, nil, nil, inerr
case merr != nil:
return nil, nil, nil, nil, nil, nil, nil, merr
case verr != nil:
return nil, nil, nil, nil, nil, nil, nil, verr
case serr != nil:
return nil, nil, nil, nil, nil, nil, nil, serr
}
return
}
// FixedVal will infer a fixed size value from the passed data
func FixedVal(data uint64, t Type) (interface{}, error) {
switch t {
case Int32Type:
return int32(data), nil
case Uint32Type:
return uint32(data), nil
case Int64Type:
return int64(data), nil
case Uint64Type:
return data, nil
case FloatType:
return math.Float32frombits(uint32(data)), nil
case DoubleType:
return math.Float64frombits(data), nil
}
return nil, errors.New("invalid type")
}
|