dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] trace arena
@ 2024-03-03 23:22 Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2024-03-03 23:22 UTC (permalink / raw)
  To: spew

---
 sv.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/sv.c b/sv.c
index 9c7f3ba..a04e1dc 100644
--- a/sv.c
+++ b/sv.c
@@ -297,6 +297,44 @@ Public API:
 	++PL_sv_count;					\
     } STMT_END
 
+#define TRACE_ARENA 1
+#if TRACE_ARENA
+#include <time.h>
+#include <stdio.h>
+#include <stdlib.h>
+static time_t trace_start = -1;
+static int trace_arena_alloc;
+
+static void __attribute__((constructor)) init_arena_trace(void)
+{
+	const char *t = getenv("TRACE_ARENA");
+	if (t && *t)
+		trace_arena_alloc = 1;
+}
+
+static void trace_arena(const svtype sv_type, size_t req, size_t actual)
+{
+	struct timespec ts;
+
+	if (!trace_arena_alloc)
+		return;
+
+	(void)clock_gettime(CLOCK_MONOTONIC, &ts);
+	if (trace_start < 0) {
+		trace_start = ts.tv_sec + 10;
+	} else if (trace_start > 0) {
+		if (ts.tv_sec > trace_start)
+			trace_start = 0;
+	}
+	if (!trace_start)
+		dprintf(2, "%ld %u %zu -> %zu\n",
+			ts.tv_sec, sv_type, req, actual);
+}
+#else /* !TRACE_ARENA */
+static void trace_arena(const svtype sv_type, size_t req, size_t actual)
+{
+}
+#endif /* TRACE_ARENA */
 
 /* make some more SVs by adding another arena */
 
@@ -306,6 +344,7 @@ S_more_sv(pTHX)
     SV* sv;
     char *chunk;                /* must use New here to match call to */
     Newx(chunk,PERL_ARENA_SIZE,char);  /* Safefree() in sv_free_arenas() */
+    trace_arena(SVt_NULL, 0, PERL_ARENA_SIZE);
     sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
     uproot_SV(sv);
     return sv;
@@ -1091,6 +1130,8 @@ Perl_more_bodies (pTHX_ const svtype sv_type, const size_t body_size,
 #if defined(DEBUGGING) && !defined(PERL_GLOBAL_STRUCT)
     static bool done_sanity_check;
 
+    trace_arena(sv_type, arena_size, good_arena_size);
+
     /* PERL_GLOBAL_STRUCT cannot coexist with global
      * variables like done_sanity_check. */
     if (!done_sanity_check) {

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH] trace arena
@ 2024-03-03 23:25 Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2024-03-03 23:25 UTC (permalink / raw)
  To: spew

---
 sv.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/sv.c b/sv.c
index 9c7f3ba..77b679e 100644
--- a/sv.c
+++ b/sv.c
@@ -297,6 +297,44 @@ Public API:
 	++PL_sv_count;					\
     } STMT_END
 
+#define TRACE_ARENA 1
+#if TRACE_ARENA
+#include <time.h>
+#include <stdio.h>
+#include <stdlib.h>
+static time_t trace_start = -1;
+static int trace_arena_alloc;
+
+static void __attribute__((constructor)) init_arena_trace(void)
+{
+	const char *t = getenv("TRACE_ARENA");
+	if (t && *t)
+		trace_arena_alloc = 1;
+}
+
+static void trace_arena(const svtype sv_type, size_t req, size_t actual)
+{
+	struct timespec ts;
+
+	if (!trace_arena_alloc)
+		return;
+
+	(void)clock_gettime(CLOCK_MONOTONIC, &ts);
+	if (trace_start < 0) {
+		trace_start = ts.tv_sec + 10;
+	} else if (trace_start > 0) {
+		if (ts.tv_sec > trace_start)
+			trace_start = 0;
+	}
+	if (!trace_start)
+		dprintf(2, "%ld %u %zu -> %zu\n",
+			ts.tv_sec, sv_type, req, actual);
+}
+#else /* !TRACE_ARENA */
+static void trace_arena(const svtype sv_type, size_t req, size_t actual)
+{
+}
+#endif /* TRACE_ARENA */
 
 /* make some more SVs by adding another arena */
 
@@ -306,6 +344,7 @@ S_more_sv(pTHX)
     SV* sv;
     char *chunk;                /* must use New here to match call to */
     Newx(chunk,PERL_ARENA_SIZE,char);  /* Safefree() in sv_free_arenas() */
+    trace_arena(SVt_NULL, 0, PERL_ARENA_SIZE);
     sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
     uproot_SV(sv);
     return sv;
@@ -1102,6 +1141,7 @@ Perl_more_bodies (pTHX_ const svtype sv_type, const size_t body_size,
 	    assert (bodies_by_type[i].type == i);
     }
 #endif
+    trace_arena(sv_type, arena_size, good_arena_size);
 
     assert(arena_size);
 

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-03-03 23:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-03 23:25 [PATCH] trace arena Eric Wong
  -- strict thread matches above, loose matches on Subject: below --
2024-03-03 23:22 Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).