Repos / kernel.git / commitdiff
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: e91e33d)
raw | patch | inline | side by side (parent: e91e33d)
| author | zhaoyanbai <zhaoyanbai@126.com> | |
| Sat, 15 Aug 2026 01:50:27 +0000 (09:50 +0800) | ||
| committer | zhaoyanbai <zhaoyanbai@126.com> | |
| Sat, 15 Aug 2026 01:50:27 +0000 (09:50 +0800) |
| include/system.h | patch | blob | history | |
| kernel/ap.c | patch | blob | history | |
| kernel/hpet.c | patch | blob | history | |
| kernel/sched.c | patch | blob | history | |
| kernel/system.c | patch | blob | history | |
| kernel/task_root.c | patch | blob | history |
diff --git a/include/system.h b/include/system.h
--- a/include/system.h
+++ b/include/system.h
#define LEAVE_CRITICAL_ZONE(x) leave_critical_zone()
#define IN_CRITICAL_ZONE() in_critical_zone()
-#endif
+uint64_t read_uint64_consistent(volatile uint64_t* p);
+
+#endif // !ASM
#define DISABLE_IDE 1
diff --git a/kernel/ap.c b/kernel/ap.c
--- a/kernel/ap.c
+++ b/kernel/ap.c
uint64_t runtime_tsc = tsc;
uint64_t _ms = 0;
uint64_t _us = 0;
- uint64_t _ns = 0;
+ // uint64_t _ns = 0;
uint64_t _remainder = 0;
+
+ assert(tsc_khz != 0);
+
udiv64(runtime_tsc, tsc_khz, &_ms, &_remainder);
udiv64(_remainder * 1000, tsc_khz, &_us, &_remainder);
- udiv64(_remainder * 1000, tsc_khz, &_ns, NULL);
+ // udiv64(_remainder * 1000, tsc_khz, &_ns, NULL);
uint64_t total_seconds = 0;
- uint64_t days = 0;
- uint64_t hours = 0;
- uint64_t minutes = 0;
- uint64_t seconds = 0;
// total_seconds = ms / 1000;
// ms = ms % 1000
udiv64(_ms, 1000, &total_seconds, &_ms);
+ static char buffer[64];
+ buffer[0] = 0;
+
+#if 0
+ uint64_t days = 0;
+ uint64_t hours = 0;
+ uint64_t minutes = 0;
+ uint64_t seconds = 0;
+
// days = total_seconds / (60 * 60 * 24);
udiv64(total_seconds, 60 * 60 * 24, &days, &total_seconds);
udiv64(total_seconds, 60 * 60, &hours, &total_seconds);
udiv64(total_seconds, 60, &minutes, &seconds);
- static char buffer[64];
char tmp[16];
- buffer[0] = 0;
-
if (days > 0) {
sprintf(tmp, "%lud", days);
strcat(buffer, tmp);
uint32_t _ms32 = (uint32_t)_ms;
uint32_t _us32 = (uint32_t)_us;
_us32 /= 100; // 保留1位小数
- sprintf(tmp, ".%03u.%u", _ms32, _us32);
+ sprintf(tmp, ".%03u%u", _ms32, _us32);
strcat(buffer, tmp);
+#else
+ uint64_t seconds = total_seconds;
+ uint32_t _ms32 = (uint32_t)_ms;
+ uint32_t _us32 = (uint32_t)_us;
+ _us32 /= 100; // 保留1位小数
+ sprintf(buffer, "%lu.%03u%u", seconds, _ms32, _us32);
+#endif
return (char*)buffer;
}
+
void print_all_tasks() {
extern task_t* monitor_tasks[];
- ap_printl(MPL_TASK_TITLE, " NAME STATE LT/PI REASON TICKS RUNTIME");
+ ap_printl(MPL_TASK_TITLE, " NAME STATE LT/PI REASON TICKS RUNTIME");
for (int i = 0; i < 10; i++) {
task_t* p = monitor_tasks[i];
continue;
}
- char* runtime_str = format_runtime_tsc(p->st_runtime_tsc);
+ // 此处非严格意义上的统计,只用于monitor
+ // 因为中间若正好发生 schedule 结算,可能发生多算或少算,影响有限,所以用最简单的实现
+ uint64_t runtime_tsc = read_uint64_consistent(&p->st_runtime_tsc);
+ uint64_t last_exec_tsc = read_uint64_consistent(&p->st_last_exec_tsc);
+ if (last_exec_tsc != 0) { // 如果进程还在运行中,则算上还没结算的时间差
+ runtime_tsc += rdtsc() - last_exec_tsc;
+ }
+
+ char* runtime_str = format_runtime_tsc(runtime_tsc);
- ap_printl(MPL_TASK_0 + p->pid, "%08x %-6s:%u %s %02d/%02u %-10s %-9lu %16s",
+ ap_printl(MPL_TASK_0 + p->pid, "%08x %-6s:%u %s %02d/%02u %-10s %-9lu %12s",
p, //
p->name, //
p->pid, //
diff --git a/kernel/hpet.c b/kernel/hpet.c
--- a/kernel/hpet.c
+++ b/kernel/hpet.c
// tsc_khz = tsc_delta * hz / 1000;
udiv64(tsc_delta * hz, 1000, &tsc_khz, NULL);
- printk("TSC frequency: %u KHz\n", tsc_khz);
+ printk("TSC frequency: %lu KHz\n", tsc_khz);
}
void hpet_init() {
diff --git a/kernel/sched.c b/kernel/sched.c
--- a/kernel/sched.c
+++ b/kernel/sched.c
task_init_lists(&root_task);
task_init_stats(&root_task);
- root_task.st_last_exec_tsc = rdtsc();
+ // root_task.st_last_exec_tsc = rdtsc();
list_add(&root_task.list, &all_tasks);
diff --git a/kernel/system.c b/kernel/system.c
--- a/kernel/system.c
+++ b/kernel/system.c
@@ -275,6 +275,22 @@ void udiv64(uint64_t dividend, uint64_t divisor, uint64_t* quotient, uint64_t* r
}
}
+uint64_t read_uint64_consistent(volatile uint64_t* p) {
+ uint32_t hi1 = 0;
+ uint32_t low = 0;
+ uint32_t hi2 = 0;
+
+ volatile uint32_t* w = (volatile uint32_t*)p;
+
+ do {
+ hi1 = w[1];
+ low = w[0];
+ hi2 = w[1];
+ } while (hi1 != hi2);
+
+ return ((uint64_t)hi1 << 32) | low;
+}
+
paddr_t get_rcba_paddr() {
uint32_t rcba = pci_get_rcba();
diff --git a/kernel/task_root.c b/kernel/task_root.c
--- a/kernel/task_root.c
+++ b/kernel/task_root.c
p--;
}
#endif
-
+ current->st_last_exec_tsc = rdtsc();
sti();
kernel_task("init", init_task_entry, NULL);