aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteven Rostedt (Red Hat) <rostedt@goodmis.org>2016-09-12 14:06:13 -0400
committerSteven Rostedt <rostedt@goodmis.org>2016-10-30 19:46:49 -0400
commit573b2fc78efaa6ec8665a6858c7656803b8626f2 (patch)
tree18fed1cec90142668232cffa32f5da38f920d154
parent3ab8dcc7dee835a0a52ad61219024b48872109b4 (diff)
downloadtrace-cmd-573b2fc78efaa6ec8665a6858c7656803b8626f2.tar.gz
trace-cmd listen: Reuse slots in client_pid list
Instead of moving around the pids in the pid list, just leave a hole. And have the next allocation use it. Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
-rw-r--r--trace-listen.c54
1 files changed, 34 insertions, 20 deletions
diff --git a/trace-listen.c b/trace-listen.c
index 7e75ea63..a0d9945f 100644
--- a/trace-listen.c
+++ b/trace-listen.c
@@ -739,25 +739,43 @@ static int do_connection(int cfd, struct sockaddr_storage *peer_addr,
}
static int *client_pids;
+static int free_pids;
static int saved_pids;
static int size_pids;
#define PIDS_BLOCK 32
static void add_process(int pid)
{
- if (!client_pids) {
- size_pids = PIDS_BLOCK;
- client_pids = malloc(sizeof(*client_pids) * size_pids);
- if (!client_pids)
- pdie("allocating pids");
- } else if (!(saved_pids % PIDS_BLOCK)) {
- size_pids += PIDS_BLOCK;
- client_pids = realloc(client_pids,
- sizeof(*client_pids) * size_pids);
- if (!client_pids)
- pdie("realloc of pids");
+ int *client = NULL;
+ int i;
+
+ if (free_pids) {
+ for (i = 0; i < saved_pids; i++) {
+ if (!client_pids[i]) {
+ client = &client_pids[i];
+ break;
+ }
+ }
+ free_pids--;
+ if (!client)
+ warning("Could not find free pid");
+ }
+ if (!client) {
+ if (!client_pids) {
+ size_pids = PIDS_BLOCK;
+ client_pids = malloc(sizeof(*client_pids) * size_pids);
+ if (!client_pids)
+ pdie("allocating pids");
+ } else if (!(saved_pids % PIDS_BLOCK)) {
+ size_pids += PIDS_BLOCK;
+ client_pids = realloc(client_pids,
+ sizeof(*client_pids) * size_pids);
+ if (!client_pids)
+ pdie("realloc of pids");
+ }
+ client = &client_pids[saved_pids++];
}
- client_pids[saved_pids++] = pid;
+ *client = pid;
}
static void remove_process(int pid)
@@ -772,14 +790,8 @@ static void remove_process(int pid)
if (i == saved_pids)
return;
- saved_pids--;
-
- if (saved_pids == i)
- return;
-
- memmove(&client_pids[i], &client_pids[i+1],
- sizeof(*client_pids) * (saved_pids - i));
-
+ client_pids[i] = 0;
+ free_pids++;
}
static void kill_clients(void)
@@ -788,6 +800,8 @@ static void kill_clients(void)
int i;
for (i = 0; i < saved_pids; i++) {
+ if (!client_pids[i])
+ continue;
kill(client_pids[i], SIGINT);
waitpid(client_pids[i], &status, 0);
}