mirror of
https://github.com/jimeh/rbheap.git
synced 2026-02-19 12:56:46 +00:00
Improve things in main.go
This commit is contained in:
28
diffsect.go
Normal file
28
diffsect.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
// diffsect removes all items in `a` from `b`, then removes all items from `b`
|
||||||
|
// which are not in `c`. Effectively: intersect(difference(b, a), c)
|
||||||
|
func diffsect(a, b, c *[]string) *[]string {
|
||||||
|
result := []string{}
|
||||||
|
mapA := map[string]bool{}
|
||||||
|
mapC := map[string]bool{}
|
||||||
|
|
||||||
|
for _, x := range *a {
|
||||||
|
mapA[x] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, x := range *c {
|
||||||
|
mapC[x] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, x := range *b {
|
||||||
|
_, okA := mapA[x]
|
||||||
|
_, okC := mapC[x]
|
||||||
|
|
||||||
|
if !okA && okC {
|
||||||
|
result = append(result, x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &result
|
||||||
|
}
|
||||||
73
main.go
73
main.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"gopkg.in/alecthomas/kingpin.v2"
|
"gopkg.in/alecthomas/kingpin.v2"
|
||||||
)
|
)
|
||||||
@@ -15,7 +16,9 @@ var (
|
|||||||
date = "unknown"
|
date = "unknown"
|
||||||
|
|
||||||
formatFlag = kingpin.Flag("format", "Output format (\"hex\" or \"full\")").
|
formatFlag = kingpin.Flag("format", "Output format (\"hex\" or \"full\")").
|
||||||
Default("hex").String()
|
Short('f').Default("hex").String()
|
||||||
|
silentFlag = kingpin.Flag("silent", "Silence all info output").
|
||||||
|
Short('s').Bool()
|
||||||
|
|
||||||
file1Path = kingpin.Arg("dump-1", "Path to first heap dump file.").
|
file1Path = kingpin.Arg("dump-1", "Path to first heap dump file.").
|
||||||
Required().String()
|
Required().String()
|
||||||
@@ -27,46 +30,25 @@ var (
|
|||||||
|
|
||||||
func versionString() string {
|
func versionString() string {
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
|
var meta []string
|
||||||
|
|
||||||
buffer.WriteString(fmt.Sprintf("%s %s", name, version))
|
buffer.WriteString(fmt.Sprintf("%s %s", name, version))
|
||||||
|
|
||||||
if commit != "unknown" {
|
if commit != "unknown" {
|
||||||
buffer.WriteString(fmt.Sprintf(" (%s", commit))
|
meta = append(meta, commit)
|
||||||
if date != "unknown" {
|
}
|
||||||
buffer.WriteString(fmt.Sprintf(" %s", date))
|
|
||||||
}
|
if date != "unknown" {
|
||||||
buffer.WriteString(")")
|
meta = append(meta, date)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(meta) > 0 {
|
||||||
|
buffer.WriteString(fmt.Sprintf(" (%s)", strings.Join(meta, ", ")))
|
||||||
}
|
}
|
||||||
|
|
||||||
return buffer.String()
|
return buffer.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// diffsect removes all items in `a` from `b`, then removes all items from `b`
|
|
||||||
// which are not in `c`. Effectively: intersect(difference(b, a), c)
|
|
||||||
func diffsect(a, b, c *[]string) *[]string {
|
|
||||||
result := []string{}
|
|
||||||
mapA := map[string]bool{}
|
|
||||||
mapC := map[string]bool{}
|
|
||||||
|
|
||||||
for _, x := range *a {
|
|
||||||
mapA[x] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, x := range *c {
|
|
||||||
mapC[x] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, x := range *b {
|
|
||||||
_, okA := mapA[x]
|
|
||||||
_, okC := mapC[x]
|
|
||||||
|
|
||||||
if !okA && okC {
|
|
||||||
result = append(result, x)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &result
|
|
||||||
}
|
|
||||||
|
|
||||||
func printHexDiff(leaked *[]string, dump *HeapDump) {
|
func printHexDiff(leaked *[]string, dump *HeapDump) {
|
||||||
for _, index := range *leaked {
|
for _, index := range *leaked {
|
||||||
if item, ok := dump.Entries[index]; ok {
|
if item, ok := dump.Entries[index]; ok {
|
||||||
@@ -75,30 +57,37 @@ func printHexDiff(leaked *[]string, dump *HeapDump) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logMsg(msg string) {
|
||||||
|
if !*silentFlag {
|
||||||
|
fmt.Println(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadDump(filePath string) (*HeapDump, error) {
|
||||||
|
logMsg(fmt.Sprintf("--> Loading %s...", filePath))
|
||||||
|
dump, err := NewHeapDump(filePath)
|
||||||
|
logMsg(fmt.Sprintf(" Loaded %d addresses", len(dump.Index)))
|
||||||
|
return dump, err
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
kingpin.Version(versionString())
|
kingpin.Version(versionString())
|
||||||
kingpin.Parse()
|
kingpin.Parse()
|
||||||
|
|
||||||
fmt.Printf("--> Loading %s...\n", *file1Path)
|
dump1, err := loadDump(*file1Path)
|
||||||
dump1, err := NewHeapDump(*file1Path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Printf(" Loaded %d addresses\n", len(dump1.Index))
|
|
||||||
|
|
||||||
fmt.Printf("--> Loading %s...\n", *file2Path)
|
dump2, err := loadDump(*file2Path)
|
||||||
dump2, err := NewHeapDump(*file2Path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Printf(" Loaded %d addresses\n", len(dump2.Index))
|
|
||||||
|
|
||||||
fmt.Printf("--> Loading %s...\n", *file3Path)
|
dump3, err := loadDump(*file3Path)
|
||||||
dump3, err := NewHeapDump(*file3Path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Printf(" Loaded %d addresses\n", len(dump3.Index))
|
|
||||||
|
|
||||||
leaked := diffsect(&dump1.Index, &dump2.Index, &dump3.Index)
|
leaked := diffsect(&dump1.Index, &dump2.Index, &dump3.Index)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user