加入重启功能, 可以计算内存利用率

This commit is contained in:
xiaoyi1212 2024-04-10 22:15:48 +08:00
parent b197e88cb4
commit 048f85123d
9 changed files with 50 additions and 15 deletions

View File

@ -2,7 +2,7 @@
#define CRASHPOWEROS_COMMON_H #define CRASHPOWEROS_COMMON_H
#define OS_NAME "CrashPowerDOS" #define OS_NAME "CrashPowerDOS"
#define OS_VERSION "v0.2.0" #define OS_VERSION "v0.2.1"
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>

View File

@ -6,7 +6,7 @@
#include "isr.h" #include "isr.h"
#define KHEAP_INITIAL_SIZE 0xf00000 #define KHEAP_INITIAL_SIZE 0xf00000
#define KHEAP_START 0xc0000000 #define KHEAP_START 0xc0000000
#define STACK_SIZE 32768 #define STACK_SIZE 32768
#define INDEX_FROM_BIT(a) (a / (8*4)) #define INDEX_FROM_BIT(a) (a / (8*4))
@ -43,6 +43,8 @@ typedef union header {
ALIGN stub; ALIGN stub;
} header_t; } header_t;
uint32_t memory_usage();
void *memcpy(void *dst_, const void *src_, uint32_t size); void *memcpy(void *dst_, const void *src_, uint32_t size);
int memcmp(const void *a_, const void *b_, uint32_t size); int memcmp(const void *a_, const void *b_, uint32_t size);

View File

@ -12,5 +12,11 @@ void setup_shell();
void cmd_echo(int argc,char **argv); void cmd_echo(int argc,char **argv);
void cmd_proc(); void cmd_proc();
void cmd_date(); void cmd_date();
void cmd_reset();
void cmd_del(int argc, char **argv);
void cmd_mkdir(int argc, char **argv);
void cmd_read(int argc, char **argv);
void cmd_cat(int argc, char **argv);
void cmd_ls();
#endif //CRASHPOWEROS_SHELL_H #endif //CRASHPOWEROS_SHELL_H

View File

@ -21,17 +21,13 @@ struct context {
uint32_t eflags; uint32_t eflags;
}; };
struct mm_struct {
page_directory_t *pgd_dir; // 进程页表
};
// 进程控制块 PCB // 进程控制块 PCB
struct task_struct { struct task_struct {
volatile task_state state; // 进程当前状态 volatile task_state state; // 进程当前状态
int pid; // 进程标识符 int pid; // 进程标识符
char *name; // 进程名 char *name; // 进程名
void *stack; // 进程的内核栈地址 void *stack; // 进程的内核栈地址
struct mm_struct *mm; // 当前进程的内存地址映像 page_directory_t *pgd_dir; // 进程页表
struct context context; // 进程切换需要的上下文信息 struct context context; // 进程切换需要的上下文信息
struct task_struct *next; // 链表指针 struct task_struct *next; // 链表指针
}; };

View File

@ -13,7 +13,6 @@
extern uint32_t end; extern uint32_t end;
uint32_t placement_address = (uint32_t) & end; uint32_t placement_address = (uint32_t) & end;
void kernel_main() { void kernel_main() {
io_cli(); io_cli();
vga_install(); vga_install();
@ -28,6 +27,7 @@ void kernel_main() {
printf("[kernel]: PCB load success!\n"); printf("[kernel]: PCB load success!\n");
init_keyboard(); init_keyboard();
printf("[kernel]: Keyboard driver load success!\n"); printf("[kernel]: Keyboard driver load success!\n");
print_cpu_id(); print_cpu_id();
io_sti(); io_sti();

View File

@ -6,6 +6,18 @@ extern uint32_t end; // declared in linker.ld
static uint32_t placement_address = (uint32_t) &end; static uint32_t placement_address = (uint32_t) &end;
void *program_break, *program_break_end; void *program_break, *program_break_end;
uint32_t memory_usage(){
header_t *curr = head;
uint32_t size;
while (curr) {
if(!curr->s.is_free){
size += curr->s.size;
}
curr = curr->s.next;
}
return size;
}
static uint32_t kmalloc_int(uint32_t sz, uint32_t align, uint32_t *phys) { static uint32_t kmalloc_int(uint32_t sz, uint32_t align, uint32_t *phys) {
if (program_break) { if (program_break) {
// 有内存堆 // 有内存堆
@ -24,6 +36,7 @@ static uint32_t kmalloc_int(uint32_t sz, uint32_t align, uint32_t *phys) {
if (phys) *phys = placement_address; if (phys) *phys = placement_address;
uint32_t tmp = placement_address; uint32_t tmp = placement_address;
placement_address += sz; placement_address += sz;
return tmp; return tmp;
} }

View File

@ -6,6 +6,8 @@ struct task_struct *running_proc_head = NULL;
struct task_struct *wait_proc_head = NULL; struct task_struct *wait_proc_head = NULL;
struct task_struct *current = NULL; struct task_struct *current = NULL;
extern page_directory_t *kernel_directory;
extern void switch_to(struct context *prev, struct context *next); extern void switch_to(struct context *prev, struct context *next);
int now_pid = 0; int now_pid = 0;
@ -62,7 +64,10 @@ void print_proc(){
void schedule() { void schedule() {
if (current) { if (current) {
change_task_to(current->next); volatile task_state state = current->next->state;
if(state == TASK_RUNNABLE || state == TASK_SLEEPING ){
change_task_to(current->next);
}
} }
} }
@ -70,6 +75,7 @@ void change_task_to(struct task_struct *next) {
if (current != next) { if (current != next) {
struct task_struct *prev = current; struct task_struct *prev = current;
current = next; current = next;
//switch_page_directory(current->pgd_dir);
switch_to(&(prev->context), &(current->context)); switch_to(&(prev->context), &(current->context));
} }
} }
@ -84,7 +90,8 @@ int32_t kernel_thread(int (*fn)(void *), void *arg,char* name) {
new_task->state = TASK_RUNNABLE; new_task->state = TASK_RUNNABLE;
new_task->stack = current; new_task->stack = current;
new_task->pid = now_pid++; new_task->pid = now_pid++;
new_task->mm = NULL; new_task->pgd_dir = (page_directory_t *) kmalloc_a(sizeof(page_directory_t));
new_task->name = name; new_task->name = name;
uint32_t *stack_top = (uint32_t * )((uint32_t) new_task + STACK_SIZE); uint32_t *stack_top = (uint32_t * )((uint32_t) new_task + STACK_SIZE);
@ -125,7 +132,7 @@ void init_sched() {
current->state = TASK_RUNNABLE; current->state = TASK_RUNNABLE;
current->pid = now_pid++; current->pid = now_pid++;
current->stack = current; // 该成员指向栈低地址 current->stack = current; // 该成员指向栈低地址
current->mm = NULL; // 内核线程不需要该成员 current->pgd_dir = kernel_directory;
current->name = "CPOS-System"; current->name = "CPOS-System";
current->next = current; current->next = current;

View File

@ -13,7 +13,7 @@ int setup_date(){
clock_sleep(5); clock_sleep(5);
date_info = get_date_time(); //11 date_info = get_date_time(); //11
i = 0; i = 0;
for(size_t x = VGA_WIDTH - 17; x < VGA_WIDTH ; x++){ for(size_t x = VGA_WIDTH - 19; x < VGA_WIDTH ; x++){
const size_t index = (VGA_HEIGHT - 1) * VGA_WIDTH + x; const size_t index = (VGA_HEIGHT - 1) * VGA_WIDTH + x;
terminal_buffer[index] = vga_entry(date_info[i],terminal_color); terminal_buffer[index] = vga_entry(date_info[i],terminal_color);
i++; i++;

View File

@ -2,17 +2,17 @@
#include "../include/queue.h" #include "../include/queue.h"
#include "../include/vga.h" #include "../include/vga.h"
#include "../include/common.h" #include "../include/common.h"
#include "../include/io.h"
#include "../include/task.h" #include "../include/task.h"
#include "../include/cmos.h" #include "../include/cmos.h"
#include "../include/fat16.h" #include "../include/fat16.h"
#include "../include/timer.h"
#include "../include/io.h"
extern Queue *key_char_queue; extern Queue *key_char_queue;
char getc() { char getc() {
while (key_char_queue->size == 0x00) { while (key_char_queue->size == 0x00) {
io_hlt(); printf("");
} }
return queue_pop(key_char_queue); return queue_pop(key_char_queue);
} }
@ -73,7 +73,9 @@ void cmd_proc(){
void cmd_date(){ void cmd_date(){
printf("System Time: %s\n",get_date_time()); printf("System Time: %s\n",get_date_time());
printf("Memory Usage: %dKB | All Size: %dMB\n",memory_usage()/1024,(KHEAP_START+KHEAP_INITIAL_SIZE)/1024/1024);
print_cpu_id(); print_cpu_id();
vga_writestring("\n");
} }
void cmd_ls() { void cmd_ls() {
@ -161,6 +163,12 @@ void cmd_del(int argc, char **argv) {
kfree(info); kfree(info);
} }
void cmd_reset(){
printf("Restart %s for x86...");
clock_sleep(10);
outb(0x64,0xfe);
}
void setup_shell(){ void setup_shell(){
vga_clear(); vga_clear();
printf("%s for x86 [Version %s] \n",OS_NAME, OS_VERSION); printf("%s for x86 [Version %s] \n",OS_NAME, OS_VERSION);
@ -200,6 +208,8 @@ void setup_shell(){
cmd_mkdir(argc, argv); cmd_mkdir(argc, argv);
else if (!strcmp("del", argv[0]) || !strcmp("rm", argv[0])) else if (!strcmp("del", argv[0]) || !strcmp("rm", argv[0]))
cmd_del(argc, argv); cmd_del(argc, argv);
else if (!strcmp("reset", argv[0]))
cmd_reset();
else if (!strcmp("help", argv[0]) || !strcmp("?", argv[0]) || !strcmp("h", argv[0])) { else if (!strcmp("help", argv[0]) || !strcmp("?", argv[0]) || !strcmp("h", argv[0])) {
vga_writestring("-=[CrashPowerShell Helper]=-\n"); vga_writestring("-=[CrashPowerShell Helper]=-\n");
vga_writestring("help ? h Print shell help info.\n"); vga_writestring("help ? h Print shell help info.\n");
@ -212,6 +222,7 @@ void setup_shell(){
vga_writestring("del rm <name> Delete a file\n"); vga_writestring("del rm <name> Delete a file\n");
vga_writestring("sysinfo Print system info.\n"); vga_writestring("sysinfo Print system info.\n");
vga_writestring("proc Lists all running processes.\n"); vga_writestring("proc Lists all running processes.\n");
vga_writestring("reset Reset OS.\n");
} else printf("[Shell]: Unknown command '%s'.\n", argv[0]); } else printf("[Shell]: Unknown command '%s'.\n", argv[0]);
} }
} }