From 141162907f14f4bd68a239f9e692b8583488f65d Mon Sep 17 00:00:00 2001 From: Yibo Cai Date: Fri, 15 Nov 2019 01:51:28 +0000 Subject: [PATCH] innobase/os: refine os_total_large_mem_allocated with c++11 atomics Though only atomicity is required, current code uses gcc __sync_xxx builtins which introduces unnecessary memory barriers. Refine with C++11 relaxed memory order. --- storage/innobase/include/os0proc.h | 3 ++- storage/innobase/os/os0proc.cc | 16 ++++++++-------- storage/innobase/srv/srv0srv.cc | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/storage/innobase/include/os0proc.h b/storage/innobase/include/os0proc.h index 13633bb..66356fa 100644 --- a/storage/innobase/include/os0proc.h +++ b/storage/innobase/include/os0proc.h @@ -35,6 +35,7 @@ this program; if not, write to the Free Software Foundation, Inc., #define os0proc_h #include "univ.i" +#include "os0atomic.h" #ifdef UNIV_LINUX #include @@ -46,7 +47,7 @@ typedef unsigned long int os_process_id_t; /** The total amount of memory currently allocated from the operating system with os_mem_alloc_large(). */ -extern ulint os_total_large_mem_allocated; +extern os_atomic_t os_total_large_mem_allocated; /** Whether to use large pages in the buffer pool */ extern bool os_use_large_pages; diff --git a/storage/innobase/os/os0proc.cc b/storage/innobase/os/os0proc.cc index ed46602..3aa1a7f 100644 --- a/storage/innobase/os/os0proc.cc +++ b/storage/innobase/os/os0proc.cc @@ -52,7 +52,7 @@ MAP_ANON but MAP_ANON is marked as deprecated */ /** The total amount of memory currently allocated from the operating system with os_mem_alloc_large(). */ -ulint os_total_large_mem_allocated = 0; +os_atomic_t os_total_large_mem_allocated; /** Whether to use large pages in the buffer pool */ bool os_use_large_pages; @@ -110,7 +110,7 @@ void *os_mem_alloc_large(ulint *n) { if (ptr) { *n = size; - os_atomic_increment_ulint(&os_total_large_mem_allocated, size); + os_total_large_mem_allocated.fetch_add(size, order_relaxed); UNIV_MEM_ALLOC(ptr, size); return (ptr); @@ -137,7 +137,7 @@ skip: " Windows error " << GetLastError(); } else { - os_atomic_increment_ulint(&os_total_large_mem_allocated, size); + os_total_large_mem_allocated.fetch_add(size, order_relaxed); UNIV_MEM_ALLOC(ptr, size); } #else @@ -154,7 +154,7 @@ skip: << errno; ptr = NULL; } else { - os_atomic_increment_ulint(&os_total_large_mem_allocated, size); + os_total_large_mem_allocated.fetch_add(size, order_relaxed); UNIV_MEM_ALLOC(ptr, size); } #endif @@ -165,11 +165,11 @@ skip: @param[in] ptr pointer returned by os_mem_alloc_large() @param[in] size size returned by os_mem_alloc_large() */ void os_mem_free_large(void *ptr, ulint size) { - ut_a(os_total_large_mem_allocated >= size); + ut_a(os_total_large_mem_allocated.load(order_relaxed) >= size); #if defined HAVE_LINUX_LARGE_PAGES && defined UNIV_LINUX if (os_use_large_pages && os_large_page_size && !shmdt(ptr)) { - os_atomic_decrement_ulint(&os_total_large_mem_allocated, size); + os_total_large_mem_allocated.fetch_sub(size, order_relaxed); UNIV_MEM_FREE(ptr, size); return; } @@ -181,7 +181,7 @@ void os_mem_free_large(void *ptr, ulint size) { ib::error(ER_IB_MSG_857) << "VirtualFree(" << ptr << ", " << size << ") failed; Windows error " << GetLastError(); } else { - os_atomic_decrement_ulint(&os_total_large_mem_allocated, size); + os_total_large_mem_allocated.fetch_sub(size, order_relaxed); UNIV_MEM_FREE(ptr, size); } #elif !defined OS_MAP_ANON @@ -197,7 +197,7 @@ void os_mem_free_large(void *ptr, ulint size) { " errno " << errno; } else { - os_atomic_decrement_ulint(&os_total_large_mem_allocated, size); + os_total_large_mem_allocated.fetch_sub(size, order_relaxed); UNIV_MEM_FREE(ptr, size); } #endif diff --git a/storage/innobase/srv/srv0srv.cc b/storage/innobase/srv/srv0srv.cc index 9e885bd..27ba3b1 100644 --- a/storage/innobase/srv/srv0srv.cc +++ b/storage/innobase/srv/srv0srv.cc @@ -1397,7 +1397,7 @@ ibool srv_printf_innodb_monitor( "Total large memory allocated " ULINTPF "\n" "Dictionary memory allocated " ULINTPF "\n", - os_total_large_mem_allocated, dict_sys->size); + os_total_large_mem_allocated.load(order_relaxed), dict_sys->size); buf_print_io(file); -- 2.7.4