parseHeapSample parses a single row from a heap profile into a new Sample.
(line string, rate int64, sampling string, includeAlloc bool)
| 587 | |
| 588 | // parseHeapSample parses a single row from a heap profile into a new Sample. |
| 589 | func parseHeapSample(line string, rate int64, sampling string, includeAlloc bool) (value []int64, blocksize int64, addrs []uint64, err error) { |
| 590 | sampleData := heapSampleRE.FindStringSubmatch(line) |
| 591 | if len(sampleData) != 6 { |
| 592 | return nil, 0, nil, fmt.Errorf("unexpected number of sample values: got %d, want 6", len(sampleData)) |
| 593 | } |
| 594 | |
| 595 | // This is a local-scoped helper function to avoid needing to pass |
| 596 | // around rate, sampling and many return parameters. |
| 597 | addValues := func(countString, sizeString string, label string) error { |
| 598 | count, err := strconv.ParseInt(countString, 10, 64) |
| 599 | if err != nil { |
| 600 | return fmt.Errorf("malformed sample: %s: %v", line, err) |
| 601 | } |
| 602 | size, err := strconv.ParseInt(sizeString, 10, 64) |
| 603 | if err != nil { |
| 604 | return fmt.Errorf("malformed sample: %s: %v", line, err) |
| 605 | } |
| 606 | if count == 0 && size != 0 { |
| 607 | return fmt.Errorf("%s count was 0 but %s bytes was %d", label, label, size) |
| 608 | } |
| 609 | if count != 0 { |
| 610 | blocksize = size / count |
| 611 | if sampling == "v2" { |
| 612 | count, size = scaleHeapSample(count, size, rate) |
| 613 | } |
| 614 | } |
| 615 | value = append(value, count, size) |
| 616 | return nil |
| 617 | } |
| 618 | |
| 619 | if includeAlloc { |
| 620 | if err := addValues(sampleData[3], sampleData[4], "allocation"); err != nil { |
| 621 | return nil, 0, nil, err |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | if err := addValues(sampleData[1], sampleData[2], "inuse"); err != nil { |
| 626 | return nil, 0, nil, err |
| 627 | } |
| 628 | |
| 629 | addrs, err = parseHexAddresses(sampleData[5]) |
| 630 | if err != nil { |
| 631 | return nil, 0, nil, fmt.Errorf("malformed sample: %s: %v", line, err) |
| 632 | } |
| 633 | |
| 634 | return value, blocksize, addrs, nil |
| 635 | } |
| 636 | |
| 637 | // parseHexAddresses extracts hex numbers from a string, attempts to convert |
| 638 | // each to an unsigned 64-bit number and returns the resulting numbers as a |
no test coverage detected
searching dependent graphs…