From 67843151d3e9f0283c4a11f3a12873878543f441 Mon Sep 17 00:00:00 2001 From: Tomoki Shirasawa Date: Mon, 3 Jul 2017 16:27:46 +0900 Subject: [PATCH] fix how to count rss and num of threads refs #864 refs #865 --- kernel/ap.c | 4 -- kernel/include/rusage.h | 111 +++++++++++++++++++++++++++++++++++++++- kernel/mem.c | 48 ++++++++--------- kernel/process.c | 42 ++------------- kernel/rusage.c | 15 ------ kernel/syscall.c | 9 ---- 6 files changed, 137 insertions(+), 92 deletions(-) diff --git a/kernel/ap.c b/kernel/ap.c index 455083ec..3e243188 100644 --- a/kernel/ap.c +++ b/kernel/ap.c @@ -124,10 +124,6 @@ void ap_init(void) num_processors++; } kprintf("BSP: booted %d AP CPUs\n", cpu_info->ncpus - 1); -#ifdef ENABLE_RUSAGE - rusage_num_threads = 0; - rusage_max_num_threads = 0; -#endif } #include diff --git a/kernel/include/rusage.h b/kernel/include/rusage.h index e9a81a9e..2a8e2147 100644 --- a/kernel/include/rusage.h +++ b/kernel/include/rusage.h @@ -75,9 +75,116 @@ unsigned long rusage_hugetlb_max_usage; unsigned long rusage_numa_stat[1024]; unsigned long rusage_max_memory; -#define RUSAGE_MEM_LIMIT 2000000 +#define RUSAGE_MEM_LIMIT (2 * 1024 * 1024) // 2MB void rusage_init(); -void rusage_inc_num_threads(int count); + +#ifdef ENABLE_RUSAGE +extern void event_signal(); + +static inline void +rusage_max_memory_add(unsigned long size) +{ + rusage_max_memory += size; +} + +static inline void +rusage_rss_add(unsigned long size) +{ + unsigned long newval = __sync_add_and_fetch(&rusage_rss_current, size); + unsigned long oldval = rusage_rss_max; + unsigned long retval; + + while (newval > oldval) { + retval = __sync_val_compare_and_swap(&rusage_rss_max, oldval, + newval); + if (retval == oldval) { + if (rusage_max_memory - newval < RUSAGE_MEM_LIMIT) { + event_signal(); + } + break; + } + oldval = retval; + } +} + +static inline void +rusage_rss_sub(unsigned long size) +{ + __sync_sub_and_fetch(&rusage_rss_current, size); +} + +static inline void +rusage_numa_add(int numa_id, unsigned long size) +{ + __sync_add_and_fetch(rusage_numa_stat + numa_id, size); + rusage_rss_add(size); +} + +static inline void +rusage_numa_sub(int numa_id, unsigned long size) +{ + rusage_rss_sub(size); + __sync_sub_and_fetch(rusage_numa_stat + numa_id, size); +} + +static inline void +rusage_num_threads_inc() +{ + unsigned long newval = __sync_add_and_fetch(&rusage_num_threads, 1); + unsigned long oldval = rusage_max_num_threads; + unsigned long retval; + + while (newval > oldval) { + retval = __sync_val_compare_and_swap(&rusage_max_num_threads, + oldval, newval); + if (retval == oldval) { + break; + } + oldval = retval; + } +} + +static inline void +rusage_num_threads_dec() +{ + __sync_sub_and_fetch(&rusage_num_threads, 1); +} +#else +static inline void +rusage_max_memory_add(unsigned long size) +{ +} + +static inline void +rusage_rss_add(unsigned long size) +{ +} + +static inline void +rusage_rss_sub(unsigned long size) +{ +} + +static inline void +rusage_numa_add(int numa_id, unsigned long size) +{ +} + +static inline void +rusage_numa_sub(int numa_id, unsigned long size) +{ +} + +static inline void +rusage_num_threads_inc() +{ +} + +static inline void +rusage_num_threads_dec() +{ +} +#endif // ENABLE_RUSAGE #endif diff --git a/kernel/mem.c b/kernel/mem.c index 185db06e..db93c7fb 100644 --- a/kernel/mem.c +++ b/kernel/mem.c @@ -499,6 +499,7 @@ static void *mckernel_allocate_aligned_pages_node(int npages, int p2align, unsigned long pa = 0; int i, node; struct ihk_page_allocator_desc *pa_allocator; + int numa_id; /* Not yet initialized or idle process */ if (!cpu_local_var_initialized || @@ -530,6 +531,8 @@ static void *mckernel_allocate_aligned_pages_node(int npages, int p2align, ihk_mc_get_numa_id(), npages, node); + rusage_numa_add(pref_node, npages * PAGE_SIZE); + return phys_to_virt(pa); } else { @@ -556,9 +559,11 @@ static void *mckernel_allocate_aligned_pages_node(int npages, int p2align, continue; } + numa_id = memory_nodes[node]. + nodes_by_distance[i].id; list_for_each_entry(pa_allocator, - &memory_nodes[memory_nodes[node]. - nodes_by_distance[i].id].allocators, list) { + &memory_nodes[numa_id]. + allocators, list) { pa = ihk_pagealloc_alloc(pa_allocator, npages, p2align); if (pa) { @@ -567,9 +572,10 @@ static void *mckernel_allocate_aligned_pages_node(int npages, int p2align, __FUNCTION__, ihk_mc_get_numa_id(), npages, node); -#ifdef ENABLE_RUSAGE - rusage_numa_stat[ihk_mc_get_numa_id()] += npages * PAGE_SIZE; -#endif + + rusage_numa_add(numa_id, + npages * PAGE_SIZE); + break; } } @@ -605,10 +611,9 @@ distance_based: goto order_based; for (i = 0; i < ihk_mc_get_nr_numa_nodes(); ++i) { - + numa_id = memory_nodes[node].nodes_by_distance[i].id; list_for_each_entry(pa_allocator, - &memory_nodes[memory_nodes[node]. - nodes_by_distance[i].id].allocators, list) { + &memory_nodes[numa_id].allocators, list) { pa = ihk_pagealloc_alloc(pa_allocator, npages, p2align); if (pa) { @@ -618,9 +623,7 @@ distance_based: ihk_mc_get_numa_id(), npages, memory_nodes[node].nodes_by_distance[i].id); -#ifdef ENABLE_RUSAGE - rusage_numa_stat[ihk_mc_get_numa_id()] += npages * PAGE_SIZE; -#endif + rusage_numa_add(numa_id, npages * PAGE_SIZE); break; } } @@ -636,15 +639,14 @@ order_based: /* Fall back to regular order */ for (i = 0; i < ihk_mc_get_nr_numa_nodes(); ++i) { - + numa_id = (node + i) % ihk_mc_get_nr_numa_nodes(); list_for_each_entry(pa_allocator, - &memory_nodes[(node + i) % - ihk_mc_get_nr_numa_nodes()].allocators, list) { + &memory_nodes[numa_id].allocators, list) { pa = ihk_pagealloc_alloc(pa_allocator, npages, p2align); -#ifdef ENABLE_RUSAGE - rusage_numa_stat[ihk_mc_get_numa_id()] += npages * PAGE_SIZE; -#endif - if (pa) break; + if (pa) { + rusage_numa_add(numa_id, npages * PAGE_SIZE); + break; + } } if (pa) break; @@ -675,9 +677,7 @@ static void __mckernel_free_pages_in_allocator(void *va, int npages) if (pa_start >= pa_allocator->start && pa_end <= pa_allocator->end) { ihk_pagealloc_free(pa_allocator, pa_start, npages); -#ifdef ENABLE_RUSAGE - rusage_numa_stat[i] -= npages * PAGE_SIZE; -#endif + rusage_numa_sub(i, npages * PAGE_SIZE); return; } } @@ -1102,9 +1102,9 @@ static void numa_init(void) ihk_pagealloc_count(allocator) * PAGE_SIZE, ihk_pagealloc_count(allocator), numa_id); -#ifdef ENABLE_RUSAGE - rusage_max_memory += ihk_pagealloc_count(allocator) * PAGE_SIZE; -#endif + + rusage_max_memory_add(ihk_pagealloc_count(allocator) * + PAGE_SIZE); } } diff --git a/kernel/process.c b/kernel/process.c index f1ccf6c5..4c2fea9d 100644 --- a/kernel/process.c +++ b/kernel/process.c @@ -333,9 +333,7 @@ struct thread *create_thread(unsigned long user_pc, ihk_mc_spinlock_init(&thread->spin_sleep_lock); thread->spin_sleep = 0; -#ifdef ENABLE_RUSAGE - rusage_inc_num_threads(1); -#endif + return thread; err: @@ -486,10 +484,6 @@ clone_thread(struct thread *org, unsigned long pc, unsigned long sp, ihk_mc_spinlock_init(&thread->spin_sleep_lock); thread->spin_sleep = 0; -#ifdef ENABLE_RUSAGE - rusage_inc_num_threads(1); -#endif - #ifdef PROFILE_ENABLE thread->profile = org->profile | proc->profile; #endif @@ -2010,18 +2004,6 @@ int init_process_stack(struct thread *thread, struct program_load_desc *pn, thread->vm->region.stack_end = end; thread->vm->region.stack_start = start; -#ifdef ENABLE_RUSAGE -{ - long curr; - curr = ihk_atomic_add_long_return ((minsz >> PAGE_SHIFT) * PAGE_SIZE, &rusage_rss_current); - if (rusage_rss_max < curr) { - atomic_cmpxchg8(&rusage_rss_max, rusage_rss_max, curr); - } - if (rusage_max_memory - curr < RUSAGE_MEM_LIMIT) { - event_signal(); - } -} -#endif return 0; } @@ -2071,19 +2053,6 @@ unsigned long extend_process_region(struct process_vm *vm, dkprintf("%s: new_end_allocated: 0x%lu, align_size: %lu, align_mask: %lx\n", __FUNCTION__, new_end_allocated, align_size, align_mask); -#ifdef ENABLE_RUSAGE -{ - long curr; - curr = ihk_atomic_add_long_return (((new_end_allocated - end_allocated) >> PAGE_SHIFT) * PAGE_SIZE, &rusage_rss_current); - if (rusage_rss_max < curr) { - atomic_cmpxchg8(&rusage_rss_max, rusage_rss_max, curr); - } - if (rusage_max_memory - curr < RUSAGE_MEM_LIMIT) { - event_signal(); - } -} -#endif - return new_end_allocated; } @@ -2385,12 +2354,6 @@ void destroy_thread(struct thread *thread) release_sigcommon(thread->sigcommon); -#ifdef ENABLE_RUSAGE -{ - ihk_atomic_add_ulong ( -1, &rusage_num_threads); -} -#endif - ihk_mc_free_pages(thread, KERNEL_STACK_NR_PAGES); } @@ -2422,6 +2385,7 @@ void release_thread(struct thread *thread) destroy_thread(thread); release_process_vm(vm); + rusage_num_threads_dec(); } void cpu_set(int cpu, cpu_set_t *cpu_set, ihk_spinlock_t *lock) @@ -3137,6 +3101,8 @@ void runq_add_thread(struct thread *thread, int cpu_id) procfs_create_thread(thread); + rusage_num_threads_inc(); + /* Kick scheduler */ if (cpu_id != ihk_mc_get_processor_id()) ihk_mc_interrupt_cpu( diff --git a/kernel/rusage.c b/kernel/rusage.c index adf5f360..204705cb 100644 --- a/kernel/rusage.c +++ b/kernel/rusage.c @@ -50,21 +50,6 @@ void rusage_init() { rusage_rss_max = 0; } -void rusage_inc_num_threads(int count) { - volatile unsigned long max_obs1, max_obs2; - ihk_atomic_add_ulong(count, &rusage_num_threads); - max_obs1 = rusage_max_num_threads; - if (max_obs1 < rusage_num_threads) { - retry: - max_obs2 = atomic_cmpxchg8(&rusage_max_num_threads, max_obs1, rusage_num_threads); - if(max_obs2 != max_obs1 && - max_obs2 < rusage_num_threads) { - max_obs1 = max_obs2; - goto retry; - } - } -} - /* count total rss */ unsigned long count_rss () { return rusage_rss_current; diff --git a/kernel/syscall.c b/kernel/syscall.c index 64fcbd29..208faae6 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -4909,9 +4909,6 @@ do_exit(int code) if(nproc == 1){ // process has only one thread terminate(exit_status, sig); -#ifdef ENABLE_RUSAGE - rusage_num_threads--; -#endif return; } @@ -4938,9 +4935,6 @@ do_exit(int code) if(proc->status == PS_EXITED){ mcs_rwlock_reader_unlock(&proc->threads_lock, &lock); terminate(exit_status, 0); -#ifdef ENABLE_RUSAGE - rusage_num_threads--; -#endif return; } preempt_disable(); @@ -4951,9 +4945,6 @@ do_exit(int code) preempt_enable(); schedule(); -#ifdef ENABLE_RUSAGE - rusage_num_threads--; -#endif return; }