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.

193 lines
6.4 KiB

make_win_bin_dist CMakeLists.txt resolveip.c Makefile.am: Better Windows support in the scripts directory mysql_config.pl.in, mysql_install_db.pl.in: New Perl version of Unix shell script, mainly for Windows Many files in scripts directory: Use default Perl location "#!/usr/bin/perl" instead of the build host path extra/CMakeLists.txt: Added target for executable "resolveip" extra/resolveip.c: Exclude Unix specific headers when compiling on Windows scripts/CMakeLists.txt: On Windows, filter Perl scripts and change name from ".sh" to ".pl" mysql_convert_table_format.sh mysql_explain_log.sh mysql_secure_installation.sh mysql_tableinfo.sh mysqld_multi.sh mysqldumpslow.sh mysqlhotcopy.sh Do the same for the new Windows specific Perl versions of shell scripts mysql_config.pl.in mysql_install_db.pl.in In CMake, set reasonable values for 'CFLAGS', 'prefix', 'datadir' and so on. scripts/Makefile.am: Include "mysql_config.pl.in" and "mysql_install_db.pl.in" in the source TAR scripts/make_win_bin_dist: Only include explicitly listed scripts from the "scripts" directory scripts/mysql_convert_table_format.sh: Use default Perl location "#!/usr/bin/perl" instead of the build host path scripts/mysql_explain_log.sh: Use default Perl location "#!/usr/bin/perl" instead of the build host path scripts/mysql_tableinfo.sh: Use default Perl location "#!/usr/bin/perl" instead of the build host path scripts/mysqld_multi.sh: Use default Perl location "#!/usr/bin/perl" instead of the build host path scripts/mysqldumpslow.sh: Use default Perl location "#!/usr/bin/perl" instead of the build host path scripts/mysqlhotcopy.sh: Use default Perl location "#!/usr/bin/perl" instead of the build host path scripts/mysql_config.pl.in: New Perl version of Unix shell script, mainly for Windows scripts/mysql_install_db.pl.in: New Perl version of Unix shell script, mainly for Windows
18 years ago
  1. #!/usr/bin/perl
  2. # mysqldumpslow - parse and summarize the MySQL slow query log
  3. # Original version by Tim Bunce, sometime in 2000.
  4. # Further changes by Tim Bunce, 8th March 2001.
  5. # Handling of strings with \ and double '' by Monty 11 Aug 2001.
  6. use strict;
  7. use Getopt::Long;
  8. # t=time, l=lock time, r=rows
  9. # at, al, and ar are the corresponding averages
  10. my %opt = (
  11. s => 'at',
  12. h => '*',
  13. );
  14. GetOptions(\%opt,
  15. 'v|verbose+',# verbose
  16. 'help+', # write usage info
  17. 'd|debug+', # debug
  18. 's=s', # what to sort by (al, at, ar, c, t, l, r)
  19. 'r!', # reverse the sort order (largest last instead of first)
  20. 't=i', # just show the top n queries
  21. 'a!', # don't abstract all numbers to N and strings to 'S'
  22. 'n=i', # abstract numbers with at least n digits within names
  23. 'g=s', # grep: only consider stmts that include this string
  24. 'h=s', # hostname of db server for *-slow.log filename (can be wildcard)
  25. 'i=s', # name of server instance (if using mysql.server startup script)
  26. 'l!', # don't subtract lock time from total time
  27. ) or usage("bad option");
  28. $opt{'help'} and usage();
  29. unless (@ARGV) {
  30. my $defaults = `my_print_defaults mysqld`;
  31. my $basedir = ($defaults =~ m/--basedir=(.*)/)[0]
  32. or die "Can't determine basedir from 'my_print_defaults mysqld' output: $defaults";
  33. warn "basedir=$basedir\n" if $opt{v};
  34. my $datadir = ($defaults =~ m/--datadir=(.*)/)[0];
  35. my $slowlog = ($defaults =~ m/--log-slow-queries=(.*)/)[0];
  36. if (!$datadir or $opt{i}) {
  37. # determine the datadir from the instances section of /etc/my.cnf, if any
  38. my $instances = `my_print_defaults instances`;
  39. die "Can't determine datadir from 'my_print_defaults mysqld' output: $defaults"
  40. unless $instances;
  41. my @instances = ($instances =~ m/^--(\w+)-/mg);
  42. die "No -i 'instance_name' specified to select among known instances: @instances.\n"
  43. unless $opt{i};
  44. die "Instance '$opt{i}' is unknown (known instances: @instances)\n"
  45. unless grep { $_ eq $opt{i} } @instances;
  46. $datadir = ($instances =~ m/--$opt{i}-datadir=(.*)/)[0]
  47. or die "Can't determine --$opt{i}-datadir from 'my_print_defaults instances' output: $instances";
  48. warn "datadir=$datadir\n" if $opt{v};
  49. }
  50. if ( -f $slowlog ) {
  51. @ARGV = ($slowlog);
  52. die "Can't find '$slowlog'\n" unless @ARGV;
  53. } else {
  54. @ARGV = <$datadir/$opt{h}-slow.log>;
  55. die "Can't find '$datadir/$opt{h}-slow.log'\n" unless @ARGV;
  56. }
  57. }
  58. warn "\nReading mysql slow query log from @ARGV\n";
  59. my @pending;
  60. my %stmt;
  61. $/ = ";\n#"; # read entire statements using paragraph mode
  62. while ( defined($_ = shift @pending) or defined($_ = <>) ) {
  63. warn "[[$_]]\n" if $opt{d}; # show raw paragraph being read
  64. my @chunks = split /^\/.*Version.*started with[\000-\377]*?Time.*Id.*Command.*Argument.*\n/m;
  65. if (@chunks > 1) {
  66. unshift @pending, map { length($_) ? $_ : () } @chunks;
  67. warn "<<".join(">>\n<<",@chunks).">>" if $opt{d};
  68. next;
  69. }
  70. s/^#? Time: \d{6}\s+\d+:\d+:\d+.*\n//;
  71. my ($user,$host) = s/^#? User\@Host:\s+(\S+)\s+\@\s+(\S+).*\n// ? ($1,$2) : ('','');
  72. s/^# Query_time: ([0-9.]+)\s+Lock_time: ([0-9.]+)\s+Rows_sent: ([0-9.]+).*\n//;
  73. my ($t, $l, $r) = ($1, $2, $3);
  74. $t -= $l unless $opt{l};
  75. # remove fluff that mysqld writes to log when it (re)starts:
  76. s!^/.*Version.*started with:.*\n!!mg;
  77. s!^Tcp port: \d+ Unix socket: \S+\n!!mg;
  78. s!^Time.*Id.*Command.*Argument.*\n!!mg;
  79. s/^use \w+;\n//; # not consistently added
  80. s/^SET timestamp=\d+;\n//;
  81. s/^[ ]*\n//mg; # delete blank lines
  82. s/^[ ]*/ /mg; # normalize leading whitespace
  83. s/\s*;\s*(#\s*)?$//; # remove trailing semicolon(+newline-hash)
  84. next if $opt{g} and !m/$opt{g}/io;
  85. unless ($opt{a}) {
  86. s/\b\d+\b/N/g;
  87. s/\b0x[0-9A-Fa-f]+\b/N/g;
  88. s/''/'S'/g;
  89. s/""/"S"/g;
  90. s/(\\')//g;
  91. s/(\\")//g;
  92. s/'[^']+'/'S'/g;
  93. s/"[^"]+"/"S"/g;
  94. # -n=8: turn log_20001231 into log_NNNNNNNN
  95. s/([a-z_]+)(\d{$opt{n},})/$1.('N' x length($2))/ieg if $opt{n};
  96. # abbreviate massive "in (...)" statements and similar
  97. s!(([NS],){100,})!sprintf("$2,{repeated %d times}",length($1)/2)!eg;
  98. }
  99. my $s = $stmt{$_} ||= { users=>{}, hosts=>{} };
  100. $s->{c} += 1;
  101. $s->{t} += $t;
  102. $s->{l} += $l;
  103. $s->{r} += $r;
  104. $s->{users}->{$user}++ if $user;
  105. $s->{hosts}->{$host}++ if $host;
  106. warn "{{$_}}\n\n" if $opt{d}; # show processed statement string
  107. }
  108. foreach (keys %stmt) {
  109. my $v = $stmt{$_} || die;
  110. my ($c, $t, $l, $r) = @{ $v }{qw(c t l r)};
  111. $v->{at} = $t / $c;
  112. $v->{al} = $l / $c;
  113. $v->{ar} = $r / $c;
  114. }
  115. my @sorted = sort { $stmt{$b}->{$opt{s}} <=> $stmt{$a}->{$opt{s}} } keys %stmt;
  116. @sorted = @sorted[0 .. $opt{t}-1] if $opt{t};
  117. @sorted = reverse @sorted if $opt{r};
  118. foreach (@sorted) {
  119. my $v = $stmt{$_} || die;
  120. my ($c, $t,$at, $l,$al, $r,$ar) = @{ $v }{qw(c t at l al r ar)};
  121. my @users = keys %{$v->{users}};
  122. my $user = (@users==1) ? $users[0] : sprintf "%dusers",scalar @users;
  123. my @hosts = keys %{$v->{hosts}};
  124. my $host = (@hosts==1) ? $hosts[0] : sprintf "%dhosts",scalar @hosts;
  125. printf "Count: %d Time=%.2fs (%ds) Lock=%.2fs (%ds) Rows=%.1f (%d), $user\@$host\n%s\n\n",
  126. $c, $at,$t, $al,$l, $ar,$r, $_;
  127. }
  128. sub usage {
  129. my $str= shift;
  130. my $text= <<HERE;
  131. Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]
  132. Parse and summarize the MySQL slow query log. Options are
  133. --verbose verbose
  134. --debug debug
  135. --help write this text to standard output
  136. -v verbose
  137. -d debug
  138. -s ORDER what to sort by (al, at, ar, c, l, r, t), 'at' is default
  139. al: average lock time
  140. ar: average rows sent
  141. at: average query time
  142. c: count
  143. l: lock time
  144. r: rows sent
  145. t: query time
  146. -r reverse the sort order (largest last instead of first)
  147. -t NUM just show the top n queries
  148. -a don't abstract all numbers to N and strings to 'S'
  149. -n NUM abstract numbers with at least n digits within names
  150. -g PATTERN grep: only consider stmts that include this string
  151. -h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard),
  152. default is '*', i.e. match all
  153. -i NAME name of server instance (if using mysql.server startup script)
  154. -l don't subtract lock time from total time
  155. HERE
  156. if ($str) {
  157. print STDERR "ERROR: $str\n\n";
  158. print STDERR $text;
  159. exit 1;
  160. } else {
  161. print $text;
  162. exit 0;
  163. }
  164. }