blob: 91ee24df8bdecd2665cce73f675f5a215c8d0b14 [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.
29 CPUTime int64
30 // Maximum size of files that the process may create.
31 FileSize int64
32 // Maximum size of the process's data segment (initialized data,
33 // uninitialized data, and heap).
34 DataSize int64
35 // Maximum size of the process stack in bytes.
36 StackSize int64
37 // Maximum size of a core file.
38 CoreFileSize int64
39 // Limit of the process's resident set in pages.
40 ResidentSet int64
41 // Maximum number of processes that can be created for the real user ID of
42 // the calling process.
43 Processes int64
44 // Value one greater than the maximum file descriptor number that can be
45 // opened by this process.
46 OpenFiles int64
47 // Maximum number of bytes of memory that may be locked into RAM.
48 LockedMemory int64
49 // Maximum size of the process's virtual memory address space in bytes.
50 AddressSpace int64
51 // Limit on the combined number of flock(2) locks and fcntl(2) leases that
52 // this process may establish.
53 FileLocks int64
54 // Limit of signals that may be queued for the real user ID of the calling
55 // process.
56 PendingSignals int64
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.
59 MsqqueueSize int64
60 // Limit of the nice priority set using setpriority(2) or nice(2).
61 NicePriority int64
62 // Limit of the real-time priority set using sched_setscheduler(2) or
63 // sched_setparam(2).
64 RealtimePriority int64
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.
68 RealtimeTimeout int64
69}
70
71const (
72 limitsFields = 3
73 limitsUnlimited = "unlimited"
74)
75
76var (
77 limitsDelimiter = regexp.MustCompile(" +")
78)
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 )
99 for s.Scan() {
100 fields := limitsDelimiter.Split(s.Text(), limitsFields)
101 if len(fields) != limitsFields {
102 return ProcLimits{}, fmt.Errorf(
103 "couldn't parse %s line %s", f.Name(), s.Text())
104 }
105
106 switch fields[0] {
107 case "Max cpu time":
108 l.CPUTime, err = parseInt(fields[1])
109 case "Max file size":
110 l.FileSize, err = parseInt(fields[1])
111 case "Max data size":
112 l.DataSize, err = parseInt(fields[1])
113 case "Max stack size":
114 l.StackSize, err = parseInt(fields[1])
115 case "Max core file size":
116 l.CoreFileSize, err = parseInt(fields[1])
117 case "Max resident set":
118 l.ResidentSet, err = parseInt(fields[1])
119 case "Max processes":
120 l.Processes, err = parseInt(fields[1])
121 case "Max open files":
122 l.OpenFiles, err = parseInt(fields[1])
123 case "Max locked memory":
124 l.LockedMemory, err = parseInt(fields[1])
125 case "Max address space":
126 l.AddressSpace, err = parseInt(fields[1])
127 case "Max file locks":
128 l.FileLocks, err = parseInt(fields[1])
129 case "Max pending signals":
130 l.PendingSignals, err = parseInt(fields[1])
131 case "Max msgqueue size":
132 l.MsqqueueSize, err = parseInt(fields[1])
133 case "Max nice priority":
134 l.NicePriority, err = parseInt(fields[1])
135 case "Max realtime priority":
136 l.RealtimePriority, err = parseInt(fields[1])
137 case "Max realtime timeout":
138 l.RealtimeTimeout, err = parseInt(fields[1])
139 }
140 if err != nil {
141 return ProcLimits{}, err
142 }
143 }
144
145 return l, s.Err()
146}
147
148func parseInt(s string) (int64, error) {
149 if s == limitsUnlimited {
150 return -1, nil
151 }
152 i, err := strconv.ParseInt(s, 10, 64)
153 if err != nil {
154 return 0, fmt.Errorf("couldn't parse value %s: %s", s, err)
155 }
156 return i, nil
157}