fix how to count rss and num of threads

refs #864
refs #865
This commit is contained in:
Tomoki Shirasawa
2017-07-03 16:27:46 +09:00
parent 083cf3fcc9
commit 67843151d3
6 changed files with 137 additions and 92 deletions

View File

@ -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 <sysfs.h>

View File

@ -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

View File

@ -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);
}
}

View File

@ -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(

View File

@ -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;

View File

@ -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;
}