Skip to content

Commit

Permalink
Lab syscall: sysinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
foyoodo committed Mar 11, 2021
1 parent 9b48439 commit 0389301
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ UPROGS=\
$U/_wc\
$U/_zombie\
$U/_trace\
$U/_sysinfotest



Expand Down
2 changes: 2 additions & 0 deletions kernel/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void ramdiskrw(struct buf*);
void* kalloc(void);
void kfree(void *);
void kinit(void);
uint64 getfreemem(void);

// log.c
void initlog(int, struct superblock*);
Expand Down Expand Up @@ -104,6 +105,7 @@ void yield(void);
int either_copyout(int user_dst, uint64 dst, void *src, uint64 len);
int either_copyin(void *dst, int user_src, uint64 src, uint64 len);
void procdump(void);
uint64 getnproc(void);

// swtch.S
void swtch(struct context*, struct context*);
Expand Down
17 changes: 17 additions & 0 deletions kernel/kalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,20 @@ kalloc(void)
memset((char*)r, 5, PGSIZE); // fill with junk
return (void*)r;
}

uint64
getfreemem(void)
{
struct run *r;
uint64 n = 0;

acquire(&kmem.lock);
r = kmem.freelist;
while (r) {
++n;
r = r->next;
}
release(&kmem.lock);

return n * PGSIZE;
}
11 changes: 11 additions & 0 deletions kernel/proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,14 @@ procdump(void)
printf("\n");
}
}

uint64
getnproc(void)
{
uint64 n = 0;
struct proc *p;
for (p = proc; p < &proc[NPROC]; ++p) {
if (p->state != UNUSED) ++n;
}
return n;
}
2 changes: 2 additions & 0 deletions kernel/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ extern uint64 sys_wait(void);
extern uint64 sys_write(void);
extern uint64 sys_uptime(void);
extern uint64 sys_trace(void);
extern uint64 sys_sysinfo(void);

static uint64 (*syscalls[])(void) = {
[SYS_fork] sys_fork,
Expand All @@ -129,6 +130,7 @@ static uint64 (*syscalls[])(void) = {
[SYS_mkdir] sys_mkdir,
[SYS_close] sys_close,
[SYS_trace] sys_trace,
[SYS_sysinfo] sys_sysinfo,
};

static char *syscall_list[23] = {
Expand Down
1 change: 1 addition & 0 deletions kernel/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
#define SYS_mkdir 20
#define SYS_close 21
#define SYS_trace 22
#define SYS_sysinfo 23
21 changes: 21 additions & 0 deletions kernel/sysproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "memlayout.h"
#include "spinlock.h"
#include "proc.h"
#include "sysinfo.h"

uint64
sys_exit(void)
Expand Down Expand Up @@ -108,3 +109,23 @@ sys_trace(void)

return 0;
}

uint64
sys_sysinfo(void)
{
uint64 addr; // user virtual address, pointing to a struct sysinfo.

if (argaddr(0, &addr) < 0)
return -1;

struct proc *p = myproc();
struct sysinfo si;

si.freemem = getfreemem();
si.nproc = getnproc();

if (copyout(p->pagetable, addr, (char *)&si, sizeof(si)) < 0)
return -1;

return 0;
}
2 changes: 2 additions & 0 deletions user/user.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
struct stat;
struct rtcdate;
struct sysinfo;

// system calls
int fork(void);
Expand All @@ -24,6 +25,7 @@ char* sbrk(int);
int sleep(int);
int uptime(void);
int trace(int);
int sysinfo(struct sysinfo *);

// ulib.c
int stat(const char*, struct stat*);
Expand Down
1 change: 1 addition & 0 deletions user/usys.pl
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ sub entry {
entry("sleep");
entry("uptime");
entry("trace");
entry("sysinfo");

0 comments on commit 0389301

Please sign in to comment.