From eba2be8a35c17fd8d2c811bfca9e297d1fd70a17 Mon Sep 17 00:00:00 2001 From: Tomoki Shirasawa Date: Thu, 18 Feb 2016 13:14:18 +0900 Subject: [PATCH] support times --- arch/x86/kernel/cpu.c | 10 +++ arch/x86/kernel/include/syscall_list.h | 1 + arch/x86/kernel/interrupt.S | 4 +- arch/x86/kernel/syscall.c | 10 ++- kernel/include/process.h | 11 +++- kernel/include/syscall.h | 3 + kernel/include/time.h | 22 +++++++ kernel/mem.c | 3 + kernel/process.c | 7 ++ kernel/syscall.c | 91 ++++++++++++++++++++++++++ 10 files changed, 159 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/cpu.c b/arch/x86/kernel/cpu.c index 57543763..d8528c97 100644 --- a/arch/x86/kernel/cpu.c +++ b/arch/x86/kernel/cpu.c @@ -82,6 +82,7 @@ void x86_init_perfctr(void); int gettime_local_support = 0; extern int kprintf(const char *format, ...); +extern int interrupt_from_user(void *); static struct idt_entry{ uint32_t desc[4]; @@ -786,6 +787,8 @@ void handle_interrupt(int vector, struct x86_user_context *regs) lapic_ack(); ++v->in_interrupt; + set_cputime(interrupt_from_user(regs)? 1: 2); + dkprintf("CPU[%d] got interrupt, vector: %d, RIP: 0x%lX\n", ihk_mc_get_processor_id(), vector, regs->gpr.rip); @@ -862,12 +865,14 @@ void handle_interrupt(int vector, struct x86_user_context *regs) check_signal(0, regs, 0); check_need_resched(); + set_cputime(0); --v->in_interrupt; } void gpe_handler(struct x86_user_context *regs) { + set_cputime(interrupt_from_user(regs)? 1: 2); kprintf("General protection fault (err: %lx, %lx:%lx)\n", regs->gpr.error, regs->gpr.cs, regs->gpr.rip); arch_show_interrupt_context(regs); @@ -877,6 +882,7 @@ void gpe_handler(struct x86_user_context *regs) set_signal(SIGSEGV, regs, NULL); check_signal(0, regs, 0); check_need_resched(); + set_cputime(0); // panic("GPF"); } @@ -886,6 +892,7 @@ void debug_handler(struct x86_user_context *regs) int si_code = 0; struct siginfo info; + set_cputime(interrupt_from_user(regs)? 1: 2); #ifdef DEBUG_PRINT_CPU kprintf("debug exception (err: %lx, %lx:%lx)\n", regs->gpr.error, regs->gpr.cs, regs->gpr.rip); @@ -905,12 +912,14 @@ void debug_handler(struct x86_user_context *regs) set_signal(SIGTRAP, regs, &info); check_signal(0, regs, 0); check_need_resched(); + set_cputime(0); } void int3_handler(struct x86_user_context *regs) { struct siginfo info; + set_cputime(interrupt_from_user(regs)? 1: 2); #ifdef DEBUG_PRINT_CPU kprintf("int3 exception (err: %lx, %lx:%lx)\n", regs->gpr.error, regs->gpr.cs, regs->gpr.rip); @@ -922,6 +931,7 @@ void int3_handler(struct x86_user_context *regs) set_signal(SIGTRAP, regs, &info); check_signal(0, regs, 0); check_need_resched(); + set_cputime(0); } diff --git a/arch/x86/kernel/include/syscall_list.h b/arch/x86/kernel/include/syscall_list.h index 6f429df3..daf632b7 100644 --- a/arch/x86/kernel/include/syscall_list.h +++ b/arch/x86/kernel/include/syscall_list.h @@ -69,6 +69,7 @@ SYSCALL_DELEGATED(79, getcwd) SYSCALL_DELEGATED(89, readlink) SYSCALL_HANDLED(96, gettimeofday) SYSCALL_HANDLED(97, getrlimit) +SYSCALL_HANDLED(100, times) SYSCALL_HANDLED(101, ptrace) SYSCALL_HANDLED(102, getuid) SYSCALL_HANDLED(104, getgid) diff --git a/arch/x86/kernel/interrupt.S b/arch/x86/kernel/interrupt.S index 760c2722..74f88fd7 100644 --- a/arch/x86/kernel/interrupt.S +++ b/arch/x86/kernel/interrupt.S @@ -209,7 +209,9 @@ enter_user_mode: callq release_runq_lock movq $0, %rdi movq %rsp, %rsi - call check_signal + call check_signal + movq $0, %rdi + call set_cputime POP_ALL_REGS addq $8, %rsp iretq diff --git a/arch/x86/kernel/syscall.c b/arch/x86/kernel/syscall.c index dea1ca0f..b091e595 100644 --- a/arch/x86/kernel/syscall.c +++ b/arch/x86/kernel/syscall.c @@ -756,6 +756,14 @@ hassigpending(struct thread *thread) return getsigpending(thread, 0); } +int +interrupt_from_user(void *regs0) +{ + struct x86_user_context *regs = regs0; + + return !(regs->gpr.rsp & 0x8000000000000000); +} + void check_signal(unsigned long rc, void *regs0, int num) { @@ -785,7 +793,7 @@ check_signal(unsigned long rc, void *regs0, int num) return; } - if(regs != NULL && (regs->gpr.rsp & 0x8000000000000000)) { + if(regs != NULL && !interrupt_from_user(regs)) { return; } diff --git a/kernel/include/process.h b/kernel/include/process.h index 4ad8b8d6..9eeee9b8 100644 --- a/kernel/include/process.h +++ b/kernel/include/process.h @@ -347,7 +347,7 @@ typedef void pgio_func_t(void *arg); * special "init" process */ struct process { struct list_head hash_list; - mcs_rwlock_lock_t update_lock; // lock for parent, status, ...? + mcs_rwlock_lock_t update_lock; // lock for parent, status, cpu time... // process vm struct process_vm *vm; @@ -431,6 +431,10 @@ struct process { ihk_spinlock_t mckfd_lock; struct mckfd *mckfd; + + // cpu time (summary) + struct timespec stime; + struct timespec utime; }; void hold_thread(struct thread *ftn); @@ -518,6 +522,11 @@ struct thread { unsigned long *ptrace_debugreg; /* debug registers for ptrace */ struct sig_pending *ptrace_recvsig; struct sig_pending *ptrace_sendsig; + + // cpu time + struct timespec stime; + struct timespec utime; + struct timespec btime; }; struct process_vm { diff --git a/kernel/include/syscall.h b/kernel/include/syscall.h index e7c86079..0b96fcb9 100644 --- a/kernel/include/syscall.h +++ b/kernel/include/syscall.h @@ -337,4 +337,7 @@ struct tod_data_s { }; extern struct tod_data_s tod_data; /* residing in arch-dependent file */ +void reset_cputime(); +void set_cputime(int mode); + #endif diff --git a/kernel/include/time.h b/kernel/include/time.h index 83e920f5..5df0d8fb 100644 --- a/kernel/include/time.h +++ b/kernel/include/time.h @@ -53,5 +53,27 @@ struct timezone int tz_dsttime; /* Nonzero if DST is ever in effect. */ }; +static inline void +ts_add(struct timespec *ats, const struct timespec *bts) +{ + ats->tv_sec += bts->tv_sec; + ats->tv_nsec += bts->tv_nsec; + while(ats->tv_nsec >= 1000000000){ + ats->tv_sec++; + ats->tv_nsec -= 1000000000; + } +} + +static inline void +ts_sub(struct timespec *ats, const struct timespec *bts) +{ + ats->tv_sec -= bts->tv_sec; + ats->tv_nsec -= bts->tv_nsec; + while(ats->tv_nsec < 0){ + ats->tv_sec--; + ats->tv_nsec += 1000000000; + } +} + #endif // __TIME_H diff --git a/kernel/mem.c b/kernel/mem.c index 1fadb31f..a06140cf 100644 --- a/kernel/mem.c +++ b/kernel/mem.c @@ -53,6 +53,7 @@ static unsigned long pa_start, pa_end; static struct page *pa_pages; extern int ihk_mc_pt_print_pte(struct page_table *pt, void *virt); +extern int interrupt_from_user(void *); struct tlb_flush_entry tlb_flush_vector[IHK_TLB_FLUSH_IRQ_VECTOR_SIZE]; @@ -369,6 +370,7 @@ static void page_fault_handler(void *fault_addr, uint64_t reason, void *regs) struct thread *thread = cpu_local_var(current); int error; + set_cputime(interrupt_from_user(regs)? 1: 2); dkprintf("[%d]page_fault_handler(%p,%lx,%p)\n", ihk_mc_get_processor_id(), fault_addr, reason, regs); @@ -427,6 +429,7 @@ out: ihk_mc_get_processor_id(), fault_addr, reason, regs, error); check_need_resched(); + set_cputime(0); return; } diff --git a/kernel/process.c b/kernel/process.c index 49a48f08..c609cbbb 100644 --- a/kernel/process.c +++ b/kernel/process.c @@ -2161,11 +2161,17 @@ void destroy_thread(struct thread *thread) void release_thread(struct thread *thread) { struct process_vm *vm; + struct mcs_rwlock_node lock; if (!ihk_atomic_dec_and_test(&thread->refcount)) { return; } + mcs_rwlock_writer_lock_noirq(&thread->proc->update_lock, &lock); + ts_add(&thread->proc->stime, &thread->stime); + ts_add(&thread->proc->utime, &thread->utime); + mcs_rwlock_writer_unlock_noirq(&thread->proc->update_lock, &lock); + vm = thread->vm; procfs_delete_thread(thread); @@ -2547,6 +2553,7 @@ redo: if (prev != next) { switch_ctx = 1; v->current = next; + reset_cputime(); } if (switch_ctx) { diff --git a/kernel/syscall.c b/kernel/syscall.c index 41c27e6f..6985749c 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -113,6 +113,7 @@ extern unsigned long ihk_mc_get_ns_per_tsc(void); extern int ptrace_detach(int pid, int data); extern void debug_log(unsigned long); extern void free_all_process_memory_range(struct process_vm *vm); +extern struct cpu_local_var *clv; int prepare_process_ranges_args_envs(struct thread *thread, struct program_load_desc *pn, @@ -401,6 +402,10 @@ do_wait(int pid, int *status, int options, void *rusage) ret = wait_zombie(thread, child, status, options); mcs_rwlock_writer_unlock_noirq(&thread->proc->children_lock, &lock); if(!(options & WNOWAIT)){ + mcs_rwlock_writer_lock_noirq(&proc->update_lock, &lock); + ts_add(&proc->stime, &child->stime); + ts_add(&proc->utime, &child->utime); + mcs_rwlock_writer_unlock_noirq(&proc->update_lock, &lock); release_process(child); } goto out_found; @@ -2034,6 +2039,37 @@ SYSCALL_DECLARE(set_tid_address) return cpu_local_var(current)->proc->pid; } +static unsigned long +timespec_to_jiffy(const struct timespec *ats) +{ + return ats->tv_sec * 100 + ats->tv_nsec / 10000000; +} + +SYSCALL_DECLARE(times) +{ + struct tms { + unsigned long tms_utime; + unsigned long tms_stime; + unsigned long tms_cutime; + unsigned long tms_cstime; + }; + struct tms mytms; + struct tms *buf = (struct tms *)ihk_mc_syscall_arg0(ctx); + struct thread *thread = cpu_local_var(current); + struct process *proc = thread->proc; + struct timespec ats = {0, 0}; + + mytms.tms_utime = timespec_to_jiffy(&thread->utime); + mytms.tms_stime = timespec_to_jiffy(&thread->stime); + mytms.tms_cutime = timespec_to_jiffy(&proc->utime); + mytms.tms_cstime = timespec_to_jiffy(&proc->stime); + if(copy_to_user(buf, &mytms, sizeof mytms)) + return -EFAULT; + if(gettime_local_support) + calculate_time_from_tsc(&ats); + return timespec_to_jiffy(&ats); +} + SYSCALL_DECLARE(kill) { int pid = ihk_mc_syscall_arg0(ctx); @@ -6604,13 +6640,67 @@ SYSCALL_DECLARE(pmc_reset) return ihk_mc_perfctr_reset(counter); } +void +reset_cputime() +{ + struct thread *thread; + + if(clv == NULL) + return; + + if(!(thread = cpu_local_var(current))) + return; + + thread->btime.tv_sec = 0; + thread->btime.tv_nsec = 0; +} + +void +set_cputime(int mode) +{ + struct thread *thread; + struct timespec ats; + + if(!gettime_local_support) + return; + + if(clv == NULL) + return; + + if(!(thread = cpu_local_var(current))) + return; + + calculate_time_from_tsc(&ats); + if(thread->btime.tv_sec != 0 && thread->btime.tv_nsec != 0){ + struct timespec dts; + + dts.tv_sec = ats.tv_sec; + dts.tv_nsec = ats.tv_nsec; + ts_sub(&dts, &thread->btime); + if(mode == 1) + ts_add(&thread->utime, &dts); + else + ts_add(&thread->stime, &dts); + } + if(mode == 2){ + thread->btime.tv_sec = 0; + thread->btime.tv_nsec = 0; + } + else{ + thread->btime.tv_sec = ats.tv_sec; + thread->btime.tv_nsec = ats.tv_nsec; + } +} + long syscall(int num, ihk_mc_user_context_t *ctx) { long l; + set_cputime(1); if(cpu_local_var(current)->proc->status == PS_EXITED && (num != __NR_exit && num != __NR_exit_group)){ check_signal(-EINVAL, NULL, 0); + set_cputime(0); return -EINVAL; } @@ -6669,5 +6759,6 @@ long syscall(int num, ihk_mc_user_context_t *ctx) ptrace_syscall_exit(cpu_local_var(current)); } + set_cputime(0); return l; }