Memory page

FossilOrigin-Name: d99c3ec4b0090f12c8c3a4fbc26254a65ed3d37b2aaa2c7ef3fb45ff345dbe6d
This commit is contained in:
nekobit 2022-10-17 20:38:40 +00:00
parent f2107a803a
commit 0ed7b49709
7 changed files with 126 additions and 3 deletions

View File

@ -6,7 +6,7 @@ our @EXPORT = qw( );
use Exporter 'import';
use template_helpers 'simple_page';
use template_helpers qw( simple_page to_template );
sub about
{
@ -18,4 +18,20 @@ sub license
simple_page @_, 'license.tt';
}
sub memory
{
my ($ssn, $data, $mem_alloc_est, $mem_alloc, $mem_free, $mem_time_alloc, $mem_time_freed) = @_;
my %vars = (
ssn => $ssn,
mem_alloc_est => $mem_alloc_est,
mem_alloc => $mem_alloc,
mem_free => $mem_free,
mem_time_alloc => $mem_time_alloc,
mem_time_freed => $mem_time_freed,
);
to_template(\%vars, \$data->{'memory.tt'});
}
1;

View File

@ -51,6 +51,7 @@
#include "../templates/license.ctt"
#include "../templates/login.ctt"
#include "../templates/status_interactions.ctt"
#include "../templates/memory.ctt"
PerlInterpreter* my_perl;
HV* template_files;
@ -92,6 +93,7 @@ void init_template_files(pTHX)
hv_stores(template_files, "license.tt", newSVpv(data_license_tt, data_license_tt_size));
hv_stores(template_files, "login.tt", newSVpv(data_login_tt, data_login_tt_size));
hv_stores(template_files, "status_interactions.tt", newSVpv(data_status_interactions_tt, data_status_interactions_tt_size));
hv_stores(template_files, "memory.tt", newSVpv(data_memory_tt, data_memory_tt_size));
}
void cleanup_template_files()

View File

@ -31,6 +31,7 @@
#include "login.h"
#include "local_config.h"
#include "cookie.h"
#include "memory_page.h"
#include "query.h"
#include "status.h"
#include "lists.h"
@ -120,6 +121,8 @@ static struct path_info paths[] = {
{ "/quit", exit_treebird },
{ "/exit", exit_treebird },
#endif
// Debug, but cool to see
{ "/memory_stats", content_memory_stats },
// API
{ "/treebird_api/v1/notifications", api_notifications },
{ "/treebird_api/v1/interact", api_status_interact },

View File

@ -51,13 +51,13 @@ void* tb_malloc(size_t size)
// Nothing....
void* tb_calloc(size_t nmemb, size_t size)
{
tb_memory_allocated_est += size * nmemb;
tb_memory_allocated_est = size * nmemb;
return calloc(nmemb, size);
}
void* tb_realloc(void* ptr, size_t size)
{
tb_memory_allocated_est += size;
tb_memory_allocated_est = size;
return realloc(ptr, size);
}

48
src/memory_page.c Normal file
View File

@ -0,0 +1,48 @@
/*
* Treebird - Lightweight frontend for Pleroma
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "memory_page.h"
#include "base_page.h"
#include "memory.h"
void content_memory_stats(PATH_ARGS)
{
PERL_STACK_INIT;
HV* session_hv = perlify_session(ssn);
mXPUSHs(newRV_inc((SV*)session_hv));
mXPUSHs(newRV_inc((SV*)template_files));
// Args
mXPUSHi(tb_memory_allocated_est);
mXPUSHi(tb_memory_allocated);
mXPUSHi(tb_memory_free);
mXPUSHi(tb_memory_times_allocated);
mXPUSHi(tb_memory_times_freed);
PERL_STACK_SCALAR_CALL("meta::memory");
char* dup = PERL_GET_STACK_EXIT;
struct base_page b = {
.category = BASE_CAT_NONE,
.content = dup,
.session = session_hv,
.sidebar_left = NULL
};
render_base_page(&b, req, ssn, api);
tb_free(dup);
}

27
src/memory_page.h Normal file
View File

@ -0,0 +1,27 @@
/*
* Treebird - Lightweight frontend for Pleroma
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef MEMORY_PAGE_H
#define MEMORY_PAGE_H
#include "memory.h"
#include "session.h"
#include "path.h"
void content_memory_stats(PATH_ARGS);
#endif // MEMORY_PAGE_H

27
templates/memory.tt Normal file
View File

@ -0,0 +1,27 @@
<div class="simple-page">
<h1>Memory information</h1>
<p>Information scaling doesn't mean there is a memleak! Memory free'd won't match because we haven't hit the next free since this page was rendered.</p>
<table class="present">
<tr>
<th>Memory Allocated (malloc)</th>
<td>$mem_alloc</td>
</tr>
<tr>
<th>Memory Allocated Total (estimated)</th>
<td>$mem_alloc_est</td>
</tr>
<tr>
<th>Memory Free</th>
<td>$mem_free</td>
</tr>
<tr>
<th>Times Allocated (total)</th>
<td>$mem_time_alloc</td>
</tr>
<tr>
<th>Times Free'd (total)</th>
<td>$mem_time_freed</td>
</tr>
</table>
</div>