blob: 0251c83bfe81945aaf6688d02b4f895554d733d4 [file] [log] [blame]
sslobodrd046be82019-01-16 10:02:22 -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 "fmt"
18 "io/ioutil"
19 "os"
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// NewIO creates a new ProcIO instance from a given Proc instance.
43func (p Proc) NewIO() (ProcIO, error) {
44 pio := ProcIO{}
45
46 f, err := os.Open(p.path("io"))
47 if err != nil {
48 return pio, err
49 }
50 defer f.Close()
51
52 data, err := ioutil.ReadAll(f)
53 if err != nil {
54 return pio, err
55 }
56
57 ioFormat := "rchar: %d\nwchar: %d\nsyscr: %d\nsyscw: %d\n" +
58 "read_bytes: %d\nwrite_bytes: %d\n" +
59 "cancelled_write_bytes: %d\n"
60
61 _, err = fmt.Sscanf(string(data), ioFormat, &pio.RChar, &pio.WChar, &pio.SyscR,
62 &pio.SyscW, &pio.ReadBytes, &pio.WriteBytes, &pio.CancelledWriteBytes)
63
64 return pio, err
65}