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