Debian Codegraph @

R/unix/Sys.ps.R

raw
1 #### Martin Maechler, Aug.2000, originally in /u/maechler/R/MISC/ps.R
2
3 ### --> ../../man/unix/Sys.ps.Rd to see more comments
4
5 ## I would really like builtin Sys.ps() for these
6
7 ### Sys.ps.cmd() ---> now moved to ../u.goodies.R
8
9 ## These only apply to "System V" compatible `ps', not to BSD ones
10 .Sys.ps.fields <-
11 list(POSIX = c("args", "comm", "time", "etime", "nice", "pcpu",
12 "pid", "pgid", "ppid", "group", "rgroup", "user", "ruser",
13 "tty", "vsz"),
14
15 ## Now the extras, not in above POSIX:
16 SunOS = c( "addr", "pri", "c", "rgid", "class", "rss", "f",
17 "ruid", "fname", "s", "gid", "sid", "opri", "stime", "osz",
18 "uid", "pmem", "wchan"),
19 Linux =## These are Linux (RH 6.2):"Docu" at end ..
20 c("%cpu", "%mem", "alarm", "blocked", "bsdstart", "bsdtime",
21 "c", "caught", "cmd", "command", "cputime", "drs", "dsiz", "egid",
22 "egroup", "eip", "esp", "euid", "euser", "f", "fgid", "fgroup",
23 "flag", "flags", "fname", "fsgid", "fsgroup", "fsuid", "fsuser",
24 "fuid", "fuser", "gid", "ignored", "intpri", "lim", "longtname",
25 "lstart", "m_drs", "m_trs", "maj_flt", "majflt", "min_flt", "minflt",
26 "ni", "nwchan", "opri", "pagein", "pending", "pgrp", "pmem",
27 "pri", "rgid", "rss", "rssize", "rsz", "ruid", "s", "sess", "session",
28 "sgi_p", "sgi_rss", "sgid", "sgroup", "sid", "sig", "sig_block",
29 "sig_catch", "sig_ignore", "sig_pend", "sigcatch", "sigignore",
30 "sigmask", "stackp", "start", "start_stack", "start_time", "stat",
31 "state", "stime", "suid", "suser", "svgid", "svgroup", "svuid",
32 "svuser", "sz", "timeout", "tmout", "tname", "tpgid", "trs",
33 "trss", "tsiz", "tt", "tty4", "tty8", "ucomm", "uid", "uid_hack",
34 "uname", "vsize", "wchan")
35 )
36
37 ## Note that proc.time() gives part of that info better
38
39 ## command == cmd == args gives "command + arguments : too long
40 .Sys.ps.multifields <- c("command", "cmd","args", "lstart")
41
42 Sys.ps <-
43 function(process = Sys.getpid(),
44 fields = c("pid", "pcpu", "time", "vsz", "comm"),
45 usefile = length(fields) > 10,
46 ps.cmd = Sys.ps.cmd(),
47 verbose = getOption("verbose"),
48 warn.multi = verbose || any(fields != "ALL"))
49 {
50 if(!is.null(tp <- attr(ps.cmd,"type")) && tp == "BSD")
51 stop("this function cannot work with BSD kind of `ps'.")
52
53 ps.opt <- {
54 if(is.numeric(process) && process == round(process))
55 paste("-p",process) # PID
56 else if(process == "ALL") {
57 warning("`process = \"ALL\"' not yet working properly")
58 "-e" # all process
59 }
60 else if(is.character(process) && length(process) == 1)
61 paste("-C",process) # Command name
62 else stop(paste("invalid `process':",format(process)))
63 }
64 if(length(ps.opt) > 1)
65 warning("Multiple processes : not yet working ...")
66
67 Sys.ps.fields <- c(.Sys.ps.fields $ POSIX,
68 if(any(ii <- Sys.info()["sysname"] ==
69 names(.Sys.ps.fields)))
70 .Sys.ps.fields[ii][[1]])
71
72 if(identical(fields, "ALL"))
73 i.field <- TRUE
74 else {
75 i.field <- pmatch(fields, Sys.ps.fields) # allow abbreviated ones
76 if(any(ina <- is.na(i.field))) {
77 warning(paste("Dropping invalid field names",
78 fields[ina]))
79 i.field <- i.field[!ina]
80 }
81 }
82 fields <- Sys.ps.fields[i.field]
83 imult <- !is.na(match(fields, .Sys.ps.multifields))
84 if(any(imult) && length(fields) > 1) {
85 if(warn.multi)
86 warning(paste("Not using `multi fields' ",
87 paste(fields[imult],collapse=",")))
88 fields <- fields[!imult]
89 imult <- FALSE
90 }
91 ## Don't use "-w" with cmd/args, or command : gives space in between
92 ## Must use "--width" (GNU ps only) when there are many fields ...
93 ## Need temporary file & scan since system cannot get very long
94 ## lines ...
95 if(usefile)
96 ofile <- tempfile("R.Sys.ps")
97 cmd <- paste(ps.cmd, ps.opt,
98 "-o", paste(fields, collapse=","),
99 if(usefile) paste(" >", ofile))
100 if(verbose) cat("Now calling\n\t",cmd,"\n")
101 lines <- system(cmd, intern = !usefile)
102 if(usefile) {
103 if(lines) warning(paste("system() returned non-0 :",lines))
104 lines <- scan(ofile, what = "", sep="\n", quiet = TRUE)## incl header
105 }
106 if(length(lines) <= 1)
107 stop(paste("call returned less than two lines:", lines, sep="\n"))
108
109 r <- sub("^ ","", gsub("[ ]+"," ", lines))
110 ## SP & TAB
111 if(length(fields) == 1) {
112 if(length(r) == 2)
113 return(structure(r[2], names = fields))
114 else
115 warning(paste("Funny result with one `fields': length(r)=",
116 length(r)))
117 }
118 ## else {
119 ll <- strsplit(r, " ")
120 d.len <- diff(lenl <- sapply(ll, length))
121 if(lenl[1] == length(fields))
122 ## use fields!
123 ll[[1]] <- fields
124 else
125 warning(paste("Number returned headers =", lenl[1], " != ",
126 "#{fields} =", length(fields)))
127 if(d.len) { # names and result differ
128 warning(paste("Lengths differ:",
129 paste(lenl, collapse=",")))
130 }
131 r <- c(ll[[2]], rep(NA, max(0,-d.len)))
132 names(r) <-
133 if( d.len > 0) c(ll[[1]], rep(".x.",d.len)) else ll[[1]][1:lenl[2]]
134 r
135 ##}
136 }
137
138 Sys.sizes <- function(process = Sys.getpid(),
139 ps.cmd = Sys.ps.cmd())
140 {
141 ## For both Solaris and GNU(Linux); GNU/Linux additionally has dsize
142
143 if(!is.null(tp <- attr(ps.cmd,"type")) && tp == "BSD") {
144 ## a *real* hack [needed for Linux 2.0 or SunOS 4.x ..]
145 r <- system(paste(ps.cmd,"m",process), intern = TRUE)[1:2] # 2 lines
146 r <- strsplit(r," *")
147 hd <- r[[1]]; hd <- hd[hd != "" & hd != "COMMAND"]
148 i <- match(c("RSS","DRS"), hd)
149 r <- structure(r[[2]][i], names = hd[i])
150 }
151 else { ## proper "System V like" ps :
152 r <- Sys.ps(process, c("rss","vsz"))
153 }
154 storage.mode(r) <- "integer"
155 r
156 }
157
158 if(Sys.info()[["sysname"]] == "Linux") { ##----- Linux-only ----
159
160 Sys.procinfo <- function(procfile)
161 {
162 l2 <- strsplit(readLines(procfile),"[ \t]*:[ \t]*")
163 r <- sapply(l2[sapply(l2, length) == 2],
164 function(c2)structure(c2[2], names= c2[1]))
165 attr(r,"Name") <- procfile
166 names(r) <- make.names(names(r), unique = TRUE) # <- so the result can be name-indexed!
167 class(r) <- "simple.list"
168 r
169 }
170
171 } else { ## non-Linux "unix" -- including MacOS X "Darwin"
172
173 Sys.procinfo <- function(procfile) {
174 stop("Sys.procinfo() is not yet implemented for non-Linux unix-alikes")
175 }
176
177 }
178
179 Sys.cpuinfo <- function() Sys.procinfo("/proc/cpuinfo")
180 Sys.meminfo <- function() Sys.procinfo("/proc/meminfo")
181
182 Sys.MIPS <- function() as.numeric(Sys.cpuinfo()["bogomips"])
183
184 Sys.memGB <- function(kind = "MemTotal") {
185 mm <- drop(read.dcf("/proc/meminfo", fields=kind))
186 if(any(is.na(mm))) stop("Non-existing 'kind': ", names(mm)[is.na(mm)][1])
187 if(!all(grepl(" kB$", mm)))
188 stop("Memory info ", dQuote(kind), " is not returned in 'kB' aka kiloBytes")
189 ## return memory in giga bytes
190 as.numeric(sub(" kB$", "", mm)) / (1000 * 1024)
191 }