ktest.pl 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  1. #!/usr/bin/perl -w
  2. #
  3. # Copywrite 2010 - Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
  4. # Licensed under the terms of the GNU GPL License version 2
  5. #
  6. use strict;
  7. use IPC::Open2;
  8. use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
  9. use File::Path qw(mkpath);
  10. use File::Copy qw(cp);
  11. use FileHandle;
  12. my $VERSION = "0.2";
  13. $| = 1;
  14. my %opt;
  15. my %repeat_tests;
  16. my %repeats;
  17. my %default;
  18. #default opts
  19. $default{"NUM_TESTS"} = 1;
  20. $default{"REBOOT_TYPE"} = "grub";
  21. $default{"TEST_TYPE"} = "test";
  22. $default{"BUILD_TYPE"} = "randconfig";
  23. $default{"MAKE_CMD"} = "make";
  24. $default{"TIMEOUT"} = 120;
  25. $default{"TMP_DIR"} = "/tmp/ktest";
  26. $default{"SLEEP_TIME"} = 60; # sleep time between tests
  27. $default{"BUILD_NOCLEAN"} = 0;
  28. $default{"REBOOT_ON_ERROR"} = 0;
  29. $default{"POWEROFF_ON_ERROR"} = 0;
  30. $default{"REBOOT_ON_SUCCESS"} = 1;
  31. $default{"POWEROFF_ON_SUCCESS"} = 0;
  32. $default{"BUILD_OPTIONS"} = "";
  33. $default{"BISECT_SLEEP_TIME"} = 60; # sleep time between bisects
  34. $default{"CLEAR_LOG"} = 0;
  35. $default{"SUCCESS_LINE"} = "login:";
  36. $default{"BOOTED_TIMEOUT"} = 1;
  37. $default{"DIE_ON_FAILURE"} = 1;
  38. $default{"SSH_EXEC"} = "ssh \$SSH_USER\@\$MACHINE \$SSH_COMMAND";
  39. $default{"SCP_TO_TARGET"} = "scp \$SRC_FILE \$SSH_USER\@\$MACHINE:\$DST_FILE";
  40. $default{"REBOOT"} = "ssh \$SSH_USER\@\$MACHINE reboot";
  41. $default{"STOP_AFTER_SUCCESS"} = 10;
  42. $default{"STOP_AFTER_FAILURE"} = 60;
  43. $default{"LOCALVERSION"} = "-test";
  44. my $ktest_config;
  45. my $version;
  46. my $machine;
  47. my $ssh_user;
  48. my $tmpdir;
  49. my $builddir;
  50. my $outputdir;
  51. my $output_config;
  52. my $test_type;
  53. my $build_type;
  54. my $build_options;
  55. my $reboot_type;
  56. my $reboot_script;
  57. my $power_cycle;
  58. my $reboot;
  59. my $reboot_on_error;
  60. my $poweroff_on_error;
  61. my $die_on_failure;
  62. my $powercycle_after_reboot;
  63. my $poweroff_after_halt;
  64. my $ssh_exec;
  65. my $scp_to_target;
  66. my $power_off;
  67. my $grub_menu;
  68. my $grub_number;
  69. my $target;
  70. my $make;
  71. my $post_install;
  72. my $noclean;
  73. my $minconfig;
  74. my $addconfig;
  75. my $in_bisect = 0;
  76. my $bisect_bad = "";
  77. my $reverse_bisect;
  78. my $in_patchcheck = 0;
  79. my $run_test;
  80. my $redirect;
  81. my $buildlog;
  82. my $dmesg;
  83. my $monitor_fp;
  84. my $monitor_pid;
  85. my $monitor_cnt = 0;
  86. my $sleep_time;
  87. my $bisect_sleep_time;
  88. my $store_failures;
  89. my $timeout;
  90. my $booted_timeout;
  91. my $console;
  92. my $success_line;
  93. my $stop_after_success;
  94. my $stop_after_failure;
  95. my $build_target;
  96. my $target_image;
  97. my $localversion;
  98. my $iteration = 0;
  99. my $successes = 0;
  100. my %entered_configs;
  101. my %config_help;
  102. $config_help{"MACHINE"} = << "EOF"
  103. The machine hostname that you will test.
  104. EOF
  105. ;
  106. $config_help{"SSH_USER"} = << "EOF"
  107. The box is expected to have ssh on normal bootup, provide the user
  108. (most likely root, since you need privileged operations)
  109. EOF
  110. ;
  111. $config_help{"BUILD_DIR"} = << "EOF"
  112. The directory that contains the Linux source code (full path).
  113. EOF
  114. ;
  115. $config_help{"OUTPUT_DIR"} = << "EOF"
  116. The directory that the objects will be built (full path).
  117. (can not be same as BUILD_DIR)
  118. EOF
  119. ;
  120. $config_help{"BUILD_TARGET"} = << "EOF"
  121. The location of the compiled file to copy to the target.
  122. (relative to OUTPUT_DIR)
  123. EOF
  124. ;
  125. $config_help{"TARGET_IMAGE"} = << "EOF"
  126. The place to put your image on the test machine.
  127. EOF
  128. ;
  129. $config_help{"POWER_CYCLE"} = << "EOF"
  130. A script or command to reboot the box.
  131. Here is a digital loggers power switch example
  132. POWER_CYCLE = wget --no-proxy -O /dev/null -q --auth-no-challenge 'http://admin:admin\@power/outlet?5=CCL'
  133. Here is an example to reboot a virtual box on the current host
  134. with the name "Guest".
  135. POWER_CYCLE = virsh destroy Guest; sleep 5; virsh start Guest
  136. EOF
  137. ;
  138. $config_help{"CONSOLE"} = << "EOF"
  139. The script or command that reads the console
  140. If you use ttywatch server, something like the following would work.
  141. CONSOLE = nc -d localhost 3001
  142. For a virtual machine with guest name "Guest".
  143. CONSOLE = virsh console Guest
  144. EOF
  145. ;
  146. $config_help{"LOCALVERSION"} = << "EOF"
  147. Required version ending to differentiate the test
  148. from other linux builds on the system.
  149. EOF
  150. ;
  151. $config_help{"REBOOT_TYPE"} = << "EOF"
  152. Way to reboot the box to the test kernel.
  153. Only valid options so far are "grub" and "script".
  154. If you specify grub, it will assume grub version 1
  155. and will search in /boot/grub/menu.lst for the title \$GRUB_MENU
  156. and select that target to reboot to the kernel. If this is not
  157. your setup, then specify "script" and have a command or script
  158. specified in REBOOT_SCRIPT to boot to the target.
  159. The entry in /boot/grub/menu.lst must be entered in manually.
  160. The test will not modify that file.
  161. EOF
  162. ;
  163. $config_help{"GRUB_MENU"} = << "EOF"
  164. The grub title name for the test kernel to boot
  165. (Only mandatory if REBOOT_TYPE = grub)
  166. Note, ktest.pl will not update the grub menu.lst, you need to
  167. manually add an option for the test. ktest.pl will search
  168. the grub menu.lst for this option to find what kernel to
  169. reboot into.
  170. For example, if in the /boot/grub/menu.lst the test kernel title has:
  171. title Test Kernel
  172. kernel vmlinuz-test
  173. GRUB_MENU = Test Kernel
  174. EOF
  175. ;
  176. $config_help{"REBOOT_SCRIPT"} = << "EOF"
  177. A script to reboot the target into the test kernel
  178. (Only mandatory if REBOOT_TYPE = script)
  179. EOF
  180. ;
  181. sub get_ktest_config {
  182. my ($config) = @_;
  183. return if (defined($opt{$config}));
  184. if (defined($config_help{$config})) {
  185. print "\n";
  186. print $config_help{$config};
  187. }
  188. for (;;) {
  189. print "$config = ";
  190. if (defined($default{$config})) {
  191. print "\[$default{$config}\] ";
  192. }
  193. $entered_configs{$config} = <STDIN>;
  194. $entered_configs{$config} =~ s/^\s*(.*\S)\s*$/$1/;
  195. if ($entered_configs{$config} =~ /^\s*$/) {
  196. if ($default{$config}) {
  197. $entered_configs{$config} = $default{$config};
  198. } else {
  199. print "Your answer can not be blank\n";
  200. next;
  201. }
  202. }
  203. last;
  204. }
  205. }
  206. sub get_ktest_configs {
  207. get_ktest_config("MACHINE");
  208. get_ktest_config("SSH_USER");
  209. get_ktest_config("BUILD_DIR");
  210. get_ktest_config("OUTPUT_DIR");
  211. get_ktest_config("BUILD_TARGET");
  212. get_ktest_config("TARGET_IMAGE");
  213. get_ktest_config("POWER_CYCLE");
  214. get_ktest_config("CONSOLE");
  215. get_ktest_config("LOCALVERSION");
  216. my $rtype = $opt{"REBOOT_TYPE"};
  217. if (!defined($rtype)) {
  218. if (!defined($opt{"GRUB_MENU"})) {
  219. get_ktest_config("REBOOT_TYPE");
  220. $rtype = $entered_configs{"REBOOT_TYPE"};
  221. } else {
  222. $rtype = "grub";
  223. }
  224. }
  225. if ($rtype eq "grub") {
  226. get_ktest_config("GRUB_MENU");
  227. } else {
  228. get_ktest_config("REBOOT_SCRIPT");
  229. }
  230. }
  231. sub set_value {
  232. my ($lvalue, $rvalue) = @_;
  233. if (defined($opt{$lvalue})) {
  234. die "Error: Option $lvalue defined more than once!\n";
  235. }
  236. if ($rvalue =~ /^\s*$/) {
  237. delete $opt{$lvalue};
  238. } else {
  239. $opt{$lvalue} = $rvalue;
  240. }
  241. }
  242. sub read_config {
  243. my ($config) = @_;
  244. open(IN, $config) || die "can't read file $config";
  245. my $name = $config;
  246. $name =~ s,.*/(.*),$1,;
  247. my $test_num = 0;
  248. my $default = 1;
  249. my $repeat = 1;
  250. my $num_tests_set = 0;
  251. my $skip = 0;
  252. my $rest;
  253. while (<IN>) {
  254. # ignore blank lines and comments
  255. next if (/^\s*$/ || /\s*\#/);
  256. if (/^\s*TEST_START(.*)/) {
  257. $rest = $1;
  258. if ($num_tests_set) {
  259. die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
  260. }
  261. my $old_test_num = $test_num;
  262. my $old_repeat = $repeat;
  263. $test_num += $repeat;
  264. $default = 0;
  265. $repeat = 1;
  266. if ($rest =~ /\s+SKIP(.*)/) {
  267. $rest = $1;
  268. $skip = 1;
  269. } else {
  270. $skip = 0;
  271. }
  272. if ($rest =~ /\s+ITERATE\s+(\d+)(.*)$/) {
  273. $repeat = $1;
  274. $rest = $2;
  275. $repeat_tests{"$test_num"} = $repeat;
  276. }
  277. if ($rest =~ /\s+SKIP(.*)/) {
  278. $rest = $1;
  279. $skip = 1;
  280. }
  281. if ($rest !~ /^\s*$/) {
  282. die "$name: $.: Gargbage found after TEST_START\n$_";
  283. }
  284. if ($skip) {
  285. $test_num = $old_test_num;
  286. $repeat = $old_repeat;
  287. }
  288. } elsif (/^\s*DEFAULTS(.*)$/) {
  289. $default = 1;
  290. $rest = $1;
  291. if ($rest =~ /\s+SKIP(.*)/) {
  292. $rest = $1;
  293. $skip = 1;
  294. } else {
  295. $skip = 0;
  296. }
  297. if ($rest !~ /^\s*$/) {
  298. die "$name: $.: Gargbage found after DEFAULTS\n$_";
  299. }
  300. } elsif (/^\s*([A-Z_\[\]\d]+)\s*=\s*(.*?)\s*$/) {
  301. next if ($skip);
  302. my $lvalue = $1;
  303. my $rvalue = $2;
  304. if (!$default &&
  305. ($lvalue eq "NUM_TESTS" ||
  306. $lvalue eq "LOG_FILE" ||
  307. $lvalue eq "CLEAR_LOG")) {
  308. die "$name: $.: $lvalue must be set in DEFAULTS section\n";
  309. }
  310. if ($lvalue eq "NUM_TESTS") {
  311. if ($test_num) {
  312. die "$name: $.: Can not specify both NUM_TESTS and TEST_START\n";
  313. }
  314. if (!$default) {
  315. die "$name: $.: NUM_TESTS must be set in default section\n";
  316. }
  317. $num_tests_set = 1;
  318. }
  319. if ($default || $lvalue =~ /\[\d+\]$/) {
  320. set_value($lvalue, $rvalue);
  321. } else {
  322. my $val = "$lvalue\[$test_num\]";
  323. set_value($val, $rvalue);
  324. if ($repeat > 1) {
  325. $repeats{$val} = $repeat;
  326. }
  327. }
  328. } else {
  329. die "$name: $.: Garbage found in config\n$_";
  330. }
  331. }
  332. close(IN);
  333. if ($test_num) {
  334. $test_num += $repeat - 1;
  335. $opt{"NUM_TESTS"} = $test_num;
  336. }
  337. # make sure we have all mandatory configs
  338. get_ktest_configs;
  339. # set any defaults
  340. foreach my $default (keys %default) {
  341. if (!defined($opt{$default})) {
  342. $opt{$default} = $default{$default};
  343. }
  344. }
  345. }
  346. sub _logit {
  347. if (defined($opt{"LOG_FILE"})) {
  348. open(OUT, ">> $opt{LOG_FILE}") or die "Can't write to $opt{LOG_FILE}";
  349. print OUT @_;
  350. close(OUT);
  351. }
  352. }
  353. sub logit {
  354. if (defined($opt{"LOG_FILE"})) {
  355. _logit @_;
  356. } else {
  357. print @_;
  358. }
  359. }
  360. sub doprint {
  361. print @_;
  362. _logit @_;
  363. }
  364. sub run_command;
  365. sub reboot {
  366. # try to reboot normally
  367. if (run_command $reboot) {
  368. if (defined($powercycle_after_reboot)) {
  369. sleep $powercycle_after_reboot;
  370. run_command "$power_cycle";
  371. }
  372. } else {
  373. # nope? power cycle it.
  374. run_command "$power_cycle";
  375. }
  376. }
  377. sub do_not_reboot {
  378. my $i = $iteration;
  379. return $test_type eq "build" ||
  380. ($test_type eq "patchcheck" && $opt{"PATCHCHECK_TYPE[$i]"} eq "build") ||
  381. ($test_type eq "bisect" && $opt{"BISECT_TYPE[$i]"} eq "build");
  382. }
  383. sub dodie {
  384. doprint "CRITICAL FAILURE... ", @_, "\n";
  385. my $i = $iteration;
  386. if ($reboot_on_error && !do_not_reboot) {
  387. doprint "REBOOTING\n";
  388. reboot;
  389. } elsif ($poweroff_on_error && defined($power_off)) {
  390. doprint "POWERING OFF\n";
  391. `$power_off`;
  392. }
  393. die @_, "\n";
  394. }
  395. sub open_console {
  396. my ($fp) = @_;
  397. my $flags;
  398. my $pid = open($fp, "$console|") or
  399. dodie "Can't open console $console";
  400. $flags = fcntl($fp, F_GETFL, 0) or
  401. dodie "Can't get flags for the socket: $!";
  402. $flags = fcntl($fp, F_SETFL, $flags | O_NONBLOCK) or
  403. dodie "Can't set flags for the socket: $!";
  404. return $pid;
  405. }
  406. sub close_console {
  407. my ($fp, $pid) = @_;
  408. doprint "kill child process $pid\n";
  409. kill 2, $pid;
  410. print "closing!\n";
  411. close($fp);
  412. }
  413. sub start_monitor {
  414. if ($monitor_cnt++) {
  415. return;
  416. }
  417. $monitor_fp = \*MONFD;
  418. $monitor_pid = open_console $monitor_fp;
  419. return;
  420. open(MONFD, "Stop perl from warning about single use of MONFD");
  421. }
  422. sub end_monitor {
  423. if (--$monitor_cnt) {
  424. return;
  425. }
  426. close_console($monitor_fp, $monitor_pid);
  427. }
  428. sub wait_for_monitor {
  429. my ($time) = @_;
  430. my $line;
  431. doprint "** Wait for monitor to settle down **\n";
  432. # read the monitor and wait for the system to calm down
  433. do {
  434. $line = wait_for_input($monitor_fp, $time);
  435. print "$line" if (defined($line));
  436. } while (defined($line));
  437. print "** Monitor flushed **\n";
  438. }
  439. sub fail {
  440. if ($die_on_failure) {
  441. dodie @_;
  442. }
  443. doprint "FAILED\n";
  444. my $i = $iteration;
  445. # no need to reboot for just building.
  446. if (!do_not_reboot) {
  447. doprint "REBOOTING\n";
  448. reboot;
  449. start_monitor;
  450. wait_for_monitor $sleep_time;
  451. end_monitor;
  452. }
  453. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  454. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  455. doprint "KTEST RESULT: TEST $i Failed: ", @_, "\n";
  456. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  457. doprint "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
  458. return 1 if (!defined($store_failures));
  459. my @t = localtime;
  460. my $date = sprintf "%04d%02d%02d%02d%02d%02d",
  461. 1900+$t[5],$t[4],$t[3],$t[2],$t[1],$t[0];
  462. my $type = $build_type;
  463. if ($type =~ /useconfig/) {
  464. $type = "useconfig";
  465. }
  466. my $dir = "$machine-$test_type-$type-fail-$date";
  467. my $faildir = "$store_failures/$dir";
  468. if (!-d $faildir) {
  469. mkpath($faildir) or
  470. die "can't create $faildir";
  471. }
  472. if (-f "$output_config") {
  473. cp "$output_config", "$faildir/config" or
  474. die "failed to copy .config";
  475. }
  476. if (-f $buildlog) {
  477. cp $buildlog, "$faildir/buildlog" or
  478. die "failed to move $buildlog";
  479. }
  480. if (-f $dmesg) {
  481. cp $dmesg, "$faildir/dmesg" or
  482. die "failed to move $dmesg";
  483. }
  484. doprint "*** Saved info to $faildir ***\n";
  485. return 1;
  486. }
  487. sub run_command {
  488. my ($command) = @_;
  489. my $dolog = 0;
  490. my $dord = 0;
  491. my $pid;
  492. $command =~ s/\$SSH_USER/$ssh_user/g;
  493. $command =~ s/\$MACHINE/$machine/g;
  494. doprint("$command ... ");
  495. $pid = open(CMD, "$command 2>&1 |") or
  496. (fail "unable to exec $command" and return 0);
  497. if (defined($opt{"LOG_FILE"})) {
  498. open(LOG, ">>$opt{LOG_FILE}") or
  499. dodie "failed to write to log";
  500. $dolog = 1;
  501. }
  502. if (defined($redirect)) {
  503. open (RD, ">$redirect") or
  504. dodie "failed to write to redirect $redirect";
  505. $dord = 1;
  506. }
  507. while (<CMD>) {
  508. print LOG if ($dolog);
  509. print RD if ($dord);
  510. }
  511. waitpid($pid, 0);
  512. my $failed = $?;
  513. close(CMD);
  514. close(LOG) if ($dolog);
  515. close(RD) if ($dord);
  516. if ($failed) {
  517. doprint "FAILED!\n";
  518. } else {
  519. doprint "SUCCESS\n";
  520. }
  521. return !$failed;
  522. }
  523. sub run_ssh {
  524. my ($cmd) = @_;
  525. my $cp_exec = $ssh_exec;
  526. $cp_exec =~ s/\$SSH_COMMAND/$cmd/g;
  527. return run_command "$cp_exec";
  528. }
  529. sub run_scp {
  530. my ($src, $dst) = @_;
  531. my $cp_scp = $scp_to_target;
  532. $cp_scp =~ s/\$SRC_FILE/$src/g;
  533. $cp_scp =~ s/\$DST_FILE/$dst/g;
  534. return run_command "$cp_scp";
  535. }
  536. sub get_grub_index {
  537. if ($reboot_type ne "grub") {
  538. return;
  539. }
  540. return if (defined($grub_number));
  541. doprint "Find grub menu ... ";
  542. $grub_number = -1;
  543. my $ssh_grub = $ssh_exec;
  544. $ssh_grub =~ s,\$SSH_COMMAND,cat /boot/grub/menu.lst,g;
  545. open(IN, "$ssh_grub |")
  546. or die "unable to get menu.lst";
  547. while (<IN>) {
  548. if (/^\s*title\s+$grub_menu\s*$/) {
  549. $grub_number++;
  550. last;
  551. } elsif (/^\s*title\s/) {
  552. $grub_number++;
  553. }
  554. }
  555. close(IN);
  556. die "Could not find '$grub_menu' in /boot/grub/menu on $machine"
  557. if ($grub_number < 0);
  558. doprint "$grub_number\n";
  559. }
  560. sub wait_for_input
  561. {
  562. my ($fp, $time) = @_;
  563. my $rin;
  564. my $ready;
  565. my $line;
  566. my $ch;
  567. if (!defined($time)) {
  568. $time = $timeout;
  569. }
  570. $rin = '';
  571. vec($rin, fileno($fp), 1) = 1;
  572. $ready = select($rin, undef, undef, $time);
  573. $line = "";
  574. # try to read one char at a time
  575. while (sysread $fp, $ch, 1) {
  576. $line .= $ch;
  577. last if ($ch eq "\n");
  578. }
  579. if (!length($line)) {
  580. return undef;
  581. }
  582. return $line;
  583. }
  584. sub reboot_to {
  585. if ($reboot_type eq "grub") {
  586. run_ssh "'(echo \"savedefault --default=$grub_number --once\" | grub --batch; reboot)'";
  587. return;
  588. }
  589. run_command "$reboot_script";
  590. }
  591. sub get_sha1 {
  592. my ($commit) = @_;
  593. doprint "git rev-list --max-count=1 $commit ... ";
  594. my $sha1 = `git rev-list --max-count=1 $commit`;
  595. my $ret = $?;
  596. logit $sha1;
  597. if ($ret) {
  598. doprint "FAILED\n";
  599. dodie "Failed to get git $commit";
  600. }
  601. print "SUCCESS\n";
  602. chomp $sha1;
  603. return $sha1;
  604. }
  605. sub monitor {
  606. my $booted = 0;
  607. my $bug = 0;
  608. my $skip_call_trace = 0;
  609. my $loops;
  610. wait_for_monitor 5;
  611. my $line;
  612. my $full_line = "";
  613. open(DMESG, "> $dmesg") or
  614. die "unable to write to $dmesg";
  615. reboot_to;
  616. my $success_start;
  617. my $failure_start;
  618. for (;;) {
  619. if ($booted) {
  620. $line = wait_for_input($monitor_fp, $booted_timeout);
  621. } else {
  622. $line = wait_for_input($monitor_fp);
  623. }
  624. last if (!defined($line));
  625. doprint $line;
  626. print DMESG $line;
  627. # we are not guaranteed to get a full line
  628. $full_line .= $line;
  629. if ($full_line =~ /$success_line/) {
  630. $booted = 1;
  631. $success_start = time;
  632. }
  633. if ($booted && defined($stop_after_success) &&
  634. $stop_after_success >= 0) {
  635. my $now = time;
  636. if ($now - $success_start >= $stop_after_success) {
  637. doprint "Test forced to stop after $stop_after_success seconds after success\n";
  638. last;
  639. }
  640. }
  641. if ($full_line =~ /\[ backtrace testing \]/) {
  642. $skip_call_trace = 1;
  643. }
  644. if ($full_line =~ /call trace:/i) {
  645. if (!$skip_call_trace) {
  646. $bug = 1;
  647. $failure_start = time;
  648. }
  649. }
  650. if ($bug && defined($stop_after_failure) &&
  651. $stop_after_failure >= 0) {
  652. my $now = time;
  653. if ($now - $failure_start >= $stop_after_failure) {
  654. doprint "Test forced to stop after $stop_after_failure seconds after failure\n";
  655. last;
  656. }
  657. }
  658. if ($full_line =~ /\[ end of backtrace testing \]/) {
  659. $skip_call_trace = 0;
  660. }
  661. if ($full_line =~ /Kernel panic -/) {
  662. $bug = 1;
  663. }
  664. if ($line =~ /\n/) {
  665. $full_line = "";
  666. }
  667. }
  668. close(DMESG);
  669. if ($bug) {
  670. return 0 if ($in_bisect);
  671. fail "failed - got a bug report" and return 0;
  672. }
  673. if (!$booted) {
  674. return 0 if ($in_bisect);
  675. fail "failed - never got a boot prompt." and return 0;
  676. }
  677. return 1;
  678. }
  679. sub install {
  680. run_scp "$outputdir/$build_target", "$target_image" or
  681. dodie "failed to copy image";
  682. my $install_mods = 0;
  683. # should we process modules?
  684. $install_mods = 0;
  685. open(IN, "$output_config") or dodie("Can't read config file");
  686. while (<IN>) {
  687. if (/CONFIG_MODULES(=y)?/) {
  688. $install_mods = 1 if (defined($1));
  689. last;
  690. }
  691. }
  692. close(IN);
  693. if (!$install_mods) {
  694. doprint "No modules needed\n";
  695. return;
  696. }
  697. run_command "$make INSTALL_MOD_PATH=$tmpdir modules_install" or
  698. dodie "Failed to install modules";
  699. my $modlib = "/lib/modules/$version";
  700. my $modtar = "ktest-mods.tar.bz2";
  701. run_ssh "rm -rf $modlib" or
  702. dodie "failed to remove old mods: $modlib";
  703. # would be nice if scp -r did not follow symbolic links
  704. run_command "cd $tmpdir && tar -cjf $modtar lib/modules/$version" or
  705. dodie "making tarball";
  706. run_scp "$tmpdir/$modtar", "/tmp" or
  707. dodie "failed to copy modules";
  708. unlink "$tmpdir/$modtar";
  709. run_ssh "'(cd / && tar xf /tmp/$modtar)'" or
  710. dodie "failed to tar modules";
  711. run_ssh "rm -f /tmp/$modtar";
  712. return if (!defined($post_install));
  713. my $cp_post_install = $post_install;
  714. $cp_post_install = s/\$KERNEL_VERSION/$version/g;
  715. run_command "$cp_post_install" or
  716. dodie "Failed to run post install";
  717. }
  718. sub check_buildlog {
  719. my ($patch) = @_;
  720. my @files = `git show $patch | diffstat -l`;
  721. open(IN, "git show $patch |") or
  722. dodie "failed to show $patch";
  723. while (<IN>) {
  724. if (m,^--- a/(.*),) {
  725. chomp $1;
  726. $files[$#files] = $1;
  727. }
  728. }
  729. close(IN);
  730. open(IN, $buildlog) or dodie "Can't open $buildlog";
  731. while (<IN>) {
  732. if (/^\s*(.*?):.*(warning|error)/) {
  733. my $err = $1;
  734. foreach my $file (@files) {
  735. my $fullpath = "$builddir/$file";
  736. if ($file eq $err || $fullpath eq $err) {
  737. fail "$file built with warnings" and return 0;
  738. }
  739. }
  740. }
  741. }
  742. close(IN);
  743. return 1;
  744. }
  745. sub build {
  746. my ($type) = @_;
  747. my $defconfig = "";
  748. unlink $buildlog;
  749. if ($type =~ /^useconfig:(.*)/) {
  750. run_command "cp $1 $output_config" or
  751. dodie "could not copy $1 to .config";
  752. $type = "oldconfig";
  753. }
  754. # old config can ask questions
  755. if ($type eq "oldconfig") {
  756. $type = "oldnoconfig";
  757. # allow for empty configs
  758. run_command "touch $output_config";
  759. run_command "mv $output_config $outputdir/config_temp" or
  760. dodie "moving .config";
  761. if (!$noclean && !run_command "$make mrproper") {
  762. dodie "make mrproper";
  763. }
  764. run_command "mv $outputdir/config_temp $output_config" or
  765. dodie "moving config_temp";
  766. } elsif (!$noclean) {
  767. unlink "$output_config";
  768. run_command "$make mrproper" or
  769. dodie "make mrproper";
  770. }
  771. # add something to distinguish this build
  772. open(OUT, "> $outputdir/localversion") or dodie("Can't make localversion file");
  773. print OUT "$localversion\n";
  774. close(OUT);
  775. if (defined($minconfig)) {
  776. $defconfig = "KCONFIG_ALLCONFIG=$minconfig";
  777. }
  778. run_command "$defconfig $make $type" or
  779. dodie "failed make config";
  780. $redirect = "$buildlog";
  781. if (!run_command "$make $build_options") {
  782. undef $redirect;
  783. # bisect may need this to pass
  784. return 0 if ($in_bisect);
  785. fail "failed build" and return 0;
  786. }
  787. undef $redirect;
  788. return 1;
  789. }
  790. sub halt {
  791. if (!run_ssh "halt" or defined($power_off)) {
  792. if (defined($poweroff_after_halt)) {
  793. sleep $poweroff_after_halt;
  794. run_command "$power_off";
  795. }
  796. } else {
  797. # nope? the zap it!
  798. run_command "$power_off";
  799. }
  800. }
  801. sub success {
  802. my ($i) = @_;
  803. $successes++;
  804. doprint "\n\n*******************************************\n";
  805. doprint "*******************************************\n";
  806. doprint "KTEST RESULT: TEST $i SUCCESS!!!! **\n";
  807. doprint "*******************************************\n";
  808. doprint "*******************************************\n";
  809. if ($i != $opt{"NUM_TESTS"} && !do_not_reboot) {
  810. doprint "Reboot and wait $sleep_time seconds\n";
  811. reboot;
  812. start_monitor;
  813. wait_for_monitor $sleep_time;
  814. end_monitor;
  815. }
  816. }
  817. sub get_version {
  818. # get the release name
  819. doprint "$make kernelrelease ... ";
  820. $version = `$make kernelrelease | tail -1`;
  821. chomp($version);
  822. doprint "$version\n";
  823. }
  824. sub child_run_test {
  825. my $failed = 0;
  826. # child should have no power
  827. $reboot_on_error = 0;
  828. $poweroff_on_error = 0;
  829. $die_on_failure = 1;
  830. run_command $run_test or $failed = 1;
  831. exit $failed;
  832. }
  833. my $child_done;
  834. sub child_finished {
  835. $child_done = 1;
  836. }
  837. sub do_run_test {
  838. my $child_pid;
  839. my $child_exit;
  840. my $line;
  841. my $full_line;
  842. my $bug = 0;
  843. wait_for_monitor 1;
  844. doprint "run test $run_test\n";
  845. $child_done = 0;
  846. $SIG{CHLD} = qw(child_finished);
  847. $child_pid = fork;
  848. child_run_test if (!$child_pid);
  849. $full_line = "";
  850. do {
  851. $line = wait_for_input($monitor_fp, 1);
  852. if (defined($line)) {
  853. # we are not guaranteed to get a full line
  854. $full_line .= $line;
  855. if ($full_line =~ /call trace:/i) {
  856. $bug = 1;
  857. }
  858. if ($full_line =~ /Kernel panic -/) {
  859. $bug = 1;
  860. }
  861. if ($line =~ /\n/) {
  862. $full_line = "";
  863. }
  864. }
  865. } while (!$child_done && !$bug);
  866. if ($bug) {
  867. doprint "Detected kernel crash!\n";
  868. # kill the child with extreme prejudice
  869. kill 9, $child_pid;
  870. }
  871. waitpid $child_pid, 0;
  872. $child_exit = $?;
  873. if ($bug || $child_exit) {
  874. return 0 if $in_bisect;
  875. fail "test failed" and return 0;
  876. }
  877. return 1;
  878. }
  879. sub run_git_bisect {
  880. my ($command) = @_;
  881. doprint "$command ... ";
  882. my $output = `$command 2>&1`;
  883. my $ret = $?;
  884. logit $output;
  885. if ($ret) {
  886. doprint "FAILED\n";
  887. dodie "Failed to git bisect";
  888. }
  889. doprint "SUCCESS\n";
  890. if ($output =~ m/^(Bisecting: .*\(roughly \d+ steps?\))\s+\[([[:xdigit:]]+)\]/) {
  891. doprint "$1 [$2]\n";
  892. } elsif ($output =~ m/^([[:xdigit:]]+) is the first bad commit/) {
  893. $bisect_bad = $1;
  894. doprint "Found bad commit... $1\n";
  895. return 0;
  896. } else {
  897. # we already logged it, just print it now.
  898. print $output;
  899. }
  900. return 1;
  901. }
  902. # returns 1 on success, 0 on failure
  903. sub run_bisect_test {
  904. my ($type, $buildtype) = @_;
  905. my $failed = 0;
  906. my $result;
  907. my $output;
  908. my $ret;
  909. $in_bisect = 1;
  910. build $buildtype or $failed = 1;
  911. if ($type ne "build") {
  912. dodie "Failed on build" if $failed;
  913. # Now boot the box
  914. get_grub_index;
  915. get_version;
  916. install;
  917. start_monitor;
  918. monitor or $failed = 1;
  919. if ($type ne "boot") {
  920. dodie "Failed on boot" if $failed;
  921. do_run_test or $failed = 1;
  922. }
  923. end_monitor;
  924. }
  925. if ($failed) {
  926. $result = 0;
  927. # reboot the box to a good kernel
  928. if ($type ne "build") {
  929. doprint "Reboot and sleep $bisect_sleep_time seconds\n";
  930. reboot;
  931. start_monitor;
  932. wait_for_monitor $bisect_sleep_time;
  933. end_monitor;
  934. }
  935. } else {
  936. $result = 1;
  937. }
  938. $in_bisect = 0;
  939. return $result;
  940. }
  941. sub run_bisect {
  942. my ($type) = @_;
  943. my $buildtype = "oldconfig";
  944. # We should have a minconfig to use?
  945. if (defined($minconfig)) {
  946. $buildtype = "useconfig:$minconfig";
  947. }
  948. my $ret = run_bisect_test $type, $buildtype;
  949. # Are we looking for where it worked, not failed?
  950. if ($reverse_bisect) {
  951. $ret = !$ret;
  952. }
  953. if ($ret) {
  954. return "good";
  955. } else {
  956. return "bad";
  957. }
  958. }
  959. sub bisect {
  960. my ($i) = @_;
  961. my $result;
  962. die "BISECT_GOOD[$i] not defined\n" if (!defined($opt{"BISECT_GOOD[$i]"}));
  963. die "BISECT_BAD[$i] not defined\n" if (!defined($opt{"BISECT_BAD[$i]"}));
  964. die "BISECT_TYPE[$i] not defined\n" if (!defined($opt{"BISECT_TYPE[$i]"}));
  965. my $good = $opt{"BISECT_GOOD[$i]"};
  966. my $bad = $opt{"BISECT_BAD[$i]"};
  967. my $type = $opt{"BISECT_TYPE[$i]"};
  968. my $start = $opt{"BISECT_START[$i]"};
  969. my $replay = $opt{"BISECT_REPLAY[$i]"};
  970. # convert to true sha1's
  971. $good = get_sha1($good);
  972. $bad = get_sha1($bad);
  973. if (defined($opt{"BISECT_REVERSE[$i]"}) &&
  974. $opt{"BISECT_REVERSE[$i]"} == 1) {
  975. doprint "Performing a reverse bisect (bad is good, good is bad!)\n";
  976. $reverse_bisect = 1;
  977. } else {
  978. $reverse_bisect = 0;
  979. }
  980. # Can't have a test without having a test to run
  981. if ($type eq "test" && !defined($run_test)) {
  982. $type = "boot";
  983. }
  984. my $check = $opt{"BISECT_CHECK[$i]"};
  985. if (defined($check) && $check ne "0") {
  986. # get current HEAD
  987. my $head = get_sha1("HEAD");
  988. if ($check ne "good") {
  989. doprint "TESTING BISECT BAD [$bad]\n";
  990. run_command "git checkout $bad" or
  991. die "Failed to checkout $bad";
  992. $result = run_bisect $type;
  993. if ($result ne "bad") {
  994. fail "Tested BISECT_BAD [$bad] and it succeeded" and return 0;
  995. }
  996. }
  997. if ($check ne "bad") {
  998. doprint "TESTING BISECT GOOD [$good]\n";
  999. run_command "git checkout $good" or
  1000. die "Failed to checkout $good";
  1001. $result = run_bisect $type;
  1002. if ($result ne "good") {
  1003. fail "Tested BISECT_GOOD [$good] and it failed" and return 0;
  1004. }
  1005. }
  1006. # checkout where we started
  1007. run_command "git checkout $head" or
  1008. die "Failed to checkout $head";
  1009. }
  1010. run_command "git bisect start" or
  1011. dodie "could not start bisect";
  1012. run_command "git bisect good $good" or
  1013. dodie "could not set bisect good to $good";
  1014. run_git_bisect "git bisect bad $bad" or
  1015. dodie "could not set bisect bad to $bad";
  1016. if (defined($replay)) {
  1017. run_command "git bisect replay $replay" or
  1018. dodie "failed to run replay";
  1019. }
  1020. if (defined($start)) {
  1021. run_command "git checkout $start" or
  1022. dodie "failed to checkout $start";
  1023. }
  1024. my $test;
  1025. do {
  1026. $result = run_bisect $type;
  1027. $test = run_git_bisect "git bisect $result";
  1028. } while ($test);
  1029. run_command "git bisect log" or
  1030. dodie "could not capture git bisect log";
  1031. run_command "git bisect reset" or
  1032. dodie "could not reset git bisect";
  1033. doprint "Bad commit was [$bisect_bad]\n";
  1034. success $i;
  1035. }
  1036. my %config_ignore;
  1037. my %config_set;
  1038. my %config_list;
  1039. my %null_config;
  1040. my %dependency;
  1041. sub process_config_ignore {
  1042. my ($config) = @_;
  1043. open (IN, $config)
  1044. or dodie "Failed to read $config";
  1045. while (<IN>) {
  1046. if (/^(.*?(CONFIG\S*)(=.*| is not set))/) {
  1047. $config_ignore{$2} = $1;
  1048. }
  1049. }
  1050. close(IN);
  1051. }
  1052. sub read_current_config {
  1053. my ($config_ref) = @_;
  1054. %{$config_ref} = ();
  1055. undef %{$config_ref};
  1056. my @key = keys %{$config_ref};
  1057. if ($#key >= 0) {
  1058. print "did not delete!\n";
  1059. exit;
  1060. }
  1061. open (IN, "$output_config");
  1062. while (<IN>) {
  1063. if (/^(CONFIG\S+)=(.*)/) {
  1064. ${$config_ref}{$1} = $2;
  1065. }
  1066. }
  1067. close(IN);
  1068. }
  1069. sub get_dependencies {
  1070. my ($config) = @_;
  1071. my $arr = $dependency{$config};
  1072. if (!defined($arr)) {
  1073. return ();
  1074. }
  1075. my @deps = @{$arr};
  1076. foreach my $dep (@{$arr}) {
  1077. print "ADD DEP $dep\n";
  1078. @deps = (@deps, get_dependencies $dep);
  1079. }
  1080. return @deps;
  1081. }
  1082. sub create_config {
  1083. my @configs = @_;
  1084. open(OUT, ">$output_config") or dodie "Can not write to $output_config";
  1085. foreach my $config (@configs) {
  1086. print OUT "$config_set{$config}\n";
  1087. my @deps = get_dependencies $config;
  1088. foreach my $dep (@deps) {
  1089. print OUT "$config_set{$dep}\n";
  1090. }
  1091. }
  1092. foreach my $config (keys %config_ignore) {
  1093. print OUT "$config_ignore{$config}\n";
  1094. }
  1095. close(OUT);
  1096. # exit;
  1097. run_command "$make oldnoconfig" or
  1098. dodie "failed make config oldconfig";
  1099. }
  1100. sub compare_configs {
  1101. my (%a, %b) = @_;
  1102. foreach my $item (keys %a) {
  1103. if (!defined($b{$item})) {
  1104. print "diff $item\n";
  1105. return 1;
  1106. }
  1107. delete $b{$item};
  1108. }
  1109. my @keys = keys %b;
  1110. if ($#keys) {
  1111. print "diff2 $keys[0]\n";
  1112. }
  1113. return -1 if ($#keys >= 0);
  1114. return 0;
  1115. }
  1116. sub run_config_bisect_test {
  1117. my ($type) = @_;
  1118. return run_bisect_test $type, "oldconfig";
  1119. }
  1120. sub process_passed {
  1121. my (%configs) = @_;
  1122. doprint "These configs had no failure: (Enabling them for further compiles)\n";
  1123. # Passed! All these configs are part of a good compile.
  1124. # Add them to the min options.
  1125. foreach my $config (keys %configs) {
  1126. if (defined($config_list{$config})) {
  1127. doprint " removing $config\n";
  1128. $config_ignore{$config} = $config_list{$config};
  1129. delete $config_list{$config};
  1130. }
  1131. }
  1132. doprint "config copied to $outputdir/config_good\n";
  1133. run_command "cp -f $output_config $outputdir/config_good";
  1134. }
  1135. sub process_failed {
  1136. my ($config) = @_;
  1137. doprint "\n\n***************************************\n";
  1138. doprint "Found bad config: $config\n";
  1139. doprint "***************************************\n\n";
  1140. }
  1141. sub run_config_bisect {
  1142. my @start_list = keys %config_list;
  1143. if ($#start_list < 0) {
  1144. doprint "No more configs to test!!!\n";
  1145. return -1;
  1146. }
  1147. doprint "***** RUN TEST ***\n";
  1148. my $type = $opt{"CONFIG_BISECT_TYPE[$iteration]"};
  1149. my $ret;
  1150. my %current_config;
  1151. my $count = $#start_list + 1;
  1152. doprint " $count configs to test\n";
  1153. my $half = int($#start_list / 2);
  1154. do {
  1155. my @tophalf = @start_list[0 .. $half];
  1156. create_config @tophalf;
  1157. read_current_config \%current_config;
  1158. $count = $#tophalf + 1;
  1159. doprint "Testing $count configs\n";
  1160. my $found = 0;
  1161. # make sure we test something
  1162. foreach my $config (@tophalf) {
  1163. if (defined($current_config{$config})) {
  1164. logit " $config\n";
  1165. $found = 1;
  1166. }
  1167. }
  1168. if (!$found) {
  1169. # try the other half
  1170. doprint "Top half produced no set configs, trying bottom half\n";
  1171. @tophalf = @start_list[$half .. $#start_list];
  1172. create_config @tophalf;
  1173. read_current_config \%current_config;
  1174. foreach my $config (@tophalf) {
  1175. if (defined($current_config{$config})) {
  1176. logit " $config\n";
  1177. $found = 1;
  1178. }
  1179. }
  1180. if (!$found) {
  1181. doprint "Failed: Can't make new config with current configs\n";
  1182. foreach my $config (@start_list) {
  1183. doprint " CONFIG: $config\n";
  1184. }
  1185. return -1;
  1186. }
  1187. $count = $#tophalf + 1;
  1188. doprint "Testing $count configs\n";
  1189. }
  1190. $ret = run_config_bisect_test $type;
  1191. if ($ret) {
  1192. process_passed %current_config;
  1193. return 0;
  1194. }
  1195. doprint "This config had a failure.\n";
  1196. doprint "Removing these configs that were not set in this config:\n";
  1197. doprint "config copied to $outputdir/config_bad\n";
  1198. run_command "cp -f $output_config $outputdir/config_bad";
  1199. # A config exists in this group that was bad.
  1200. foreach my $config (keys %config_list) {
  1201. if (!defined($current_config{$config})) {
  1202. doprint " removing $config\n";
  1203. delete $config_list{$config};
  1204. }
  1205. }
  1206. @start_list = @tophalf;
  1207. if ($#start_list == 0) {
  1208. process_failed $start_list[0];
  1209. return 1;
  1210. }
  1211. # remove half the configs we are looking at and see if
  1212. # they are good.
  1213. $half = int($#start_list / 2);
  1214. } while ($half > 0);
  1215. # we found a single config, try it again
  1216. my @tophalf = @start_list[0 .. 0];
  1217. $ret = run_config_bisect_test $type;
  1218. if ($ret) {
  1219. process_passed %current_config;
  1220. return 0;
  1221. }
  1222. process_failed $start_list[0];
  1223. return 1;
  1224. }
  1225. sub config_bisect {
  1226. my ($i) = @_;
  1227. my $start_config = $opt{"CONFIG_BISECT[$i]"};
  1228. my $tmpconfig = "$tmpdir/use_config";
  1229. # Make the file with the bad config and the min config
  1230. if (defined($minconfig)) {
  1231. # read the min config for things to ignore
  1232. run_command "cp $minconfig $tmpconfig" or
  1233. dodie "failed to copy $minconfig to $tmpconfig";
  1234. } else {
  1235. unlink $tmpconfig;
  1236. }
  1237. # Add other configs
  1238. if (defined($addconfig)) {
  1239. run_command "cat $addconfig >> $tmpconfig" or
  1240. dodie "failed to append $addconfig";
  1241. }
  1242. my $defconfig = "";
  1243. if (-f $tmpconfig) {
  1244. $defconfig = "KCONFIG_ALLCONFIG=$tmpconfig";
  1245. process_config_ignore $tmpconfig;
  1246. }
  1247. # now process the start config
  1248. run_command "cp $start_config $output_config" or
  1249. dodie "failed to copy $start_config to $output_config";
  1250. # read directly what we want to check
  1251. my %config_check;
  1252. open (IN, $output_config)
  1253. or dodie "faied to open $output_config";
  1254. while (<IN>) {
  1255. if (/^((CONFIG\S*)=.*)/) {
  1256. $config_check{$2} = $1;
  1257. }
  1258. }
  1259. close(IN);
  1260. # Now run oldconfig with the minconfig (and addconfigs)
  1261. run_command "$defconfig $make oldnoconfig" or
  1262. dodie "failed make config oldconfig";
  1263. # check to see what we lost (or gained)
  1264. open (IN, $output_config)
  1265. or dodie "Failed to read $start_config";
  1266. my %removed_configs;
  1267. my %added_configs;
  1268. while (<IN>) {
  1269. if (/^((CONFIG\S*)=.*)/) {
  1270. # save off all options
  1271. $config_set{$2} = $1;
  1272. if (defined($config_check{$2})) {
  1273. if (defined($config_ignore{$2})) {
  1274. $removed_configs{$2} = $1;
  1275. } else {
  1276. $config_list{$2} = $1;
  1277. }
  1278. } elsif (!defined($config_ignore{$2})) {
  1279. $added_configs{$2} = $1;
  1280. $config_list{$2} = $1;
  1281. }
  1282. }
  1283. }
  1284. close(IN);
  1285. my @confs = keys %removed_configs;
  1286. if ($#confs >= 0) {
  1287. doprint "Configs overridden by default configs and removed from check:\n";
  1288. foreach my $config (@confs) {
  1289. doprint " $config\n";
  1290. }
  1291. }
  1292. @confs = keys %added_configs;
  1293. if ($#confs >= 0) {
  1294. doprint "Configs appearing in make oldconfig and added:\n";
  1295. foreach my $config (@confs) {
  1296. doprint " $config\n";
  1297. }
  1298. }
  1299. my %config_test;
  1300. my $once = 0;
  1301. # Sometimes kconfig does weird things. We must make sure
  1302. # that the config we autocreate has everything we need
  1303. # to test, otherwise we may miss testing configs, or
  1304. # may not be able to create a new config.
  1305. # Here we create a config with everything set.
  1306. create_config (keys %config_list);
  1307. read_current_config \%config_test;
  1308. foreach my $config (keys %config_list) {
  1309. if (!defined($config_test{$config})) {
  1310. if (!$once) {
  1311. $once = 1;
  1312. doprint "Configs not produced by kconfig (will not be checked):\n";
  1313. }
  1314. doprint " $config\n";
  1315. delete $config_list{$config};
  1316. }
  1317. }
  1318. my $ret;
  1319. do {
  1320. $ret = run_config_bisect;
  1321. } while (!$ret);
  1322. return $ret if ($ret < 0);
  1323. success $i;
  1324. }
  1325. sub patchcheck {
  1326. my ($i) = @_;
  1327. die "PATCHCHECK_START[$i] not defined\n"
  1328. if (!defined($opt{"PATCHCHECK_START[$i]"}));
  1329. die "PATCHCHECK_TYPE[$i] not defined\n"
  1330. if (!defined($opt{"PATCHCHECK_TYPE[$i]"}));
  1331. my $start = $opt{"PATCHCHECK_START[$i]"};
  1332. my $end = "HEAD";
  1333. if (defined($opt{"PATCHCHECK_END[$i]"})) {
  1334. $end = $opt{"PATCHCHECK_END[$i]"};
  1335. }
  1336. # Get the true sha1's since we can use things like HEAD~3
  1337. $start = get_sha1($start);
  1338. $end = get_sha1($end);
  1339. my $type = $opt{"PATCHCHECK_TYPE[$i]"};
  1340. # Can't have a test without having a test to run
  1341. if ($type eq "test" && !defined($run_test)) {
  1342. $type = "boot";
  1343. }
  1344. open (IN, "git log --pretty=oneline $end|") or
  1345. dodie "could not get git list";
  1346. my @list;
  1347. while (<IN>) {
  1348. chomp;
  1349. $list[$#list+1] = $_;
  1350. last if (/^$start/);
  1351. }
  1352. close(IN);
  1353. if ($list[$#list] !~ /^$start/) {
  1354. fail "SHA1 $start not found";
  1355. }
  1356. # go backwards in the list
  1357. @list = reverse @list;
  1358. my $save_clean = $noclean;
  1359. $in_patchcheck = 1;
  1360. foreach my $item (@list) {
  1361. my $sha1 = $item;
  1362. $sha1 =~ s/^([[:xdigit:]]+).*/$1/;
  1363. doprint "\nProcessing commit $item\n\n";
  1364. run_command "git checkout $sha1" or
  1365. die "Failed to checkout $sha1";
  1366. # only clean on the first and last patch
  1367. if ($item eq $list[0] ||
  1368. $item eq $list[$#list]) {
  1369. $noclean = $save_clean;
  1370. } else {
  1371. $noclean = 1;
  1372. }
  1373. if (defined($minconfig)) {
  1374. build "useconfig:$minconfig" or return 0;
  1375. } else {
  1376. # ?? no config to use?
  1377. build "oldconfig" or return 0;
  1378. }
  1379. check_buildlog $sha1 or return 0;
  1380. next if ($type eq "build");
  1381. get_grub_index;
  1382. get_version;
  1383. install;
  1384. my $failed = 0;
  1385. start_monitor;
  1386. monitor or $failed = 1;
  1387. if (!$failed && $type ne "boot"){
  1388. do_run_test or $failed = 1;
  1389. }
  1390. end_monitor;
  1391. return 0 if ($failed);
  1392. }
  1393. $in_patchcheck = 0;
  1394. success $i;
  1395. return 1;
  1396. }
  1397. $#ARGV < 1 or die "ktest.pl version: $VERSION\n usage: ktest.pl config-file\n";
  1398. if ($#ARGV == 0) {
  1399. $ktest_config = $ARGV[0];
  1400. if (! -f $ktest_config) {
  1401. print "$ktest_config does not exist.\n";
  1402. my $ans;
  1403. for (;;) {
  1404. print "Create it? [Y/n] ";
  1405. $ans = <STDIN>;
  1406. chomp $ans;
  1407. if ($ans =~ /^\s*$/) {
  1408. $ans = "y";
  1409. }
  1410. last if ($ans =~ /^y$/i || $ans =~ /^n$/i);
  1411. print "Please answer either 'y' or 'n'.\n";
  1412. }
  1413. if ($ans !~ /^y$/i) {
  1414. exit 0;
  1415. }
  1416. }
  1417. } else {
  1418. $ktest_config = "ktest.conf";
  1419. }
  1420. if (! -f $ktest_config) {
  1421. open(OUT, ">$ktest_config") or die "Can not create $ktest_config";
  1422. print OUT << "EOF"
  1423. # Generated by ktest.pl
  1424. #
  1425. # Define each test with TEST_START
  1426. # The config options below it will override the defaults
  1427. TEST_START
  1428. DEFAULTS
  1429. EOF
  1430. ;
  1431. close(OUT);
  1432. }
  1433. read_config $ktest_config;
  1434. # Append any configs entered in manually to the config file.
  1435. my @new_configs = keys %entered_configs;
  1436. if ($#new_configs >= 0) {
  1437. print "\nAppending entered in configs to $ktest_config\n";
  1438. open(OUT, ">>$ktest_config") or die "Can not append to $ktest_config";
  1439. foreach my $config (@new_configs) {
  1440. print OUT "$config = $entered_configs{$config}\n";
  1441. $opt{$config} = $entered_configs{$config};
  1442. }
  1443. }
  1444. if ($opt{"CLEAR_LOG"} && defined($opt{"LOG_FILE"})) {
  1445. unlink $opt{"LOG_FILE"};
  1446. }
  1447. doprint "\n\nSTARTING AUTOMATED TESTS\n\n";
  1448. for (my $i = 0, my $repeat = 1; $i <= $opt{"NUM_TESTS"}; $i += $repeat) {
  1449. if (!$i) {
  1450. doprint "DEFAULT OPTIONS:\n";
  1451. } else {
  1452. doprint "\nTEST $i OPTIONS";
  1453. if (defined($repeat_tests{$i})) {
  1454. $repeat = $repeat_tests{$i};
  1455. doprint " ITERATE $repeat";
  1456. }
  1457. doprint "\n";
  1458. }
  1459. foreach my $option (sort keys %opt) {
  1460. if ($option =~ /\[(\d+)\]$/) {
  1461. next if ($i != $1);
  1462. } else {
  1463. next if ($i);
  1464. }
  1465. doprint "$option = $opt{$option}\n";
  1466. }
  1467. }
  1468. sub set_test_option {
  1469. my ($name, $i) = @_;
  1470. my $option = "$name\[$i\]";
  1471. if (defined($opt{$option})) {
  1472. return $opt{$option};
  1473. }
  1474. foreach my $test (keys %repeat_tests) {
  1475. if ($i >= $test &&
  1476. $i < $test + $repeat_tests{$test}) {
  1477. $option = "$name\[$test\]";
  1478. if (defined($opt{$option})) {
  1479. return $opt{$option};
  1480. }
  1481. }
  1482. }
  1483. if (defined($opt{$name})) {
  1484. return $opt{$name};
  1485. }
  1486. return undef;
  1487. }
  1488. # First we need to do is the builds
  1489. for (my $i = 1; $i <= $opt{"NUM_TESTS"}; $i++) {
  1490. $iteration = $i;
  1491. my $makecmd = set_test_option("MAKE_CMD", $i);
  1492. $machine = set_test_option("MACHINE", $i);
  1493. $ssh_user = set_test_option("SSH_USER", $i);
  1494. $tmpdir = set_test_option("TMP_DIR", $i);
  1495. $outputdir = set_test_option("OUTPUT_DIR", $i);
  1496. $builddir = set_test_option("BUILD_DIR", $i);
  1497. $test_type = set_test_option("TEST_TYPE", $i);
  1498. $build_type = set_test_option("BUILD_TYPE", $i);
  1499. $build_options = set_test_option("BUILD_OPTIONS", $i);
  1500. $power_cycle = set_test_option("POWER_CYCLE", $i);
  1501. $reboot = set_test_option("REBOOT", $i);
  1502. $noclean = set_test_option("BUILD_NOCLEAN", $i);
  1503. $minconfig = set_test_option("MIN_CONFIG", $i);
  1504. $run_test = set_test_option("TEST", $i);
  1505. $addconfig = set_test_option("ADD_CONFIG", $i);
  1506. $reboot_type = set_test_option("REBOOT_TYPE", $i);
  1507. $grub_menu = set_test_option("GRUB_MENU", $i);
  1508. $post_install = set_test_option("POST_INSTALL", $i);
  1509. $reboot_script = set_test_option("REBOOT_SCRIPT", $i);
  1510. $reboot_on_error = set_test_option("REBOOT_ON_ERROR", $i);
  1511. $poweroff_on_error = set_test_option("POWEROFF_ON_ERROR", $i);
  1512. $die_on_failure = set_test_option("DIE_ON_FAILURE", $i);
  1513. $power_off = set_test_option("POWER_OFF", $i);
  1514. $powercycle_after_reboot = set_test_option("POWERCYCLE_AFTER_REBOOT", $i);
  1515. $poweroff_after_halt = set_test_option("POWEROFF_AFTER_HALT", $i);
  1516. $sleep_time = set_test_option("SLEEP_TIME", $i);
  1517. $bisect_sleep_time = set_test_option("BISECT_SLEEP_TIME", $i);
  1518. $store_failures = set_test_option("STORE_FAILURES", $i);
  1519. $timeout = set_test_option("TIMEOUT", $i);
  1520. $booted_timeout = set_test_option("BOOTED_TIMEOUT", $i);
  1521. $console = set_test_option("CONSOLE", $i);
  1522. $success_line = set_test_option("SUCCESS_LINE", $i);
  1523. $stop_after_success = set_test_option("STOP_AFTER_SUCCESS", $i);
  1524. $stop_after_failure = set_test_option("STOP_AFTER_FAILURE", $i);
  1525. $build_target = set_test_option("BUILD_TARGET", $i);
  1526. $ssh_exec = set_test_option("SSH_EXEC", $i);
  1527. $scp_to_target = set_test_option("SCP_TO_TARGET", $i);
  1528. $target_image = set_test_option("TARGET_IMAGE", $i);
  1529. $localversion = set_test_option("LOCALVERSION", $i);
  1530. chdir $builddir || die "can't change directory to $builddir";
  1531. if (!-d $tmpdir) {
  1532. mkpath($tmpdir) or
  1533. die "can't create $tmpdir";
  1534. }
  1535. $ENV{"SSH_USER"} = $ssh_user;
  1536. $ENV{"MACHINE"} = $machine;
  1537. $target = "$ssh_user\@$machine";
  1538. $buildlog = "$tmpdir/buildlog-$machine";
  1539. $dmesg = "$tmpdir/dmesg-$machine";
  1540. $make = "$makecmd O=$outputdir";
  1541. $output_config = "$outputdir/.config";
  1542. if ($reboot_type eq "grub") {
  1543. dodie "GRUB_MENU not defined" if (!defined($grub_menu));
  1544. } elsif (!defined($reboot_script)) {
  1545. dodie "REBOOT_SCRIPT not defined"
  1546. }
  1547. my $run_type = $build_type;
  1548. if ($test_type eq "patchcheck") {
  1549. $run_type = $opt{"PATCHCHECK_TYPE[$i]"};
  1550. } elsif ($test_type eq "bisect") {
  1551. $run_type = $opt{"BISECT_TYPE[$i]"};
  1552. } elsif ($test_type eq "config_bisect") {
  1553. $run_type = $opt{"CONFIG_BISECT_TYPE[$i]"};
  1554. }
  1555. # mistake in config file?
  1556. if (!defined($run_type)) {
  1557. $run_type = "ERROR";
  1558. }
  1559. doprint "\n\n";
  1560. doprint "RUNNING TEST $i of $opt{NUM_TESTS} with option $test_type $run_type\n\n";
  1561. unlink $dmesg;
  1562. unlink $buildlog;
  1563. if (!defined($minconfig)) {
  1564. $minconfig = $addconfig;
  1565. } elsif (defined($addconfig)) {
  1566. run_command "cat $addconfig $minconfig > $tmpdir/add_config" or
  1567. dodie "Failed to create temp config";
  1568. $minconfig = "$tmpdir/add_config";
  1569. }
  1570. my $checkout = $opt{"CHECKOUT[$i]"};
  1571. if (defined($checkout)) {
  1572. run_command "git checkout $checkout" or
  1573. die "failed to checkout $checkout";
  1574. }
  1575. if ($test_type eq "bisect") {
  1576. bisect $i;
  1577. next;
  1578. } elsif ($test_type eq "config_bisect") {
  1579. config_bisect $i;
  1580. next;
  1581. } elsif ($test_type eq "patchcheck") {
  1582. patchcheck $i;
  1583. next;
  1584. }
  1585. if ($build_type ne "nobuild") {
  1586. build $build_type or next;
  1587. }
  1588. if ($test_type ne "build") {
  1589. get_grub_index;
  1590. get_version;
  1591. install;
  1592. my $failed = 0;
  1593. start_monitor;
  1594. monitor or $failed = 1;;
  1595. if (!$failed && $test_type ne "boot" && defined($run_test)) {
  1596. do_run_test or $failed = 1;
  1597. }
  1598. end_monitor;
  1599. next if ($failed);
  1600. }
  1601. success $i;
  1602. }
  1603. if ($opt{"POWEROFF_ON_SUCCESS"}) {
  1604. halt;
  1605. } elsif ($opt{"REBOOT_ON_SUCCESS"} && !do_not_reboot) {
  1606. reboot;
  1607. }
  1608. doprint "\n $successes of $opt{NUM_TESTS} tests were successful\n\n";
  1609. exit 0;