ktest.pl 45 KB

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