[PATCH v3] Reduce memory waste while managing tcbs
Nahim El Atmani
nahim+dev at naam.me
Tue Mar 1 18:38:00 UTC 2016
From: Nahim El Atmani <naam at lse.epita.fr>
* strace.c: alloctcb() when no tcbs are available (first call and subsequent
calls when tcbtab is full) call expand_tcbtab(). Now, expand_tcbtab() allocate
the minimum required amount of memory to create the tcbtab if needed and expend
otherwise instead of relying on the preallocation of a large amount of tcbs
and expand if needed.
Signed-off-by: Nahim El Atmani <nahim+dev at naam.me>
Reviewed-By: Gabriel Laskar <gabriel at lse.epita.fr>
Reported-by: haris iqbal <haris.phnx at gmail.com>
---
strace.c | 31 ++++++++++++++++---------------
1 file changed, 16 insertions(+), 15 deletions(-)
diff --git a/strace.c b/strace.c
index 785cc60..e97fe9e 100644
--- a/strace.c
+++ b/strace.c
@@ -691,16 +691,26 @@ newoutf(struct tcb *tcp)
static void
expand_tcbtab(void)
{
- /* Allocate some more TCBs and expand the table.
+ /* Allocate some (more) TCBs (and expand the table).
We don't want to relocate the TCBs because our
callers have pointers and it would be a pain.
So tcbtab is a table of pointers. Since we never
free the TCBs, we allocate a single chunk of many. */
- unsigned int i = tcbtabsize;
- struct tcb *newtcbs = xcalloc(tcbtabsize, sizeof(newtcbs[0]));
- struct tcb **newtab = xreallocarray(tcbtab, tcbtabsize * 2,
- sizeof(tcbtab[0]));
- tcbtabsize *= 2;
+ unsigned int i, new_tcbtabsize, alloc_tcbtabsize;
+ struct tcb *newtcbs;
+ struct tcb **newtab;
+
+ if (tcbtabsize) {
+ alloc_tcbtabsize = tcbtabsize;
+ new_tcbtabsize = tcbtabsize * 2;
+ } else {
+ new_tcbtabsize = alloc_tcbtabsize = 1;
+ }
+
+ i = tcbtabsize;
+ newtcbs = xcalloc(alloc_tcbtabsize, sizeof(newtcbs[0]));
+ newtab = xreallocarray(tcbtab, new_tcbtabsize, sizeof(tcbtab[0]));
+ tcbtabsize = new_tcbtabsize;
tcbtab = newtab;
while (i < tcbtabsize)
tcbtab[i++] = newtcbs++;
@@ -1484,10 +1494,8 @@ get_os_release(void)
static void ATTRIBUTE_NOINLINE
init(int argc, char *argv[])
{
- struct tcb *tcp;
int c, i;
int optF = 0;
- unsigned int tcbi;
struct sigaction sa;
progname = argv[0] ? argv[0] : "strace";
@@ -1503,13 +1511,6 @@ init(int argc, char *argv[])
os_release = get_os_release();
- /* Allocate the initial tcbtab. */
- tcbtabsize = argc; /* Surely enough for all -p args. */
- tcbtab = xcalloc(tcbtabsize, sizeof(tcbtab[0]));
- tcp = xcalloc(tcbtabsize, sizeof(*tcp));
- for (tcbi = 0; tcbi < tcbtabsize; tcbi++)
- tcbtab[tcbi] = tcp++;
-
shared_log = stderr;
set_sortby(DEFAULT_SORTBY);
set_personality(DEFAULT_PERSONALITY);
--
Nahim El Atmani <nahim+dev at naam.me>
http://naam.me/
More information about the Strace-devel
mailing list