Skip to content

Commit

Permalink
Periodically run GC in all dgraph commands. (#4032)
Browse files Browse the repository at this point in the history
* Periodically run GC in all dgraph binaries.

Currently, only the bulk loader is making sure the garbage collector is
run periodically. This PR moves that logic to main.go so that it's run
by all dgraph commands.

* fix imports.

* Print glog.Infof
  • Loading branch information
martinmr authored and manishrjain committed Sep 20, 2019
1 parent cb075bf commit 4f15d4d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
23 changes: 0 additions & 23 deletions dgraph/cmd/bulk/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ import (
"runtime"
"strconv"
"strings"
"time"

"github.com/dgraph-io/dgraph/tok"
"github.com/dgraph-io/dgraph/x"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -204,27 +202,6 @@ func run() {
defer os.RemoveAll(opt.TmpDir)
}

// Bulk loader can take up a lot of RAM. So, run GC often.
go func() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()

var lastNum uint32
var ms runtime.MemStats
for range ticker.C {
runtime.ReadMemStats(&ms)
fmt.Printf("GC: %d. InUse: %s. Idle: %s\n", ms.NumGC, humanize.Bytes(ms.HeapInuse),
humanize.Bytes(ms.HeapIdle-ms.HeapReleased))
if ms.NumGC > lastNum {
// GC was already run by the Go runtime. No need to run it again.
lastNum = ms.NumGC
} else {
runtime.GC()
lastNum = ms.NumGC + 1
}
}
}()

loader := newLoader(opt)
if !opt.SkipMapPhase {
loader.mapStage()
Expand Down
25 changes: 25 additions & 0 deletions dgraph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"time"

"github.com/dgraph-io/dgraph/dgraph/cmd"
"github.com/dustin/go-humanize"
"github.com/golang/glog"
)

func main() {
Expand All @@ -30,5 +32,28 @@ func main() {
// improving throughput. The extra CPU overhead is almost negligible in comparison. The
// benchmark notes are located in badger-bench/randread.
runtime.GOMAXPROCS(128)

// Make sure the garbage collector is run periodically.
go func() {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()

var lastNum uint32
var ms runtime.MemStats
for range ticker.C {
runtime.ReadMemStats(&ms)
if ms.NumGC > lastNum {
// GC was already run by the Go runtime. No need to run it again.
lastNum = ms.NumGC
} else {
runtime.GC()
glog.V(2).Infof("GC: %d. InUse: %s. Idle: %s\n", ms.NumGC,
humanize.Bytes(ms.HeapInuse),
humanize.Bytes(ms.HeapIdle-ms.HeapReleased))
lastNum = ms.NumGC + 1
}
}
}()

cmd.Execute()
}

0 comments on commit 4f15d4d

Please sign in to comment.