2018-12-29 13:04:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2019-07-19 14:52:46 +00:00
|
|
|
"hash"
|
2018-12-29 13:04:17 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
|
|
|
|
2022-12-10 10:15:37 +00:00
|
|
|
"github.com/TrueCloudLab/tzhash/tz"
|
2022-03-09 13:58:21 +00:00
|
|
|
"golang.org/x/sys/cpu"
|
2018-12-29 13:04:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
|
|
|
|
memprofile = flag.String("memprofile", "", "write memory profile to `file`")
|
|
|
|
filename = flag.String("name", "-", "file to use")
|
2019-07-19 15:59:41 +00:00
|
|
|
hashimpl = flag.String("impl", "", "implementation to use")
|
2018-12-29 13:04:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var (
|
|
|
|
f io.Reader
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
if *cpuprofile != "" {
|
|
|
|
f, err := os.Create(*cpuprofile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("could not create CPU profile: ", err)
|
|
|
|
}
|
|
|
|
if err := pprof.StartCPUProfile(f); err != nil {
|
|
|
|
log.Fatal("could not start CPU profile: ", err)
|
|
|
|
}
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
|
|
|
if *filename != "-" {
|
|
|
|
if f, err = os.Open(*filename); err != nil {
|
|
|
|
log.Fatal("could not open file: ", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
f = os.Stdin
|
|
|
|
}
|
|
|
|
|
2022-03-09 13:58:21 +00:00
|
|
|
// Override CPU feature flags to make sure a proper backend is used.
|
2019-07-19 14:52:46 +00:00
|
|
|
var h hash.Hash
|
|
|
|
switch *hashimpl {
|
|
|
|
case "avx":
|
2022-03-09 13:58:21 +00:00
|
|
|
cpu.X86.HasAVX = true
|
|
|
|
cpu.X86.HasAVX2 = false
|
|
|
|
h = tz.New()
|
2019-07-19 14:52:46 +00:00
|
|
|
case "avx2":
|
2022-03-09 13:58:21 +00:00
|
|
|
cpu.X86.HasAVX = true
|
|
|
|
cpu.X86.HasAVX2 = true
|
2019-07-19 14:52:46 +00:00
|
|
|
h = tz.New()
|
2022-03-09 13:58:21 +00:00
|
|
|
case "generic":
|
|
|
|
cpu.X86.HasAVX = false
|
|
|
|
cpu.X86.HasAVX2 = false
|
|
|
|
h = tz.New()
|
|
|
|
default:
|
|
|
|
log.Fatalf("Invalid backend: %s", *hashimpl)
|
2019-07-19 14:52:46 +00:00
|
|
|
}
|
|
|
|
|
2018-12-29 13:04:17 +00:00
|
|
|
if _, err := io.Copy(h, f); err != nil {
|
|
|
|
log.Fatal("error while reading file: ", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("%x\t%s\n", h.Sum(nil), *filename)
|
|
|
|
|
|
|
|
if *memprofile != "" {
|
|
|
|
f, err := os.Create(*memprofile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("could not create memory profile: ", err)
|
|
|
|
}
|
|
|
|
runtime.GC() // get up-to-date statistics
|
|
|
|
if err := pprof.WriteHeapProfile(f); err != nil {
|
|
|
|
log.Fatal("could not write memory profile: ", err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}
|