#!/usr/bin/perl use strict; use DBI; use Unix::Syslog qw(:subs :macros); use Getopt::Long; use POSIX qw( setsid strftime); use Time::HiRes qw( gettimeofday tv_interval); use Data::Dumper; my $VERSION = "1.0"; my $sleep = 10; my %opt = (); my @addranks=[]; Getopt::Long::Configure('no_ignore_case'); GetOptions(\%opt, 'help|h', 'version|V', 'host=s', 'verbose|v+', 'u|user=s','p|password=s','h|host=s','dbname=s','s|sleep=i' ) or exit(1); usage() if $opt{help}; if($opt{version}) { print "test dictionary memleak $VERSION by brice+mysql\@daysofwonder.com\n"; exit; } my $dbuser = $opt{u} || "root"; my $dbpass = $opt{p} || ""; my $dbname = $opt{dbname} || "test"; my $host = $opt{h} || 'localhost'; $sleep = defined $opt{sleep} ? $opt{sleep} : $sleep; sub prepare($$) { my $dbh = shift; my $verbose = shift; print "creating : test_rename_tmp\n" if $verbose; $dbh->do( qq{CREATE TABLE IF NOT EXISTS test_rename_tmp ( rank integer not null auto_increment primary key, playerID integer not null default 0, index pid(playerID) ) Engine=InnoDB }); print "creating : test_rename\n" if $verbose; $dbh->do( qq{CREATE TABLE IF NOT EXISTS test_rename ( rank integer not null auto_increment primary key, playerID integer not null default 0, index pid(playerID) ) Engine=InnoDB }); # restart the connection print "truncating : test_rename_tmp if necessary\n" if $verbose; $dbh->do("TRUNCATE TABLE test_rename_tmp"); return 1; # prepared } my $data_source = "DBI:mysql:database=$dbname;host=$host"; my $prepared = 0; my $dbh = undef; my $last_mem = 0; my $last_mem_add = 0; UPPER: while(1) { eval { $dbh->disconnect() if ($dbh); $dbh = undef; }; eval { print "Connection to mysql: $data_source as $dbuser\n" if $opt{verbose}; $dbh = DBI->connect($data_source, $dbuser, $dbpass,{ RaiseError => 1, AutoCommit => 1 } ); $prepared = prepare($dbh, $opt{verbose}) if (!$prepared); }; if ($@) { syslog(LOG_CRIT, "Can't connect to database, sleeping: $data_source, $@"); $prepared = 0; sleep(5); next; } # exchange the table print "renaming : test_rename_tmp to test_rename\n" if $opt{verbose}; eval { $dbh->do("RENAME TABLE test_rename TO test_rename_tmp2 , test_rename_tmp TO test_rename, test_rename_tmp2 TO test_rename_tmp"); print "truncating : test_rename_tmp\n" if $opt{verbose}; $dbh->do("TRUNCATE TABLE test_rename_tmp"); }; if ($@) { syslog(LOG_CRIT, "Error while rotating tables: $@\n"); warn "Error while rotating tables: $@"; $prepared = 0; next; } # fetch size of allocated mem my $status = $dbh->selectrow_arrayref("SHOW INNODB STATUS"); # print Dumper($status); if ($status->[2] =~ /Dictionary cache\s+\d+\s+\((\d+)\s+\+\s+(\d+)\)/ ) { my $mem = $1; my $mem_add_pool = $2; print "consumed: $mem + $mem_add_pool\n"; if ($mem > $last_mem) { print "Dictionary Cache increased since last run: ". ($mem - $last_mem) . "\n"; $last_mem = $mem; } if ($mem_add_pool > $last_mem_add) { print "Dictionary Cache dynamic size increased since last run: ". ($mem_add_pool - $last_mem_add) . "\n"; $last_mem_add = $mem_add_pool; } } eval { $dbh->disconnect(); }; if ($@) { syslog(LOG_CRIT, "Error while tearing down db connection: $@\n"); warn "Error while tearing down db connection: $@"; $prepared = 0; next; } #sleep $sleep; }