Added print tree function

This commit is contained in:
Dan Snyder 2025-01-14 16:02:30 -05:00
parent 4942b9857d
commit 810586053d
2 changed files with 51 additions and 0 deletions

View File

@ -10,6 +10,7 @@ typedef struct st_process {
struct st_process *parent; // parent process struct st_process *parent; // parent process
struct st_process *child; // pointer to first child struct st_process *child; // pointer to first child
struct st_process *sibling; // pointer to next sibling struct st_process *sibling; // pointer to next sibling
int visited;
uint32_t flags; uint32_t flags;
char cpuset[128]; char cpuset[128];
int tgid; int tgid;

View File

@ -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<depth; i++) puts(" ");
if (depth > 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) { void aggregateStats(Process *head) {
} }
@ -204,6 +253,7 @@ char *readProcesses(char *procdir) {
cur->next = NULL; cur->next = NULL;
linkFamily(head); linkFamily(head);
printFamilyTree(head);
int clocks = sysconf(_SC_CLK_TCK); int clocks = sysconf(_SC_CLK_TCK);
char *output = malloc(8 * 1024 * 1024); char *output = malloc(8 * 1024 * 1024);