File: stat.go

package info (click to toggle)
golang-github-containers-psgo 1.7.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 372 kB
  • sloc: makefile: 46
file content (170 lines) | stat: -rw-r--r-- 6,271 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2018 psgo 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.

package proc

import (
	"errors"
	"fmt"
	"io/ioutil"
	"strings"
)

// Stat is a direct translation of a `/proc/[pid]/stat` file as described in
// the proc(5) manpage. Please note that it is not a full translation as not
// all fields are in the scope of this library and higher indices are
// Kernel-version dependent.
type Stat struct {
	// (1) The process ID
	Pid string
	// (2) The filename of the executable, in parentheses. This is visible
	// whether or not the executable is swapped out.
	Comm string
	// (3) The process state (e.g., running, sleeping, zombie, dead).
	// Refer to proc(5) for further deatils.
	State string
	// (4) The PID of the parent of this process.
	Ppid string
	// (5) The process group ID of the process.
	Pgrp string
	// (6) The session ID of the process.
	Session string
	// (7) The controlling terminal of the process. (The minor device
	// number is contained in the combination of bits 31 to 20 and 7 to 0;
	// the major device number is in bits 15 to 8.)
	TtyNr string
	// (8) The ID of the foreground process group of the controlling
	// terminal of the process.
	Tpgid string
	// (9) The kernel flags word of the process. For bit meanings, see the
	// PF_* defines in the Linux kernel source file
	// include/linux/sched.h. Details depend on the kernel version.
	Flags string
	// (10) The number of minor faults the process has made which have not
	// required loading a memory page from disk.
	Minflt string
	// (11) The number of minor faults that the process's waited-for
	// children have made.
	Cminflt string
	// (12) The number of major faults the process has made which have
	// required loading a memory page from disk.
	Majflt string
	// (13) The number of major faults that the process's waited-for
	// children have made.
	Cmajflt string
	// (14) Amount of time that this process has been scheduled in user
	// mode, measured in clock ticks (divide by
	// sysconf(_SC_CLK_TCK)). This includes guest time, guest_time
	// (time spent running a virtual CPU, see below), so that applications
	// that are not aware of the guest time field do not lose that time
	// from their calculations.
	Utime string
	// (15) Amount of time that this process has been scheduled in kernel
	// mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK)).
	Stime string
	// (16) Amount of time that this process's waited-for children have
	// been scheduled in user mode, measured in clock ticks (divide by
	// sysconf(_SC_CLK_TCK)). (See also times(2).) This includes guest
	// time, cguest_time (time spent running a virtual CPU, see below).
	Cutime string
	// (17) Amount of time that this process's waited-for children have
	// been scheduled in kernel mode, measured in clock ticks (divide by
	// sysconf(_SC_CLK_TCK)).
	Cstime string
	// (18) (Explanation for Linux 2.6+) For processes running a real-time
	// scheduling policy (policy below; see sched_setscheduler(2)), this is
	// the negated scheduling pri- ority, minus one; that is, a number
	// in the range -2 to -100, corresponding to real-time priorities 1 to
	// 99. For processes running under a non-real-time scheduling
	// policy, this is the raw nice value (setpriority(2)) as represented
	// in the kernel. The kernel stores nice values as numbers in the
	// range 0 (high) to 39 (low), corresponding to the user-visible nice
	// range of -20 to 19.
	Priority string
	// (19) The nice value (see setpriority(2)), a value in the range 19
	// (low priority) to -20 (high priority).
	Nice string
	// (20) Number of threads in this process (since Linux 2.6). Before
	// kernel 2.6, this field was hard coded to 0 as a placeholder for an
	// earlier removed field.
	NumThreads string
	// (21) The time in jiffies before the next SIGALRM is sent to the
	// process due to an interval timer. Since kernel 2.6.17, this
	// field is no longer maintained, and is hard coded as 0.
	Itrealvalue string
	// (22) The time the process started after system boot. In kernels
	// before Linux 2.6, this value was expressed in jiffies. Since
	// Linux 2.6, the value is expressed in clock ticks (divide by
	// sysconf(_SC_CLK_TCK)).
	Starttime string
	// (23) Virtual memory size in bytes.
	Vsize string
}

// readStat is used for mocking in unit tests.
var readStat = func(path string) (string, error) {
	rawData, err := ioutil.ReadFile(path)
	if err != nil {
		return "", err
	}
	return string(rawData), nil
}

// ParseStat parses the /proc/$pid/stat file and returns a Stat.
func ParseStat(pid string) (*Stat, error) {
	data, err := readStat(fmt.Sprintf("/proc/%s/stat", pid))
	if err != nil {
		return nil, err
	}

	firstParen := strings.IndexByte(data, '(')
	lastParen := strings.LastIndexByte(data, ')')
	if firstParen == -1 || lastParen == -1 {
		return nil, errors.New("invalid format in stat")
	}
	pidstr := data[0 : firstParen-1]
	comm := data[firstParen+1 : lastParen]
	rest := strings.Fields(data[lastParen+1:])
	fields := append([]string{pidstr, comm}, rest...)

	fieldAt := func(i int) string {
		return fields[i-1]
	}

	return &Stat{
		Pid:         fieldAt(1),
		Comm:        fieldAt(2),
		State:       fieldAt(3),
		Ppid:        fieldAt(4),
		Pgrp:        fieldAt(5),
		Session:     fieldAt(6),
		TtyNr:       fieldAt(7),
		Tpgid:       fieldAt(8),
		Flags:       fieldAt(9),
		Minflt:      fieldAt(10),
		Cminflt:     fieldAt(11),
		Majflt:      fieldAt(12),
		Cmajflt:     fieldAt(13),
		Utime:       fieldAt(14),
		Stime:       fieldAt(15),
		Cutime:      fieldAt(16),
		Cstime:      fieldAt(17),
		Priority:    fieldAt(18),
		Nice:        fieldAt(19),
		NumThreads:  fieldAt(20),
		Itrealvalue: fieldAt(21),
		Starttime:   fieldAt(22),
		Vsize:       fieldAt(23),
	}, nil
}