Skip to content

Commit

Permalink
sched: track memory usage across processes and threads
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Sep 9, 2024
1 parent a2fbef2 commit 9adf83f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/include/kernel/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ typedef struct Thread {
SyscallRequest syscall; // for when the thread is blocked
int exitStatus; // for zombie threads

int pages; // memory pages used

struct Thread *next;
void *context; // platform-specific (page tables, registers, etc)

Expand All @@ -59,6 +61,8 @@ typedef struct Process {
struct IODescriptor io[MAX_IO_DESCRIPTORS];
int iodCount;

int pages; // memory pages used

size_t threadCount;
size_t childrenCount;

Expand Down
2 changes: 2 additions & 0 deletions src/platform/x86_64/sched/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ int platformSetContext(Thread *t, uintptr_t entry, uintptr_t highest, const char
ctx->regs.rsp = stack;

t->highest = stack + PAGE_SIZE; // requisite to sbrk() someday

t->pages = (t->highest - USER_BASE_ADDRESS + PAGE_SIZE - 1) / PAGE_SIZE;
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions src/sched/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ pid_t execveMemory(const void *ptr, const char **argv, const char **envp) {
return 0;
}

process->pages = process->threads[0]->pages;

KDEBUG("created new process with pid %d\n", pid);

processes++;
Expand Down
7 changes: 7 additions & 0 deletions src/sched/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ pid_t fork(Thread *t) {
p->threads[0]->pid = pid;
p->threads[0]->tid = pid;
p->threads[0]->context = calloc(1, PLATFORM_CONTEXT_SIZE);
p->threads[0]->highest = t->highest;
p->threads[0]->pages = t->pages;

// NOTE: fork() only clones one thread, which is why we're not cloning the
// entire process memory, but just the calling thread
p->pages = t->pages;

if(!p->threads[0]->context) {
free(p->threads[0]);
free(p->threads);
Expand Down

0 comments on commit 9adf83f

Please sign in to comment.