Skip to content

Commit

Permalink
Add PGO
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Apr 20, 2023
1 parent 9f78585 commit 248ef3a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ endif

export CGO_ENABLED ?= 0
BUILD_PKG = ./cmd/catp
BUILD_LDFLAGS=-s -w
BUILD_FLAGS=-trimpath -pgo=./cmd/catp.pgo

-include $(DEVGO_PATH)/makefiles/main.mk
-include $(DEVGO_PATH)/makefiles/lint.mk
Expand All @@ -39,5 +41,3 @@ BUILD_PKG = ./cmd/catp

# Add your custom targets here.

## Run tests
test: test-unit
Binary file added cmd/catp.pgo
Binary file not shown.
70 changes: 65 additions & 5 deletions cmd/catp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package main

import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"log"
"os"
"runtime/pprof"
"strings"
"time"

"github.com/bool64/progress"
"github.com/klauspost/compress/zstd"
gzip "github.com/klauspost/pgzip"
_ "gitlab.com/rackn/seekable-zstd"
)

type runner struct {
Expand All @@ -22,6 +23,9 @@ type runner struct {
readBytes int64
totalBytes int64

grep [][]byte
reverse bool

currentFile *progress.CountingReader
currentTotal int64
}
Expand All @@ -44,15 +48,34 @@ func (r *runner) st(s progress.ProgressStatus) string {
return res
}

func readFile(r io.Reader) {
rd := bufio.NewReader(r)
func (r *runner) readFile(rd io.Reader) {
b := bufio.NewReaderSize(rd, 64*1024)

_, err := io.Copy(os.Stdout, rd)
_, err := io.Copy(os.Stdout, b)
if err != nil {
log.Fatal(err)
}
}

func (r *runner) scanFile(rd io.Reader) {
s := bufio.NewScanner(rd)
s.Buffer(make([]byte, 64*1024), 10*1024*1024)

for s.Scan() {
for _, g := range r.grep {
if bytes.Contains(s.Bytes(), g) {
_, _ = os.Stdout.Write(s.Bytes())

break
}
}
}

if err := s.Err(); err != nil {
log.Fatal(err)
}
}

func (r *runner) cat(filename string) {
file, err := os.Open(filename)
if err != nil {
Expand All @@ -75,6 +98,10 @@ func (r *runner) cat(filename string) {
}
}

if r.reverse {

}

r.pr.Start(func(t *progress.Task) {
t.TotalBytes = func() int64 {
return r.totalBytes
Expand All @@ -86,16 +113,49 @@ func (r *runner) cat(filename string) {
t.Task = filename
t.Continue = true
})
readFile(rd)

if len(r.grep) > 0 || r.reverse {
r.scanFile(rd)
} else {
r.readFile(rd)
}

r.pr.Stop()
r.readBytes += r.currentFile.Bytes()
}

func main() {
grep := flag.String("grep", "", "grep pattern, may contain multiple patterns separated by \\|")
cpuProfile := flag.String("dbg-cpu-prof", "", "write first 10 seconds of CPU profile to file")

flag.Parse()

if *cpuProfile != "" {
f, err := os.Create(*cpuProfile) //nolint:gosec
if err != nil {
log.Fatal(err)
}

if err = pprof.StartCPUProfile(f); err != nil {
log.Fatal(err)
}

go func() {
time.Sleep(10 * time.Second)
pprof.StopCPUProfile()
println("CPU profile written to", *cpuProfile)
}()
}

r := &runner{}
r.reverse = *reverse

if *grep != "" {
for _, s := range strings.Split(*grep, "\\|") {
r.grep = append(r.grep, []byte(s))
}
}

r.sizes = make(map[string]int64)
r.pr = &progress.Progress{
Interval: 5 * time.Second,
Expand Down
6 changes: 0 additions & 6 deletions cmd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,4 @@ require (
github.com/bool64/progress v0.1.0
github.com/klauspost/compress v1.16.5
github.com/klauspost/pgzip v1.2.5
gitlab.com/rackn/seekable-zstd v0.8.1
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
gitlab.com/rackn/simplecache v0.0.0-20230324193231-44368de53d93 // indirect
)
10 changes: 0 additions & 10 deletions cmd/go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
github.com/bool64/dev v0.2.27 h1:mFT+B74mFVgUeUmm/EbfM6ELPA55lEXBjQ/AOHCwCOc=
github.com/bool64/progress v0.1.0 h1:khsGInrTrQMyskfcRTE9gP982koSqKobYFJ977JLxhA=
github.com/bool64/progress v0.1.0/go.mod h1:XwIA2+r2sEKxIRa6TFR0FqD3GHt0wz4dk4IL5CwbuKo=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI=
github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE=
github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
gitlab.com/rackn/seekable-zstd v0.8.1 h1:vemYjUK3hr1uSipdVQGKX/SOOlWUr1kXTfjfgLfCPVE=
gitlab.com/rackn/seekable-zstd v0.8.1/go.mod h1:9z8nf3qNXOi73VRm7KQgTmI3T0tz9YzDKKL7fzEBz9M=
gitlab.com/rackn/simplecache v0.0.0-20230324193231-44368de53d93 h1:lXoXk/e9YrtTyWzNZs1ak/ijpwZQDaJLEwKCjhp/dCw=
gitlab.com/rackn/simplecache v0.0.0-20230324193231-44368de53d93/go.mod h1:pXhP0EyrEy0pGf2DW4vTKub/As/UiamLFaZ1Q9YaFTs=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

0 comments on commit 248ef3a

Please sign in to comment.