github.com/ava-labs/subnet-evm@v0.6.4/metrics/disk_linux.go (about) 1 // (c) 2022, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2015 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 // Contains the Linux implementation of process disk IO counter retrieval. 28 29 package metrics 30 31 import ( 32 "bufio" 33 "fmt" 34 "io" 35 "os" 36 "strconv" 37 "strings" 38 ) 39 40 // ReadDiskStats retrieves the disk IO stats belonging to the current process. 41 func ReadDiskStats(stats *DiskStats) error { 42 // Open the process disk IO counter file 43 inf, err := os.Open(fmt.Sprintf("/proc/%d/io", os.Getpid())) 44 if err != nil { 45 return err 46 } 47 defer inf.Close() 48 in := bufio.NewReader(inf) 49 50 // Iterate over the IO counter, and extract what we need 51 for { 52 // Read the next line and split to key and value 53 line, err := in.ReadString('\n') 54 if err != nil { 55 if err == io.EOF { 56 return nil 57 } 58 return err 59 } 60 parts := strings.Split(line, ":") 61 if len(parts) != 2 { 62 continue 63 } 64 key := strings.TrimSpace(parts[0]) 65 value, err := strconv.ParseInt(strings.TrimSpace(parts[1]), 10, 64) 66 if err != nil { 67 return err 68 } 69 70 // Update the counter based on the key 71 switch key { 72 case "syscr": 73 stats.ReadCount = value 74 case "syscw": 75 stats.WriteCount = value 76 case "rchar": 77 stats.ReadBytes = value 78 case "wchar": 79 stats.WriteBytes = value 80 } 81 } 82 }