]> git.sesse.net Git - vlc/blob - modules/access/mmap.c
mmap: Use fnctl to set the READAHEAD flag when supported. This should speedup the...
[vlc] / modules / access / mmap.c
1 /*****************************************************************************
2  * mmap.c: memory-mapped file input
3  *****************************************************************************
4  * Copyright © 2007-2008 Rémi Denis-Courmont
5  * $Id$
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <vlc_common.h>
27 #include <vlc_plugin.h>
28 #include <vlc_access.h>
29 #include <vlc_input.h>
30 #include <vlc_charset.h>
31 #include <vlc_interface.h>
32
33 #include <assert.h>
34
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <sys/stat.h>
40 #include <sys/mman.h>
41
42 #define FILE_MMAP_TEXT N_("Use file memory mapping")
43 #define FILE_MMAP_LONGTEXT N_( \
44     "Try to use memory mapping to read files and block devices." )
45
46 #ifndef NDEBUG
47 /*# define MMAP_DEBUG 1*/
48 #endif
49
50 static int Open (vlc_object_t *);
51 static void Close (vlc_object_t *);
52
53 vlc_module_begin();
54     set_shortname (N_("MMap"));
55     set_description (N_("Memory-mapped file input"));
56     set_category (CAT_INPUT);
57     set_subcategory (SUBCAT_INPUT_ACCESS);
58     set_capability ("access", 52);
59     add_shortcut ("file");
60     set_callbacks (Open, Close);
61
62     add_bool ("file-mmap", true, NULL,
63               FILE_MMAP_TEXT, FILE_MMAP_LONGTEXT, true);
64 vlc_module_end();
65
66 static block_t *Block (access_t *);
67 static int Seek (access_t *, int64_t);
68 static int Control (access_t *, int, va_list);
69
70 struct access_sys_t
71 {
72     size_t page_size;
73     size_t mtu;
74     int    fd;
75 };
76
77 #define MMAP_SIZE (1 << 20)
78
79 static int Open (vlc_object_t *p_this)
80 {
81     access_t *p_access = (access_t *)p_this;
82     access_sys_t *p_sys;
83     const char *path = p_access->psz_path;
84     int fd;
85
86     if (!var_CreateGetBool (p_this, "file-mmap"))
87         return VLC_EGENERIC; /* disabled */
88
89     STANDARD_BLOCK_ACCESS_INIT;
90
91     if (!strcmp (p_access->psz_path, "-"))
92         fd = dup (0);
93     else
94     {
95         msg_Dbg (p_access, "opening file %s", path);
96         fd = utf8_open (path, O_RDONLY | O_NOCTTY, 0666);
97     }
98
99     if (fd == -1)
100     {
101         msg_Warn (p_access, "cannot open %s: %m", path);
102         goto error;
103     }
104
105     /* mmap() is only safe for regular and block special files.
106      * For other types, it may be some idiosyncrasic interface (e.g. packet
107      * sockets are a good example), if it works at all. */
108     struct stat st;
109
110     if (fstat (fd, &st))
111     {
112         msg_Err (p_access, "cannot stat %s: %m", path);
113         goto error;
114     }
115
116     if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode))
117     {
118         msg_Dbg (p_access, "skipping non regular file %s", path);
119         goto error;
120     }
121
122 # if defined(HAVE_FCNTL_H) && defined(F_FDAHEAD) && defined(F_NOCACHE)
123     /* We'd rather use any available memory for reading ahead
124      * than for caching what we've already mmap'ed */
125     fcntl (fd, F_RDAHEAD, 1);
126     fcntl (fd, F_NOCACHE, 1);
127 # endif
128
129     /* Autodetect mmap() support */
130     if (st.st_size > 0)
131     {
132         void *addr = mmap (NULL, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
133         if (addr != MAP_FAILED)
134             munmap (addr, 1);
135         else
136             goto error;
137     }
138
139     p_sys->page_size = sysconf (_SC_PAGE_SIZE);
140     p_sys->mtu = MMAP_SIZE;
141     if (p_sys->mtu < p_sys->page_size)
142         p_sys->mtu = p_sys->page_size;
143     p_sys->fd = fd;
144
145     p_access->info.i_size = st.st_size;
146 #ifdef HAVE_POSIX_FADVISE    
147     posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
148 #endif 
149
150     return VLC_SUCCESS;
151
152 error:
153     if (fd != -1)
154         close (fd);
155     free (p_sys);
156     return VLC_EGENERIC;
157 }
158
159
160 static void Close (vlc_object_t * p_this)
161 {
162     access_t *p_access = (access_t *)p_this;
163     access_sys_t *p_sys = p_access->p_sys;
164
165     close (p_sys->fd); /* don't care about error when only reading */
166     free (p_sys);
167 }
168
169 static block_t *Block (access_t *p_access)
170 {
171     access_sys_t *p_sys = p_access->p_sys;
172
173     /* Check if file size changed... */
174     struct stat st;
175
176     if ((fstat (p_sys->fd, &st) == 0)
177      && (st.st_size != p_access->info.i_size))
178     {
179         p_access->info.i_size = st.st_size;
180         p_access->info.i_update |= INPUT_UPDATE_SIZE;
181     }
182
183     if ((uint64_t)p_access->info.i_pos >= (uint64_t)p_access->info.i_size)
184     {
185         /* We are at end of file */
186         p_access->info.b_eof = true;
187         msg_Dbg (p_access, "at end of memory mapped file");
188         return NULL;
189     }
190
191 #ifdef MMAP_DEBUG
192     int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR);
193     if (dbgpos != p_access->info.i_pos)
194         msg_Err (p_access, "position: 0x%08llx instead of 0x%08llx",
195                  p_access->info.i_pos, dbgpos);
196 #endif
197
198     const uintptr_t page_mask = p_sys->page_size - 1;
199     /* Start the mapping on a page boundary: */
200     off_t  outer_offset = p_access->info.i_pos & ~page_mask;
201     /* Skip useless bytes at the beginning of the first page: */
202     size_t inner_offset = p_access->info.i_pos & page_mask;
203     /* Map no more bytes than remain: */
204     size_t length = p_sys->mtu;
205     if (outer_offset + length > p_access->info.i_size)
206         length = p_access->info.i_size - outer_offset;
207
208     assert (outer_offset <= p_access->info.i_pos);          /* and */
209     assert (p_access->info.i_pos < p_access->info.i_size); /* imply */
210     assert (outer_offset < p_access->info.i_size);         /* imply */
211     assert (length > 0);
212
213     /* NOTE: We use PROT_WRITE and MAP_PRIVATE so that the block can be
214      * modified down the chain, without messing up with the underlying
215      * original file. This does NOT need open write permission. */
216     void *addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE,
217                        p_sys->fd, outer_offset);
218     if (addr == MAP_FAILED)
219     {
220         msg_Err (p_access, "memory mapping failed (%m)");
221         intf_UserFatal (p_access, false, _("File reading failed"),
222                         _("VLC could not read the file."));
223         goto fatal;
224     }
225 #ifdef HAVE_POSIX_MADVISE    
226     posix_madvise (addr, length, POSIX_MADV_SEQUENTIAL);
227 #endif
228
229     block_t *block = block_mmap_Alloc (addr, length);
230     if (block == NULL)
231         goto fatal;
232
233     block->p_buffer += inner_offset;
234     block->i_buffer -= inner_offset;
235
236 #ifdef MMAP_DEBUG
237     msg_Dbg (p_access, "mapped 0x%lx bytes at %p from offset 0x%lx",
238              (unsigned long)length, addr, (unsigned long)outer_offset);
239
240     /* Compare normal I/O with memory mapping */
241     char *buf = malloc (block->i_buffer);
242     ssize_t i_read = read (p_sys->fd, buf, block->i_buffer);
243
244     if (i_read != (ssize_t)block->i_buffer)
245         msg_Err (p_access, "read %u instead of %u bytes", (unsigned)i_read,
246                  (unsigned)block->i_buffer);
247     if (memcmp (buf, block->p_buffer, block->i_buffer))
248         msg_Err (p_access, "inconsistent data buffer");
249     free (buf);
250 #endif
251
252     p_access->info.i_pos = outer_offset + length;
253     return block;
254
255 fatal:
256     p_access->info.b_eof = true;
257     return NULL;
258 }
259
260
261 static int Seek (access_t *p_access, int64_t i_pos)
262 {
263 #ifdef MMAP_DEBUG
264     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
265 #endif
266
267     p_access->info.i_pos = i_pos;
268     p_access->info.b_eof = false;
269     return VLC_SUCCESS;
270 }
271
272
273 static int Control (access_t *p_access, int query, va_list args)
274 {
275     access_sys_t *p_sys = p_access->p_sys;
276
277     switch (query)
278     {
279         case ACCESS_CAN_SEEK:
280         case ACCESS_CAN_FASTSEEK:
281         case ACCESS_CAN_PAUSE:
282         case ACCESS_CAN_CONTROL_PACE:
283             *((bool *)va_arg (args, bool *)) = true;
284             return VLC_SUCCESS;
285
286         case ACCESS_GET_MTU:
287             *((int *)va_arg (args, int *)) = p_sys->mtu;
288             return VLC_SUCCESS;
289
290         case ACCESS_GET_PTS_DELAY:
291         {
292             int delay_ms = var_CreateGetInteger (p_access, "file-caching");
293             *((int64_t *)va_arg (args, int64_t *)) = delay_ms * INT64_C (1000);
294             return VLC_SUCCESS;
295         }
296
297         case ACCESS_GET_TITLE_INFO:
298         case ACCESS_GET_META:
299             break;
300
301         case ACCESS_SET_PAUSE_STATE:
302             return VLC_SUCCESS;
303
304         case ACCESS_SET_TITLE:
305         case ACCESS_SET_SEEKPOINT:
306         case ACCESS_SET_PRIVATE_ID_STATE:
307         case ACCESS_SET_PRIVATE_ID_CA:
308         case ACCESS_GET_PRIVATE_ID_STATE:
309         case ACCESS_GET_CONTENT_TYPE:
310             break;
311
312         default:
313             msg_Warn (p_access, "unimplemented query %d in control", query);
314     }
315
316     return VLC_EGENERIC;
317 }