summaryrefslogtreecommitdiff
path: root/storage/xtradb/os/os0proc.c
diff options
context:
space:
mode:
Diffstat (limited to 'storage/xtradb/os/os0proc.c')
-rw-r--r--storage/xtradb/os/os0proc.c172
1 files changed, 172 insertions, 0 deletions
diff --git a/storage/xtradb/os/os0proc.c b/storage/xtradb/os/os0proc.c
index 48922886f23..c101db3d179 100644
--- a/storage/xtradb/os/os0proc.c
+++ b/storage/xtradb/os/os0proc.c
@@ -229,3 +229,175 @@ os_mem_free_large(
}
#endif
}
+
+/****************************************************************//**
+Allocates or attaches and reuses shared memory segment.
+The content is not cleared automatically.
+@return allocated memory */
+UNIV_INTERN
+void*
+os_shm_alloc(
+/*=========*/
+ ulint* n, /*!< in/out: number of bytes */
+ uint key,
+ ibool* is_new)
+{
+ void* ptr;
+#if defined HAVE_SYS_IPC_H && HAVE_SYS_SHM_H
+ ulint size;
+ int shmid;
+#endif
+
+ *is_new = FALSE;
+#if defined HAVE_SYS_IPC_H && HAVE_SYS_SHM_H
+ fprintf(stderr,
+ "InnoDB: The shared memory key %#x (%d) is specified.\n",
+ key, key);
+# if defined HAVE_LARGE_PAGES && defined UNIV_LINUX
+ if (!os_use_large_pages || !os_large_page_size) {
+ goto skip;
+ }
+
+ /* Align block size to os_large_page_size */
+ ut_ad(ut_is_2pow(os_large_page_size));
+ size = ut_2pow_round(*n + (os_large_page_size - 1),
+ os_large_page_size);
+
+ shmid = shmget((key_t)key, (size_t)size,
+ IPC_CREAT | IPC_EXCL | SHM_HUGETLB | SHM_R | SHM_W);
+ if (shmid < 0) {
+ if (errno == EEXIST) {
+ fprintf(stderr,
+ "InnoDB: HugeTLB: The shared memory segment seems to exist already.\n");
+ shmid = shmget((key_t)key, (size_t)size,
+ SHM_HUGETLB | SHM_R | SHM_W);
+ if (shmid < 0) {
+ fprintf(stderr,
+ "InnoDB: HugeTLB: Warning: Failed to allocate %lu bytes.(reuse) errno %d\n",
+ size, errno);
+ goto skip;
+ } else {
+ fprintf(stderr,
+ "InnoDB: HugeTLB: The existent shared memory segment is used.\n");
+ }
+ } else {
+ fprintf(stderr,
+ "InnoDB: HugeTLB: Warning: Failed to allocate %lu bytes.(new) errno %d\n",
+ size, errno);
+ goto skip;
+ }
+ } else {
+ *is_new = TRUE;
+ fprintf(stderr,
+ "InnoDB: HugeTLB: The new shared memory segment is created.\n");
+ }
+
+ ptr = shmat(shmid, NULL, 0);
+ if (ptr == (void *)-1) {
+ fprintf(stderr,
+ "InnoDB: HugeTLB: Warning: Failed to attach shared memory segment, errno %d\n",
+ errno);
+ ptr = NULL;
+ }
+
+ if (ptr) {
+ *n = size;
+ os_fast_mutex_lock(&ut_list_mutex);
+ ut_total_allocated_memory += size;
+ os_fast_mutex_unlock(&ut_list_mutex);
+ UNIV_MEM_ALLOC(ptr, size);
+ return(ptr);
+ }
+skip:
+ *is_new = FALSE;
+# endif /* HAVE_LARGE_PAGES && defined UNIV_LINUX */
+# ifdef HAVE_GETPAGESIZE
+ size = getpagesize();
+# else
+ size = UNIV_PAGE_SIZE;
+# endif
+ /* Align block size to system page size */
+ ut_ad(ut_is_2pow(size));
+ size = *n = ut_2pow_round(*n + (size - 1), size);
+
+ shmid = shmget((key_t)key, (size_t)size,
+ IPC_CREAT | IPC_EXCL | SHM_R | SHM_W);
+ if (shmid < 0) {
+ if (errno == EEXIST) {
+ fprintf(stderr,
+ "InnoDB: The shared memory segment seems to exist already.\n");
+ shmid = shmget((key_t)key, (size_t)size,
+ SHM_R | SHM_W);
+ if (shmid < 0) {
+ fprintf(stderr,
+ "InnoDB: Warning: Failed to allocate %lu bytes.(reuse) errno %d\n",
+ size, errno);
+ ptr = NULL;
+ goto end;
+ } else {
+ fprintf(stderr,
+ "InnoDB: The existent shared memory segment is used.\n");
+ }
+ } else {
+ fprintf(stderr,
+ "InnoDB: Warning: Failed to allocate %lu bytes.(new) errno %d\n",
+ size, errno);
+ ptr = NULL;
+ goto end;
+ }
+ } else {
+ *is_new = TRUE;
+ fprintf(stderr,
+ "InnoDB: The new shared memory segment is created.\n");
+ }
+
+ ptr = shmat(shmid, NULL, 0);
+ if (ptr == (void *)-1) {
+ fprintf(stderr,
+ "InnoDB: Warning: Failed to attach shared memory segment, errno %d\n",
+ errno);
+ ptr = NULL;
+ }
+
+ if (ptr) {
+ *n = size;
+ os_fast_mutex_lock(&ut_list_mutex);
+ ut_total_allocated_memory += size;
+ os_fast_mutex_unlock(&ut_list_mutex);
+ UNIV_MEM_ALLOC(ptr, size);
+ }
+end:
+#else /* HAVE_SYS_IPC_H && HAVE_SYS_SHM_H */
+ fprintf(stderr, "InnoDB: shared memory segment is not supported.\n");
+ ptr = NULL;
+#endif /* HAVE_SYS_IPC_H && HAVE_SYS_SHM_H */
+ return(ptr);
+}
+
+/****************************************************************//**
+Detach shared memory segment. */
+UNIV_INTERN
+void
+os_shm_free(
+/*========*/
+ void *ptr, /*!< in: pointer returned by
+ os_shm_alloc() */
+ ulint size) /*!< in: size returned by
+ os_shm_alloc() */
+{
+ os_fast_mutex_lock(&ut_list_mutex);
+ ut_a(ut_total_allocated_memory >= size);
+ os_fast_mutex_unlock(&ut_list_mutex);
+
+#if defined HAVE_SYS_IPC_H && HAVE_SYS_SHM_H
+ if (!shmdt(ptr)) {
+ os_fast_mutex_lock(&ut_list_mutex);
+ ut_a(ut_total_allocated_memory >= size);
+ ut_total_allocated_memory -= size;
+ os_fast_mutex_unlock(&ut_list_mutex);
+ UNIV_MEM_FREE(ptr, size);
+ }
+#else /* HAVE_SYS_IPC_H && HAVE_SYS_SHM_H */
+ fprintf(stderr, "InnoDB: shared memory segment is not supported.\n");
+#endif /* HAVE_SYS_IPC_H && HAVE_SYS_SHM_H */
+}