Debian Codegraph @

src/read_cache.c

raw
1 /*
2 * Copyright (C) 2000 Rich Wareham <[email protected]>
3 * 2001-2004 the dvdnav project
4 *
5 * This file is part of libdvdnav, a DVD navigation library.
6 *
7 * libdvdnav 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 * libdvdnav 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 along
18 * with libdvdnav; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21 /*
22 * There was a multithreaded read ahead cache in here for some time, but
23 * it had only been used for a short time. If you want to have a look at it,
24 * search the CVS attic.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <assert.h>
32 #include <inttypes.h>
33 #include <stdlib.h>
34 #include <limits.h>
35 #include <sys/time.h>
36 #include <time.h>
37 #include "dvdnav/dvdnav.h"
38 #include <dvdread/nav_types.h>
39 #include "vm/decoder.h"
40 #include "vm/vm.h"
41 #include "dvdnav_internal.h"
42 #include "read_cache.h"
43
44 #define READ_CACHE_CHUNKS 10
45
46 /* all cache chunks must be memory aligned to allow use of raw devices */
47 #define ALIGNMENT 2048
48
49 #define READ_AHEAD_SIZE_MIN 4
50 #define READ_AHEAD_SIZE_MAX 512
51
52 typedef struct read_cache_chunk_s {
53 uint8_t *cache_buffer;
54 uint8_t *cache_buffer_base; /* used in malloc and free for alignment */
55 int32_t cache_start_sector; /* -1 means cache invalid */
56 int32_t cache_read_count; /* this many sectors are already read */
57 size_t cache_block_count; /* this many sectors will go in this chunk */
58 size_t cache_malloc_size;
59 int cache_valid;
60 int usage_count; /* counts how many buffers where issued from this chunk */
61 } read_cache_chunk_t;
62
63 struct read_cache_s {
64 read_cache_chunk_t chunk[READ_CACHE_CHUNKS];
65 int current;
66 int freeing; /* is set to one when we are about to dispose the cache */
67 uint32_t read_ahead_size;
68 int read_ahead_incr;
69 int last_sector;
70 pthread_mutex_t lock;
71
72 /* Bit of strange cross-linking going on here :) -- Gotta love C :) */
73 dvdnav_t *dvd_self;
74 };
75
76 /*
77 #define READ_CACHE_TRACE 0
78 */
79
80 #ifdef __GNUC__
81 # if READ_CACHE_TRACE
82 # define dprintf(fmt, args...) fprintf(MSG_OUT, "libdvdnav: %s: "fmt, __func__ , ## args)
83 # else
84 # define dprintf(fmt, args...) /* Nowt */
85 # endif
86 #else
87 # if READ_CACHE_TRACE
88 # define dprintf(fmt, ...) fprintf(MSG_OUT, "libdvdnav: %s: "fmt, __func__ , __VA_ARGS__)
89 # else
90 #ifdef _MSC_VER
91 # define dprintf(fmt, str) /* Nowt */
92 #else
93 # define dprintf(fmt, ...) /* Nowt */
94 #endif /* _MSC_VER */
95 # endif
96 #endif
97
98
99 read_cache_t *dvdnav_read_cache_new(dvdnav_t* dvd_self) {
100 read_cache_t *self;
101 int i;
102
103 self = (read_cache_t *)calloc(1, sizeof(read_cache_t));
104
105 if(!self)
106 return NULL;
107
108 self->dvd_self = dvd_self;
109 self->read_ahead_size = READ_AHEAD_SIZE_MIN;
110 pthread_mutex_init(&self->lock, NULL);
111 dvdnav_read_cache_clear(self);
112 for (i = 0; i < READ_CACHE_CHUNKS; i++) {
113 self->chunk[i].cache_buffer = NULL;
114 self->chunk[i].usage_count = 0;
115 }
116
117 return self;
118 }
119
120 void dvdnav_read_cache_free(read_cache_t* self) {
121 dvdnav_t *tmp;
122 int i;
123
124 pthread_mutex_lock(&self->lock);
125 self->freeing = 1;
126 for (i = 0; i < READ_CACHE_CHUNKS; i++)
127 if (self->chunk[i].cache_buffer && self->chunk[i].usage_count == 0) {
128 free(self->chunk[i].cache_buffer_base);
129 self->chunk[i].cache_buffer = NULL;
130 }
131 pthread_mutex_unlock(&self->lock);
132
133 for (i = 0; i < READ_CACHE_CHUNKS; i++)
134 if (self->chunk[i].cache_buffer) return;
135
136 /* all buffers returned, free everything */
137 tmp = self->dvd_self;
138 pthread_mutex_destroy(&self->lock);
139 free(self);
140 free(tmp);
141 }
142
143 /* This function MUST be called whenever self->file changes. */
144 void dvdnav_read_cache_clear(read_cache_t *self) {
145 int i;
146
147 if(!self)
148 return;
149
150 pthread_mutex_lock(&self->lock);
151 for (i = 0; i < READ_CACHE_CHUNKS; i++)
152 self->chunk[i].cache_valid = 0;
153 pthread_mutex_unlock(&self->lock);
154 }
155
156 /* This function is called just after reading the NAV packet. */
157 void dvdnav_pre_cache_blocks(read_cache_t *self, int sector, size_t block_count) {
158 int i, use;
159
160 if(!self)
161 return;
162
163 if(!self->dvd_self->use_read_ahead)
164 return;
165
166 pthread_mutex_lock(&self->lock);
167
168 /* find a free cache chunk that best fits the required size */
169 use = -1;
170 for (i = 0; i < READ_CACHE_CHUNKS; i++)
171 if (self->chunk[i].usage_count == 0 && self->chunk[i].cache_buffer &&
172 self->chunk[i].cache_malloc_size >= block_count &&
173 (use == -1 || self->chunk[use].cache_malloc_size > self->chunk[i].cache_malloc_size))
174 use = i;
175
176 if (use == -1) {
177 /* we haven't found a cache chunk, so we try to reallocate an existing one */
178 for (i = 0; i < READ_CACHE_CHUNKS; i++)
179 if (self->chunk[i].usage_count == 0 && self->chunk[i].cache_buffer &&
180 (use == -1 || self->chunk[use].cache_malloc_size < self->chunk[i].cache_malloc_size))
181 use = i;
182 if (use >= 0) {
183 self->chunk[use].cache_buffer_base = realloc(self->chunk[use].cache_buffer_base,
184 block_count * DVD_VIDEO_LB_LEN + ALIGNMENT);
185 self->chunk[use].cache_buffer =
186 (uint8_t *)(((uintptr_t)self->chunk[use].cache_buffer_base & ~((uintptr_t)(ALIGNMENT - 1))) + ALIGNMENT);
187 dprintf("pre_cache DVD read realloc happened\n");
188 self->chunk[use].cache_malloc_size = block_count;
189 } else {
190 /* we still haven't found a cache chunk, let's allocate a new one */
191 for (i = 0; i < READ_CACHE_CHUNKS; i++)
192 if (!self->chunk[i].cache_buffer) {
193 use = i;
194 break;
195 }
196 if (use >= 0) {
197 /* We start with a sensible figure for the first malloc of 500 blocks.
198 * Some DVDs I have seen venture to 450 blocks.
199 * This is so that fewer realloc's happen if at all.
200 */
201 self->chunk[i].cache_buffer_base =
202 malloc((block_count > 500 ? block_count : 500) * DVD_VIDEO_LB_LEN + ALIGNMENT);
203 self->chunk[i].cache_buffer =
204 (uint8_t *)(((uintptr_t)self->chunk[i].cache_buffer_base & ~((uintptr_t)(ALIGNMENT - 1))) + ALIGNMENT);
205 self->chunk[i].cache_malloc_size = block_count > 500 ? block_count : 500;
206 dprintf("pre_cache DVD read malloc %d blocks\n",
207 (block_count > 500 ? block_count : 500 ));
208 }
209 }
210 }
211
212 if (use >= 0) {
213 self->chunk[use].cache_start_sector = sector;
214 self->chunk[use].cache_block_count = block_count;
215 self->chunk[use].cache_read_count = 0;
216 self->chunk[use].cache_valid = 1;
217 self->current = use;
218 } else {
219 dprintf("pre_caching was impossible, no cache chunk available\n");
220 }
221 pthread_mutex_unlock(&self->lock);
222 }
223
224 int dvdnav_read_cache_block(read_cache_t *self, int sector, size_t block_count, uint8_t **buf) {
225 int i, use;
226 int start;
227 int size;
228 int incr;
229 uint8_t *read_ahead_buf;
230 int32_t res;
231
232 if(!self)
233 return 0;
234
235 use = -1;
236
237 if(self->dvd_self->use_read_ahead) {
238 /* first check, if sector is in current chunk */
239 read_cache_chunk_t cur = self->chunk[self->current];
240 if (cur.cache_valid && sector >= cur.cache_start_sector &&
241 sector <= (cur.cache_start_sector + cur.cache_read_count) &&
242 sector + block_count <= cur.cache_start_sector + cur.cache_block_count)
243 use = self->current;
244 else
245 for (i = 0; i < READ_CACHE_CHUNKS; i++)
246 if (self->chunk[i].cache_valid &&
247 sector >= self->chunk[i].cache_start_sector &&
248 sector <= (self->chunk[i].cache_start_sector + self->chunk[i].cache_read_count) &&
249 sector + block_count <= self->chunk[i].cache_start_sector + self->chunk[i].cache_block_count)
250 use = i;
251 }
252
253 if (use >= 0) {
254 read_cache_chunk_t *chunk;
255
256 /* Increment read-ahead size if sector follows the last sector */
257 if (sector == (self->last_sector + 1)) {
258 if (self->read_ahead_incr < READ_AHEAD_SIZE_MAX)
259 self->read_ahead_incr++;
260 } else {
261 self->read_ahead_size = READ_AHEAD_SIZE_MIN;
262 self->read_ahead_incr = 0;
263 }
264 self->last_sector = sector;
265
266 /* The following resources need to be protected by a mutex :
267 * self->chunk[*].cache_buffer
268 * self->chunk[*].cache_malloc_size
269 * self->chunk[*].usage_count
270 */
271 pthread_mutex_lock(&self->lock);
272 chunk = &self->chunk[use];
273 read_ahead_buf = chunk->cache_buffer + chunk->cache_read_count * DVD_VIDEO_LB_LEN;
274 *buf = chunk->cache_buffer + (sector - chunk->cache_start_sector) * DVD_VIDEO_LB_LEN;
275 chunk->usage_count++;
276 pthread_mutex_unlock(&self->lock);
277
278 dprintf("libdvdnav: sector=%d, start_sector=%d, last_sector=%d\n", sector, chunk->cache_start_sector, chunk->cache_start_sector + chunk->cache_block_count);
279
280 /* read_ahead_size */
281 incr = self->read_ahead_incr >> 1;
282 if ((self->read_ahead_size + incr) > READ_AHEAD_SIZE_MAX) {
283 self->read_ahead_size = READ_AHEAD_SIZE_MAX;
284 } else {
285 self->read_ahead_size += incr;
286 }
287
288 /* real read size */
289 start = chunk->cache_start_sector + chunk->cache_read_count;
290 if (chunk->cache_read_count + self->read_ahead_size > chunk->cache_block_count) {
291 size = chunk->cache_block_count - chunk->cache_read_count;
292 } else {
293 size = self->read_ahead_size;
294 /* ensure that the sector we want will be read */
295 if (sector >= chunk->cache_start_sector + chunk->cache_read_count + size)
296 size = sector - chunk->cache_start_sector - chunk->cache_read_count;
297 }
298 dprintf("libdvdnav: read_ahead_size=%d, size=%d\n", self->read_ahead_size, size);
299
300 if (size)
301 chunk->cache_read_count += DVDReadBlocks(self->dvd_self->file,
302 start,
303 size,
304 read_ahead_buf);
305
306 res = DVD_VIDEO_LB_LEN * block_count;
307
308 } else {
309
310 if (self->dvd_self->use_read_ahead) {
311 dprintf("cache miss on sector %d\n", sector);
312 }
313
314 res = DVDReadBlocks(self->dvd_self->file,
315 sector,
316 block_count,
317 *buf) * DVD_VIDEO_LB_LEN;
318 }
319
320 return res;
321
322 }
323
324 dvdnav_status_t dvdnav_free_cache_block(dvdnav_t *self, unsigned char *buf) {
325 read_cache_t *cache;
326 int i;
327
328 if (!self)
329 return DVDNAV_STATUS_ERR;
330
331 cache = self->cache;
332 if (!cache)
333 return DVDNAV_STATUS_ERR;
334
335 pthread_mutex_lock(&cache->lock);
336 for (i = 0; i < READ_CACHE_CHUNKS; i++) {
337 if (cache->chunk[i].cache_buffer && buf >= cache->chunk[i].cache_buffer &&
338 buf < cache->chunk[i].cache_buffer + cache->chunk[i].cache_malloc_size * DVD_VIDEO_LB_LEN) {
339 assert(cache->chunk[i].usage_count > 0);
340 cache->chunk[i].usage_count--;
341 }
342 }
343 pthread_mutex_unlock(&cache->lock);
344
345 if (cache->freeing)
346 /* when we want to dispose the cache, try freeing it now */
347 dvdnav_read_cache_free(cache);
348
349 return DVDNAV_STATUS_OK;
350 }