Debian Codegraph @

prometheus/process_collector_darwin.go

raw
1 // Copyright 2024 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
14 //go:build darwin && !ios
15
16 package prometheus
17
18 import (
19 "errors"
20 "fmt"
21 "os"
22 "syscall"
23 "time"
24
25 "golang.org/x/sys/unix"
26 )
27
28 // errNotImplemented is returned by stub functions that replace cgo functions, when cgo
29 // isn't available.
30 var errNotImplemented = errors.New("not implemented")
31
32 type memoryInfo struct {
33 vsize uint64 // Virtual memory size in bytes
34 rss uint64 // Resident memory size in bytes
35 }
36
37 func canCollectProcess() bool {
38 return true
39 }
40
41 func getSoftLimit(which int) (uint64, error) {
42 rlimit := syscall.Rlimit{}
43
44 if err := syscall.Getrlimit(which, &rlimit); err != nil {
45 return 0, err
46 }
47
48 return rlimit.Cur, nil
49 }
50
51 func getOpenFileCount() (float64, error) {
52 // Alternately, the undocumented proc_pidinfo(PROC_PIDLISTFDS) can be used to
53 // return a list of open fds, but that requires a way to call C APIs. The
54 // benefits, however, include fewer system calls and not failing when at the
55 // open file soft limit.
56
57 if dir, err := os.Open("/dev/fd"); err != nil {
58 return 0.0, err
59 } else {
60 defer dir.Close()
61
62 // Avoid ReadDir(), as it calls stat(2) on each descriptor. Not only is
63 // that info not used, but KQUEUE descriptors fail stat(2), which causes
64 // the whole method to fail.
65 if names, err := dir.Readdirnames(0); err != nil {
66 return 0.0, err
67 } else {
68 // Subtract 1 to ignore the open /dev/fd descriptor above.
69 return float64(len(names) - 1), nil
70 }
71 }
72 }
73
74 func (c *processCollector) processCollect(ch chan<- Metric) {
75 if procs, err := unix.SysctlKinfoProcSlice("kern.proc.pid", os.Getpid()); err == nil {
76 if len(procs) == 1 {
77 startTime := float64(procs[0].Proc.P_starttime.Nano() / 1e9)
78 ch <- MustNewConstMetric(c.startTime, GaugeValue, startTime)
79 } else {
80 err = fmt.Errorf("sysctl() returned %d proc structs (expected 1)", len(procs))
81 c.reportError(ch, c.startTime, err)
82 }
83 } else {
84 c.reportError(ch, c.startTime, err)
85 }
86
87 // The proc structure returned by kern.proc.pid above has an Rusage member,
88 // but it is not filled in, so it needs to be fetched by getrusage(2). For
89 // that call, the UTime, STime, and Maxrss members are filled out, but not
90 // Ixrss, Idrss, or Isrss for the memory usage. Memory stats will require
91 // access to the C API to call task_info(TASK_BASIC_INFO).
92 rusage := unix.Rusage{}
93
94 if err := unix.Getrusage(syscall.RUSAGE_SELF, &rusage); err == nil {
95 cpuTime := time.Duration(rusage.Stime.Nano() + rusage.Utime.Nano()).Seconds()
96 ch <- MustNewConstMetric(c.cpuTotal, CounterValue, cpuTime)
97 } else {
98 c.reportError(ch, c.cpuTotal, err)
99 }
100
101 if memInfo, err := getMemory(); err == nil {
102 ch <- MustNewConstMetric(c.rss, GaugeValue, float64(memInfo.rss))
103 ch <- MustNewConstMetric(c.vsize, GaugeValue, float64(memInfo.vsize))
104 } else if !errors.Is(err, errNotImplemented) {
105 // Don't report an error when support is not compiled in.
106 c.reportError(ch, c.rss, err)
107 c.reportError(ch, c.vsize, err)
108 }
109
110 if fds, err := getOpenFileCount(); err == nil {
111 ch <- MustNewConstMetric(c.openFDs, GaugeValue, fds)
112 } else {
113 c.reportError(ch, c.openFDs, err)
114 }
115
116 if openFiles, err := getSoftLimit(syscall.RLIMIT_NOFILE); err == nil {
117 ch <- MustNewConstMetric(c.maxFDs, GaugeValue, float64(openFiles))
118 } else {
119 c.reportError(ch, c.maxFDs, err)
120 }
121
122 if addressSpace, err := getSoftLimit(syscall.RLIMIT_AS); err == nil {
123 ch <- MustNewConstMetric(c.maxVsize, GaugeValue, float64(addressSpace))
124 } else {
125 c.reportError(ch, c.maxVsize, err)
126 }
127
128 // TODO: socket(PF_SYSTEM) to fetch "com.apple.network.statistics" might
129 // be able to get the per-process network send/receive counts.
130 }