1#!/sw/bin/gawk -f 2 3# Usage: get_system.awk [--force] 4# 5# Copyright 2002 Stephan Schulz, schulz@informatik.tu-muenchen.de 6# 7# Determine a string describing the processor type and speed. Option 8# --force will force only the first recognized processor to be 9# considered. 10# 11 12 13# Similar to C assert() 14 15function assert(val, comment) 16{ 17 if(!val) 18 { 19 print "Assertion fail: " comment > "/dev/stderr"; 20 exit 1; 21 } 22} 23 24# round --- do normal rounding 25# 26# Arnold Robbins, arnold@gnu.org, August, 1996 27# Public Domain 28 29function round(x, ival, aval, fraction) 30{ 31 ival = int(x) # integer part, int() truncates 32 33 # see if fractional part 34 if (ival == x) # no fraction 35 return x 36 37 if (x < 0) { 38 aval = -x # absolute value 39 ival = int(aval) 40 fraction = aval - ival 41 if (fraction >= .5) 42 return int(x) - 1 # -2.5 --> -3 43 else 44 return int(x) # -2.3 --> -2 45 } else { 46 fraction = x - ival 47 if (fraction >= .5) 48 return ival + 1 49 else 50 return ival 51 } 52} 53 54 55# Return the result of a single, simple shell command yieding exactly 56# one line 57 58function get_shell_res(cmd, tmp) 59{ 60 cmd | getline tmp; 61 close(cmd); 62 63 assert(tmp, "No result found (get_shell_res)"); 64 return tmp; 65} 66 67 68# Return an OS string sufficient to tell us how to proceed. 69 70function get_OS( tmp, res, arr, elements) 71{ 72 73 res = ""; 74 75 tmp = get_shell_res("uname -a"); 76 elements = split(tmp, arr); 77 if(!elements) 78 { 79 print "Cannot get operation system info!" > "/dev/stderr"; 80 exit 1; 81 } 82 83 if((arr[1] == "SunOS")&& (index(arr[3], "5.") == 1)) 84 { 85 res = "SunOS-5"; 86 } 87 else if(arr[1] == "Linux") 88 { 89 res = "Linux"; 90 } 91 else if(arr[1] == "Darwin") 92 { 93 res = "Darwin"; 94 } 95 else 96 { 97 print "Warning: Unknown operating system!" > "/dev/stderr"; 98 res = "unknown"; 99 } 100 return res; 101} 102 103function get_platform_info_SunOS5( pipe, tmp, res, tmpres, i, arr, elements) 104{ 105 tmp = ""; 106 res = ""; 107 pipe = "psrinfo -v"; 108 while((pipe | getline tmp )>0) 109 { 110 if(i = index(tmp, "processor operates at")) 111 { 112 elements = split(tmp, arr); 113 if(elements!=7) 114 { 115 print "Cannot parse processor information" > "/dev/stderr"; 116 exit 1; 117 } 118 tmpres = arr[2] "-" arr[6]; 119 if(res && (res!=tmpres)) 120 { 121 if(first_proc_only) 122 { 123 print "Two different processors found! Ignoring second"\ 124 " one (it's your responsibility now!)" >\ 125 "/dev/stderr"; 126 tmpres = "NC-" res; 127 } 128 else 129 { 130 print "Two different processors found! Machine"\ 131 " is unsuitable for E-MARK evaluation" > "/dev/stderr"; 132 exit 1; 133 } 134 exit 1; 135 } 136 else if(res) 137 { 138 print "Two or more identical processors found!" > "/dev/stderr"; 139 } 140 res = tmpres; 141 } 142 } 143 close(pipe); 144 tmp = get_shell_res("uname -i"); 145 gsub(",","-",tmp); 146 147 return tmp "-" res; 148} 149 150 151function get_platform_info_Linux( pipe, tmp, res, tmpres, i, arr, elements) 152{ 153 pipe = "cat /proc/cpuinfo"; 154 res = ""; 155 tmpname = ""; 156 tmpmhz = ""; 157 while((pipe | getline tmp )>0) 158 { 159 if(i = index(tmp, "model name")==1) 160 { 161 elements = split(tmp, arr, ": "); 162 if(elements!=2) 163 { 164 print "Cannot parse processor information" > "/dev/stderr"; 165 exit 1; 166 } 167 tmpname = arr[2]; 168 } 169 if(i = index(tmp, "cpu MHz")==1) 170 { 171 elements = split(tmp, arr, ": "); 172 if(elements!=2) 173 { 174 print "Cannot parse processor information" > "/dev/stderr"; 175 exit 1; 176 } 177 tmpmhz = round(arr[2]); # PCs have NO reliable frequency! 178 } 179 if(i = index(tmp, "bogomips")==1) 180 { 181 tmpres = tmpname "-" tmpmhz; 182 if(res && (res!=tmpres)) 183 { 184 if(first_proc_only) 185 { 186 print "Two different processors found! Ignoring second"\ 187 " one (it's your responsibility now!)" >\ 188 "/dev/stderr"; 189 tmpres = "NC-" res; 190 } 191 else 192 { 193 print "Two different processors found! Machine"\ 194 " is unsuitable for E-MARK evaluation" > "/dev/stderr"; 195 exit 1; 196 } 197 } 198 else if(res) 199 { 200 print "Two or more identical processors found!" > "/dev/stderr"; 201 } 202 res = tmpres; 203 tmpres = ""; 204 tmpname = ""; 205 tmpmhz = ""; 206 } 207 } 208 close(pipe); 209 gsub(" ","-",res); 210 gsub("\\(","",res); 211 gsub("\\)","",res); 212 213 return res; 214} 215 216function get_platform_info_Darwin( pipe, tmp, i, arr, elements, 217 tmpname, freq) 218{ 219 pipe = "hostinfo"; 220 221 while((pipe | getline tmp )>0) 222 { 223 if(i = index(tmp, "Processor type")==1) 224 { 225 elements = split(tmp, arr, ": "); 226 if(elements!=2) 227 { 228 print "Cannot parse processor information" > "/dev/stderr"; 229 exit 1; 230 } 231 tmp = arr[2]; 232 split(tmp, arr, " "); 233 tmpname = arr[1]; 234 } 235 } 236 close(pipe); 237 tmp = get_shell_res("sysctl -n hw.cpufrequency"); 238 freq = tmp/1000000 239 return tmpname "-" freq; 240} 241 242 243function get_procinfo( os, res) 244{ 245 os = get_OS(); 246 247 if(os == "SunOS-5") 248 { 249 res = get_platform_info_SunOS5(); 250 } 251 else if(os == "Linux") 252 { 253 res = get_platform_info_Linux(); 254 } 255 else if(os == "Darwin") 256 { 257 res = get_platform_info_Darwin() 258 } 259 else 260 { 261 assert((os==unknown), "OS identification failed (get_procinfo())"); 262 print "Warning: Unknown OS. Trying...."; 263 264 res = get_procinfo_SunOS5(); 265 if(!res) 266 { 267 res = get_platform_info_Linux(); 268 } 269 if(!res) 270 { 271 print "Cannot get system information" > "/dev/stderr"; 272 exit 1; 273 } 274 } 275 return res; 276} 277 278 279BEGIN{ 280 if(ARGV[1]=="--force") 281 { 282 first_proc_only = 1; 283 } 284 print get_procinfo(); 285} 286 287 288 289 290