|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "runtime" |
| 10 | + "runtime/pprof" |
| 11 | + |
| 12 | + tbio "github.com/wchargin/tensorboard-data-server/io" |
| 13 | +) |
| 14 | + |
| 15 | +var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") |
| 16 | +var memprofile = flag.String("memprofile", "", "write memory profile to this file") |
| 17 | + |
| 18 | +var bufsize = flag.Int("bufsize", 8192, "bufio.NewReaderSize capacity") |
| 19 | + |
| 20 | +var checksum = flag.Bool("checksum", false, "validate TFRecord payloads against CRC-32") |
| 21 | + |
| 22 | +func main() { |
| 23 | + flag.Parse() |
| 24 | + args := flag.Args() |
| 25 | + if len(args) != 1 { |
| 26 | + fmt.Fprintf(os.Stderr, "must specify event file path as only argument\n") |
| 27 | + os.Exit(1) |
| 28 | + } |
| 29 | + |
| 30 | + // Profiling structure copied from "go doc runtime/pprof". |
| 31 | + if *cpuprofile != "" { |
| 32 | + f, err := os.Create(*cpuprofile) |
| 33 | + if err != nil { |
| 34 | + fmt.Fprintf(os.Stderr, "creating CPU profile: %v\n", err) |
| 35 | + os.Exit(1) |
| 36 | + } |
| 37 | + pprof.StartCPUProfile(f) |
| 38 | + defer pprof.StopCPUProfile() |
| 39 | + } |
| 40 | + |
| 41 | + ReadAllRecords(args[0]) |
| 42 | + |
| 43 | + if *memprofile != "" { |
| 44 | + fmt.Println("creating memory profile...") |
| 45 | + f, err := os.Create(*memprofile) |
| 46 | + if err != nil { |
| 47 | + fmt.Fprintf(os.Stderr, "creating memory profile: %v\n", err) |
| 48 | + os.Exit(1) |
| 49 | + } |
| 50 | + defer f.Close() |
| 51 | + runtime.GC() |
| 52 | + if err := pprof.WriteHeapProfile(f); err != nil { |
| 53 | + fmt.Fprintf(os.Stderr, "writing memory profile: %v\n", err) |
| 54 | + } |
| 55 | + fmt.Println("done with memory profile") |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func ReadAllRecords(fileName string) { |
| 60 | + rawFile, err := os.Open(fileName) |
| 61 | + if err != nil { |
| 62 | + panic(err) |
| 63 | + } |
| 64 | + f := bufio.NewReaderSize(rawFile, *bufsize) |
| 65 | + recordsRead := 0 |
| 66 | + totalPayloadSize := 0 |
| 67 | + for { |
| 68 | + payloadSize, more := ProcessOneRecord(f) |
| 69 | + recordsRead++ |
| 70 | + totalPayloadSize += payloadSize |
| 71 | + if !more { |
| 72 | + break |
| 73 | + } |
| 74 | + } |
| 75 | + fmt.Printf("all done; read %v records (%v bytes payload)\n", recordsRead, totalPayloadSize) |
| 76 | +} |
| 77 | + |
| 78 | +func ProcessOneRecord(r io.Reader) (payloadSize int, more bool) { |
| 79 | + var state *tbio.TFRecordState |
| 80 | + rec, err := tbio.ReadRecord(&state, r) |
| 81 | + if err == io.EOF { |
| 82 | + return 0, false |
| 83 | + } |
| 84 | + if err != nil { |
| 85 | + fmt.Fprintf(os.Stderr, "reading record: %v\n", err) |
| 86 | + os.Exit(1) |
| 87 | + } |
| 88 | + if *checksum { |
| 89 | + if err := rec.Checksum(); err != nil { |
| 90 | + fmt.Fprintf(os.Stderr, "checksum failure: %v\n", err) |
| 91 | + } |
| 92 | + } |
| 93 | + return len(rec.Data), true |
| 94 | +} |
0 commit comments