[virt-tools-list] [PATCH 4] show `vcpu usages' by `virt-top -1'

Hu Tao hutao at cn.fujitsu.com
Wed Apr 18 11:14:58 UTC 2012


Before this patch, `virt-top -1' shows total cpu usages
which euqal to `vcpu usages' + `hypervisor usages'. This
patch adds another column for domains showing `vcpu
usages'. An example is:

PHYCPU %CPU example_domain
   0   10.4 10.4  0.8
   1    1.6  1.6  1.4
   2    2.6  2.6  2.6
   3    0.0  0.0  0.1
---
 virt-top/virt_top.ml |   62 +++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 46 insertions(+), 16 deletions(-)

diff --git a/virt-top/virt_top.ml b/virt-top/virt_top.ml
index e2fe554..4591208 100644
--- a/virt-top/virt_top.ml
+++ b/virt-top/virt_top.ml
@@ -448,6 +448,7 @@ let collect, clear_pcpu_display_data =
 
   (* Save pcpu_usages structures across redraws too (only for pCPU display). *)
   let last_pcpu_usages = Hashtbl.create 13 in
+  let last_vcpu_usages = Hashtbl.create 13 in
 
   let clear_pcpu_display_data () =
     (* Clear out pcpu_usages used by PCPUDisplay display_mode
@@ -652,7 +653,7 @@ let collect, clear_pcpu_display_data =
 	      (try
 		 let domid = rd.rd_domid in
 		 let maplen = C.cpumaplen nr_pcpus in
-		 let cpu_stats = D.get_cpu_stats rd.rd_dom nr_pcpus in
+		 let cpu_stats = D.get_cpu_stats rd.rd_dom nr_pcpus 0 in
 		 let rec find_usages_from_stats = function
 		   | ("cpu_time", D.TypedFieldUInt64 usages) :: _ -> usages
 		   | _ :: params -> find_usages_from_stats params
@@ -669,11 +670,20 @@ let collect, clear_pcpu_display_data =
 		 (* Update last_pcpu_usages. *)
 		 Hashtbl.replace last_pcpu_usages domid pcpu_usages;
 
-		 (match prev_pcpu_usages with
-		  | Some prev_pcpu_usages
+                 (* vcpu usages *)
+                 let vcpu_stats = D.get_cpu_stats rd.rd_dom nr_pcpus 8 in
+		 let vcpu_usages = Array.map find_usages_from_stats vcpu_stats in
+                 let prev_vcpu_usages =
+                   try Some (Hashtbl.find last_vcpu_usages domid)
+                   with Not_found -> None in
+		 Hashtbl.replace last_vcpu_usages domid vcpu_usages;
+
+		 (match prev_pcpu_usages, prev_vcpu_usages with
+		  | Some prev_pcpu_usages, Some prev_vcpu_usages
 		      when Array.length prev_pcpu_usages = Array.length pcpu_usages ->
-		      Some (domid, name, nr_vcpus, vcpu_infos, pcpu_usages,
-			    prev_pcpu_usages, cpumaps, maplen)
+                      Some (domid, name, nr_vcpus, vcpu_infos, pcpu_usages,
+                      prev_pcpu_usages, vcpu_usages, prev_vcpu_usages,
+                      cpumaps, maplen)
 		  | _ -> None (* ignore missing / unequal length prev_vcpu_infos *)
 		 );
 	       with
@@ -691,13 +701,24 @@ let collect, clear_pcpu_display_data =
 
 	List.iteri (
 	  fun di (domid, name, nr_vcpus, vcpu_infos, pcpu_usages,
-		  prev_pcpu_usages, cpumaps, maplen) ->
+		  prev_pcpu_usages, vcpu_usages, prev_vcpu_usages,
+                  cpumaps, maplen) ->
 	    (* Which pCPUs can this dom run on? *)
 	    for p = 0 to Array.length pcpu_usages - 1 do
 	      pcpus.(p).(di) <- pcpu_usages.(p) -^ prev_pcpu_usages.(p)
-	    done
+            done
 	) doms;
 
+        let vcpus = Array.make_matrix nr_pcpus nr_doms 0L in
+        List.iteri (
+	  fun di (domid, name, nr_vcpus, vcpu_infos, pcpu_usages,
+		  prev_pcpu_usages, vcpu_usages, prev_vcpu_usages,
+                  cpumaps, maplen) ->
+	    for p = 0 to Array.length vcpu_usages - 1 do
+	      vcpus.(p).(di) <- vcpu_usages.(p) -^ prev_vcpu_usages.(p)
+            done
+        ) doms;
+
 	(* Sum the CPU time used by each pCPU, for the %CPU column. *)
 	let pcpus_cpu_time = Array.map (
 	  fun row ->
@@ -709,7 +730,7 @@ let collect, clear_pcpu_display_data =
 	    Int64.to_float !cpu_time
 	) pcpus in
 
-	Some (doms, pcpus, pcpus_cpu_time)
+	Some (doms, pcpus, vcpus, pcpus_cpu_time)
       ) else
 	None in
 
@@ -913,7 +934,7 @@ let redraw =
 	 loop domains_lineno doms
 
      | PCPUDisplay -> (*---------- Showing physical CPUs ----------*)
-	 let doms, pcpus, pcpus_cpu_time =
+	 let doms, pcpus, vcpus, pcpus_cpu_time =
 	   match pcpu_display with
 	   | Some p -> p
 	   | None -> failwith "internal error: no pcpu_display data" in
@@ -922,9 +943,9 @@ let redraw =
 	 let dom_names =
 	   String.concat "" (
 	     List.map (
-	       fun (_, name, _, _, _, _, _, _) ->
+	       fun (_, name, _, _, _, _, _, _, _, _) ->
 		 let len = String.length name in
-		 let width = max (len+1) 7 in
+		 let width = max (len+1) 12 in
 		 pad width name
 	     ) doms
 	   ) in
@@ -941,18 +962,27 @@ let redraw =
 	     addch ' ';
 
 	     List.iteri (
-	       fun di (domid, name, _, _, _, _, _, _) ->
+	       fun di (domid, name, _, _, _, _, _, _, _, _) ->
 		 let t = pcpus.(p).(di) in
+                 let tv = vcpus.(p).(di) in
 		 let len = String.length name in
-		 let width = max (len+1) 7 in
-		 let str =
+		 let width = max (len+1) 12 in
+		 let str_pcpu =
 		   if t <= 0L then ""
 		   else (
 		     let t = Int64.to_float t in
 		     let percent = 100. *. t /. total_cpu_per_pcpu in
-		     sprintf "%s " (Show.percent percent)
+		     sprintf "%s" (Show.percent percent)
 		   ) in
-		 addstr (pad width str);
+                 let str_vcpu =
+                    if tv <= 0L then ""
+                    else (
+                      let tv = Int64.to_float tv in
+                      let percent = 100. *. tv /. total_cpu_per_pcpu in
+                      sprintf "%s" (Show.percent percent)
+                    ) in
+                 let str = sprintf "%s %s" str_pcpu str_vcpu in
+                 addstr (pad width str);
 		 ()
 	     ) doms
 	 ) pcpus;
-- 
1.7.1




More information about the virt-tools-list mailing list