shithub: riscv

Download patch

ref: bd1305a0b9841d65f95b4d81fa0d29067425833a
parent: 15d1425b276495a82621a8d075176a59a512cdf3
author: Jacob Moody <moody@posixcafe.org>
date: Sat May 4 21:57:22 EDT 2024

libc: compress directly recursive functions while profiling

When a function calls itself, the execution slot of its child is now
just added to its own time.  This makes conceptual sense and also
reduces a big cause of depth inflation.

--- a/sys/src/libc/port/profile.c
+++ b/sys/src/libc/port/profile.c
@@ -19,6 +19,7 @@
 	long	pc;
 	long	count;
 	vlong	time;
+	uint	rec;
 };
 
 #pragma profile off
@@ -44,6 +45,11 @@
 	pp = _tos->prof.pp;
 	if(pp == 0 || (_tos->prof.pid && _tos->pid != _tos->prof.pid))
 		return _restore(arg, ret);
+	if(pc == pp->pc){
+		pp->rec++;
+		p = pp;
+		goto out;
+	}
 	for(p=pp->down; p; p=p->link)
 		if(p->pc == pc)
 			goto out;
@@ -113,7 +119,10 @@
 		p->time = p->time + _tos->clock;
 		break;
 	}
-	_tos->prof.pp = p->old;
+	if(p->rec)
+		p->rec--;
+	else
+		_tos->prof.pp = p->old;
 	return _restore(arg, ret);
 }