Skip to content

Commit

Permalink
pkg/trace/watchdog: add user cpu reader for AIX
Browse files Browse the repository at this point in the history
  • Loading branch information
knusbaum committed Nov 18, 2020
1 parent ba057e5 commit ea1514a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ replace (
github.com/mholt/archiver => github.com/mholt/archiver v2.0.1-0.20171012052341-26cf5bb32d07+incompatible
github.com/prometheus/client_golang => github.com/prometheus/client_golang v0.9.2
github.com/spf13/cast => github.com/DataDog/cast v1.3.1-0.20190301154711-1ee8c8bd14a3
github.com/spf13/viper => github.com/DataDog/viper v1.8.0
github.com/spf13/viper => github.com/DataDog/viper v1.8.1-0.20201112231441-af23aee54ecc
github.com/ugorji/go => github.com/ugorji/go v1.1.7
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ github.com/DataDog/mmh3 v0.0.0-20200316233529-f5b682d8c981 h1:UcKqIrOowv2PjTkOC2
github.com/DataDog/mmh3 v0.0.0-20200316233529-f5b682d8c981/go.mod h1:SvsjzyJlSg0rKsqYgdcFxeEVflx3ZNAyFfkUHP0TxXg=
github.com/DataDog/viper v1.8.0 h1:+0s6jzOzQEpMCW+c4bkirz+4vNoWm4jmiQNmKfO2p38=
github.com/DataDog/viper v1.8.0/go.mod h1:Gx7+/WONkbQIh3ac52KqcFTzipQ1OYHhCQGWAgHlzpc=
github.com/DataDog/viper v1.8.1-0.20201112231441-af23aee54ecc h1:O5aUg0dgJYZHxjkmXa8k5f9kQgOYrs4JsgTtZhQokIo=
github.com/DataDog/viper v1.8.1-0.20201112231441-af23aee54ecc/go.mod h1:Gx7+/WONkbQIh3ac52KqcFTzipQ1OYHhCQGWAgHlzpc=
github.com/DataDog/watermarkpodautoscaler v0.1.0 h1:EK8mxbhGt+jNRu/q8fdGi05aX7buVSQqONWxS/DnzXM=
github.com/DataDog/watermarkpodautoscaler v0.1.0/go.mod h1:JHOlfw4/9Ng1io9QDotxKggdFEYFd2Au9cj+lXzQSRA=
github.com/DataDog/zstd v0.0.0-20160706220725-2bf71ec48360 h1:CiuXIvblnzlQEnFP5tvrzcONd0AZpOuqzFg+bkCq95U=
Expand Down
2 changes: 1 addition & 1 deletion pkg/trace/watchdog/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2020 Datadog, Inc.

// +build !windows
// +build !windows,!aix

package watchdog

Expand Down
106 changes: 106 additions & 0 deletions pkg/trace/watchdog/cpu_aix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2020 Datadog, Inc.

package watchdog

import (
"encoding/binary"
"fmt"
"os"
)

// From proc(5) on AIX 7.2
// status
// Contains state information about the process and one of its
// representative thread. The file is formatted as a struct pstatus
// type containing the following members:
//
// uint32_t pr_flag; /* process flags from proc struct p_flag */
// uint32_t pr_flag2; /* process flags from proc struct p_flag2 */
// uint32_t pr_flags; /* /proc flags */
// uint32_t pr_nlwp; /* number of threads in the process */
// char pr_stat; /* process state from proc p_stat */
// char pr_dmodel; /* data model for the process */
// char pr__pad1[6]; /* reserved for future use */
// pr_sigset_t pr_sigpend; /* set of process pending signals */
// prptr64_t pr_brkbase; /* address of the process heap */
// uint64_t pr_brksize; /* size of the process heap, in bytes */
// prptr64_t pr_stkbase; /* address of the process stack */
// uint64_t pr_stksize; /* size of the process stack, in bytes */
// pid64_t pr_pid; /* process id */
// pid64_t pr_ppid; /* parent process id */
// pid64_t pr_pgid; /* process group id */
// pid64_t pr_sid; /* session id */
// struct pr_timestruc64_t pr_utime; /* process user cpu time */
// struct pr_timestruc64_t pr_stime; /* process system cpu time */
// struct pr_timestruc64_t pr_cutime; /* sum of children's user times */
// struct pr_timestruc64_t pr_cstime; /* sum of children's system times */
// pr_sigset_t pr_sigtrace; /* mask of traced signals */
// fltset_t pr_flttrace; /* mask of traced hardware faults */
// uint32_t pr_sysentry_offset; /* offset into pstatus file of sysset_t
// * identifying system calls traced on
//
// * entry. If 0, then no entry syscalls
// * are being traced. */
// uint32_t pr_sysexit_offset; /* offset into pstatus file of sysset_t
// * identifying system calls traced on
// * exit. If 0, then no exit syscalls
// * are being traced. */
// uint64_t pr__pad[8]; /* reserved for future use */
// lwpstatus_t pr_lwp; /* "representative" thread status */
//
// From /usr/include/sys/procfs.h
// typedef struct pr_sigset
// {
// uint64_t ss_set[4]; /* signal set */
// } pr_sigset_t;
//
// typedef struct pr_timestruc64
// {
// int64_t tv_sec; /* 64 bit time_t value */
// int32_t tv_nsec; /* 32 bit suseconds_t value */
// uint32_t __pad; /* reserved for future use */
// } pr_timestruc64_t;
//
// typedef void * prptr64_t;
//
// The fields before the user cpu time (pr_utime) are:
// uint32_t pr_flag; 4 4
// uint32_t pr_flag2; 4 8
// uint32_t pr_flags; 4 12
// uint32_t pr_nlwp; 4 16
// char pr_stat; 1 17
// char pr_dmodel; 1 18
// char pr__pad1[6]; 6 24
// pr_sigset_t pr_sigpend; (4 * 8) = 32 56
// prptr64_t pr_brkbase; 8 64
// uint64_t pr_brksize; 8 72
// prptr64_t pr_stkbase; 8 80
// uint64_t pr_stksize; 8 88
// pid64_t pr_pid; 8 96
// pid64_t pr_ppid; 8 104
// pid64_t pr_pgid; 8 112
// pid64_t pr_sid; 8 120
// total: 120
// followed by:
// struct pr_timestruc64_t pr_utime; /* process user cpu time */

// As explained above, we will skip 120 bytes into the status file to locate the user CPU time.
const skip = 120

func cpuTimeUser(pid int32) (float64, error) {
f, err := os.Open(fmt.Sprintf("/proc/%d/status", pid))
if err != nil {
return 0, err
}
defer f.Close()
f.Seek(skip, os.SEEK_SET)
var userSecs int64
var userNsecs int32
binary.Read(f, binary.BigEndian, &userSecs)
binary.Read(f, binary.BigEndian, &userNsecs)
time := float64(userSecs) + (float64(userNsecs) / 1000000000)
return time, nil
}

0 comments on commit ea1514a

Please sign in to comment.