github.com/dshekhar95/sub_dgraph@v0.0.0-20230424164411-6be28e40bbf1/dgraph/main.go (about) 1 /* 2 * Copyright 2016-2022 Dgraph Labs, Inc. and Contributors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "math/rand" 21 "runtime" 22 "time" 23 24 "github.com/dustin/go-humanize" 25 "github.com/golang/glog" 26 27 "github.com/dgraph-io/dgraph/dgraph/cmd" 28 "github.com/dgraph-io/ristretto/z" 29 ) 30 31 func main() { 32 rand.Seed(time.Now().UnixNano()) 33 // Setting a higher number here allows more disk I/O calls to be scheduled, hence considerably 34 // improving throughput. The extra CPU overhead is almost negligible in comparison. The 35 // benchmark notes are located in badger-bench/randread. 36 runtime.GOMAXPROCS(128) 37 38 absDiff := func(a, b uint64) uint64 { 39 if a > b { 40 return a - b 41 } 42 return b - a 43 } 44 45 ticker := time.NewTicker(10 * time.Second) 46 47 // Make sure the garbage collector is run periodically. 48 go func() { 49 minDiff := uint64(2 << 30) 50 51 var ms runtime.MemStats 52 var lastMs runtime.MemStats 53 var lastNumGC uint32 54 55 var js z.MemStats 56 var lastAlloc uint64 57 58 for range ticker.C { 59 // Read Jemalloc stats first. Print if there's a big difference. 60 z.ReadMemStats(&js) 61 if diff := absDiff(uint64(z.NumAllocBytes()), lastAlloc); diff > 1<<30 { 62 glog.V(2).Infof("NumAllocBytes: %s jemalloc: Active %s Allocated: %s"+ 63 " Resident: %s Retained: %s\n", 64 humanize.IBytes(uint64(z.NumAllocBytes())), 65 humanize.IBytes(js.Active), humanize.IBytes(js.Allocated), 66 humanize.IBytes(js.Resident), humanize.IBytes(js.Retained)) 67 lastAlloc = uint64(z.NumAllocBytes()) 68 } else { 69 // Don't update the lastJs here. 70 } 71 72 runtime.ReadMemStats(&ms) 73 diff := absDiff(ms.HeapAlloc, lastMs.HeapAlloc) 74 75 switch { 76 case ms.NumGC > lastNumGC: 77 // GC was already run by the Go runtime. No need to run it again. 78 lastNumGC = ms.NumGC 79 lastMs = ms 80 81 case diff < minDiff: 82 // Do not run the GC if the allocated memory has not shrunk or expanded by 83 // more than 0.5GB since the last time the memory stats were collected. 84 lastNumGC = ms.NumGC 85 // Nobody ran a GC. Don't update lastMs. 86 87 case ms.NumGC == lastNumGC: 88 runtime.GC() 89 glog.V(2).Infof("GC: %d. InUse: %s. Idle: %s. jemalloc: %s.\n", ms.NumGC, 90 humanize.IBytes(ms.HeapInuse), 91 humanize.IBytes(ms.HeapIdle-ms.HeapReleased), 92 humanize.IBytes(js.Active)) 93 lastNumGC = ms.NumGC + 1 94 lastMs = ms 95 } 96 } 97 }() 98 99 // Run the program. 100 cmd.Execute() 101 ticker.Stop() 102 103 glog.V(2).Infof("Num Allocated Bytes at program end: %d", z.NumAllocBytes()) 104 if z.NumAllocBytes() > 0 { 105 glog.Warningf("MEMORY LEAK detected of size: %s\n", 106 humanize.Bytes(uint64(z.NumAllocBytes()))) 107 glog.Warningf("%s", z.Leaks()) 108 } 109 }