| 1 | // Copyright 2018 psgo authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package proc |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "fmt" |
| 20 | "os" |
| 21 | "strings" |
| 22 | ) |
| 23 | |
| 24 | // Stat is a direct translation of a `/proc/[pid]/stat` file as described in |
| 25 | // the proc(5) manpage. Please note that it is not a full translation as not |
| 26 | // all fields are in the scope of this library and higher indices are |
| 27 | // Kernel-version dependent. |
| 28 | type Stat struct { |
| 29 | // (1) The process ID |
| 30 | Pid string |
| 31 | // (2) The filename of the executable, in parentheses. This is visible |
| 32 | // whether or not the executable is swapped out. |
| 33 | Comm string |
| 34 | // (3) The process state (e.g., running, sleeping, zombie, dead). |
| 35 | // Refer to proc(5) for further details. |
| 36 | State string |
| 37 | // (4) The PID of the parent of this process. |
| 38 | Ppid string |
| 39 | // (5) The process group ID of the process. |
| 40 | Pgrp string |
| 41 | // (6) The session ID of the process. |
| 42 | Session string |
| 43 | // (7) The controlling terminal of the process. (The minor device |
| 44 | // number is contained in the combination of bits 31 to 20 and 7 to 0; |
| 45 | // the major device number is in bits 15 to 8.) |
| 46 | TtyNr string |
| 47 | // (8) The ID of the foreground process group of the controlling |
| 48 | // terminal of the process. |
| 49 | Tpgid string |
| 50 | // (9) The kernel flags word of the process. For bit meanings, see the |
| 51 | // PF_* defines in the Linux kernel source file |
| 52 | // include/linux/sched.h. Details depend on the kernel version. |
| 53 | Flags string |
| 54 | // (10) The number of minor faults the process has made which have not |
| 55 | // required loading a memory page from disk. |
| 56 | Minflt string |
| 57 | // (11) The number of minor faults that the process's waited-for |
| 58 | // children have made. |
| 59 | Cminflt string |
| 60 | // (12) The number of major faults the process has made which have |
| 61 | // required loading a memory page from disk. |
| 62 | Majflt string |
| 63 | // (13) The number of major faults that the process's waited-for |
| 64 | // children have made. |
| 65 | Cmajflt string |
| 66 | // (14) Amount of time that this process has been scheduled in user |
| 67 | // mode, measured in clock ticks (divide by |
| 68 | // sysconf(_SC_CLK_TCK)). This includes guest time, guest_time |
| 69 | // (time spent running a virtual CPU, see below), so that applications |
| 70 | // that are not aware of the guest time field do not lose that time |
| 71 | // from their calculations. |
| 72 | Utime string |
| 73 | // (15) Amount of time that this process has been scheduled in kernel |
| 74 | // mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK)). |
| 75 | Stime string |
| 76 | // (16) Amount of time that this process's waited-for children have |
| 77 | // been scheduled in user mode, measured in clock ticks (divide by |
| 78 | // sysconf(_SC_CLK_TCK)). (See also times(2).) This includes guest |
| 79 | // time, cguest_time (time spent running a virtual CPU, see below). |
| 80 | Cutime string |
| 81 | // (17) Amount of time that this process's waited-for children have |
| 82 | // been scheduled in kernel mode, measured in clock ticks (divide by |
| 83 | // sysconf(_SC_CLK_TCK)). |
| 84 | Cstime string |
| 85 | // (18) (Explanation for Linux 2.6+) For processes running a real-time |
| 86 | // scheduling policy (policy below; see sched_setscheduler(2)), this is |
| 87 | // the negated scheduling pri- ority, minus one; that is, a number |
| 88 | // in the range -2 to -100, corresponding to real-time priorities 1 to |
| 89 | // 99. For processes running under a non-real-time scheduling |
| 90 | // policy, this is the raw nice value (setpriority(2)) as represented |
| 91 | // in the kernel. The kernel stores nice values as numbers in the |
| 92 | // range 0 (high) to 39 (low), corresponding to the user-visible nice |
| 93 | // range of -20 to 19. |
| 94 | Priority string |
| 95 | // (19) The nice value (see setpriority(2)), a value in the range 19 |
| 96 | // (low priority) to -20 (high priority). |
| 97 | Nice string |
| 98 | // (20) Number of threads in this process (since Linux 2.6). Before |
| 99 | // kernel 2.6, this field was hard coded to 0 as a placeholder for an |
| 100 | // earlier removed field. |
| 101 | NumThreads string |
| 102 | // (21) The time in jiffies before the next SIGALRM is sent to the |
| 103 | // process due to an interval timer. Since kernel 2.6.17, this |
| 104 | // field is no longer maintained, and is hard coded as 0. |
| 105 | Itrealvalue string |
| 106 | // (22) The time the process started after system boot. In kernels |
| 107 | // before Linux 2.6, this value was expressed in jiffies. Since |
| 108 | // Linux 2.6, the value is expressed in clock ticks (divide by |
| 109 | // sysconf(_SC_CLK_TCK)). |
| 110 | Starttime string |
| 111 | // (23) Virtual memory size in bytes. |
| 112 | Vsize string |
| 113 | } |
| 114 | |
| 115 | // readStat is used for mocking in unit tests. |
| 116 | var readStat = func(path string) (string, error) { |
| 117 | rawData, err := os.ReadFile(path) |
| 118 | if err != nil { |
| 119 | return "", err |
| 120 | } |
| 121 | return string(rawData), nil |
| 122 | } |
| 123 | |
| 124 | // ParseStat parses the /proc/$pid/stat file and returns a Stat. |
| 125 | func ParseStat(pid string) (*Stat, error) { |
| 126 | data, err := readStat(fmt.Sprintf("/proc/%s/stat", pid)) |
| 127 | if err != nil { |
| 128 | return nil, err |
| 129 | } |
| 130 | |
| 131 | firstParen := strings.IndexByte(data, '(') |
| 132 | lastParen := strings.LastIndexByte(data, ')') |
| 133 | if firstParen == -1 || lastParen == -1 { |
| 134 | return nil, errors.New("invalid format in stat") |
| 135 | } |
| 136 | pidstr := data[0 : firstParen-1] |
| 137 | comm := data[firstParen+1 : lastParen] |
| 138 | rest := strings.Fields(data[lastParen+1:]) |
| 139 | fields := append([]string{pidstr, comm}, rest...) |
| 140 | |
| 141 | fieldAt := func(i int) string { |
| 142 | return fields[i-1] |
| 143 | } |
| 144 | |
| 145 | return &Stat{ |
| 146 | Pid: fieldAt(1), |
| 147 | Comm: fieldAt(2), |
| 148 | State: fieldAt(3), |
| 149 | Ppid: fieldAt(4), |
| 150 | Pgrp: fieldAt(5), |
| 151 | Session: fieldAt(6), |
| 152 | TtyNr: fieldAt(7), |
| 153 | Tpgid: fieldAt(8), |
| 154 | Flags: fieldAt(9), |
| 155 | Minflt: fieldAt(10), |
| 156 | Cminflt: fieldAt(11), |
| 157 | Majflt: fieldAt(12), |
| 158 | Cmajflt: fieldAt(13), |
| 159 | Utime: fieldAt(14), |
| 160 | Stime: fieldAt(15), |
| 161 | Cutime: fieldAt(16), |
| 162 | Cstime: fieldAt(17), |
| 163 | Priority: fieldAt(18), |
| 164 | Nice: fieldAt(19), |
| 165 | NumThreads: fieldAt(20), |
| 166 | Itrealvalue: fieldAt(21), |
| 167 | Starttime: fieldAt(22), |
| 168 | Vsize: fieldAt(23), |
| 169 | }, nil |
| 170 | } |