Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
93 changes: 93 additions & 0 deletions process_metrics_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//go:build darwin && !ios

package metrics

import (
"io"
"log"
"os"
"syscall"
"time"

"golang.org/x/sys/unix"
)

func writeProcessMetrics(w io.Writer) {
if procs, err := unix.SysctlKinfoProcSlice("kern.proc.pid", os.Getpid()); err == nil {
if len(procs) == 1 {
startTime := float64(procs[0].Proc.P_starttime.Nano() / 1e9)
WriteGaugeUint64(w, "process_start_time_seconds", uint64(startTime))
} else {
log.Printf("ERROR: metrics: sysctl() returned %d proc structs (expected 1)", len(procs))
}
} else {
log.Printf("ERROR: metrics: %s", err)
}

// The proc structure returned by kern.proc.pid above has an Rusage member,
// but it is not filled in, so it needs to be fetched by getrusage(2). For
// that call, the UTime, STime, and Maxrss members are filled out, but not
// Ixrss, Idrss, or Isrss for the memory usage. Memory stats will require
// access to the C API to call task_info(TASK_BASIC_INFO).
rusage := unix.Rusage{}

if err := unix.Getrusage(syscall.RUSAGE_SELF, &rusage); err == nil {
cpuTime := time.Duration(rusage.Stime.Nano() + rusage.Utime.Nano()).Seconds()
WriteGaugeFloat64(w, "process_cpu_seconds_total", cpuTime)
} else {
log.Printf("ERROR: metrics: %s", err)
}

if addressSpace, err := getSoftLimit(syscall.RLIMIT_AS); err == nil {
WriteGaugeFloat64(w, "process_virtual_memory_max_bytes", float64(addressSpace))
} else {
log.Printf("ERROR: metrics: %s", err)
}
}

func writeFDMetrics(w io.Writer) {
if fds, err := getOpenFileCount(); err == nil {
WriteGaugeFloat64(w, "process_open_fds", fds)
} else {
log.Printf("ERROR: metrics: %s", err)
}

if openFiles, err := getSoftLimit(syscall.RLIMIT_NOFILE); err == nil {
WriteGaugeFloat64(w, "process_max_fds", float64(openFiles))
} else {
log.Printf("ERROR: metrics: %s", err)
}
}

func getOpenFileCount() (float64, error) {
// Alternately, the undocumented proc_pidinfo(PROC_PIDLISTFDS) can be used to
// return a list of open fds, but that requires a way to call C APIs. The
// benefits, however, include fewer system calls and not failing when at the
// open file soft limit.

if dir, err := os.Open("/dev/fd"); err != nil {
return 0.0, err
} else {
defer dir.Close()

// Avoid ReadDir(), as it calls stat(2) on each descriptor. Not only is
// that info not used, but KQUEUE descriptors fail stat(2), which causes
// the whole method to fail.
if names, err := dir.Readdirnames(0); err != nil {
return 0.0, err
} else {
// Subtract 1 to ignore the open /dev/fd descriptor above.
return float64(len(names) - 1), nil
}
}
}

func getSoftLimit(which int) (uint64, error) {
rlimit := syscall.Rlimit{}

if err := syscall.Getrlimit(which, &rlimit); err != nil {
return 0, err
}

return rlimit.Cur, nil
}
2 changes: 1 addition & 1 deletion process_metrics_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !linux && !windows && !solaris
//go:build !linux && !windows && !solaris && !darwin

package metrics

Expand Down
2 changes: 2 additions & 0 deletions vendor/golang.org/x/sys/unix/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

184 changes: 184 additions & 0 deletions vendor/golang.org/x/sys/unix/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions vendor/golang.org/x/sys/unix/affinity_linux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions vendor/golang.org/x/sys/unix/aliases.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading