proc_limits.go 4.78 KB
Newer Older
ale's avatar
ale committed
1
2
3
4
5
6
7
8
9
10
11
12
13
// Copyright 2018 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

ale's avatar
ale committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package procfs

import (
	"bufio"
	"fmt"
	"os"
	"regexp"
	"strconv"
)

// ProcLimits represents the soft limits for each of the process's resource
// limits. For more information see getrlimit(2):
// http://man7.org/linux/man-pages/man2/getrlimit.2.html.
type ProcLimits struct {
	// CPU time limit in seconds.
29
	CPUTime uint64
ale's avatar
ale committed
30
	// Maximum size of files that the process may create.
31
	FileSize uint64
ale's avatar
ale committed
32
33
	// Maximum size of the process's data segment (initialized data,
	// uninitialized data, and heap).
34
	DataSize uint64
ale's avatar
ale committed
35
	// Maximum size of the process stack in bytes.
36
	StackSize uint64
ale's avatar
ale committed
37
	// Maximum size of a core file.
38
	CoreFileSize uint64
ale's avatar
ale committed
39
	// Limit of the process's resident set in pages.
40
	ResidentSet uint64
ale's avatar
ale committed
41
42
	// Maximum number of processes that can be created for the real user ID of
	// the calling process.
43
	Processes uint64
ale's avatar
ale committed
44
45
	// Value one greater than the maximum file descriptor number that can be
	// opened by this process.
46
	OpenFiles uint64
ale's avatar
ale committed
47
	// Maximum number of bytes of memory that may be locked into RAM.
48
	LockedMemory uint64
ale's avatar
ale committed
49
	// Maximum size of the process's virtual memory address space in bytes.
50
	AddressSpace uint64
ale's avatar
ale committed
51
52
	// Limit on the combined number of flock(2) locks and fcntl(2) leases that
	// this process may establish.
53
	FileLocks uint64
ale's avatar
ale committed
54
55
	// Limit of signals that may be queued for the real user ID of the calling
	// process.
56
	PendingSignals uint64
ale's avatar
ale committed
57
58
	// Limit on the number of bytes that can be allocated for POSIX message
	// queues for the real user ID of the calling process.
59
	MsqqueueSize uint64
ale's avatar
ale committed
60
	// Limit of the nice priority set using setpriority(2) or nice(2).
61
	NicePriority uint64
ale's avatar
ale committed
62
63
	// Limit of the real-time priority set using sched_setscheduler(2) or
	// sched_setparam(2).
64
	RealtimePriority uint64
ale's avatar
ale committed
65
66
67
	// Limit (in microseconds) on the amount of CPU time that a process
	// scheduled under a real-time scheduling policy may consume without making
	// a blocking system call.
68
	RealtimeTimeout uint64
ale's avatar
ale committed
69
70
71
}

const (
72
	limitsFields    = 4
ale's avatar
ale committed
73
74
75
76
	limitsUnlimited = "unlimited"
)

var (
77
	limitsMatch = regexp.MustCompile(`(Max \w+\s{0,1}?\w*\s{0,1}\w*)\s{2,}(\w+)\s+(\w+)`)
ale's avatar
ale committed
78
79
80
)

// NewLimits returns the current soft limits of the process.
81
82
//
// Deprecated: use p.Limits() instead
ale's avatar
ale committed
83
func (p Proc) NewLimits() (ProcLimits, error) {
84
85
86
87
88
	return p.Limits()
}

// Limits returns the current soft limits of the process.
func (p Proc) Limits() (ProcLimits, error) {
ale's avatar
ale committed
89
90
91
92
93
94
95
96
97
98
	f, err := os.Open(p.path("limits"))
	if err != nil {
		return ProcLimits{}, err
	}
	defer f.Close()

	var (
		l = ProcLimits{}
		s = bufio.NewScanner(f)
	)
99
100
101

	s.Scan() // Skip limits header

ale's avatar
ale committed
102
	for s.Scan() {
103
104
		//fields := limitsMatch.Split(s.Text(), limitsFields)
		fields := limitsMatch.FindStringSubmatch(s.Text())
ale's avatar
ale committed
105
		if len(fields) != limitsFields {
106
			return ProcLimits{}, fmt.Errorf("couldn't parse %q line %q", f.Name(), s.Text())
ale's avatar
ale committed
107
108
		}

109
		switch fields[1] {
ale's avatar
ale committed
110
		case "Max cpu time":
111
			l.CPUTime, err = parseUint(fields[2])
ale's avatar
ale committed
112
		case "Max file size":
113
			l.FileSize, err = parseUint(fields[2])
ale's avatar
ale committed
114
		case "Max data size":
115
			l.DataSize, err = parseUint(fields[2])
ale's avatar
ale committed
116
		case "Max stack size":
117
			l.StackSize, err = parseUint(fields[2])
ale's avatar
ale committed
118
		case "Max core file size":
119
			l.CoreFileSize, err = parseUint(fields[2])
ale's avatar
ale committed
120
		case "Max resident set":
121
			l.ResidentSet, err = parseUint(fields[2])
ale's avatar
ale committed
122
		case "Max processes":
123
			l.Processes, err = parseUint(fields[2])
ale's avatar
ale committed
124
		case "Max open files":
125
			l.OpenFiles, err = parseUint(fields[2])
ale's avatar
ale committed
126
		case "Max locked memory":
127
			l.LockedMemory, err = parseUint(fields[2])
ale's avatar
ale committed
128
		case "Max address space":
129
			l.AddressSpace, err = parseUint(fields[2])
ale's avatar
ale committed
130
		case "Max file locks":
131
			l.FileLocks, err = parseUint(fields[2])
ale's avatar
ale committed
132
		case "Max pending signals":
133
			l.PendingSignals, err = parseUint(fields[2])
ale's avatar
ale committed
134
		case "Max msgqueue size":
135
			l.MsqqueueSize, err = parseUint(fields[2])
ale's avatar
ale committed
136
		case "Max nice priority":
137
			l.NicePriority, err = parseUint(fields[2])
ale's avatar
ale committed
138
		case "Max realtime priority":
139
			l.RealtimePriority, err = parseUint(fields[2])
ale's avatar
ale committed
140
		case "Max realtime timeout":
141
			l.RealtimeTimeout, err = parseUint(fields[2])
ale's avatar
ale committed
142
143
144
145
146
147
148
149
150
		}
		if err != nil {
			return ProcLimits{}, err
		}
	}

	return l, s.Err()
}

151
func parseUint(s string) (uint64, error) {
ale's avatar
ale committed
152
	if s == limitsUnlimited {
153
		return 18446744073709551615, nil
ale's avatar
ale committed
154
	}
155
	i, err := strconv.ParseUint(s, 10, 64)
ale's avatar
ale committed
156
	if err != nil {
157
		return 0, fmt.Errorf("couldn't parse value %q: %w", s, err)
ale's avatar
ale committed
158
159
160
	}
	return i, nil
}