Difference between revisions of "MySQL Memory Usage"

From James Dooley's Wiki
Jump to: navigation, search
(Script)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
[[Category:Misc]]
 
==Overview==
 
==Overview==
  
Line 7: Line 8:
 
==Script==
 
==Script==
  
<code>[perl,n]
+
<source lang='perl'>
    #!/usr/bin/perl -w
+
#!/usr/bin/perl
    #
+
#
    # calculate mysqld memory consumption from my.cnf
+
# calculate mysqld memory consumption from my.cnf
    # usage: mysql-memory-usage /path/to/my.cnf [/path/to/mysqld]
+
# usage: mysql-memory-usage /path/to/my.cnf [/path/to/mysqld]
    #
+
#
    # $Id: mysql-memory-usage 228 2010-05-31 10:53:33Z schwenke $
+
# $Id: mysql-memory-usage 228 2010-05-31 10:53:33Z schwenke $
   
+
 
    my $mycnf= shift;
+
my $mycnf= shift;
    $mycnf= "/etc/my.cnf" unless $mycnf;
+
$mycnf= "/etc/my.cnf" unless $mycnf;
    die "cannot read $mycnf\n" unless (-r $mycnf);
+
die "cannot read $mycnf\n" unless (-r $mycnf);
   
+
 
    my $mysqld= shift;
+
my $mysqld= shift;
    unless ($mysqld) {
+
unless ($mysqld) {
    # find mysqld relative to mysql
+
# find mysqld relative to mysql
    $mysqld= `which mysqld` or $mysqld= `which mysql` or die;
+
$mysqld= `which mysqld` or $mysqld= `which mysql` or die;
    chomp $mysqld;
+
chomp $mysqld;
    $mysqld =~ s(bin/mysql$)(libexec/mysqld);
+
$mysqld =~ s(bin/mysql$)(libexec/mysqld);
    }
+
}
    die "cannot execute $mysqld\n" unless (-x $mysqld);
+
die "cannot execute $mysqld\n" unless (-x $mysqld);
   
+
 
    # read configuration
+
# read configuration
    my %conf;
+
my %conf;
    open M, "$mysqld --defaults-file=$mycnf --help --verbose |" or die;
+
open M, "$mysqld --defaults-file=$mycnf --help --verbose |" or die;
    while (<M>) { # skip to parameter list
+
while (<M>) { # skip to parameter list
    last if (/^\-{5,} \-{5,}$/o);
+
last if (/^\-{5,} \-{5,}$/o);
    }
+
}
    while (<M>) {
+
while (<M>) {
    last if /^$/o;
+
last if /^$/o;
    my ($var, $val)= split;
+
my ($var, $val)= split;
    $conf{$var}= $val;
+
$conf{$var}= $val;
    }
+
}
    close M;
+
close M;
   
+
 
    # static memory consumption
+
# static memory consumption
    my $static= $conf{'key_buffer_size'} + $conf{'query_cache_size'};
+
my $static= $conf{'key_buffer_size'} + $conf{'query_cache_size'};
    # has innodb
+
# has innodb
    $static+= $conf{'innodb-buffer-pool-size'} + $conf{'innodb-log-buffer-size'}
+
$static+= $conf{'innodb-buffer-pool-size'} + $conf{'innodb-log-buffer-size'}
    + $conf{'innodb-additional-mem-pool-size'} if (($conf{'innodb'} eq 'TRUE') or ($conf{'innodb'} eq 'ON'));
+
+ $conf{'innodb-additional-mem-pool-size'} if (($conf{'innodb'} eq 'TRUE') or ($conf{'innodb'} eq 'ON'));
   
+
 
    # per thread memory consumption
+
# per thread memory consumption
    my $thread= $conf{'thread_stack'}
+
my $thread= $conf{'thread_stack'}
    + 2*$conf{'net_buffer_length'} + $conf{'read_buffer_size'}
+
+ 2*$conf{'net_buffer_length'} + $conf{'read_buffer_size'}
    + $conf{'read_rnd_buffer_size'} + $conf{'sort_buffer_size'};
+
+ $conf{'read_rnd_buffer_size'} + $conf{'sort_buffer_size'};
   
+
 
    # writes binlog
+
# writes binlog
    $thread += $conf{'binlog_cache_size'} if ($conf{'log-bin'} ne '(No');
+
$thread += $conf{'binlog_cache_size'} if ($conf{'log-bin'} ne '(No');
   
+
 
    # MyISAM repair thread(s)
+
# MyISAM repair thread(s)
    my $repair= 0;
+
my $repair= 0;
    $repair+= $conf{'myisam_repair_threads'} * $conf{'myisam_sort_buffer_size'}
+
$repair+= $conf{'myisam_repair_threads'} * $conf{'myisam_sort_buffer_size'}
    if ($conf{'myisam-recover'} ne 'OFF');
+
if ($conf{'myisam-recover'} ne 'OFF');
   
+
 
    # Memory (HEAP) tables
+
# Memory (HEAP) tables
    my $heaptables= $conf{'max_heap_table_size'};
+
my $heaptables= $conf{'max_heap_table_size'};
   
+
 
    # temp. tables
+
# temp. tables
    my $tmptables= min($conf{'tmp_table_size'}, $conf{'max_heap_table_size'});
+
my $tmptables= min($conf{'tmp_table_size'}, $conf{'max_heap_table_size'});
   
+
 
    #
+
#
    # show result
+
# show result
    #
+
#
    print "$mysqld will use at most:\n";
+
print "$mysqld will use at most:\n";
    printf "%-10s for global stuff\n", p($static);
+
printf "%-10s for global stuff\n", p($static);
    printf "%-10s per thread\n", p($thread);
+
printf "%-10s per thread\n", p($thread);
    printf "%-10s total (with %d active threads)\n",
+
printf "%-10s total (with %d active threads)\n", p($static + $conf{'max_connections'}*$thread), $conf{'max_connections'};
    p($static + $conf{'max_connections'}*$thread), $conf{'max_connections'};
+
print "and additionally:\n";
    print "and additionally:\n";
+
printf "%-10s while recovering MyISAM tables\n", p($repair) if ($repair);
    printf "%-10s while recovering MyISAM tables\n", p($repair) if ($repair);
+
printf "%-10s for each HEAP table\n", p($heaptables);
    printf "%-10s for each HEAP table\n", p($heaptables);
+
printf "%-10s for each temporary table\n", p($tmptables);
    printf "%-10s for each temporary table\n", p($tmptables);
+
exit 0;
    exit 0;
+
 
   
+
 
   
+
sub min {
    sub min {
+
my ($a, $b)= @_;
    my ($a, $b)= @_;
+
return $a <$b ? $a : $b;
    return $a <$b ? $a : $b;
+
}
    }
+
 
   
+
sub p {
    sub p {
+
my $n= shift;
    my $n= shift;
+
my $prefix= 0;
    my $prefix= 0;
+
while ($n >= 1024 && $prefix < 5) {
    while ($n >= 1024 && $prefix < 5) {
+
$n/= 1024;
    $n/= 1024;
+
$prefix++;
    $prefix++;
+
}
    }
+
sprintf $prefix?"%.1f %s":"%d %s", $n, qw"Bytes KB MB GB TB"[$prefix];
    sprintf $prefix?"%.1f %s":"%d %s", $n, qw"Bytes KB MB GB TB"[$prefix];
+
}
    }
+
</source>
</code>
 
  
 
==Usage==
 
==Usage==
Line 101: Line 101:
 
Direct download and execution of script (auto mode)
 
Direct download and execution of script (auto mode)
  
<code>[bash,n]wget http://wiki.jamesdooley.us/scripts/sqlmem -O - | perl</code>
+
<source lang='bash'>wget http://svn.jamesdooley.us/svn/JDooley/MySQLMemoryUsage/sqlmem.pl -O - | perl</source>
  
  
 
Default
 
Default
  
<code>[bash,n]perl sqlmem.pl</code>
+
<source lang='bash'>perl sqlmem.pl</source>
  
 
Defaults will use the cpanel default MySQL locations.
 
Defaults will use the cpanel default MySQL locations.
Line 112: Line 112:
 
Specify paths
 
Specify paths
  
<code>[bash,n]perl sqlmem.pl [/path/to/my.cnf] [/path/to/mysqld]</code>
+
<source lang='bash'>perl sqlmem.pl [/path/to/my.cnf] [/path/to/mysqld]</source>
  
 
==Credits==
 
==Credits==

Latest revision as of 18:00, 8 July 2014

Overview

Perl script to calculate MySQL max memory allotment.

Modified original code to search for cPanel defaults.

Script

#!/usr/bin/perl
#
# calculate mysqld memory consumption from my.cnf
# usage: mysql-memory-usage /path/to/my.cnf [/path/to/mysqld]
#
# $Id: mysql-memory-usage 228 2010-05-31 10:53:33Z schwenke $

my $mycnf= shift;
$mycnf= "/etc/my.cnf" unless $mycnf;
die "cannot read $mycnf\n" unless (-r $mycnf);

my $mysqld= shift;
unless ($mysqld) {
	# find mysqld relative to mysql
	$mysqld= `which mysqld` or $mysqld= `which mysql` or die;
	chomp $mysqld;
	$mysqld =~ s(bin/mysql$)(libexec/mysqld);
}
die "cannot execute $mysqld\n" unless (-x $mysqld);

# read configuration
my %conf;
open M, "$mysqld --defaults-file=$mycnf --help --verbose |" or die;
while (<M>) { # skip to parameter list
	last if (/^\-{5,} \-{5,}$/o);
}
while (<M>) {
	last if /^$/o;
	my ($var, $val)= split;
	$conf{$var}= $val;
}
close M;

# static memory consumption
my $static= $conf{'key_buffer_size'} + $conf{'query_cache_size'};
# has innodb
$static+= $conf{'innodb-buffer-pool-size'} + $conf{'innodb-log-buffer-size'}
+ $conf{'innodb-additional-mem-pool-size'} if (($conf{'innodb'} eq 'TRUE') or ($conf{'innodb'} eq 'ON'));

# per thread memory consumption
my $thread= $conf{'thread_stack'}
+ 2*$conf{'net_buffer_length'} + $conf{'read_buffer_size'}
+ $conf{'read_rnd_buffer_size'} + $conf{'sort_buffer_size'};

# writes binlog
$thread += $conf{'binlog_cache_size'} if ($conf{'log-bin'} ne '(No');

# MyISAM repair thread(s)
my $repair= 0;
$repair+= $conf{'myisam_repair_threads'} * $conf{'myisam_sort_buffer_size'}
if ($conf{'myisam-recover'} ne 'OFF');

# Memory (HEAP) tables
my $heaptables= $conf{'max_heap_table_size'};

# temp. tables
my $tmptables= min($conf{'tmp_table_size'}, $conf{'max_heap_table_size'});

#
# show result
#
print "$mysqld will use at most:\n";
printf "%-10s for global stuff\n", p($static);
printf "%-10s per thread\n", p($thread);
printf "%-10s total (with %d active threads)\n", p($static + $conf{'max_connections'}*$thread), $conf{'max_connections'};
print "and additionally:\n";
printf "%-10s while recovering MyISAM tables\n", p($repair) if ($repair);
printf "%-10s for each HEAP table\n", p($heaptables);
printf "%-10s for each temporary table\n", p($tmptables);
exit 0;


sub min {
	my ($a, $b)= @_;
	return $a <$b ? $a : $b;
}

sub p {
	my $n= shift;
	my $prefix= 0;
	while ($n >= 1024 && $prefix < 5) {
		$n/= 1024;
		$prefix++;
	}
	sprintf $prefix?"%.1f %s":"%d %s", $n, qw"Bytes KB MB GB TB"[$prefix];
}

Usage

Direct download and execution of script (auto mode)

wget http://svn.jamesdooley.us/svn/JDooley/MySQLMemoryUsage/sqlmem.pl -O - | perl


Default

perl sqlmem.pl

Defaults will use the cpanel default MySQL locations.

Specify paths

perl sqlmem.pl [/path/to/my.cnf] [/path/to/mysqld]

Credits

Original code created by:

Axel Schwenke:

http://forge.mysql.com/tools/tool.php?id=266