Skip to content

Commit 139c440

Browse files
committed
SyscallCounter syscall and SyscallCounter Test added to project.
1 parent 9ea62bd commit 139c440

11 files changed

+91
-18
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ UPROGS=\
183183
_zombie\
184184
_getParentIDtest\
185185
_getChildrentest\
186+
_getSyscallCounterTest\
186187

187188
fs.img: mkfs README $(UPROGS)
188189
./mkfs fs.img README $(UPROGS)
@@ -255,6 +256,7 @@ EXTRA=\
255256
printf.c umalloc.c\
256257
getParentIDtest.c\
257258
getChildrentest.c\
259+
getSyscallCounterTest.c\
258260
README dot-bochsrc *.pl toc.* runoff runoff1 runoff.list\
259261
.gdbinit.tmpl gdbutil\
260262

defs.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ void userinit(void);
120120
int wait(void);
121121
void wakeup(void*);
122122
void yield(void);
123-
int getparentid(void);
124-
int getChildren(char* ptr);
123+
int getparentid(void); // (Added)
124+
int getChildren(char* ptr); // (Added)
125+
int getSyscallCounter(int syscallNo); // (Added)
125126

126127
// swtch.S
127128
void swtch(struct context**, struct context*);

getSyscallCounterTest.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# include "types.h"
2+
# include "stat.h"
3+
# include "user.h"
4+
5+
6+
int main(void)
7+
{
8+
int p;
9+
10+
printf(1, "Parent process sys_fork count = %d\n", getSyscallCounter(1));
11+
12+
p = fork();
13+
14+
if (p == 0)
15+
{
16+
printf(1, "Child process sys_fork count = %d\n", getSyscallCounter(1));
17+
18+
for (int i = 0; i < 16; i++)
19+
getpid();
20+
21+
printf(1, "child sys_getpid count %d\n", getSyscallCounter(11));
22+
}
23+
else
24+
{
25+
wait();
26+
printf(1, "parent fork count %d\n", getSyscallCounter(1));
27+
sleep(1);
28+
printf(1, "parent sys_sleep count %d\n", getSyscallCounter(13));
29+
}
30+
printf(1, "process sys_wait count = %d\n", getSyscallCounter(3));
31+
printf(1, "process sys_syscallcounter count = %d\n", getSyscallCounter(24));
32+
33+
exit();
34+
35+
return 0;
36+
}

proc.c

+13
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ procdump(void)
533533
}
534534
}
535535

536+
// return parent ID of a process (Added)
536537
int
537538
getparentid(void)
538539
{
@@ -541,6 +542,7 @@ getparentid(void)
541542
return chlid_pid;
542543
}
543544

545+
// return children ID of a process (Added)
544546
int
545547
getChildren(char * ptr)
546548
{
@@ -557,4 +559,15 @@ getChildren(char * ptr)
557559
}
558560
}
559561
return 1;
562+
}
563+
564+
// return occurrence of a syscall for current process (Added)
565+
int
566+
getSyscallCounter(int syscallNo){
567+
568+
struct proc *curproc = myproc();
569+
int occurrence;
570+
571+
occurrence = curproc->syscall_occurrence[syscallNo-1];
572+
return occurrence;
560573
}

proc.h

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct proc {
4949
struct file *ofile[NOFILE]; // Open files
5050
struct inode *cwd; // Current directory
5151
char name[16]; // Process name (debugging)
52+
int syscall_occurrence[24]; // save occurrence of each syscall (Added)
5253
};
5354

5455
// Process memory is laid out contiguously, low addresses first:

syscall.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ extern int sys_unlink(void);
103103
extern int sys_wait(void);
104104
extern int sys_write(void);
105105
extern int sys_uptime(void);
106-
extern int sys_getparentid(void);
107-
extern int sys_getChildren(void);
106+
extern int sys_getparentid(void); // Added
107+
extern int sys_getChildren(void); // Added
108+
extern int sys_getSyscallCounter(void); // Added
108109

109110
static int (*syscalls[])(void) = {
110111
[SYS_fork] sys_fork,
@@ -128,8 +129,9 @@ static int (*syscalls[])(void) = {
128129
[SYS_link] sys_link,
129130
[SYS_mkdir] sys_mkdir,
130131
[SYS_close] sys_close,
131-
[SYS_getparentid] sys_getparentid,
132-
[SYS_getChildren] sys_getChildren
132+
[SYS_getparentid] sys_getparentid, // (Added)
133+
[SYS_getChildren] sys_getChildren, // (Added)
134+
[SYS_getSyscallCounter] sys_getSyscallCounter // (Added)
133135
};
134136

135137
void
@@ -140,6 +142,7 @@ syscall(void)
140142

141143
num = curproc->tf->eax;
142144
if(num > 0 && num < NELEM(syscalls) && syscalls[num]) {
145+
curproc->syscall_occurrence[num-1]++; // (Added)
143146
curproc->tf->eax = syscalls[num]();
144147
} else {
145148
cprintf("%d %s: unknown sys call %d\n",

syscall.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
#define SYS_link 19
2121
#define SYS_mkdir 20
2222
#define SYS_close 21
23-
#define SYS_getparentid 22
24-
#define SYS_getChildren 23
23+
#define SYS_getparentid 22 // (Added)
24+
#define SYS_getChildren 23 // (Added)
25+
#define SYS_getSyscallCounter 24 // (Added)

sysfile.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ sys_exec(void)
421421

422422
int
423423
sys_pipe(void)
424-
{
424+
{
425425
int *fd;
426426
struct file *rf, *wf;
427427
int fd0, fd1;

sysproc.c

+20-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ sys_fork(void)
1515

1616
int
1717
sys_exit(void)
18-
{
18+
{
1919
exit();
2020
return 0; // not reached
2121
}
@@ -28,8 +28,8 @@ sys_wait(void)
2828

2929
int
3030
sys_kill(void)
31-
{
32-
int pid;
31+
{
32+
int pid;
3333

3434
if(argint(0, &pid) < 0)
3535
return -1;
@@ -38,13 +38,13 @@ sys_kill(void)
3838

3939
int
4040
sys_getpid(void)
41-
{
41+
{
4242
return myproc()->pid;
4343
}
4444

4545
int
4646
sys_sbrk(void)
47-
{
47+
{
4848
int addr;
4949
int n;
5050

@@ -81,7 +81,7 @@ sys_sleep(void)
8181
// since start.
8282
int
8383
sys_uptime(void)
84-
{
84+
{
8585
uint xticks;
8686

8787
acquire(&tickslock);
@@ -90,12 +90,14 @@ sys_uptime(void)
9090
return xticks;
9191
}
9292

93+
// return parent ID of a process (Added)
9394
int
9495
sys_getparentid(void)
9596
{
9697
return getparentid();
9798
}
9899

100+
// return children ID of a process (Added)
99101
int
100102
sys_getChildren(void)
101103
{
@@ -106,4 +108,16 @@ sys_getChildren(void)
106108

107109
getChildren(ptr);
108110
return 1;
111+
}
112+
113+
// return occurrence of a syscall for current process (Added)
114+
int
115+
sys_getSyscallCounter()
116+
{
117+
int syscallNo;
118+
119+
if(argint(0, &syscallNo) < 0)
120+
return -1;
121+
122+
return getSyscallCounter(syscallNo);
109123
}

user.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ int getpid(void);
2323
char* sbrk(int);
2424
int sleep(int);
2525
int uptime(void);
26-
int getparentid(void);
27-
int getChildren(char* ptr);
26+
int getparentid(void); // (Added)
27+
int getChildren(char* ptr); // (Added)
28+
int getSyscallCounter(int syscallNo); // (Added)
2829

2930
// ulib.c
3031
int stat(const char*, struct stat*);

usys.S

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ SYSCALL(sbrk)
3030
SYSCALL(sleep)
3131
SYSCALL(uptime)
3232
SYSCALL(getparentid)
33-
SYSCALL(getChildren)
33+
SYSCALL(getChildren)
34+
SYSCALL(getSyscallCounter)

0 commit comments

Comments
 (0)