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 }
53 ERROR: unknown-cpuinfo-line string ;
55 : line>processor-info ( processor-info string -- processor-info )
57 [ CHAR: \t = ] trim-tail [ [ CHAR: \s = ] trim ] bi@
60 "," split [ [ CHAR: \s = ] trim " " split first string>number ] map
63 { "apicid" [ string>number >>apicid ] }
64 { "bogomips" [ string>number >>bogomips ] }
66 " " split first [ CHAR: \s = ] trim
67 string>number 1024 * >>cache-size
69 { "cache_alignment" [ string>number >>cache-alignment ] }
70 { "clflush size" [ string>number >>clflush-size ] }
71 { "coma_bug" [ "yes" = >>coma-bug? ] }
72 { "core id" [ string>number >>core-id ] }
73 { "cpu MHz" [ string>number >>cpu-mhz ] }
74 { "cpu cores" [ string>number >>cpu-cores ] }
75 { "cpu family" [ string>number >>cpu-family ] }
76 { "cpuid level" [ string>number >>cpuid-level ] }
77 { "f00f_bug" [ "yes" = >>f00f-bug? ] }
78 { "fdiv_bug" [ "yes" = >>fdiv-bug? ] }
79 { "flags" [ " " split harvest >>flags ] }
80 { "fpu" [ "yes" = >>fpu? ] }
81 { "fpu_exception" [ "yes" = >>fpu-exception? ] }
82 { "hlt_bug" [ "yes" = >>hlt-bug? ] }
83 { "initial apicid" [ string>number >>initial-apicid ] }
84 { "microcode" [ string>number >>microcode ] }
85 { "model" [ string>number >>model ] }
86 { "model name" [ >>model-name ] }
87 { "physical id" [ string>number >>physical-id ] }
88 { "power management" [ >>power-management ] }
89 { "processor" [ string>number >>processor ] }
90 { "siblings" [ string>number >>siblings ] }
91 { "stepping" [ string>number >>stepping ] }
92 { "vendor_id" [ >>vendor-id ] }
93 { "wp" [ "yes" = >>wp? ] }
94 { "TLB size" [ >>tlb-size ] }
95 [ throw-unknown-cpuinfo-line ]
99 ! Linux 2.6 has fewer values than new kernels
100 : lines>processor-info ( strings -- processor-info )
101 [ processor-info new ] dip
102 [ line>processor-info ] each ;
104 : parse-proc-cpuinfo ( -- seq )
105 "/proc/cpuinfo" utf8 file-lines
106 { "" } split harvest [ lines>processor-info ] map ;
108 : sort-cpus ( seq -- seq )
109 { { physical-id>> <=> } { core-id>> <=> } } sort-by
110 [ [ physical-id>> ] bi@ = ] monotonic-split
111 [ [ [ core-id>> ] bi@ = ] monotonic-split ] map ;
113 : cpu-counts ( seq -- #cpus #cores #hyperthread )
115 [ [ length ] map-sum ]
116 [ [ [ length ] map-sum ] map-sum ] tri ;
127 : parse-proc-loadavg ( -- obj )
128 "/proc/loadavg" utf8 file-lines first
134 [ "/" split1 [ string>number ] bi@ ]
137 ] input<sequence proc-loadavg boa ;
140 ! In the file as kb, convert to bytes
185 ! Different kernels have fewer fields. Make sure we have enough.
186 : parse-proc-meminfo ( -- meminfo )
187 "/proc/meminfo" utf8 file-lines
188 [ " " split harvest second string>number 1024 * ] map
189 proc-meminfo "slots" word-prop length f pad-tail
190 [ proc-meminfo boa ] input<sequence ;
192 ! All cpu-stat fields are measured in jiffies.
204 TUPLE: proc-cpu-stat name user nice system idle iowait irq softirq steal guest guest-nice ;
206 : line>cpu ( string -- cpu )
209 [ [ [ CHAR: \s = ] trim string>number ] map ] dip prefix
210 [ proc-cpu-stat boa ] input<sequence ;
212 : parse-proc-stat ( -- obj )
213 "/proc/stat" utf8 file-lines
214 [ first ] [ 7 head* rest ] [ 7 tail* ] tri 3array {
216 [ second [ line>cpu ] map ]
219 [ " " split1 nip " " split [ string>number ] map ] map
232 } cleave proc-stat boa ;
234 : active-cpus ( -- n )
235 parse-proc-stat procs-running>> ;
237 TUPLE: proc-partition major minor #blocks name ;
239 : parse-proc-partitions ( -- partitions )
240 "/proc/partitions" utf8 file-lines 2 tail
250 ] input<sequence proc-partition boa
253 TUPLE: proc-swap filename type size used priority ;
255 : parse-proc-swaps ( -- sequence )
256 "/proc/swaps" utf8 file-lines rest
267 ] input<sequence proc-swap boa
270 TUPLE: proc-uptime up idle ;
272 : parse-proc-uptime ( -- uptime )
273 "/proc/uptime" utf8 file-lines first
274 " " split first2 [ string>number seconds ] bi@
279 GENERIC# proc-pid-path 1 ( object string -- path )
281 M: integer proc-pid-path ( pid string -- path )
283 [ number>string "/" append ] dip
286 M: string proc-pid-path ( pid-string string -- path )
287 [ "/proc/" ] 2dip [ append-path ] dip append-path ;
289 : proc-file-lines ( path -- strings ) utf8 file-lines ;
290 : proc-first-line ( path -- string/f ) proc-file-lines ?first ;
292 : proc-pid-first-line ( pid string -- string )
293 proc-pid-path proc-first-line ;
295 : parse-proc-pid-cmdline ( pid -- string/f )
296 "cmdline" proc-pid-path proc-first-line ;
298 TUPLE: pid-stat pid filename state parent-pid group-id session-id terminal#
299 terminal-group-id task-flags
300 #minor-page-faults #minor-page-faults-child
301 #major-page-faults #major-page-faults-child
303 cpu-user-children cpu-kernel-children
310 resident-set-size resident-set-limit
311 start-address end-address stack-start-address
312 current-stack-pointer current-instruction-pointer
313 pending-signals blocked-signals ignored-signals
323 guest-time children-guest-time
330 : parse-proc-pid-stat ( pid -- stat )
334 pid-stat "slots" word-prop length "0" pad-tail
335 [ dup string>number [ nip ] when* ] map
336 [ pid-stat boa ] input<sequence ;