From 810586053d68768f651cd27d5ca302d5b5cae786 Mon Sep 17 00:00:00 2001 From: Dan Snyder Date: Tue, 14 Jan 2025 16:02:30 -0500 Subject: [PATCH] Added print tree function --- src/process.h | 1 + src/server.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/process.h b/src/process.h index 5b41db1..15403fd 100644 --- a/src/process.h +++ b/src/process.h @@ -10,6 +10,7 @@ typedef struct st_process { struct st_process *parent; // parent process struct st_process *child; // pointer to first child struct st_process *sibling; // pointer to next sibling + int visited; uint32_t flags; char cpuset[128]; int tgid; diff --git a/src/server.c b/src/server.c index 10ecd77..1677dc9 100644 --- a/src/server.c +++ b/src/server.c @@ -117,6 +117,55 @@ void linkFamily(Process *head) { } } +int resetVisits(Process *head) { + Process *current = head; + int processCount = 0; + + while (current != NULL) { + current->visited = 0; + current = current->next; + } + + return processCount; +} + +void printFamilyTree(Process *head) { + int processCount = resetVisits(head); + int visited = 0; + Process *current = head; + int depth = 0; + + while (visited < processCount) { + if (current == NULL) { + printf("Bad visit count: %d visited of %d processes\n", visited, processCount); + break; + } + + current->visited = 1; + printf("%10d ", current->pid); + // `ps f` style tree + for (int i=1; i 0) puts(" \\_ "); + printf("%s\n", current->label); + + if (current->child != NULL) { + // process children first + depth++; + current = current->child; + } else if (current->sibling != NULL) { + // process siblings next + current = current->sibling; + } else if (current->parent != NULL) { + // return to parent when tree is exhausted + depth--; + current = current->parent; + } else { + // no parent - scan for unvisited process + while (current != NULL && current->visited == 1) current = current->next; + } + } +} + void aggregateStats(Process *head) { } @@ -204,6 +253,7 @@ char *readProcesses(char *procdir) { cur->next = NULL; linkFamily(head); + printFamilyTree(head); int clocks = sysconf(_SC_CLK_TCK); char *output = malloc(8 * 1024 * 1024);