#!/usr/bin/perl -w # # command_summary - Prints a summary of MySQL commands executed # # USAGE: command_summary # # FIELDS: # Command MySQL Command name # seconds Time taken by MySQL command # calls Number of samples # errors Number of errors executing command # # Author: Neelakanth Nadgir (Based off hotuser script by Brendan Gregg) # # 19-May-2009 Neelakanth Nadgir Created this. # use strict; use Getopt::Std; sub cleanupsig { } $SIG{INT} = \&cleanupsig; # Ctrl-C $SIG{QUIT} = \&cleanupsig; # Ctrl-\ $SIG{TERM} = \&cleanupsig; # TERM # Declare DTrace script # my $dtrace = <cmd = arg1; self->start = timestamp; } mysqld*::command-done /self->cmd/ { \@command[cmd[self->cmd]] = count(); \@elap[cmd[self->cmd]] = sum((timestamp - self->start)/1000000); \@status[cmd[self->cmd],arg0] = count(); } END { printa("COUNT %-30s %\@12d\\n", \@command); printa("TIME %-30s %\@12d\\n", \@elap); printa("STATUS %-30s %d %\@12d\\n", \@status); } ' ENDDTRACE my %count; my %tme; my %err; my $tc = 0; my $tt = 0; my $te = 0; open DTRACE, "$dtrace |" or die "ERROR1: Can't run dtrace (perms?): $!\n"; print "Sampling... Hit Ctrl-C to end.\n"; while (my $line = ) { next if $line =~ /^\s*$/; my ($tag, $cmd, $val) = split ' ', $line; if ($line =~ /^COUNT/) { $count{$cmd}=$val; $tc += $val; } if ($line =~ /^TIME/) { $tme{$cmd}=$val; $tt += $val; } if ($line =~ /^STATUS/) { my ($tag, $cmd, $stat, $val) = split ' ', $line; $err{$cmd} += ($stat != 0) ? $val : 0; $te += ($stat != 0) ? $val : 0; } } close DTRACE; printf("\nCommand seconds calls errors\n"); foreach my $k (sort { $count{$a} <=> $count{$b} } keys %count) { printf("%-19.19s %12.3f %9d %7s\n", $k, $tme{$k}/1000.0, $count{$k}, $err{$k} == 0? "": $err{$k}); } printf("%-19.19s %12s %9s %7s\n"," ","-------", "-----", "------"); printf("%-19.19s %12s %9s %7s\n", "total:", $tt/1000.0, $tc,$te);