From fdd6efb69a2b6225a2405d4ca2d360f317c58daf Mon Sep 17 00:00:00 2001
From: unknown <stewart@willster.(none)>
Date: Fri, 10 Nov 2006 01:08:35 +1100
Subject: [PATCH] BUG#24143 Heavy file fragmentation with multiple ndbd on
 single fs

If we have the XFS headers (at build time) we can use XFS specific ioctls
(once testing the file is on XFS) to better allocate space.

This dramatically improves performance of mysql-test-run cases as well:

e.g.
number of extents for ndb_dd_basic tablespaces and log files
BEFORE this patch: 57, 13, 212, 95, 17, 113
WITH this patch  :  ALL 1 or 2 extents

(results are consistent over multiple runs. BEFORE always has several files
with lots of extents).

As for timing of test run:
BEFORE
ndb_dd_basic                   [ pass ]         107727
real    3m2.683s
user    0m1.360s
sys     0m1.192s

AFTER
ndb_dd_basic                   [ pass ]          70060
real    2m30.822s
user    0m1.220s
sys     0m1.404s

(results are again consistent over various runs)

similar for other tests (BEFORE and AFTER):
ndb_dd_alter                   [ pass ]         245360
ndb_dd_alter                   [ pass ]         211632


configure.in:
  Check for XFS headers
storage/ndb/src/kernel/blocks/ndbfs/AsyncFile.cpp:
  If the XFS headers are installed, use the xfsctl(XFS_IOC_RESVSP64) call (a
  wrapper around ioctl) to tell the xfs file system how much space to reserve
  for this file.
---
 configure.in                                     |  2 ++
 .../ndb/src/kernel/blocks/ndbfs/AsyncFile.cpp    | 16 ++++++++++++++++
 2 files changed, 18 insertions(+)

diff --git a/configure.in b/configure.in
index 61eb8a9d25..ce7f9cbd12 100644
--- a/configure.in
+++ b/configure.in
@@ -697,6 +697,8 @@ AC_CHECK_HEADERS(fcntl.h float.h floatingpoint.h ieeefp.h limits.h \
  sys/ioctl.h malloc.h sys/malloc.h sys/ipc.h sys/shm.h linux/config.h \
  sys/resource.h sys/param.h)
 
+AC_CHECK_HEADERS([xfs/xfs.h])
+
 #--------------------------------------------------------------------
 # Check for system libraries. Adds the library to $LIBS
 # and defines HAVE_LIBM etc
diff --git a/storage/ndb/src/kernel/blocks/ndbfs/AsyncFile.cpp b/storage/ndb/src/kernel/blocks/ndbfs/AsyncFile.cpp
index 149310d03a..89d393557c 100644
--- a/storage/ndb/src/kernel/blocks/ndbfs/AsyncFile.cpp
+++ b/storage/ndb/src/kernel/blocks/ndbfs/AsyncFile.cpp
@@ -18,6 +18,10 @@
 #include <my_sys.h>
 #include <my_pthread.h>
 
+#ifdef HAVE_XFS_XFS_H
+#include <xfs/xfs.h>
+#endif
+
 #include "AsyncFile.hpp"
 
 #include <ErrorHandlingMacros.hpp>
@@ -459,6 +463,18 @@ no_odirect:
     Uint32 index = 0;
     Uint32 block = refToBlock(request->theUserReference);
 
+#ifdef HAVE_XFS_XFS_H
+    if(platform_test_xfs_fd(theFd))
+    {
+      ndbout_c("Using xfsctl(XFS_IOC_RESVSP64) to allocate disk space");
+      xfs_flock64_t fl;
+      fl.l_whence= 0;
+      fl.l_start= 0;
+      fl.l_len= (off64_t)sz;
+      if(xfsctl(NULL, theFd, XFS_IOC_RESVSP64, &fl) < 0)
+        ndbout_c("failed to optimally allocate disk space");
+    }
+#endif
 #ifdef HAVE_POSIX_FALLOCATE
     posix_fallocate(theFd, 0, sz);
 #endif
-- 
2.30.9