2 * orderfiles.c: order file accesses to optimise disk load
4 * Copyright (C) 2014 Colin Watson.
6 * Inspired by and loosely based on dpkg/src/filesdb.c, which is:
7 * Copyright (C) 1995 Ian Jackson <ian@chiark.greenend.org.uk>
8 * Copyright (C) 2000,2001 Wichert Akkerman <wakkerma@debian.org>
9 * Copyright (C) 2008-2014 Guillem Jover <guillem@debian.org>
11 * This file is part of man-db.
13 * man-db is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * man-db is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with man-db; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #endif /* HAVE_CONFIG_H */
32 #ifdef HAVE_LINUX_FIEMAP_H
33 # include <linux/fiemap.h>
34 # include <linux/fs.h>
35 # include <sys/ioctl.h>
37 #endif /* HAVE_LINUX_FIEMAP_H */
39 #include <sys/types.h>
47 #include "manconfig.h"
49 #include "hashtable.h"
51 struct hashtable *physical_offsets = NULL;
53 #if defined(HAVE_LINUX_FIEMAP_H)
54 int compare_physical_offsets (const void *a, const void *b)
56 const char *left = *(const char **) a;
57 const char *right = *(const char **) b;
58 uint64_t *left_offset_p = hashtable_lookup (physical_offsets,
60 uint64_t *right_offset_p = hashtable_lookup (physical_offsets,
61 right, strlen (right));
62 uint64_t left_offset = left_offset_p ? *left_offset_p : UINT64_MAX;
63 uint64_t right_offset = right_offset_p ? *right_offset_p : UINT64_MAX;
65 if (left_offset < right_offset)
67 else if (left_offset > right_offset)
73 void order_files (const char *dir, char **basenames, size_t n_basenames)
75 int dir_fd_open_flags;
80 dir_fd_open_flags = O_SEARCH | O_DIRECTORY;
82 dir_fd_open_flags |= O_PATH;
84 dir_fd = open (dir, dir_fd_open_flags);
88 if (fstatfs (dir_fd, &fs) < 0) {
93 /* Sort files by the physical locations of their first blocks, in an
94 * attempt to minimise disk drive head movements. This assumes that
95 * files are small enough that they are likely to be in one block or
96 * a small number of contiguous blocks, which seems a reasonable
97 * assumption for manual pages.
99 physical_offsets = hashtable_create (&free);
100 for (i = 0; i < n_basenames; ++i) {
102 struct fiemap fiemap;
103 struct fiemap_extent extent;
107 fd = openat (dir_fd, basenames[i], O_RDONLY);
111 memset (&fm, 0, sizeof (fm));
112 fm.fiemap.fm_start = 0;
113 fm.fiemap.fm_length = fs.f_bsize;
114 fm.fiemap.fm_flags = 0;
115 fm.fiemap.fm_extent_count = 1;
117 if (ioctl (fd, FS_IOC_FIEMAP, (unsigned long) &fm) == 0) {
118 uint64_t *offset = XMALLOC (uint64_t);
119 *offset = fm.fiemap.fm_extents[0].fe_physical;
120 hashtable_install (physical_offsets, basenames[i],
121 strlen (basenames[i]), offset);
126 qsort (basenames, n_basenames, sizeof *basenames,
127 compare_physical_offsets);
128 hashtable_free (physical_offsets);
129 physical_offsets = NULL;
132 #elif defined(HAVE_POSIX_FADVISE)
133 void order_files (const char *dir, char **basenames, size_t n_basenames)
135 int dir_fd_open_flags;
139 dir_fd_open_flags = O_SEARCH | O_DIRECTORY;
141 dir_fd_open_flags |= O_PATH;
143 dir_fd = open (dir, dir_fd_open_flags);
147 /* While we can't actually order the files, we can at least ask the
148 * kernel to preload them.
150 for (i = 0; i < n_basenames; ++i) {
151 int fd = openat (dir_fd, basenames[i], O_RDONLY | O_NONBLOCK);
153 posix_fadvise (fd, 0, 0, POSIX_FADV_WILLNEED);
161 void order_files (const char *dir, char **basenames, size_t n_basenames)