blob: dd20f198a30823c965d32c696b6779e1d52a1bf9 [file] [log] [blame]
khenaidoo59ce9dd2019-11-11 13:05:32 -05001// 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
14package procfs
15
16import (
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.
27type ProcLimits struct {
28 // CPU time limit in seconds.
khenaidoo26721882021-08-11 17:42:52 -040029 CPUTime uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050030 // Maximum size of files that the process may create.
khenaidoo26721882021-08-11 17:42:52 -040031 FileSize uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050032 // Maximum size of the process's data segment (initialized data,
33 // uninitialized data, and heap).
khenaidoo26721882021-08-11 17:42:52 -040034 DataSize uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050035 // Maximum size of the process stack in bytes.
khenaidoo26721882021-08-11 17:42:52 -040036 StackSize uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050037 // Maximum size of a core file.
khenaidoo26721882021-08-11 17:42:52 -040038 CoreFileSize uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050039 // Limit of the process's resident set in pages.
khenaidoo26721882021-08-11 17:42:52 -040040 ResidentSet uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050041 // Maximum number of processes that can be created for the real user ID of
42 // the calling process.
khenaidoo26721882021-08-11 17:42:52 -040043 Processes uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050044 // Value one greater than the maximum file descriptor number that can be
45 // opened by this process.
khenaidoo26721882021-08-11 17:42:52 -040046 OpenFiles uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050047 // Maximum number of bytes of memory that may be locked into RAM.
khenaidoo26721882021-08-11 17:42:52 -040048 LockedMemory uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050049 // Maximum size of the process's virtual memory address space in bytes.
khenaidoo26721882021-08-11 17:42:52 -040050 AddressSpace uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050051 // Limit on the combined number of flock(2) locks and fcntl(2) leases that
52 // this process may establish.
khenaidoo26721882021-08-11 17:42:52 -040053 FileLocks uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050054 // Limit of signals that may be queued for the real user ID of the calling
55 // process.
khenaidoo26721882021-08-11 17:42:52 -040056 PendingSignals uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050057 // Limit on the number of bytes that can be allocated for POSIX message
58 // queues for the real user ID of the calling process.
khenaidoo26721882021-08-11 17:42:52 -040059 MsqqueueSize uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050060 // Limit of the nice priority set using setpriority(2) or nice(2).
khenaidoo26721882021-08-11 17:42:52 -040061 NicePriority uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050062 // Limit of the real-time priority set using sched_setscheduler(2) or
63 // sched_setparam(2).
khenaidoo26721882021-08-11 17:42:52 -040064 RealtimePriority uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050065 // 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.
khenaidoo26721882021-08-11 17:42:52 -040068 RealtimeTimeout uint64
khenaidoo59ce9dd2019-11-11 13:05:32 -050069}
70
71const (
khenaidoo26721882021-08-11 17:42:52 -040072 limitsFields = 4
khenaidoo59ce9dd2019-11-11 13:05:32 -050073 limitsUnlimited = "unlimited"
74)
75
76var (
khenaidoo26721882021-08-11 17:42:52 -040077 limitsMatch = regexp.MustCompile(`(Max \w+\s{0,1}?\w*\s{0,1}\w*)\s{2,}(\w+)\s+(\w+)`)
khenaidoo59ce9dd2019-11-11 13:05:32 -050078)
79
80// NewLimits returns the current soft limits of the process.
81//
82// Deprecated: use p.Limits() instead
83func (p Proc) NewLimits() (ProcLimits, error) {
84 return p.Limits()
85}
86
87// Limits returns the current soft limits of the process.
88func (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 )
khenaidoo26721882021-08-11 17:42:52 -040099
100 s.Scan() // Skip limits header
101
khenaidoo59ce9dd2019-11-11 13:05:32 -0500102 for s.Scan() {
khenaidoo26721882021-08-11 17:42:52 -0400103 //fields := limitsMatch.Split(s.Text(), limitsFields)
104 fields := limitsMatch.FindStringSubmatch(s.Text())
khenaidoo59ce9dd2019-11-11 13:05:32 -0500105 if len(fields) != limitsFields {
khenaidoo26721882021-08-11 17:42:52 -0400106 return ProcLimits{}, fmt.Errorf("couldn't parse %q line %q", f.Name(), s.Text())
khenaidoo59ce9dd2019-11-11 13:05:32 -0500107 }
108
khenaidoo26721882021-08-11 17:42:52 -0400109 switch fields[1] {
khenaidoo59ce9dd2019-11-11 13:05:32 -0500110 case "Max cpu time":
khenaidoo26721882021-08-11 17:42:52 -0400111 l.CPUTime, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500112 case "Max file size":
khenaidoo26721882021-08-11 17:42:52 -0400113 l.FileSize, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500114 case "Max data size":
khenaidoo26721882021-08-11 17:42:52 -0400115 l.DataSize, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500116 case "Max stack size":
khenaidoo26721882021-08-11 17:42:52 -0400117 l.StackSize, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500118 case "Max core file size":
khenaidoo26721882021-08-11 17:42:52 -0400119 l.CoreFileSize, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500120 case "Max resident set":
khenaidoo26721882021-08-11 17:42:52 -0400121 l.ResidentSet, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500122 case "Max processes":
khenaidoo26721882021-08-11 17:42:52 -0400123 l.Processes, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500124 case "Max open files":
khenaidoo26721882021-08-11 17:42:52 -0400125 l.OpenFiles, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500126 case "Max locked memory":
khenaidoo26721882021-08-11 17:42:52 -0400127 l.LockedMemory, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500128 case "Max address space":
khenaidoo26721882021-08-11 17:42:52 -0400129 l.AddressSpace, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500130 case "Max file locks":
khenaidoo26721882021-08-11 17:42:52 -0400131 l.FileLocks, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500132 case "Max pending signals":
khenaidoo26721882021-08-11 17:42:52 -0400133 l.PendingSignals, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500134 case "Max msgqueue size":
khenaidoo26721882021-08-11 17:42:52 -0400135 l.MsqqueueSize, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500136 case "Max nice priority":
khenaidoo26721882021-08-11 17:42:52 -0400137 l.NicePriority, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500138 case "Max realtime priority":
khenaidoo26721882021-08-11 17:42:52 -0400139 l.RealtimePriority, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500140 case "Max realtime timeout":
khenaidoo26721882021-08-11 17:42:52 -0400141 l.RealtimeTimeout, err = parseUint(fields[2])
khenaidoo59ce9dd2019-11-11 13:05:32 -0500142 }
143 if err != nil {
144 return ProcLimits{}, err
145 }
146 }
147
148 return l, s.Err()
149}
150
khenaidoo26721882021-08-11 17:42:52 -0400151func parseUint(s string) (uint64, error) {
khenaidoo59ce9dd2019-11-11 13:05:32 -0500152 if s == limitsUnlimited {
khenaidoo26721882021-08-11 17:42:52 -0400153 return 18446744073709551615, nil
khenaidoo59ce9dd2019-11-11 13:05:32 -0500154 }
khenaidoo26721882021-08-11 17:42:52 -0400155 i, err := strconv.ParseUint(s, 10, 64)
khenaidoo59ce9dd2019-11-11 13:05:32 -0500156 if err != nil {
khenaidoo26721882021-08-11 17:42:52 -0400157 return 0, fmt.Errorf("couldn't parse value %q: %w", s, err)
khenaidoo59ce9dd2019-11-11 13:05:32 -0500158 }
159 return i, nil
160}