You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
7.0 KiB

  1. # -*- cperl -*-
  2. # Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Library General Public
  6. # License as published by the Free Software Foundation; version 2
  7. # of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Library General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. # This is a library file used by the Perl version of mysql-test-run,
  18. # and is part of the translation of the Bourne shell script with the
  19. # same name.
  20. use strict;
  21. use My::Platform;
  22. sub mtr_init_args ($);
  23. sub mtr_add_arg ($$@);
  24. sub mtr_args2str($@);
  25. sub mtr_path_exists(@);
  26. sub mtr_script_exists(@);
  27. sub mtr_file_exists(@);
  28. sub mtr_exe_exists(@);
  29. sub mtr_exe_maybe_exists(@);
  30. sub mtr_compress_file($);
  31. sub mtr_milli_sleep($);
  32. sub start_timer($);
  33. sub has_expired($);
  34. sub init_timers();
  35. sub mark_time_used($);
  36. sub mark_time_idle();
  37. sub add_total_times($);
  38. sub print_times_used($$);
  39. sub print_total_times($);
  40. our $opt_report_times;
  41. ##############################################################################
  42. #
  43. # Args
  44. #
  45. ##############################################################################
  46. sub mtr_init_args ($) {
  47. my $args = shift;
  48. $$args = []; # Empty list
  49. }
  50. sub mtr_add_arg ($$@) {
  51. my $args= shift;
  52. my $format= shift;
  53. my @fargs = @_;
  54. # Quote args if args contain space
  55. $format= "\"$format\""
  56. if (IS_WINDOWS and grep(/\s/, @fargs));
  57. push(@$args, sprintf($format, @fargs));
  58. }
  59. sub mtr_args2str($@) {
  60. my $exe= shift or die;
  61. return join(" ", native_path($exe), @_);
  62. }
  63. ##############################################################################
  64. #
  65. # NOTE! More specific paths should be given before less specific.
  66. # For example /client/debug should be listed before /client
  67. #
  68. sub mtr_path_exists (@) {
  69. foreach my $path ( @_ )
  70. {
  71. return $path if -e $path;
  72. }
  73. if ( @_ == 1 )
  74. {
  75. mtr_error("Could not find $_[0]");
  76. }
  77. else
  78. {
  79. mtr_error("Could not find any of " . join(" ", @_));
  80. }
  81. }
  82. #
  83. # NOTE! More specific paths should be given before less specific.
  84. # For example /client/debug should be listed before /client
  85. #
  86. sub mtr_script_exists (@) {
  87. foreach my $path ( @_ )
  88. {
  89. if(IS_WINDOWS)
  90. {
  91. return $path if -f $path;
  92. }
  93. else
  94. {
  95. return $path if -x $path;
  96. }
  97. }
  98. if ( @_ == 1 )
  99. {
  100. mtr_error("Could not find $_[0]");
  101. }
  102. else
  103. {
  104. mtr_error("Could not find any of " . join(" ", @_));
  105. }
  106. }
  107. #
  108. # NOTE! More specific paths should be given before less specific.
  109. # For example /client/debug should be listed before /client
  110. #
  111. sub mtr_file_exists (@) {
  112. foreach my $path ( @_ )
  113. {
  114. return $path if -e $path;
  115. }
  116. return "";
  117. }
  118. #
  119. # NOTE! More specific paths should be given before less specific.
  120. # For example /client/debug should be listed before /client
  121. #
  122. sub mtr_exe_maybe_exists (@) {
  123. my @path= @_;
  124. map {$_.= ".exe"} @path if IS_WINDOWS;
  125. foreach my $path ( @path )
  126. {
  127. if(IS_WINDOWS)
  128. {
  129. return $path if -f $path;
  130. }
  131. else
  132. {
  133. return $path if -x $path;
  134. }
  135. }
  136. return "";
  137. }
  138. #
  139. # NOTE! More specific paths should be given before less specific.
  140. #
  141. sub mtr_pl_maybe_exists (@) {
  142. my @path= @_;
  143. map {$_.= ".pl"} @path if IS_WINDOWS;
  144. foreach my $path ( @path )
  145. {
  146. if(IS_WINDOWS)
  147. {
  148. return $path if -f $path;
  149. }
  150. else
  151. {
  152. return $path if -x $path;
  153. }
  154. }
  155. return "";
  156. }
  157. #
  158. # NOTE! More specific paths should be given before less specific.
  159. # For example /client/debug should be listed before /client
  160. #
  161. sub mtr_exe_exists (@) {
  162. my @path= @_;
  163. if (my $path= mtr_exe_maybe_exists(@path))
  164. {
  165. return $path;
  166. }
  167. # Could not find exe, show error
  168. if ( @path == 1 )
  169. {
  170. mtr_error("Could not find $path[0]");
  171. }
  172. else
  173. {
  174. mtr_error("Could not find any of " . join(" ", @path));
  175. }
  176. }
  177. #
  178. # Try to compress file using tools that might be available.
  179. # If zip/gzip is not available, just silently ignore.
  180. #
  181. sub mtr_compress_file ($) {
  182. my ($filename)= @_;
  183. mtr_error ("File to compress not found: $filename") unless -f $filename;
  184. my $did_compress= 0;
  185. if (IS_WINDOWS)
  186. {
  187. # Capture stderr
  188. my $ziperr= `zip $filename.zip $filename 2>&1`;
  189. if ($?) {
  190. print "$ziperr\n" if $ziperr !~ /recognized as an internal or external/;
  191. } else {
  192. unlink($filename);
  193. $did_compress=1;
  194. }
  195. }
  196. else
  197. {
  198. my $gzres= system("gzip $filename");
  199. $did_compress= ! $gzres;
  200. if ($gzres && $gzres != -1) {
  201. mtr_error ("Error: have gzip but it fails to compress core file");
  202. }
  203. }
  204. mtr_print("Compressed file $filename") if $did_compress;
  205. }
  206. sub mtr_milli_sleep ($) {
  207. die "usage: mtr_milli_sleep(milliseconds)" unless @_ == 1;
  208. my ($millis)= @_;
  209. select(undef, undef, undef, ($millis/1000));
  210. }
  211. # Simple functions to start and check timers (have to be actively polled)
  212. # Timer can be "killed" by setting it to 0
  213. sub start_timer ($) { return time + $_[0]; }
  214. sub has_expired ($) { return $_[0] && time gt $_[0]; }
  215. # Below code is for time usage reporting
  216. use Time::HiRes qw(gettimeofday);
  217. my %time_used= (
  218. 'collect' => 0,
  219. 'restart' => 0,
  220. 'check' => 0,
  221. 'ch-warn' => 0,
  222. 'test' => 0,
  223. 'init' => 0,
  224. 'admin' => 0,
  225. );
  226. my %time_text= (
  227. 'collect' => "Collecting test cases",
  228. 'restart' => "Server stop/start",
  229. 'check' => "Check-testcase",
  230. 'ch-warn' => "Check for warnings",
  231. 'test' => "Test execution",
  232. 'init' => "Initialization/cleanup",
  233. 'admin' => "Test administration",
  234. );
  235. # Counts number of reports from workers
  236. my $time_totals= 0;
  237. my $last_timer_set;
  238. sub init_timers() {
  239. $last_timer_set= gettimeofday();
  240. }
  241. sub mark_time_used($) {
  242. my ($name)= @_;
  243. return unless $opt_report_times;
  244. die "Unknown timer $name" unless exists $time_used{$name};
  245. my $curr_time= gettimeofday();
  246. $time_used{$name}+= int (($curr_time - $last_timer_set) * 1000 + .5);
  247. $last_timer_set= $curr_time;
  248. }
  249. sub mark_time_idle() {
  250. $last_timer_set= gettimeofday() if $opt_report_times;
  251. }
  252. sub add_total_times($) {
  253. my ($dummy, $num, @line)= split (" ", $_[0]);
  254. $time_totals++;
  255. foreach my $elem (@line) {
  256. my ($name, $spent)= split (":", $elem);
  257. $time_used{$name}+= $spent;
  258. }
  259. }
  260. sub print_times_used($$) {
  261. my ($server, $num)= @_;
  262. return unless $opt_report_times;
  263. my $output= "SPENT $num";
  264. foreach my $name (keys %time_used) {
  265. my $spent= $time_used{$name};
  266. $output.= " $name:$spent";
  267. }
  268. print $server $output . "\n";
  269. }
  270. sub print_total_times($) {
  271. # Don't print if we haven't received all worker data
  272. return if $time_totals != $_[0];
  273. foreach my $name (keys %time_used)
  274. {
  275. my $spent= $time_used{$name}/1000;
  276. my $text= $time_text{$name};
  277. print ("Spent $spent seconds on $text\n");
  278. }
  279. }
  280. 1;