~percona-toolkit-dev/percona-toolkit/pqd-enhanced-resume-file

503.6.1 by Daniel Nichter
s/Percona Inc/Percona Ireland Ltd/g
1
# This program is copyright 2007-2011 Baron Schwartz, 2011 Percona Ireland Ltd.
2 by Daniel Nichter
Add lib/, t/lib/, and sandbox/. All modules are updated and passing on MySQL 5.1.
2
# Feedback and improvements are welcome.
3
#
4
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
5
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
6
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
7
#
8
# This program is free software; you can redistribute it and/or modify it under
9
# the terms of the GNU General Public License as published by the Free Software
10
# Foundation, version 2; OR the Perl Artistic License.  On UNIX and similar
11
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
12
# licenses.
13
#
14
# You should have received a copy of the GNU General Public License along with
15
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16
# Place, Suite 330, Boston, MA  02111-1307  USA.
17
# ###########################################################################
18 by Daniel Nichter
Remove $Revision$ and finish re-branding modules. Rename MaatkitTest.pm to PerconaTest.pm. Put copyrights on one line.
18
# SlowLogWriter package
2 by Daniel Nichter
Add lib/, t/lib/, and sandbox/. All modules are updated and passing on MySQL 5.1.
19
# ###########################################################################
9 by Daniel Nichter
Move module docu to work for NaturalDocs.
20
{
2 by Daniel Nichter
Add lib/, t/lib/, and sandbox/. All modules are updated and passing on MySQL 5.1.
21
# Package: SlowLogWriter
22
# SlowLogWriter writes events to a file in MySQL slow log format.
23
package SlowLogWriter;
24
25
use strict;
26
use warnings FATAL => 'all';
27
use English qw(-no_match_vars);
134 by Daniel Nichter
Replace MKDEBUG with PTDEBUG in modules.
28
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
2 by Daniel Nichter
Add lib/, t/lib/, and sandbox/. All modules are updated and passing on MySQL 5.1.
29
30
sub new {
31
   my ( $class ) = @_;
32
   bless {}, $class;
33
}
34
35
# Print out in slow-log format.
36
sub write {
37
   my ( $self, $fh, $event ) = @_;
38
   if ( $event->{ts} ) {
39
      print $fh "# Time: $event->{ts}\n";
40
   }
41
   if ( $event->{user} ) {
42
      printf $fh "# User\@Host: %s[%s] \@ %s []\n",
43
         $event->{user}, $event->{user}, $event->{host};
44
   }
45
   if ( $event->{ip} && $event->{port} ) {
46
      printf $fh "# Client: $event->{ip}:$event->{port}\n";
47
   }
48
   if ( $event->{Thread_id} ) {
49
      printf $fh "# Thread_id: $event->{Thread_id}\n";
50
   }
51
52
   # Tweak output according to log type: either classic or Percona-patched.
53
   my $percona_patched = exists $event->{QC_Hit} ? 1 : 0;
54
55
   # Classic slow log attribs.
56
   printf $fh
57
      "# Query_time: %.6f  Lock_time: %.6f  Rows_sent: %d  Rows_examined: %d\n",
58
      # TODO 0  Rows_affected: 0  Rows_read: 1
59
      map { $_ || 0 }
60
         @{$event}{qw(Query_time Lock_time Rows_sent Rows_examined)};
61
62
   if ( $percona_patched ) {
63
      # First 2 lines of Percona-patched attribs.
64
      printf $fh
219.2.1 by baron at percona
Update samples and tests for SlowLogWriter.pm and SlowLogParser.pm for bug 963225
65
         "# QC_Hit: %s  Full_scan: %s  Full_join: %s  Tmp_table: %s  Tmp_table_on_disk: %s\n# Filesort: %s  Filesort_on_disk: %s  Merge_passes: %d\n",
2 by Daniel Nichter
Add lib/, t/lib/, and sandbox/. All modules are updated and passing on MySQL 5.1.
66
         map { $_ || 0 }
219.2.1 by baron at percona
Update samples and tests for SlowLogWriter.pm and SlowLogParser.pm for bug 963225
67
            @{$event}{qw(QC_Hit Full_scan Full_join Tmp_table Tmp_table_on_disk Filesort Filesort_on_disk Merge_passes)};
2 by Daniel Nichter
Add lib/, t/lib/, and sandbox/. All modules are updated and passing on MySQL 5.1.
68
69
      if ( exists $event->{InnoDB_IO_r_ops} ) {
70
         # Optional 3 lines of Percona-patched InnoDB attribs.
71
         printf $fh
72
            "#   InnoDB_IO_r_ops: %d  InnoDB_IO_r_bytes: %d  InnoDB_IO_r_wait: %s\n#   InnoDB_rec_lock_wait: %s  InnoDB_queue_wait: %s\n#   InnoDB_pages_distinct: %d\n",
73
            map { $_ || 0 }
74
               @{$event}{qw(InnoDB_IO_r_ops InnoDB_IO_r_bytes InnoDB_IO_r_wait InnoDB_rec_lock_wait InnoDB_queue_wait InnoDB_pages_distinct)};
75
76
      } 
77
      else {
78
         printf $fh "# No InnoDB statistics available for this query\n";
79
      }
80
   }
81
82
   if ( $event->{db} ) {
83
      printf $fh "use %s;\n", $event->{db};
84
   }
85
   if ( $event->{arg} =~ m/^administrator command/ ) {
86
      print $fh '# ';
87
   }
88
   print $fh $event->{arg}, ";\n";
89
90
   return;
91
}
92
93
sub _d {
94
   my ($package, undef, $line) = caller 0;
95
   @_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
96
        map { defined $_ ? $_ : 'undef' }
97
        @_;
98
   print STDERR "# $package:$line $PID ", join(' ', @_), "\n";
99
}
100
101
1;
102
}
103
# ###########################################################################
104
# End SlowLogWriter package
105
# ###########################################################################