6 let font = Glut.BITMAP_HELVETICA_12
7 let draw_string ?
(font=font) x y s
=
8 GlPix.raster_pos ~x ~y
();
9 String.iter
(fun c
-> Glut.bitmapCharacter ~
font ~c
:(Char.code c
)) s
14 ; loads
: int64
* int64
* int64
24 external get_nprocs
: unit -> int = "ml_get_nprocs"
25 external idletimeofday
: Unix.file_descr
-> int -> float array
27 external sysinfo
: unit -> sysinfo
= "ml_sysinfo"
28 external waitalrm
: unit -> unit = "ml_waitalrm"
29 external get_hz
: unit -> int = "ml_get_hz"
30 external setnice
: int -> unit = "ml_nice"
31 external delay
: float -> unit = "ml_delay"
32 external is_winnt
: unit -> bool = "ml_is_winnt"
34 let winnt = is_winnt
()
44 let hz = get_hz
() |> float
46 let jiffies_to_sec j
=
50 let ic = open_in
"/proc/uptime" in
51 let vals = Scanf.fscanf
ic "%f %f" (fun u i
-> (u
, i
)) in
55 let nprocs = get_nprocs
()
57 let rec parse_int_cont s pos
=
58 let slen = String.length s
in
64 if String.get s
pos = ' '
65 then succ
pos |> skipws
70 try String.index_from s
pos ' '
71 with Not_found
-> slen
73 let i = endpos - pos |> String.sub s
pos
80 `more
(i, fun () -> succ
endpos |> parse_int_cont s
)
83 let rec tolist accu
= function
84 | `last
i -> i :: accu
85 | `more
(i, f
) -> f
() |> tolist (i :: accu
)
87 let index = String.index s ' '
in
88 let cpuname = String.sub s
0 index in
89 let vals = parse_int_cont s
(succ
index) |> tolist [] in
90 let vals = List.rev
|<
91 if List.length
vals < 7
93 0.0 :: 0.0 :: 0.0 :: 0.0 :: vals
97 cpuname, Array.of_list
vals
103 let ia = idletimeofday
Unix.stdin
nprocs in
104 let rec convert accu total n
=
108 let a = "cpu", Array.make
7 t in
111 let i = Array.get
ia n
in
112 let total = total +. i in
113 let v = "cpu" ^ string_of_int n
, Array.make
7 i in
114 convert |< v :: accu
|< total |< succ n
119 let ic = open_in
"/proc/stat" in
120 let rec loop i accu
=
123 else (input_line
ic |> parse_cpul) :: accu
|> loop (pred
i)
125 let ret = loop nprocs [] in
131 Filename.dirname
|< Unix.readlink
"/proc/self/exe"
138 [ "Amazing Piece of Code by insanely gifted programmer, Version 0.97c"
139 ; "Motivation by: gzh and afs"
141 ] |> String.concat
"\n"
144 let interval = ref 15.0
145 let devpath = NP.getselfdir () |> Filename.concat
|< "itc" |> ref
150 let verbose = ref false
152 let ksampler = ref true
155 let sigway = ref true
158 let scalebar = ref false
160 let debug = ref false
162 let uptime = ref false
164 let labels = ref true
165 let mgrid = ref false
168 let l = String.length s
in
173 let d = String.make n ' '
in
174 StringLabels.blit ~src
:s ~dst
:d
179 let sooo b
= if b
then "on" else "off"
180 let dA tos s
{contents
=v} = s ^
" (" ^ tos
v ^
")"
181 let dF = dA |< sprintf
"%4.2f"
184 let dI = dA string_of_int
185 let dS = dA (fun s
-> "`" ^
String.escaped s ^
"'")
188 "-" ^ opt
, Arg.Set_float r
, pad 9 "<float> " ^ doc
|> dF |< r
191 "-" ^ opt
, Arg.Set_int r
, pad 9 "<int> " ^ doc
|> dI |< r
194 "-" ^ opt
, Arg.Set r
, pad 9 "" ^ doc
|> dB |< r
197 "-" ^ opt
, Arg.Set_string r
, pad 9 "<string> " ^ doc
|> dS |< r
202 "-" ^ opt
, Arg.Clear r
, pad 9 "" ^ doc
|> dB |< r
204 "-" ^ opt
, Arg.Set r
, pad 9 "" ^ doc
|> dcB |< r
208 [ sF "f" freq "sampling frequency in seconds"
209 ; sF "D" delay "refresh delay in seconds"
210 ; sF "i" interval "history interval in seconds"
211 ; sI "p" pgrid "percent grid items"
212 ; sI "s" sgrid "history grid items"
215 ; sI "b" barw "bar width"
216 ; sI "B" bars "number of CPU bars"
217 ; sI "n" niceval "value to renice self on init"
218 ; sI "t" timer "timer frequency in herz"
219 ; sS "d" devpath "path to itc device"
220 ; fB "k" ksampler |< "kernel sampler"
221 ^
(if NP.winnt then "" else " (`/proc/[stat|uptime]')")
222 ; fB "g" gzh "gzh way (does not quite work yet)"
224 "`uptime' instead of `stat' as kernel sampler (UP only)"
225 ; sB "v" verbose "verbose"
226 ; fB "S" sigway "sigwait delay method"
227 ; fB "c" scalebar "constant bar width"
228 ; fB "P" poly "filled area instead of lines"
229 ; fB "I" icon "icon (hack)"
230 ; fB "l" labels "labels"
231 ; fB "m" mgrid "moving grid"
238 let nixopts = ["-n"; "-u"; "-d"; "-I"; "-S"; "-g"] in
239 prerr_endline
"Only kernel sampler is available on Windows";
240 List.filter
(fun (s
, _
, _
) -> List.mem s
nixopts |> not
) opts
247 "don't know what to do with " ^ s
|> prerr_endline
;
258 let rec furious_cycle i =
259 if not
!stop && i > 0
260 then pred
i |> furious_cycle
261 else (i, Unix.gettimeofday
())
265 let it = { Unix.it_interval
= t; it_value
= t } in
273 let sign = Sys.sigalrm
in
274 let oldh = Sys.signal
sign |< Sys.Signal_handle
handler in
275 let oldi = Unix.setitimer
Unix.ITIMER_REAL
it in
276 let oldbp = Unix.sigprocmask
Unix.SIG_BLOCK
[sign] in
277 let () = NP.waitalrm
() in
278 let () = stop := false in
279 let oldup = Unix.sigprocmask
Unix.SIG_UNBLOCK
[sign] in
280 let t1 = Unix.gettimeofday
() in
281 let n, t2
= furious_cycle max_int
in
282 let () = refdt := t2
-. t1 in
283 let () = lim := tries * (max_int
- n) in
284 let () = if verbose then
286 printf
"Completed %d iterations in %f seconds@." !lim !refdt
288 let _ = Unix.sigprocmask
Unix.SIG_UNBLOCK
oldup in
289 let _ = Unix.setitimer
Unix.ITIMER_REAL
oldi in
290 let _ = Unix.sigprocmask
Unix.SIG_BLOCK
oldbp in
291 let _ = Sys.signal
sign oldh in
301 let _, t2
= furious_cycle !lim in
304 if !Args.debug && !l > 10
307 printf
"Completed %d iterations in %f seconds load %f@."
308 !lim dt |< !refdt /. dt;
315 Unix.gettimeofday
() |> loop
317 let _ = Thread.create
thf () in
323 let prev = ref 0.0 in
326 let b = Unix.gettimeofday
() in
334 module Delay
= struct
335 let sighandler signr
= ()
337 let winfreq = ref 0.0
342 winfreq := 1.0 /. float freq
345 Sys.Signal_handle
sighandler |> Sys.set_signal
Sys.sigalrm
;
348 let l = if gzh then [Sys.sigprof
; Sys.sigvtalrm
] else [] in
349 Unix.sigprocmask
Unix.SIG_BLOCK
|< Sys.sigalrm
:: l |> ignore
;
352 let v = 1.0 /. float freq in
353 let t = { Unix.it_interval
= v; it_value
= v } in
354 let _ = Unix.setitimer
Unix.ITIMER_REAL
t in
368 try let _ = Unix.select
[] [] [] ~
-.1.0 in ()
369 with Unix.Unix_error
(Unix.EINTR
, _, _) -> ()
376 getyielder
: unit -> unit -> float option;
377 update
: float -> float -> float -> float -> unit;
380 module Sampler
(T
: sig val nsamples
: int val freq : float end) =
382 let nsamples = T.nsamples + 1
383 let samples = Array.create
nsamples 0.0
389 let n = min
nsamples n in
394 let i = if i = nsamples then 0 else i in
395 Array.set
samples i v;
396 loop (succ
i) (pred j
)
398 let () = loop !head n in
399 let () = head := (!head + n) mod nsamples in
400 let () = active := min
(!active + n) nsamples in
406 let d = !head - !active in
411 let ry = ref (fun () -> assert false) in
417 ry := succ
i |> yield;
418 Some
((i + tail) mod nsamples |> Array.get
samples)
425 let update t1 t2 i1 i2
=
428 let isamples = d /. T.freq |> truncate
in
429 let l = 1.0 -. (i /. d) in
434 module type ViewSampler
=
436 val getyielder : unit -> unit -> float option
437 val update : float -> float -> float -> float -> unit
450 val samplers
: sampler list
453 module View
(V
: sig val w : int val h : int end) = struct
458 let keyboard ~key ~x ~y
=
459 if key
= 27 || key
= Char.code 'q'
463 funcs := dri
:: !funcs
466 GlClear.clear
[`color
];
467 List.iter
(fun (display, _, _) -> display ()) !funcs;
473 List.iter
(fun (_, reshape, _) -> reshape w h) !funcs;
474 GlClear.clear
[`color
];
475 GlMat.mode `modelview
;
476 GlMat.load_identity
();
477 GlMat.mode `projection
;
478 GlMat.load_identity
();
479 GlMat.rotate ~y
:1.0 ~angle
:180.0 ();
480 GlMat.translate ~x
:~
-.1.0 ~y
:~
-.1.0 ();
481 GlMat.scale ~x
:2.0 ~y
:2.0 ();
482 Glut.postRedisplay
()
486 Glut.initDisplayMode ~double_buffer
:true ();
487 Glut.initWindowSize
V.w V.h
489 let winid = Glut.createWindow
"APC" in
490 Glut.displayFunc
display;
491 Glut.reshapeFunc
reshape;
492 Glut.keyboardFunc
keyboard;
493 GlDraw.color
(1.0, 1.0, 0.0);
497 let inc () = List.iter
(fun (_, _, inc) -> inc ()) !funcs
498 let update = Glut.postRedisplay
499 let func = Glut.idleFunc
500 let run = Glut.mainLoop
503 module Bar
(T
: sig val barw : int val bars : int end) = struct
509 let sw = float T.barw /. float !Args.w
512 let fw = 3 * Glut.bitmapWidth
font (Char.code 'W'
)
514 let base = GlList.gen_lists ~len
:2 in
515 GlList.nth
base ~
pos:0,
516 GlList.nth
base ~
pos:1
523 let xl, xr
= getlr ki
in
525 let h = !vh - 15 - y in
526 let () = GlDraw.viewport
m y !bw h in
529 GlMat.load_identity
();
530 GlMat.rotate ~
y:1.0 ~angle
:180.0 ();
531 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
532 GlMat.scale ~x
:2.0 ~
y:(2.0 /. float h) ()
535 let mspace = barm * nbars in
536 let barh = (h + 66 - mspace / 2) / nbars |> float in
537 let barm = float barm in
542 let yt = yb
+. barm in
543 let yn = yt +. barh in
544 GlDraw.vertex2
(xl, yb
);
545 GlDraw.vertex2
(xl, yt);
546 GlDraw.vertex2
(xr
, yt);
547 GlDraw.vertex2
(xr
, yb
);
550 GlDraw.color
(0.0, 0.0, 0.0);
551 GlDraw.begins `quads
;
563 (float w *. sw |> truncate
) - m
568 GlList.begins
ksepsl `compile
;
572 GlList.begins isepsl `compile
;
577 let drawseps = function
578 | `k
-> GlList.call
ksepsl
579 | `
i -> GlList.call isepsl
583 let kload = min
!kload 1.0 |> max
0.0 in
584 let iload = min
!iload 1.0 |> max
0.0 in
585 let () = GlDraw.viewport
m 0 !bw 15 in
587 GlDraw.color
(1.0, 1.0, 1.0);
588 let kload = 100.0 *. kload in
589 let iload = 100.0 *. iload in
592 GlMat.load_identity
();
593 GlMat.scale ~x
:(1.0/.float !bw) ~
y:(1.0/.30.0) ()
595 let ix = !bw / 2 - fw |> float in
596 let kx = - (fw + !bw / 2) |> float in
597 let () = sprintf
"%5.2f" iload |> draw_string ix 0.0 in
598 let () = sprintf
"%5.2f" kload |> draw_string kx 0.0 in
599 let () = GlMat.pop
() in ()
603 let h = !vh - 15 - y in
604 let () = GlDraw.viewport
m y !bw h in
607 GlMat.load_identity
();
608 GlMat.rotate ~
y:1.0 ~angle
:180.0 ();
609 GlMat.translate ~x
:~
-.1.0 ~
y:~
-.1.0 ();
610 GlMat.scale ~x
:2.0 ~
y:(2.0 /. float h) ()
612 let drawbar load ki
=
613 let xl, xr
= getlr ki
in
615 GlDraw.begins `quads
;
616 GlDraw.vertex2
(xl, yb
);
617 GlDraw.vertex2
(xl, yt);
618 GlDraw.vertex2
(xr
, yt);
619 GlDraw.vertex2
(xr
, yb
);
622 let yt = float h *. load
in
624 let () = drawquad yb yt in
625 let () = GlDraw.color
(0.5, 0.5, 0.5) in
628 let () = drawquad yb yt in
631 GlDraw.color
(1.0, 1.0, 0.0);
633 GlDraw.color
(1.0, 0.0, 0.0);
638 let update kload'
iload'
=
639 kload := kload'
/. float NP.nprocs;
640 iload := iload'
/. float NP.nprocs;
644 module Graph
(V
: View
) = struct
645 let ox = if !Args.scalebar then 0 else !Args.barw
646 let sw = float V.w /. float (!Args.w - ox)
647 let sh = float V.h /. float !Args.h
648 let sx = float (V.x
- ox) /. float V.w
649 let sy = float V.y /. float V.h
654 let scale = V.freq /. V.interval
655 let gscale = 1.0 /. float V.sgrid
660 then 3 * Glut.bitmapWidth
font (Char.code '
%'
), 20
664 let base = GlList.gen_lists ~len
:1 in
665 GlList.nth
base ~
pos:0
668 let ox = if !Args.scalebar then 0 else !Args.barw in
671 | `
labels -> (!vx + ox, !vy + 5, fw, !vh - fh
)
672 | `graph
-> (!vx + fw + 5 + ox, !vy + 5, !vw - fw - 10, !vh - fh
)
674 GlDraw.viewport x y w h;
680 let x = if i = 0 then 0.0009 else float i *. gscale in
681 GlDraw.vertex ~
x ~
y:0.0 ();
682 GlDraw.vertex ~
x ~
y:1.0 ();
688 GlDraw.line_width
1.0;
689 GlDraw.color
(0.0, 1.0, 0.0);
690 GlDraw.begins `lines
;
694 GlDraw.vertex2
(0.0009, 0.0);
695 GlDraw.vertex2
(0.0009, 1.0);
696 GlDraw.vertex2
(1.0000, 0.0);
697 GlDraw.vertex2
(1.0000, 1.0);
703 let lim = 100 / V.pgrid in
706 let y = (i * V.pgrid |> float) /. 100.0 in
707 let y = if i = lim then y -. 0.0009 else y in
708 GlDraw.vertex ~
x:0.0 ~
y ();
709 GlDraw.vertex ~
x:1.0 ~
y ();
712 let () = GlDraw.ends
() in
717 GlDraw.color
(1.0, 1.0, 1.0);
719 for i = 0 to 100 / V.pgrid
721 let p = i * V.pgrid in
722 let y = float p /. ohp in
723 let s = Printf.sprintf
"%3d%%" p in
730 let wxsw = float (w - ox) *. sw
731 and hxsh
= float h *. sh in
732 vw := wxsw |> truncate
;
733 vh := hxsh
|> truncate
;
734 vx := wxsw *. sx |> truncate
;
735 vy := hxsh
*. sy |> truncate
;
736 GlList.begins
gridlist `compile
;
742 Glut.swapBuffers
|> oohz !Args.delay;
745 let inc () = incr
nsamples
748 GlDraw.line_width
1.0;
749 GlDraw.color
(0.0, 1.0, 0.0);
750 GlDraw.begins `lines
;
752 ((pred
!nsamples |> float) *. scale /. gscale |> modf
|> fst
) *. gscale
754 for i = 0 to pred
V.sgrid
756 let x = offset +. float i *. gscale in
757 GlDraw.vertex ~
x ~
y:0.0 ();
758 GlDraw.vertex ~
x ~
y:1.0 ();
764 GlList.call
gridlist;
766 if !Args.mgrid then mgrid ();
767 GlDraw.line_width
1.5;
770 GlDraw.color sampler
.color
;
773 then GlDraw.begins `line_strip
776 GlDraw.begins `polygon
;
777 GlDraw.vertex2
(0.0, 0.0);
780 let yield = sampler
.getyielder () in
781 let rec loop last
i =
784 let x = float i *. scale in
785 GlDraw.vertex ~
x ~
y ();
793 let x = float (pred
i) *. scale in
794 GlDraw.vertex ~
x ~
y:0.0 ()
799 List.iter
sample V.samplers
;
802 let funcs = display, reshape, inc
805 let getplacements w h n barw =
806 let sr = float n |> sqrt
|> ceil
|> truncate
in
808 let r = if n mod sr = 0 then 0 else 1 in
820 let rec loop accu
i =
826 let xc = xc * vw + barw in
828 (i, xc, yc) :: accu
|> loop |< succ
i
835 let freq = !Args.freq
836 let nsamples = !Args.interval /. freq |> ceil
|> truncate
839 let placements, vw, vh = getplacements w h NP.nprocs !Args.barw in
841 let iget () = NP.idletimeofday fd
NP.nprocs in
845 let gks = NP.parse_stat () in
846 gks () |> Array.of_list
850 let crgraph (kaccu
, iaccu
, gaccu
) (i, x, y) =
851 let module Si
= Sampler
(S
) in
853 { getyielder = Si.getyielder
854 ; color
= (1.0, 1.0, 0.0)
858 let (kcalc
, ksampler) =
859 let module Sc
= Sampler
(S
) in
861 { getyielder = Sc.getyielder
862 ; color
= (1.0, 0.0, 0.0)
870 let f d'
= d := d'
in
871 let () = Gzh.gen f in
872 fun _ _ _ -> (0.0, !d)
876 let (u1
, i1
) = NP.parse_uptime () in
880 let (u2
, i2
) = NP.parse_uptime () in
882 and di
= i2
-. !i1
in
887 let i'
= if i = NP.nprocs then 0 else succ
i in
889 let g ks = Array.get
ks i'
|> snd
|> Array.get
|< n in
890 let i1 = g ks |> ref in
907 let interval = !Args.interval
908 let pgrid = !Args.pgrid
909 let sgrid = !Args.sgrid
912 then [isampler; ksampler]
916 let module Graph
= Graph
(V
) in
918 let i1 = Array.get
is i |> ref in
920 let i2 = Array.get
is i in
921 if classify_float
i2 = FP_infinite
931 then (i, kcalc
, ksampler) :: kaccu
934 kaccu, (i, icalc, isampler) :: iaccu
, Graph.funcs :: gaccu
936 let kl, il
, gl
= List.fold_left
crgraph ([], [], []) placements in
937 ((if kl == [] then (fun () -> [||]) else kget), kl), (iget, il
), gl
945 Unix.openfile path
[Unix.O_RDONLY
] 0
947 | Unix.Unix_error
(Unix.ENODEV
, s1
, s2
) ->
948 eprintf
"Could not open ITC device %S:\n%s(%s): %s)\n"
949 path s1 s2
|< Unix.error_message
Unix.ENODEV
;
950 eprintf
"(perhaps the module is not loaded?)@.";
953 | Unix.Unix_error
(Unix.ENOENT
, s1
, s2
) ->
954 eprintf
"Could not open ITC device %S:\n%s(%s): %s\n"
955 path s1 s2
|< Unix.error_message
Unix.ENOENT
;
959 eprintf
"Could not open ITC device %S:\n%s\n"
960 path
|< Printexc.to_string exn
;
964 let module X
= struct external seticon : string -> unit = "ml_seticon" end in
966 let data = String.create |< 32*len + 2*4 in
971 and a = Char.chr
a in
972 let s = String.create len in
978 x + 0 |> String.set
s |< b;
979 x + 1 |> String.set
s |< g;
980 x + 2 |> String.set
s |< r;
981 x + 3 |> String.set
s |< a;
987 let el = line 0x00 0x00 0x00 0xff
988 and kl = line 0xff 0x00 0x00 0xff
989 and il
= line 0xff 0xff 0x00 0xff in
991 let src = l and dst
= data and src_pos
= 0 in
992 let rec loop n dst_pos
=
996 StringLabels.blit ~
src ~src_pos ~dst ~dst_pos ~
len;
997 pred
n |> loop |< dst_pos
+ len
1000 (ey
- sy) |> loop |< (32 - ey
) * len + 4*2
1002 fun ~
iload ~
kload ->
1003 let iy = iload *. 32.0 |> ceil
|> truncate
|> max
0 |> min
32
1004 and ky
= kload *. 32.0 |> ceil
|> truncate
|> max
0 |> min
32 in
1007 then (fill kl 0 ky
; fill il ky
iy; iy)
1008 else (fill kl 0 ky
; ky
)
1015 let _ = Glut.init [|""|] in
1016 let () = Args.init () in
1020 "detected " ^ string_of_int
NP.nprocs ^
" CPUs" |> print_endline
1022 let () = if !Args.gzh then Gzh.init !Args.verbose in
1023 let () = Delay.init !Args.timer !Args.gzh in
1024 let () = if !Args.niceval != 0 then NP.setnice
!Args.niceval in
1027 let fd = opendev !Args.devpath in
1028 let module FullV
= View
(struct let w = w let h = h end) in
1029 let _winid = FullV.init () in
1030 let (kget, kfuncs
), (iget, ifuncs
), gl
= create fd w h in
1032 List.iter
FullV.add gl
;
1036 Bar
(struct let barw = !Args.barw let bars = !Args.bars end)
1038 FullV.add (Bar.display, Bar.reshape, fun _ -> ());
1043 let seticon = if !Args.icon then seticon () else fun ~
iload ~
kload -> () in
1044 let rec loop t1 () =
1045 let t2 = Unix.gettimeofday
() in
1046 let dt = t2 -. t1 in
1051 let rec loop2 load
s = function
1053 | (nr
, calc, sampler) :: rest
->
1054 let i1, i2 = calc s t1 t2 in
1055 let thisload = 1.0 -. ((i2 -. i1) /. dt) in
1056 let thisload = max
0.0 thisload in
1060 ("cpu load(" ^ string_of_int nr ^
"): "
1061 ^
(thisload *. 100.0 |> string_of_float
)
1064 let load = load +. thisload in
1065 sampler.update t1 t2 i1 i2;
1068 let iload = loop2 0.0 is ifuncs
in
1069 let kload = loop2 0.0 ks kfuncs
in
1073 iload |> string_of_float
|> prerr_endline
;
1074 kload |> string_of_float
|> prerr_endline
;
1077 seticon ~
iload ~
kload;
1078 bar_update kload iload;
1081 FullV.func (Some
(loop t2))
1085 FullV.func (Some
(Unix.gettimeofday
() |> loop));
1091 | Unix.Unix_error
(e
, s1
, s2
) ->
1092 eprintf
"%s(%s): %s@." s1 s2
|< Unix.error_message e
1095 Printexc.to_string exn
|> eprintf
"Exception: %s@."