pstree

package module
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2023 License: BSD-3-Clause Imports: 9 Imported by: 4

README

pstree

GoDoc

pstree is a simple minded package to retrieve the process tree from a given PID.

Installation

sh> go get github.com/sbinet/pstree

Documentation

Documentation is available on godoc:

https://godoc.org/github.com/sbinet/pstree

Example

package main

import (
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"

	"github.com/sbinet/pstree"
)

func main() {
	pid, err := strconv.Atoi(os.Args[1])
	if err != nil {
		log.Fatalf("could not retrieve pid: %v\n", err)
	}
	tree, err := pstree.New()
	if err != nil {
		log.Fatalf("error: %v\n", err)
	}

	fmt.Printf("tree[%d]: %v\n", pid, tree.Procs[pid])
	display(pid, tree, 1)
}

func display(pid int, tree *pstree.Tree, indent int) {
	str := strings.Repeat("  ", indent)
	for _, cid := range tree.Procs[pid].Children {
		proc := tree.Procs[cid]
		fmt.Printf("%s%#v\n", str, proc)
		display(cid, tree, indent+1)
	}
}

Documentation

Overview

Package pstree provides an API to retrieve the process tree from procfs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Process

type Process struct {
	Name     string      `json:"name"`
	Stat     ProcessStat `json:"stat"`
	Children []int       `json:"children"`
}

Process stores information about a UNIX process.

type ProcessStat

type ProcessStat struct {
	PID       int    `json:"pid"`       // process ID
	Comm      string `json:"comm"`      // filename of the executable in parentheses
	State     byte   `json:"state"`     // process state
	Ppid      int    `json:"ppid"`      // pid of the parent process
	Pgrp      int    `json:"pgrp"`      // process group ID of the process
	Session   int    `json:"session"`   // session ID of the process
	TTY       int    `json:"tty"`       // controlling terminal of the process
	Tpgid     int    `json:"tpgid"`     // ID of foreground process group
	Flags     uint32 `json:"flags"`     // kernel flags word of the process
	Minflt    uint64 `json:"minflt"`    // number of minor faults the process has made which have not required loading a memory page from disk
	Cminflt   uint64 `json:"cminflt"`   // number of minor faults the process's waited-for children have made
	Majflt    uint64 `json:"majflt"`    // number of major faults the process has made which have required loading a memory page from disk
	Cmajflt   uint64 `json:"cmajflt"`   // number of major faults the process's waited-for children have made
	Utime     uint64 `json:"utime"`     // user time in clock ticks
	Stime     uint64 `json:"stime"`     // system time in clock ticks
	Cutime    int64  `json:"cutime"`    // children user time in clock ticks
	Cstime    int64  `json:"cstime"`    // children system time in clock ticks
	Priority  int64  `json:"priority"`  // priority
	Nice      int64  `json:"nice"`      // the nice value
	Nthreads  int64  `json:"nthreads"`  // number of threads in this process
	Itrealval int64  `json:"itrealval"` // time in jiffies before next SIGALRM is sent to the process due to an interval timer
	Starttime int64  `json:"starttime"` // time the process started after system boot in clock ticks
	Vsize     uint64 `json:"vsize"`     // virtual memory size in bytes
	RSS       int64  `json:"rss"`       // resident set size: number of pages the process has in real memory

	Environ string `json:"environ"` // environment for the process
	Cwd     string `json:"cwd"`     // current working directory for the process
	Cmdline string `json:"cmdline"` // complete command line for the process
}

ProcessStat contains process information. see: http://man7.org/linux/man-pages/man5/proc.5.html

type Tree

type Tree struct {
	Procs map[int]Process `json:"procs"`
}

Tree is a tree of processes.

func New

func New() (*Tree, error)

New returns the whole system process tree.

Directories

Path Synopsis
cmd
procs-tree
Command procs-tree displays the tree of children processes for a given PID.
Command procs-tree displays the tree of children processes for a given PID.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL