Skip to content

Instantly share code, notes, and snippets.

@vps2fast
Last active February 21, 2021 15:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vps2fast/eb15c7de0dd7ff38a30e to your computer and use it in GitHub Desktop.
Save vps2fast/eb15c7de0dd7ff38a30e to your computer and use it in GitHub Desktop.
Script for analyze pfcache effectiveness
#!/usr/bin/perl
use strict;
use warnings;
# Pavel Odintsov odintsov@fastvps.ee
# License: GPLv2
my @pfcache_cache_contents = `find /vz/pfcache|sed 's#/vz/pfcache##'|sed 's#/##g'`;
chomp @pfcache_cache_contents;
my $pfcache_fast_lookup = {};
for my $hash (@pfcache_cache_contents) {
$pfcache_fast_lookup->{$hash} = 1;
}
# clean up memory
@pfcache_cache_contents = ();
undef @pfcache_cache_contents;
print "We loaded " . (scalar keys %$pfcache_fast_lookup) . " hashes from /vz/pfcache\n";
my $total_memory_save = 0;
my @containers = get_running_containers_list();
# В этот спец хэш мы положим размеры файликов, которые загружены в память хотя бы раз
my $uniq_global_list_of_cached_files = {};
for my $ctid (@containers) {
next if $ctid == 1;
print "Process $ctid\n";
my @processes = `cat /proc/vz/fairsched/$ctid/tasks`;
chomp @processes;
my $container_smaps = {};
for my $p (@processes) {
my @smaps = `cat /proc/$p/smaps 2>/dev/null|grep root|awk '{print \$6}'|sort|uniq`;
chomp @smaps;
for my $smap (@smaps) {
if ($smap =~ /deleted/) {
next;
}
unless (-e $smap) {
next;
}
$container_smaps->{$smap} = 1;
}
}
my $container_memory_save = 0;
my $cached_files_in_container = 0;
# Теперь обойдем все и получим хэши!
for my $mapped_file (keys %$container_smaps) {
my $file_hash = `getfattr -ntrusted.pfcache --only-values $mapped_file 2>/dev/null`;
chomp $file_hash;
my $file_size = get_file_size($mapped_file);
if ($pfcache_fast_lookup->{$file_hash}) {
$cached_files_in_container++;
$container_memory_save += $file_size;
$uniq_global_list_of_cached_files->{$file_hash} = $file_size;
}
}
print "We got " . (scalar keys %$container_smaps) . " uniq memory mapped files\n";
print "We have $cached_files_in_container cached in pfcache file in container\n";
print "We save $container_memory_save bytes of memory in this container\n";
$total_memory_save += $container_memory_save;
}
# Это объем памяти, которйы используется кэшем
my $memory_used_for_cache = 0;
for my $hash (keys %$uniq_global_list_of_cached_files) {
$memory_used_for_cache += $uniq_global_list_of_cached_files->{$hash};
}
my $total_memory_save_in_mb = int($total_memory_save / 1024 / 1024);
my $memory_used_for_cache_in_mb = int($memory_used_for_cache / 1024 / 1024);
my $pfcache_effectiveness = $total_memory_save_in_mb - $memory_used_for_cache_in_mb;
print "Pfcache cache uses $memory_used_for_cache_in_mb MB of memory\n";
print "Total use of pfcached files in containers is $total_memory_save_in_mb MB of memory\n";
print "Pfcache effectiveness: $pfcache_effectiveness MB\n";
# Получить список запущенных контейнеров
sub get_running_containers_list {
my @list_raw = `/usr/sbin/vzlist -1`;
my @list = ();
for my $ct (@list_raw) {
$ct =~ s/^\s+//g;
$ct =~ s/\s+$//g;
push @list, $ct;
}
return @list;
}
# Get file size in bytes
sub get_file_size {
my $path = shift;
my @stat_data = stat $path;
return $stat_data[7];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment