Skip to content

Instantly share code, notes, and snippets.

@julian-klode
Last active September 4, 2019 08:55
Show Gist options
  • Save julian-klode/2631d69c1ac5464b706318e3932427b7 to your computer and use it in GitHub Desktop.
Save julian-klode/2631d69c1ac5464b706318e3932427b7 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"log"
"os"
"path/filepath"
"runtime/pprof"
)
// FilePackageTuple is a tuple consisting of a file name and a package name
// containing the file.
type FilePackageTuple struct {
File string
Package string
}
func process(list string) []FilePackageTuple {
result := make([]FilePackageTuple, 0, 16)
file, _ := os.Open(list)
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
if err != nil {
break
}
result = append(result, FilePackageTuple{File: line, Package: list})
}
file.Close()
return result
}
func processChan(list string, out chan []FilePackageTuple) {
out <- process(list)
}
// NewFileToPackageMap reads a map file -> [pkg] from dpkg
func NewFileToPackageMap() map[string][]string {
match, err := filepath.Glob("/var/lib/dpkg/info/*.list")
if err != nil {
log.Fatalf("%s", err)
}
fileToPkg := make(map[string][]string, 1024*16)
// Process this bastard in parallel
out := make(chan []FilePackageTuple)
for _, list := range match {
go processChan(list, out)
}
for _ = range match {
res := <-out
for _, t := range res {
fileToPkg[t.File] = append(fileToPkg[t.File], t.Package)
}
}
close(out)
return fileToPkg
}
func main() {
f, err := os.Create("cpu")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
_ = NewFileToPackageMap()
/*for file, pkgs := range fileToPkg {
fmt.Printf("%s => %v\n", file, pkgs)
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment