1// 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	"fmt"
18
19	"github.com/prometheus/procfs/internal/util"
20)
21
22// ProcIO models the content of /proc/<pid>/io.
23type ProcIO struct {
24	// Chars read.
25	RChar uint64
26	// Chars written.
27	WChar uint64
28	// Read syscalls.
29	SyscR uint64
30	// Write syscalls.
31	SyscW uint64
32	// Bytes read.
33	ReadBytes uint64
34	// Bytes written.
35	WriteBytes uint64
36	// Bytes written, but taking into account truncation. See
37	// Documentation/filesystems/proc.txt in the kernel sources for
38	// detailed explanation.
39	CancelledWriteBytes int64
40}
41
42// IO creates a new ProcIO instance from a given Proc instance.
43func (p Proc) IO() (ProcIO, error) {
44	pio := ProcIO{}
45
46	data, err := util.ReadFileNoStat(p.path("io"))
47	if err != nil {
48		return pio, err
49	}
50
51	ioFormat := "rchar: %d\nwchar: %d\nsyscr: %d\nsyscw: %d\n" +
52		"read_bytes: %d\nwrite_bytes: %d\n" +
53		"cancelled_write_bytes: %d\n"
54
55	_, err = fmt.Sscanf(string(data), ioFormat, &pio.RChar, &pio.WChar, &pio.SyscR,
56		&pio.SyscW, &pio.ReadBytes, &pio.WriteBytes, &pio.CancelledWriteBytes)
57
58	return pio, err
59}
60