Attachment 'cpuload.pl'

Download

   1 #!/usr/bin/perl
   2 #         USAGE:  ./cpuload.pl <file> [<type] [<nb cpu>]
   3 # 
   4 #   DESCRIPTION:  
   5 #		parse a trace file and compute the time spent in idle for each CPU
   6 #		the tool can parse
   7 #		-trace-cmd raw data (trace-cmd must be installed)
   8 #		-ftrace text output with nop and sched_switch tracer. For sched_switch
   9 #				tracer you have to set the number of CPUs
  10 #		-idlestat text trace
  11 # 
  12 #        AUTHOR: Vincent Guittot (VGU), [email protected]
  13 #       COMPANY: Linaro
  14 #===============================================================================
  15 use Switch;
  16 
  17 if ($ARGV[0] eq "-h") {
  18 	print "usage:\n";
  19 	print "    cpuload.pl <file> [<type>] [<#CPUs>]\n";
  20     print "    <file> to parse\n";
  21 	print "    <type> of file : idle for idlestat file\n";
  22 	print "                     nop for nop tracer text file\n";
  23 	print "                     sched for old sched_switch tracer text file\n";
  24 	print "                     cmd for raw trace-cmd data (default)\n";
  25 	print "    <#CPUS> number of CPUs for sched_switch file\n";
  26 	print "    cpuload parses sched_switch and (soft)irq events to compute the load\n";
  27 	print "version 1.0\n";
  28 
  29 	exit;
  30 }
  31 
  32 my $FILE = $ARGV[0] || 'dft';
  33 my $TYPE = $ARGV[1] || 'cmd';
  34 
  35 unless (-e $FILE) {
  36 	print "$FILE does not exist.\n";
  37 	exit;
  38 }
  39 
  40 my $start_idle = 0;
  41 my $end_idle = 0;
  42 my $idle = 0;
  43 
  44 sub enter_idle
  45 {
  46 	$start_idle = $date;
  47 }
  48 
  49 sub exit_idle()
  50 {
  51 	$end_idle = $date;
  52 	$idle = $end_idle - $start_idle + $idle;
  53 	$start_idle = $end_idle
  54 }
  55 
  56 sub parse_one_cpu
  57 {
  58 	my $TMP = $_[0];
  59 	my $CPU = $_[1];
  60 
  61 	my $backward = 0;
  62 
  63 	$start_idle = 0;
  64 	$end_idle = 0;
  65 	$idle = 0;
  66 
  67 	my $line = `head -n 1 $TMP`;
  68 	my @arr = split(/ /, $line);
  69 	my $beg = $arr[1];
  70 	$start_idle = $beg;
  71 	$date = $beg;
  72 
  73 	my $line = `tail -n 1 $TMP`;
  74 	my @arr = split(/ /, $line);
  75 	my $end = $arr[1];
  76 
  77 	my $line = `wc -l $TMP`;
  78 	my @arr = split(/ /, $line);
  79 	my $nb_line = $arr[0];
  80 
  81 	my $cur_line = 0;
  82 
  83 	open(FILE, $TMP) or die("Could not open file.");
  84 
  85 	$count = 0; 
  86 	foreach $line (<FILE>)  {   
  87 
  88 		my @arr = split(/ /, $line);
  89 #		print "$arr[0] : $arr[1]; $arr[2];\n";
  90 
  91 		if ($arr[1] < $date) {
  92 			$backward = 1;
  93 			print "error: backward timestamp detection. $arr[1] after $date\n";
  94 			next ;
  95 		}
  96 		if ($backward) {
  97 			$backward = $arr[1]*1000 - $date*1000;
  98 			printf "error: backward timestamp detection. %.3f ms of uncertainty\n", $backward;
  99 			$backward = 0;
 100 		}
 101 		$date = $arr[1];
 102 		$cmd = $arr[2];
 103 		$arg = $arr[3];
 104 
 105 #		print "$line : $date ; $cmd ; $arg ;\n";
 106 		switch ($cmd) {
 107 			case (/.*_exit/)		{ enter_idle }
 108 			case (/.*_entry/)		{ exit_idle }
 109 			case ("sched_switch")	{ 
 110 				switch ($line) {
 111 					case (/<idle>/)							{ enter_idle }
 112 					case (/.*==> swapper.*/)				{ enter_idle }
 113 					case (/.*==> next_comm=swapper.*/)		{ enter_idle }
 114 					else									{ exit_idle }
 115 				}
 116 			}
 117 			case ("cpu_idle")	{ 
 118 				switch ($line) {
 119 					case (/state=4294967295/)		{ exit_idle }
 120 					else							{ enter_idle }
 121 				}
 122 			}
 123 			case ("==>")	{ 
 124 				switch ($line) {
 125 					case (/<idle>/)	{ enter_idle }
 126 					else			{ exit_idle }
 127 				}
 128 			}
 129 			else					{ printf "previous case not true %s \n", $line }
 130 		}
 131 #		printf "%.6f : %s", $idle,  $line;
 132 
 133 		$cur_line = $cur_line + 1;
 134 		print "CPU$CPU : $cur_line/$nb_line\r";
 135 	}
 136 
 137 	my $total = $end - $beg;
 138 	$total = $total * 1000;
 139 	$idle = $idle * 1000;
 140 
 141 	if ($total != 0) {
 142 		printf "CPU%d : idle %.3f ms / total %.3f ms\n",$CPU, $idle, $total;
 143 		my $load = 100 * (1 - $idle / $total);
 144 		printf "CPU%d : load %.1f\%\n", $CPU, $load ;
 145 	} else {
 146 		print "CPU$CPU : no trace\n";
 147 	}
 148 }
 149 
 150 switch ($TYPE) {
 151 	case ("sched")		{$NR = $ARGV[2] || 1}
 152 	case ("nop")		{$NR = `sed -n '/^#.*#P:/{p;q;}' $FILE | awk '{ print \$4 }' | sed s/^#P://`}
 153 	case ("idle")		{$NR = `sed -n '/^cpus=/{p;q;}' $FILE | sed s/^cpus=//`}
 154 	else				{$NR = `trace-cmd report $FILE 2> /dev/null | sed -n '/^cpus=/{p;q;}' | sed s/^cpus=//`}
 155 }
 156 
 157 my $rand = time;
 158 my $DATA = "cpuload-$rand-all.txt";
 159 
 160 switch ($TYPE) {
 161 		case ("sched")		{ $CMD = "grep -e '<idle>' $FILE | grep -e '==>' | awk  'BEGIN{FIELDWIDTHS=\"23 5 14 13 4 18 80\"}{print \$2, \$3, \$5, \$7}' | sed 's/  */ /g' | sed -e 's/^ //' | sed 's/://g' > $DATA;"}
 162 		case ("nop")		{ $CMD = "grep -e '<idle>-0' -e 'swapper' $FILE | grep -e 'switch' -e 'irq.*_entry' -e 'irq.*_exit' | awk  'BEGIN{FIELDWIDTHS=\"23 5 5 14 122\"}{print \$2, \$4, \$5}' | sed 's/  */ /g' | sed -e 's/^ //' | sed 's/://g' > $DATA;"}
 163 		case ("idle")		{ $CMD = "grep -e '<idle>-0' -e 'swapper' $FILE | grep -e 'cpu_idle' -e 'irq.*_entry' -e 'irq.*_exit' | awk  'BEGIN{FIELDWIDTHS=\"23 5 5 14 122\"}{print \$2, \$4, \$5}' | sed 's/  */ /g' | sed -e 's/^ //' | sed 's/://g' > $DATA;"}
 164 		else				{ $CMD = "trace-cmd report -i $FILE 2> /dev/null | grep -e '<idle>-0' -e 'swapper' | grep -e 'switch' -e 'irq.*_entry' -e 'irq.*_exit' | awk  'BEGIN{FIELDWIDTHS=\"23 5 14 22 100\"}{print \$2, \$3, \$4, \$5}' | sed 's/  */ /g' | sed -e 's/^ //' | sed 's/://g' > $DATA;"}
 165 	}
 166 
 167 #print "$CMD\n";
 168 system($CMD);
 169 
 170 for ($i = 0; $i < $NR; $i = $i + 1) {
 171 	my $TMP = "cpuload-$rand-cpu$i.txt";
 172 	$CMD = "head -n 1 $DATA | awk '{ print \$1, \$2,  \"_exit\"}' > $TMP; grep -e '\\[00$i\\]' $DATA >> $TMP; tail -n 1 $DATA | awk '{ print \$1, \$2,  \"_entry\"}' >> $TMP; ";
 173 	
 174 #	print "$CMD\n";
 175 	system($CMD);
 176 	parse_one_cpu( $TMP, $i);
 177 
 178 	unlink $TMP;
 179 }
 180 
 181 unlink $DATA;

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2011-11-15 15:49:02, 1.6 KB) [[attachment:android.png]]
  • [get | view] (2011-11-15 15:49:08, 0.7 KB) [[attachment:connect.png]]
  • [get | view] (2014-11-21 10:50:34, 5.2 KB) [[attachment:cpuload.pl]]
  • [get | view] (2011-11-15 22:22:49, 1.0 KB) [[attachment:cycles.png]]
  • [get | view] (2011-11-15 15:49:14, 2.8 KB) [[attachment:dev-platform.png]]
  • [get | view] (2011-11-15 15:49:25, 2.0 KB) [[attachment:downloads.png]]
  • [get | view] (2011-11-15 15:49:32, 2.4 KB) [[attachment:getting-started.png]]
  • [get | view] (2011-11-15 15:49:41, 1.8 KB) [[attachment:graphics.png]]
  • [get | view] (2011-11-15 15:49:47, 1.8 KB) [[attachment:how-tos.png]]
  • [get | view] (2011-11-18 16:57:53, 1.1 KB) [[attachment:hw.jpg]]
  • [get | view] (2011-11-15 15:49:53, 1.3 KB) [[attachment:infrastructure.png]]
  • [get | view] (2013-02-07 01:16:02, 2.8 KB) [[attachment:leg.png]]
  • [get | view] (2013-12-19 21:33:46, 1.5 KB) [[attachment:lhg.png]]
  • [get | view] (2013-04-16 16:55:18, 4.1 KB) [[attachment:lng.png]]
  • [get | view] (2013-08-27 17:33:00, 7.4 KB) [[attachment:lng_icon.png]]
  • [get | view] (2014-07-10 11:07:10, 1.3 KB) [[attachment:logo-lmg.png]]
  • [get | view] (2015-08-21 14:34:43, 2.0 KB) [[attachment:lsk.png]]
  • [get | view] (2011-11-15 16:06:42, 2.0 KB) [[attachment:meet-team.png]]
  • [get | view] (2011-11-15 15:50:01, 3.0 KB) [[attachment:multimedia.png]]
  • [get | view] (2011-11-15 15:50:07, 2.4 KB) [[attachment:power-mgmt.png]]
  • [get | view] (2013-12-16 09:11:32, 2.4 KB) [[attachment:security_padlock.png]]
  • [get | view] (2011-11-15 15:50:13, 1.2 KB) [[attachment:source.png]]
  • [get | view] (2011-11-15 15:50:18, 2.4 KB) [[attachment:toolchain.png]]
  • [get | view] (2011-11-15 15:50:28, 2.9 KB) [[attachment:tools.png]]
  • [get | view] (2011-11-15 15:50:23, 1.9 KB) [[attachment:tux.png]]
  • [get | view] (2011-11-15 15:50:36, 0.5 KB) [[attachment:validation.png]]
  • [get | view] (2013-02-21 09:34:12, 3.2 KB) [[attachment:virtualization.png]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.