]> git.sesse.net Git - vlc/blob - modules/access/mmap.c
mmap: default enable the module on Darwin
[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_dialog.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 #ifdef __APPLE__
62     add_bool ("file-mmap", true, NULL,
63               FILE_MMAP_TEXT, FILE_MMAP_LONGTEXT, true)
64 #else
65     add_bool ("file-mmap", false, NULL,
66               FILE_MMAP_TEXT, FILE_MMAP_LONGTEXT, true)
67 #endif
68 vlc_module_end ()
69
70 static block_t *Block (access_t *);
71 static int Seek (access_t *, int64_t);
72 static int Control (access_t *, int, va_list);
73
74 struct access_sys_t
75 {
76     size_t page_size;
77     size_t mtu;
78     int    fd;
79 };
80
81 #define MMAP_SIZE (1 << 20)
82
83 static int Open (vlc_object_t *p_this)
84 {
85     access_t *p_access = (access_t *)p_this;
86     access_sys_t *p_sys;
87     const char *path = p_access->psz_path;
88     int fd;
89
90     assert ((INT64_C(1) << 63) == ((off_t)(INT64_C(1) << 63)));
91
92     if (!var_CreateGetBool (p_this, "file-mmap"))
93         return VLC_EGENERIC; /* disabled */
94
95     STANDARD_BLOCK_ACCESS_INIT;
96
97     if (!strcmp (p_access->psz_path, "-"))
98         fd = dup (0);
99     else
100     {
101         msg_Dbg (p_access, "opening file %s", path);
102         fd = utf8_open (path, O_RDONLY | O_NOCTTY);
103     }
104
105     if (fd == -1)
106     {
107         msg_Warn (p_access, "cannot open %s: %m", path);
108         goto error;
109     }
110
111     /* mmap() is only safe for regular and block special files.
112      * For other types, it may be some idiosyncrasic interface (e.g. packet
113      * sockets are a good example), if it works at all. */
114     struct stat st;
115
116     if (fstat (fd, &st))
117     {
118         msg_Err (p_access, "cannot stat %s: %m", path);
119         goto error;
120     }
121
122     if (!S_ISREG (st.st_mode) && !S_ISBLK (st.st_mode))
123     {
124         msg_Dbg (p_access, "skipping non-regular file %s", path);
125         goto error;
126     }
127
128 #if defined(HAVE_FCNTL_H)
129     /* We'd rather use any available memory for reading ahead
130      * than for caching what we've already mmap'ed */
131 # if defined(F_RDAHEAD)
132     fcntl (fd, F_RDAHEAD, 1);
133 # endif
134 # if defined(F_NOCACHE)
135     fcntl (fd, F_NOCACHE, 1);
136 # endif
137 #endif
138
139     /* Autodetect mmap() support */
140     if (st.st_size > 0)
141     {
142         void *addr = mmap (NULL, 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
143         if (addr != MAP_FAILED)
144             munmap (addr, 1);
145         else
146             goto error;
147     }
148
149     p_sys->page_size = sysconf (_SC_PAGE_SIZE);
150     p_sys->mtu = MMAP_SIZE;
151     if (p_sys->mtu < p_sys->page_size)
152         p_sys->mtu = p_sys->page_size;
153     p_sys->fd = fd;
154
155     p_access->info.i_size = st.st_size;
156 #ifdef HAVE_POSIX_FADVISE    
157     posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
158 #endif 
159
160     return VLC_SUCCESS;
161
162 error:
163     if (fd != -1)
164         close (fd);
165     free (p_sys);
166     return VLC_EGENERIC;
167 }
168
169
170 static void Close (vlc_object_t * p_this)
171 {
172     access_t *p_access = (access_t *)p_this;
173     access_sys_t *p_sys = p_access->p_sys;
174
175     close (p_sys->fd); /* don't care about error when only reading */
176     free (p_sys);
177 }
178
179 static block_t *Block (access_t *p_access)
180 {
181     access_sys_t *p_sys = p_access->p_sys;
182
183     /* Check if file size changed... */
184     struct stat st;
185
186     if ((fstat (p_sys->fd, &st) == 0)
187      && (st.st_size != p_access->info.i_size))
188     {
189         p_access->info.i_size = st.st_size;
190         p_access->info.i_update |= INPUT_UPDATE_SIZE;
191     }
192
193     if ((uint64_t)p_access->info.i_pos >= (uint64_t)p_access->info.i_size)
194     {
195         /* We are at end of file */
196         p_access->info.b_eof = true;
197         msg_Dbg (p_access, "at end of memory mapped file");
198         return NULL;
199     }
200
201 #ifdef MMAP_DEBUG
202     int64_t dbgpos = lseek (p_sys->fd, 0, SEEK_CUR);
203     if (dbgpos != p_access->info.i_pos)
204         msg_Err (p_access, "position: 0x%016"PRIx64" instead of 0x%016"PRIx64,
205                  p_access->info.i_pos, dbgpos);
206 #endif
207
208     const uintptr_t page_mask = p_sys->page_size - 1;
209     /* Start the mapping on a page boundary: */
210     off_t  outer_offset = p_access->info.i_pos & ~(off_t)page_mask;
211     /* Skip useless bytes at the beginning of the first page: */
212     size_t inner_offset = p_access->info.i_pos & page_mask;
213     /* Map no more bytes than remain: */
214     size_t length = p_sys->mtu;
215     if (outer_offset + length > p_access->info.i_size)
216         length = p_access->info.i_size - outer_offset;
217
218     assert (outer_offset <= p_access->info.i_pos);          /* and */
219     assert (p_access->info.i_pos < p_access->info.i_size); /* imply */
220     assert (outer_offset < p_access->info.i_size);         /* imply */
221     assert (length > 0);
222
223     /* NOTE: We use PROT_WRITE and MAP_PRIVATE so that the block can be
224      * modified down the chain, without messing up with the underlying
225      * original file. This does NOT need open write permission. */
226     void *addr = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_PRIVATE
227 #ifdef MAP_NO_CACHE
228                        | MAP_NOCACHE
229 #endif
230                        , p_sys->fd, outer_offset);
231     if (addr == MAP_FAILED)
232     {
233         msg_Err (p_access, "memory mapping failed (%m)");
234         dialog_Fatal (p_access, _("File reading failed"), "%s",
235                         _("VLC could not read the file."));
236         goto fatal;
237     }
238 #ifdef HAVE_POSIX_MADVISE    
239     posix_madvise (addr, length, POSIX_MADV_SEQUENTIAL);
240 #endif
241
242     block_t *block = block_mmap_Alloc (addr, length);
243     if (block == NULL)
244         goto fatal;
245
246     block->p_buffer += inner_offset;
247     block->i_buffer -= inner_offset;
248
249 #ifdef MMAP_DEBUG
250     msg_Dbg (p_access, "mapped 0x%zx bytes at %p from offset 0x%"PRIx64,
251              length, addr, (uint64_t)outer_offset);
252
253     /* Compare normal I/O with memory mapping */
254     char *buf = malloc (block->i_buffer);
255     ssize_t i_read = read (p_sys->fd, buf, block->i_buffer);
256
257     if (i_read != (ssize_t)block->i_buffer)
258         msg_Err (p_access, "read %zd instead of %zu bytes", i_read,
259                  block->i_buffer);
260     if (memcmp (buf, block->p_buffer, block->i_buffer))
261         msg_Err (p_access, "inconsistent data buffer");
262     free (buf);
263 #endif
264
265     p_access->info.i_pos = outer_offset + length;
266     return block;
267
268 fatal:
269     p_access->info.b_eof = true;
270     return NULL;
271 }
272
273
274 static int Seek (access_t *p_access, int64_t i_pos)
275 {
276 #ifdef MMAP_DEBUG
277     lseek (p_access->p_sys->fd, i_pos, SEEK_SET);
278 #endif
279
280     p_access->info.i_pos = i_pos;
281     p_access->info.b_eof = false;
282     return VLC_SUCCESS;
283 }
284
285
286 static int Control (access_t *p_access, int query, va_list args)
287 {
288     switch (query)
289     {
290         case ACCESS_CAN_SEEK:
291         case ACCESS_CAN_FASTSEEK:
292         case ACCESS_CAN_PAUSE:
293         case ACCESS_CAN_CONTROL_PACE:
294             *va_arg(args, bool *) = true;
295             return VLC_SUCCESS;
296
297         case ACCESS_GET_PTS_DELAY:
298         {
299             int delay_ms = var_CreateGetInteger (p_access, "file-caching");
300             *va_arg(args, int64_t *) = delay_ms * INT64_C (1000);
301             return VLC_SUCCESS;
302         }
303
304         case ACCESS_GET_TITLE_INFO:
305         case ACCESS_GET_META:
306             break;
307
308         case ACCESS_SET_PAUSE_STATE:
309             return VLC_SUCCESS;
310
311         case ACCESS_SET_TITLE:
312         case ACCESS_SET_SEEKPOINT:
313         case ACCESS_SET_PRIVATE_ID_STATE:
314         case ACCESS_SET_PRIVATE_ID_CA:
315         case ACCESS_GET_PRIVATE_ID_STATE:
316         case ACCESS_GET_CONTENT_TYPE:
317             break;
318
319         default:
320             msg_Warn (p_access, "unimplemented query %d in control", query);
321     }
322
323     return VLC_EGENERIC;
324 }