|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 5 | + * or more contributor license agreements. See the NOTICE file |
| 6 | + * distributed with this work for additional information |
| 7 | + * regarding copyright ownership. The ASF licenses this file |
| 8 | + * to you under the Apache License, Version 2.0 (the |
| 9 | + * "License"); you may not use this file except in compliance |
| 10 | + * with the License. You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, |
| 15 | + * software distributed under the License is distributed on an |
| 16 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | + * KIND, either express or implied. See the License for the |
| 18 | + * specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +package common |
| 24 | + |
| 25 | +import ( |
| 26 | + "fmt" |
| 27 | + "io/ioutil" |
| 28 | + "os" |
| 29 | +) |
| 30 | + |
| 31 | +// ProcessState describes the state of a process |
| 32 | +type ProcessState rune |
| 33 | + |
| 34 | +const ( |
| 35 | + // Running state |
| 36 | + Running ProcessState = 'R' |
| 37 | + // Sleeping state |
| 38 | + Sleeping ProcessState = 'S' |
| 39 | + // Waiting state |
| 40 | + Waiting ProcessState = 'D' |
| 41 | + // Zombie state |
| 42 | + Zombie ProcessState = 'Z' |
| 43 | + // Stopped state |
| 44 | + Stopped ProcessState = 'T' |
| 45 | + // TracingStop state |
| 46 | + TracingStop ProcessState = 't' |
| 47 | + // Dead state |
| 48 | + Dead ProcessState = 'X' |
| 49 | +) |
| 50 | + |
| 51 | +// ProcessInfo describes the information of a running process |
| 52 | +type ProcessInfo struct { |
| 53 | + Process string |
| 54 | + Pid int64 |
| 55 | + Name string |
| 56 | + PPid int64 |
| 57 | + PGrp int64 |
| 58 | + Session int64 |
| 59 | + Utime int64 |
| 60 | + Stime int64 |
| 61 | + CUtime int64 |
| 62 | + CStime int64 |
| 63 | + Priority int64 |
| 64 | + Nice int64 |
| 65 | + Threads int64 |
| 66 | + Start int64 |
| 67 | + Vsize int64 |
| 68 | + RSS int64 |
| 69 | + State ProcessState |
| 70 | +} |
| 71 | + |
| 72 | +// GetProcessInfo retrieve process info from /proc |
| 73 | +func GetProcessInfo(pid int) (*ProcessInfo, error) { |
| 74 | + processPath := fmt.Sprintf("/proc/%d", pid) |
| 75 | + exe, err := os.Readlink(processPath + "/exe") |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + stat, err := ioutil.ReadFile(processPath + "/stat") |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + pi := &ProcessInfo{ |
| 86 | + Process: exe, |
| 87 | + } |
| 88 | + |
| 89 | + var name string |
| 90 | + var state rune |
| 91 | + var null int64 |
| 92 | + |
| 93 | + fmt.Sscanf(string(stat), "%d %s %c %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", |
| 94 | + &pi.Pid, &name, &state, &pi.PPid, &pi.PGrp, &pi.Session, |
| 95 | + &null, &null, &null, &null, &null, &null, &null, |
| 96 | + &pi.Utime, &pi.Stime, &pi.CUtime, &pi.CStime, &pi.Priority, &pi.Nice, &pi.Threads, |
| 97 | + &null, |
| 98 | + &pi.Start, &pi.Vsize, &pi.RSS) |
| 99 | + |
| 100 | + pi.Name = name[1 : len(name)-1] |
| 101 | + pi.State = ProcessState(state) |
| 102 | + |
| 103 | + return pi, nil |
| 104 | +} |
0 commit comments