From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id AD30F1FB0F for ; Tue, 5 Oct 2021 12:48:04 +0000 (UTC) From: Eric Wong To: mwrap-perl@80x24.org Subject: [PATCH 5/7] psgi: add total bytes + reset Date: Tue, 5 Oct 2021 12:48:01 +0000 Message-Id: <20211005124803.7215-6-e@80x24.org> In-Reply-To: <20211005124803.7215-1-e@80x24.org> References: <20211005124803.7215-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: These will probably be more useful, we'll drop total bytes allocated, for now. --- Mwrap.xs | 15 ++++++++++++ lib/Devel/Mwrap/PSGI.pm | 54 ++++++++++++++++++++++++----------------- 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/Mwrap.xs b/Mwrap.xs index b93efd6..43d1ed6 100644 --- a/Mwrap.xs +++ b/Mwrap.xs @@ -202,6 +202,7 @@ static char *int2str(unsigned num, char *dst, size_t * size) struct src_loc { pthread_mutex_t *mtx; size_t total; + size_t freed_bytes; size_t allocations; size_t frees; size_t age_total; /* (age_total / frees) => mean age at free */ @@ -402,6 +403,7 @@ void free(void *p) size_t current_bytes = uatomic_read(&total_bytes_inc); size_t age = current_bytes - h->as.live.gen; uatomic_add(&total_bytes_dec, h->size); + uatomic_add(&l->freed_bytes, h->size); uatomic_set(&h->size, 0); uatomic_add(&l->frees, 1); uatomic_add(&l->age_total, age); @@ -889,6 +891,7 @@ CODE: uatomic_set(&l->total, 0); uatomic_set(&l->allocations, 0); uatomic_set(&l->frees, 0); + uatomic_set(&l->freed_bytes, 0); uatomic_set(&l->age_total, 0); uatomic_set(&l->max_lifespan, 0); } @@ -960,6 +963,18 @@ OUTPUT: CLEANUP: --locating; +size_t +src_loc_freed_bytes(self) + Devel::Mwrap::SrcLoc self +PREINIT: +CODE: + ++locating; + RETVAL = uatomic_read(&self->freed_bytes); +OUTPUT: + RETVAL +CLEANUP: + --locating; + size_t src_loc_allocations(self) Devel::Mwrap::SrcLoc self diff --git a/lib/Devel/Mwrap/PSGI.pm b/lib/Devel/Mwrap/PSGI.pm index f47c9b0..5bb7c36 100644 --- a/lib/Devel/Mwrap/PSGI.pm +++ b/lib/Devel/Mwrap/PSGI.pm @@ -39,14 +39,16 @@ sub uri_unescape { $str; } -my @FIELDS = qw(total allocations frees live mean_life max_life location); +my @FIELDS = qw(bytes allocations frees live + mean_life max_life location); my $HDR = '' . join('', @FIELDS) . ''; sub accumulate_i { # callback for Devel::Mwrap::each my ($all, $src_loc) = @_; my $alloc = $src_loc->allocations; my $frees = $src_loc->frees; - push @$all, [ $src_loc->total, $alloc, $frees, $alloc - $frees, + push @$all, [ $src_loc->total - $src_loc->freed_bytes, + $alloc, $frees, $alloc - $frees, $src_loc->mean_lifespan, $src_loc->max_lifespan, $src_loc->name ]; } @@ -65,9 +67,10 @@ sub fh_response { } sub each_gt { - my ($env, $min, $sort) = @_; + my ($env, $min) = @_; + my ($sort) = ($env->{QUERY_STRING} =~ /\bsort=(\w+)\b/a); open my $fh, '+>', undef or die "open: $!"; - $sort //= 'total'; + $sort //= 'bytes'; my $sn = $env->{SCRIPT_NAME}; my $t = "Devel::Mwrap::each($min)"; my $all = []; @@ -93,12 +96,12 @@ sub each_gt { my $age = Devel::Mwrap::current_age(); my $live = $age - Devel::Mwrap::total_bytes_freed(); print $fh <$t

$t

-

Current age: $age (live: $live)

+$t

$t +

Current age: $age (live: $live) EOM while (my $cols = shift @all) { - # cols: [ total, allocations, frees, mean_lifespan, + # cols: [ bytes , allocations, frees, mean_lifespan, # max_lifespan, name ] my $loc_name = pop @$cols; $cols->[4] = sprintf('%0.3f', $cols->[4]); # mean_life @@ -125,8 +128,8 @@ sub each_at { my $age = Devel::Mwrap::current_age(); my $live = $age - Devel::Mwrap::total_bytes_freed(); print $fh <$t

live allocations at $t

-

Current age: $age (live: $live)

\n
@f
+$t

live allocations at $t +

Current age: $age (live: $live)\n

EOM $src_loc->each(0, \&each_at_i, $fh); @@ -151,21 +154,28 @@ EOM sub call { # PSGI entry point Devel::Mwrap::quiet(1); my (undef, $env) = @_; - my $path_info = $env->{PATH_INFO}; my $ret; - if ($path_info =~ m!\A/each/([0-9]+)\z!) { - my $min = $1 + 0; - my ($sort) = ($env->{QUERY_STRING} =~ /\bsort=(\w+)\b/a); - $ret = each_gt($env, $min, $sort); - } elsif ($path_info =~ m!\A/at/(.*)\z!) { - my $src_loc = Devel::Mwrap::get(uri_unescape($1)); - $ret = $src_loc ? each_at($env, $src_loc) : r404(); - } elsif ($path_info eq '/') { - $ret = [ 200, [ qw(Content-Type text/html - Content-Length), length($root) ], [ $root ] ] - } else { - $ret = r404(); + my $path_info = $env->{PATH_INFO}; + if ($env->{REQUEST_METHOD} eq 'GET') { + if ($path_info =~ m!\A/each/([0-9]+)\z!) { + $ret = each_gt($env, $1 + 0); + } elsif ($path_info =~ m!\A/at/(.*)\z!) { + my $src_loc = Devel::Mwrap::get(uri_unescape($1)); + $ret = $src_loc ? each_at($env, $src_loc) : r404(); + } elsif ($path_info eq '/') { + $ret = [ 200, [ qw(Content-Type text/html + Content-Length), length($root) ], + [ $root ] ] + } + } elsif ($env->{REQUEST_METHOD} eq 'POST') { + if ($path_info eq '/reset') { + Devel::Mwrap::reset(); + $ret = [ 200, [ qw(Content-Type text/html + Content-Length 5) ], + [ "done\n" ] ]; + } } + $ret //= r404(); Devel::Mwrap::quiet(0); $ret; }
sizegeneration