From c6ab3296c0436830dd7b79db59959ef04b3bb6f4 Mon Sep 17 00:00:00 2001
From: Daniel Black <daniel@linux.ibm.com>
Date: Tue, 26 Nov 2019 10:14:48 +1100
Subject: [PATCH] define linker section for global variables POC

---
 include/my_attribute.h          |  1 -
 include/my_compiler.h           |  2 ++
 sql/CMakeLists.txt              |  4 ++++
 sql/mysqld.ld                   | 21 +++++++++++++++++++++
 storage/innobase/btr/btr0sea.cc |  4 ++--
 5 files changed, 29 insertions(+), 3 deletions(-)
 create mode 100644 sql/mysqld.ld

diff --git a/include/my_attribute.h b/include/my_attribute.h
index c8e980702b6..0b8a1b7b64d 100644
--- a/include/my_attribute.h
+++ b/include/my_attribute.h
@@ -70,5 +70,4 @@
 # endif /* GNUC >= 3.1 */
 #endif
 
-
 #endif
diff --git a/include/my_compiler.h b/include/my_compiler.h
index 902ef6992a2..fdeadff4e6b 100644
--- a/include/my_compiler.h
+++ b/include/my_compiler.h
@@ -171,4 +171,6 @@ rarely invoked function for size instead for speed. */
 
 #include <my_attribute.h>
 
+#define MY_GLOBAL __attribute((section (".data.read_mostly")))
+
 #endif /* MY_COMPILER_INCLUDED */
diff --git a/sql/CMakeLists.txt b/sql/CMakeLists.txt
index 83a1ea0abe7..7f18e6bb42c 100644
--- a/sql/CMakeLists.txt
+++ b/sql/CMakeLists.txt
@@ -309,6 +309,10 @@ ENDIF(NOT WITHOUT_DYNAMIC_PLUGINS)
 
 TARGET_LINK_LIBRARIES(mysqld LINK_PRIVATE sql sql_builtins)
 
+IF(NOT MSVC)
+  SET_TARGET_PROPERTIES(mysqld PROPERTIES LINK_DEPENDS ${CMAKE_SOURCE_DIR}/sql/mysqld.ld)
+  TARGET_LINK_OPTIONS(mysqld BEFORE PRIVATE LINKER:-T,${CMAKE_SOURCE_DIR}/sql/mysqld.ld)
+ENDIF()
 
 # Provide plugins with minimal set of libraries
 SET(INTERFACE_LIBS ${LIBRT})
diff --git a/sql/mysqld.ld b/sql/mysqld.ld
new file mode 100644
index 00000000000..b7bdc64ea24
--- /dev/null
+++ b/sql/mysqld.ld
@@ -0,0 +1,21 @@
+/*
+
+Linker script to create a .data.read_mostly section with ELF linkers.
+
+This, with the approprate variable attributes, keeps read only variables being
+linked adjacent to hot variable (like pseudo number or statistic counters).
+
+256 (bytes) is from the largest cache-line size (s390) on current hardware.
+This ensures that the section afterwards is on a different cache line.  As
+we are inserting after .rodata, an initial alignment isn't needed.
+
+*/
+
+SECTIONS
+{
+    .data.read_mostly : {
+      *(.data.read_mostly)
+      . = ALIGN(256);
+    }
+}
+INSERT AFTER .rodata
diff --git a/storage/innobase/btr/btr0sea.cc b/storage/innobase/btr/btr0sea.cc
index f073dbccdbd..e3d6a060275 100644
--- a/storage/innobase/btr/btr0sea.cc
+++ b/storage/innobase/btr/btr0sea.cc
@@ -45,10 +45,10 @@ Created 2/17/1996 Heikki Tuuri
 
 /** Is search system enabled.
 Search system is protected by array of latches. */
-char		btr_search_enabled;
+char		btr_search_enabled MY_GLOBAL;
 
 /** Number of adaptive hash index partition. */
-ulong		btr_ahi_parts;
+ulong		btr_ahi_parts MY_GLOBAL;
 
 #ifdef UNIV_SEARCH_PERF_STAT
 /** Number of successful adaptive hash index lookups */
-- 
2.21.0

