MCPcopy
hub / github.com/dgraph-io/dgraph / getMemUsage

Function getMemUsage

x/metrics.go:782–826  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

780}
781
782func getMemUsage() int {
783 if runtime.GOOS != "linux" {
784 pid := os.Getpid()
785 cmd := fmt.Sprintf("ps -ao rss,pid | grep %v", pid)
786 c1, err := exec.Command("bash", "-c", cmd).Output()
787 if err != nil {
788 // In case of error running the command, resort to go way
789 var ms runtime.MemStats
790 runtime.ReadMemStats(&ms)
791 megs := ms.Alloc
792 return int(megs)
793 }
794
795 rss := strings.Split(string(c1), " ")[0]
796 kbs, err := strconv.Atoi(rss)
797 if err != nil {
798 return 0
799 }
800
801 megs := kbs << 10
802 return megs
803 }
804
805 contents, err := os.ReadFile("/proc/self/stat")
806 if err != nil {
807 glog.Errorf("Can't read the proc file. Err: %v\n", err)
808 return 0
809 }
810
811 cont := strings.Split(string(contents), " ")
812 // 24th entry of the file is the RSS which denotes the number of pages
813 // used by the process.
814 if len(cont) < 24 {
815 glog.Errorln("Error in RSS from stat")
816 return 0
817 }
818
819 rss, err := strconv.Atoi(cont[23])
820 if err != nil {
821 glog.Errorln(err)
822 return 0
823 }
824
825 return rss * os.Getpagesize()
826}
827
828func JemallocHandler(w http.ResponseWriter, r *http.Request) {
829 AddCorsHeaders(w)

Callers 1

MonitorMemoryMetricsFunction · 0.85

Calls 2

OutputMethod · 0.80
ErrorfMethod · 0.45

Tested by

no test coverage detected