1 ! Copyright (C) 2013 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators combinators.smart
4 io.encodings.utf8 io.files kernel math math.order math.parser
5 memoize sequences sorting.slots splitting splitting.monotonic
6 strings io.pathnames calendar words ;
14 TUPLE: proc-cmdline string ;
15 C: <proc-cmdline> proc-cmdline
16 : parse-proc-cmdline ( -- obj )
17 "/proc/cmdline" utf8 file-lines first <proc-cmdline> ;
23 { cpu-family integer }
29 { cache-size integer }
34 { physical-id integer }
39 { initial-apicid integer }
41 { fpu-exception? boolean }
42 { cpuid-level integer }
46 { clflush-size integer }
47 { cache-alignment integer }
48 { address-sizes array }
49 { power-management string }
54 ERROR: unknown-cpuinfo-line string ;
56 : line>processor-info ( processor-info string -- processor-info )
58 [ CHAR: \t = ] trim-tail [ [ CHAR: \s = ] trim ] bi@
61 "," split [ [ CHAR: \s = ] trim " " split first string>number ] map
64 { "apicid" [ string>number >>apicid ] }
65 { "bogomips" [ string>number >>bogomips ] }
67 " " split first [ CHAR: \s = ] trim
68 string>number 1024 * >>cache-size
70 { "cache_alignment" [ string>number >>cache-alignment ] }
71 { "clflush size" [ string>number >>clflush-size ] }
72 { "coma_bug" [ "yes" = >>coma-bug? ] }
73 { "core id" [ string>number >>core-id ] }
74 { "cpu MHz" [ string>number >>cpu-mhz ] }
75 { "cpu cores" [ string>number >>cpu-cores ] }
76 { "cpu family" [ string>number >>cpu-family ] }
77 { "cpuid level" [ string>number >>cpuid-level ] }
78 { "f00f_bug" [ "yes" = >>f00f-bug? ] }
79 { "fdiv_bug" [ "yes" = >>fdiv-bug? ] }
80 { "flags" [ " " split harvest >>flags ] }
81 { "fpu" [ "yes" = >>fpu? ] }
82 { "fpu_exception" [ "yes" = >>fpu-exception? ] }
83 { "hlt_bug" [ "yes" = >>hlt-bug? ] }
84 { "initial apicid" [ string>number >>initial-apicid ] }
85 { "microcode" [ string>number >>microcode ] }
86 { "model" [ string>number >>model ] }
87 { "model name" [ >>model-name ] }
88 { "physical id" [ string>number >>physical-id ] }
89 { "power management" [ >>power-management ] }
90 { "processor" [ string>number >>processor ] }
91 { "siblings" [ string>number >>siblings ] }
92 { "stepping" [ string>number >>stepping ] }
93 { "vendor_id" [ >>vendor-id ] }
94 { "wp" [ "yes" = >>wp? ] }
95 { "TLB size" [ >>tlb-size ] }
97 [ unknown-cpuinfo-line ]
101 ! Linux 2.6 has fewer values than new kernels
102 : lines>processor-info ( strings -- processor-info )
103 [ processor-info new ] dip
104 [ line>processor-info ] each ;
106 : parse-proc-cpuinfo ( -- seq )
107 "/proc/cpuinfo" utf8 file-lines
108 { "" } split harvest [ lines>processor-info ] map ;
110 : sort-cpus ( seq -- seq )
111 { { physical-id>> <=> } { core-id>> <=> } } sort-by
112 [ [ physical-id>> ] bi@ = ] monotonic-split
113 [ [ [ core-id>> ] bi@ = ] monotonic-split ] map ;
115 : cpu-counts ( seq -- #cpus #cores #hyperthread )
117 [ [ length ] map-sum ]
118 [ [ [ length ] map-sum ] map-sum ] tri ;
129 : parse-proc-loadavg ( -- obj )
130 "/proc/loadavg" utf8 file-lines first
136 [ "/" split1 [ string>number ] bi@ ]
139 ] input<sequence proc-loadavg boa ;
142 ! In the file as kb, convert to bytes
187 ! Different kernels have fewer fields. Make sure we have enough.
188 : parse-proc-meminfo ( -- meminfo )
189 "/proc/meminfo" utf8 file-lines
190 [ " " split harvest second string>number 1024 * ] map
191 proc-meminfo "slots" word-prop length f pad-tail
192 [ proc-meminfo boa ] input<sequence ;
194 ! All cpu-stat fields are measured in jiffies.
206 TUPLE: proc-cpu-stat name user nice system idle iowait irq softirq steal guest guest-nice ;
208 : line>cpu ( string -- cpu )
211 [ [ [ CHAR: \s = ] trim string>number ] map ] dip prefix
212 [ proc-cpu-stat boa ] input<sequence ;
214 : parse-proc-stat ( -- obj )
215 "/proc/stat" utf8 file-lines
216 [ first ] [ 7 head* rest ] [ 7 tail* ] tri 3array {
218 [ second [ line>cpu ] map ]
221 [ " " split1 nip " " split [ string>number ] map ] map
234 } cleave proc-stat boa ;
236 : active-cpus ( -- n )
237 parse-proc-stat procs-running>> ;
239 TUPLE: proc-partition major minor #blocks name ;
241 : parse-proc-partitions ( -- partitions )
242 "/proc/partitions" utf8 file-lines 2 tail
252 ] input<sequence proc-partition boa
255 TUPLE: proc-swap filename type size used priority ;
257 : parse-proc-swaps ( -- sequence )
258 "/proc/swaps" utf8 file-lines rest
269 ] input<sequence proc-swap boa
272 TUPLE: proc-uptime up idle ;
274 : parse-proc-uptime ( -- uptime )
275 "/proc/uptime" utf8 file-lines first
276 " " split first2 [ string>number seconds ] bi@
281 GENERIC#: proc-pid-path 1 ( object string -- path )
283 M: integer proc-pid-path ( pid string -- path )
285 [ number>string "/" append ] dip
288 M: string proc-pid-path ( pid-string string -- path )
289 [ "/proc/" ] 2dip [ append-path ] dip append-path ;
291 : proc-file-lines ( path -- strings ) utf8 file-lines ;
292 : proc-first-line ( path -- string/f ) proc-file-lines ?first ;
294 : proc-pid-first-line ( pid string -- string )
295 proc-pid-path proc-first-line ;
297 : parse-proc-pid-cmdline ( pid -- string/f )
298 "cmdline" proc-pid-path proc-first-line ;
300 TUPLE: pid-stat pid filename state parent-pid group-id session-id terminal#
301 terminal-group-id task-flags
302 #minor-page-faults #minor-page-faults-child
303 #major-page-faults #major-page-faults-child
305 cpu-user-children cpu-kernel-children
312 resident-set-size resident-set-limit
313 start-address end-address stack-start-address
314 current-stack-pointer current-instruction-pointer
315 pending-signals blocked-signals ignored-signals
325 guest-time children-guest-time
332 : parse-proc-pid-stat ( pid -- stat )
336 pid-stat "slots" word-prop length "0" pad-tail
337 [ dup string>number [ nip ] when* ] map
338 [ pid-stat boa ] input<sequence ;