optinal load_avg to fix

This commit is contained in:
2025-03-26 09:32:27 +08:00
parent e66ab82e63
commit 2001e8e478
11 changed files with 68 additions and 20 deletions

View File

@@ -108,6 +108,8 @@ 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);
int proc_size(void);
int get_current_load(void);
void update_load_avg(void);
// swtch.S
void swtch(struct context *, struct context *);

View File

@@ -6,6 +6,8 @@
#include "proc.h"
#include "defs.h"
uint64 load_avg = 0;
struct cpu cpus[NCPU];
struct proc proc[NPROC];
@@ -701,3 +703,23 @@ proc_size()
}
return n;
}
int
get_current_load() {
struct proc *p;
int current_load = 0;
for (p = proc; p < &proc[NPROC]; p++) {
if (p->state == RUNNING || p->state == RUNNABLE) {
current_load++;
}
}
return current_load;
}
void
update_load_avg() {
int current_load = get_current_load();
load_avg = (load_avg * ALPHA) + (current_load * (1 - ALPHA));
}

View File

@@ -1,4 +1,8 @@
#include "defs.h"
#define ALPHA 0.9
extern uint64 load_avg;
// Saved registers for kernel context switches.
struct context {
uint64 ra;

View File

@@ -152,7 +152,7 @@ syscall(void)
p->syscall_counts[num]++;
printf("%d: syscall %s(trace counts: %d) -> %d\n",
p->pid, syscalls_name[num], p->syscall_counts[num], p->trapframe->a0);
printf("a1:%d a2:%d a3:%d\n",p->trapframe->a1,p->trapframe->a2,p->trapframe->a3);
printf("a1:%d a2:%d a3:%d a4:%d a5:%d a6:%d a7:%d\n",p->trapframe->a1,p->trapframe->a2,p->trapframe->a3,p->trapframe->a4,p->trapframe->a5,p->trapframe->a6,p->trapframe->a7);
}
} else {
printf("%d %s: unknown sys call %d\n",

View File

@@ -3,4 +3,5 @@ struct sysinfo {
uint64 freemem;
uint64 nproc;
uint64 unused_proc_num;
uint64 load_avg;
};

View File

@@ -110,10 +110,12 @@ sys_sysinfo(void)
uint64 addr;
argaddr(0, &addr);
if (addr < 0) return -1;
/*update_load_avg();*/
struct proc* p = myproc();
info.nproc = proc_size();
info.freemem = freemem();
info.unused_proc_num = NPROC - info.nproc;
info.load_avg = load_avg;
if (copyout(p->pagetable, addr, (char*)&info, sizeof(info)) < 0)
return -1;
return 0;