khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | // Copyright 2018 The Prometheus Authors |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | // you may not use this file except in compliance with the License. |
| 4 | // You may obtain a copy of the License at |
| 5 | // |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | // |
| 8 | // Unless required by applicable law or agreed to in writing, software |
| 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | // See the License for the specific language governing permissions and |
| 12 | // limitations under the License. |
| 13 | |
| 14 | package procfs |
| 15 | |
| 16 | import ( |
| 17 | "bufio" |
| 18 | "fmt" |
| 19 | "os" |
| 20 | "regexp" |
| 21 | "strconv" |
| 22 | ) |
| 23 | |
| 24 | // ProcLimits represents the soft limits for each of the process's resource |
| 25 | // limits. For more information see getrlimit(2): |
| 26 | // http://man7.org/linux/man-pages/man2/getrlimit.2.html. |
| 27 | type ProcLimits struct { |
| 28 | // CPU time limit in seconds. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 29 | CPUTime uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 30 | // Maximum size of files that the process may create. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 31 | FileSize uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 32 | // Maximum size of the process's data segment (initialized data, |
| 33 | // uninitialized data, and heap). |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 34 | DataSize uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 35 | // Maximum size of the process stack in bytes. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 36 | StackSize uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 37 | // Maximum size of a core file. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 38 | CoreFileSize uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 39 | // Limit of the process's resident set in pages. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 40 | ResidentSet uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 41 | // Maximum number of processes that can be created for the real user ID of |
| 42 | // the calling process. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 43 | Processes uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 44 | // Value one greater than the maximum file descriptor number that can be |
| 45 | // opened by this process. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 46 | OpenFiles uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 47 | // Maximum number of bytes of memory that may be locked into RAM. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 48 | LockedMemory uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 49 | // Maximum size of the process's virtual memory address space in bytes. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 50 | AddressSpace uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 51 | // Limit on the combined number of flock(2) locks and fcntl(2) leases that |
| 52 | // this process may establish. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 53 | FileLocks uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 54 | // Limit of signals that may be queued for the real user ID of the calling |
| 55 | // process. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 56 | PendingSignals uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 57 | // Limit on the number of bytes that can be allocated for POSIX message |
| 58 | // queues for the real user ID of the calling process. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 59 | MsqqueueSize uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 60 | // Limit of the nice priority set using setpriority(2) or nice(2). |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 61 | NicePriority uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 62 | // Limit of the real-time priority set using sched_setscheduler(2) or |
| 63 | // sched_setparam(2). |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 64 | RealtimePriority uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 65 | // Limit (in microseconds) on the amount of CPU time that a process |
| 66 | // scheduled under a real-time scheduling policy may consume without making |
| 67 | // a blocking system call. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 68 | RealtimeTimeout uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | const ( |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 72 | limitsFields = 4 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 73 | limitsUnlimited = "unlimited" |
| 74 | ) |
| 75 | |
| 76 | var ( |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 77 | limitsMatch = regexp.MustCompile(`(Max \w+\s{0,1}?\w*\s{0,1}\w*)\s{2,}(\w+)\s+(\w+)`) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 78 | ) |
| 79 | |
| 80 | // NewLimits returns the current soft limits of the process. |
| 81 | // |
| 82 | // Deprecated: use p.Limits() instead |
| 83 | func (p Proc) NewLimits() (ProcLimits, error) { |
| 84 | return p.Limits() |
| 85 | } |
| 86 | |
| 87 | // Limits returns the current soft limits of the process. |
| 88 | func (p Proc) Limits() (ProcLimits, error) { |
| 89 | f, err := os.Open(p.path("limits")) |
| 90 | if err != nil { |
| 91 | return ProcLimits{}, err |
| 92 | } |
| 93 | defer f.Close() |
| 94 | |
| 95 | var ( |
| 96 | l = ProcLimits{} |
| 97 | s = bufio.NewScanner(f) |
| 98 | ) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 99 | |
| 100 | s.Scan() // Skip limits header |
| 101 | |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 102 | for s.Scan() { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 103 | //fields := limitsMatch.Split(s.Text(), limitsFields) |
| 104 | fields := limitsMatch.FindStringSubmatch(s.Text()) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 105 | if len(fields) != limitsFields { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 106 | return ProcLimits{}, fmt.Errorf("couldn't parse %q line %q", f.Name(), s.Text()) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 107 | } |
| 108 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 109 | switch fields[1] { |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 110 | case "Max cpu time": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 111 | l.CPUTime, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 112 | case "Max file size": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 113 | l.FileSize, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 114 | case "Max data size": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 115 | l.DataSize, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 116 | case "Max stack size": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 117 | l.StackSize, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 118 | case "Max core file size": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 119 | l.CoreFileSize, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 120 | case "Max resident set": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 121 | l.ResidentSet, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 122 | case "Max processes": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 123 | l.Processes, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 124 | case "Max open files": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 125 | l.OpenFiles, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 126 | case "Max locked memory": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 127 | l.LockedMemory, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 128 | case "Max address space": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 129 | l.AddressSpace, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 130 | case "Max file locks": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 131 | l.FileLocks, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 132 | case "Max pending signals": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 133 | l.PendingSignals, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 134 | case "Max msgqueue size": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 135 | l.MsqqueueSize, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 136 | case "Max nice priority": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 137 | l.NicePriority, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 138 | case "Max realtime priority": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 139 | l.RealtimePriority, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 140 | case "Max realtime timeout": |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 141 | l.RealtimeTimeout, err = parseUint(fields[2]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 142 | } |
| 143 | if err != nil { |
| 144 | return ProcLimits{}, err |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return l, s.Err() |
| 149 | } |
| 150 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 151 | func parseUint(s string) (uint64, error) { |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 152 | if s == limitsUnlimited { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 153 | return 18446744073709551615, nil |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 154 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 155 | i, err := strconv.ParseUint(s, 10, 64) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 156 | if err != nil { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 157 | return 0, fmt.Errorf("couldn't parse value %q: %w", s, err) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 158 | } |
| 159 | return i, nil |
| 160 | } |