annotate read_cache.c @ 242:f794e1c17947 src

porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne from Mandrake)
author mroi
date Wed, 03 Mar 2004 16:48:36 +0000
parents 065a2835ba90
children ef3b33441db5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
1 /*
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
2 * Copyright (C) 2000 Rich Wareham <richwareham@users.sourceforge.net>
242
f794e1c17947 porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents: 230
diff changeset
3 * 2001-2004 the dvdnav project
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
4 *
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
5 * This file is part of libdvdnav, a DVD navigation library.
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
6 *
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
7 * libdvdnav is free software; you can redistribute it and/or modify
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
8 * it under the terms of the GNU General Public License as published by
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
9 * the Free Software Foundation; either version 2 of the License, or
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
10 * (at your option) any later version.
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
11 *
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
12 * libdvdnav is distributed in the hope that it will be useful,
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
15 * GNU General Public License for more details.
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
16 *
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
18 * along with this program; if not, write to the Free Software
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
20 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
21 * $Id$
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
22 *
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
23 */
225
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
24 /*
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
25 * There was a multithreaded read ahead cache in here for some time, but
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
26 * it had only been used for a short time. If you want to have a look at it,
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
27 * search the CVS attic.
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
28 */
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
29
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
30 #ifdef HAVE_CONFIG_H
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
31 #include "config.h"
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
32 #endif
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
33
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
34 #include "dvdnav.h"
230
065a2835ba90 Patch to compile with mingw32 from: -
jcdutton
parents: 225
diff changeset
35 #include "dvdnav_internal.h"
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
36 #include "read_cache.h"
46
654705f4e7af Add some readcache profiling code.
jcdutton
parents: 45
diff changeset
37 #include <sys/time.h>
654705f4e7af Add some readcache profiling code.
jcdutton
parents: 45
diff changeset
38 #include <time.h>
654705f4e7af Add some readcache profiling code.
jcdutton
parents: 45
diff changeset
39
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
40 #define READ_CACHE_CHUNKS 10
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
41
74
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
42 /* all cache chunks must be memory aligned to allow use of raw devices */
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
43 #define ALIGNMENT 2048
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
44
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
45 #define READ_AHEAD_SIZE_MIN 4
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
46 #define READ_AHEAD_SIZE_MAX 512
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
47
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
48 typedef struct read_cache_chunk_s {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
49 uint8_t *cache_buffer;
74
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
50 uint8_t *cache_buffer_base; /* used in malloc and free for alignment */
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
51 int32_t cache_start_sector; /* -1 means cache invalid */
103
8905d8de7e91 changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents: 76
diff changeset
52 int32_t cache_read_count; /* this many sectors are already read */
8905d8de7e91 changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents: 76
diff changeset
53 size_t cache_block_count; /* this many sectors will go in this chunk */
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
54 size_t cache_malloc_size;
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
55 int cache_valid;
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
56 int usage_count; /* counts how many buffers where issued from this chunk */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
57 } read_cache_chunk_t;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
58
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
59 struct read_cache_s {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
60 read_cache_chunk_t chunk[READ_CACHE_CHUNKS];
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
61 int current;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
62 int freeing; /* is set to one when we are about to dispose the cache */
166
3bfaec0c1288 * fix warnings with gcc 3.3
mroi
parents: 114
diff changeset
63 uint32_t read_ahead_size;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
64 int read_ahead_incr;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
65 int last_sector;
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
66 pthread_mutex_t lock;
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
67
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
68 /* Bit of strange cross-linking going on here :) -- Gotta love C :) */
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
69 dvdnav_t *dvd_self;
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
70 };
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
71
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
72 /*
76
0e2abe7083de more consistent console output
mroi
parents: 74
diff changeset
73 #define READ_CACHE_TRACE 0
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
74 */
107
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
75
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
76 #ifdef __GNUC__
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
77 # if READ_CACHE_TRACE
114
b6834e6359cf big libdvdnav cleanup, quoting the ChangeLog:
mroi
parents: 108
diff changeset
78 # define dprintf(fmt, args...) fprintf(MSG_OUT, "libdvdnav: %s: "fmt, __func__ , ## args)
107
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
79 # else
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
80 # define dprintf(fmt, args...) /* Nowt */
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
81 # endif
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
82 #else
107
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
83 # if READ_CACHE_TRACE
114
b6834e6359cf big libdvdnav cleanup, quoting the ChangeLog:
mroi
parents: 108
diff changeset
84 # define dprintf(fmt, ...) fprintf(MSG_OUT, "libdvdnav: %s: "fmt, __func__ , __VA_ARGS__)
107
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
85 # else
176
4e099155c968 Helping with the WIN32 port.
jcdutton
parents: 166
diff changeset
86 #ifdef _MSC_VER
4e099155c968 Helping with the WIN32 port.
jcdutton
parents: 166
diff changeset
87 # define dprintf(fmt, str) /* Nowt */
4e099155c968 Helping with the WIN32 port.
jcdutton
parents: 166
diff changeset
88 #else
107
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
89 # define dprintf(fmt, ...) /* Nowt */
176
4e099155c968 Helping with the WIN32 port.
jcdutton
parents: 166
diff changeset
90 #endif /* _MSC_VER */
107
b2801805c433 include some fixes done in xine's copy to avoid merging conflicts
mroi
parents: 105
diff changeset
91 # endif
41
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
92 #endif
50e0855a2017 Experimental multi-threaded cache now enabled by default so that it can get tested during change from 0.1.1 to 0.1.2
richwareham
parents: 40
diff changeset
93
40
a049c3753f32 Added some packaging patches from Philipp Matthias Hahn <pmhahn@titan.lahn.de> and an initial (non-working) multi-threaded read-ahead cache.
richwareham
parents: 37
diff changeset
94
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
95 read_cache_t *dvdnav_read_cache_new(dvdnav_t* dvd_self) {
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
96 read_cache_t *self;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
97 int i;
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
98
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
99 self = (read_cache_t *)malloc(sizeof(read_cache_t));
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
100
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
101 if(self) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
102 self->current = 0;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
103 self->freeing = 0;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
104 self->dvd_self = dvd_self;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
105 self->last_sector = 0;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
106 self->read_ahead_size = READ_AHEAD_SIZE_MIN;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
107 self->read_ahead_incr = 0;
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
108 pthread_mutex_init(&self->lock, NULL);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
109 dvdnav_read_cache_clear(self);
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
110 for (i = 0; i < READ_CACHE_CHUNKS; i++) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
111 self->chunk[i].cache_buffer = NULL;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
112 self->chunk[i].usage_count = 0;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
113 }
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
114 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
115
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
116 return self;
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
117 }
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
118
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
119 void dvdnav_read_cache_free(read_cache_t* self) {
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
120 dvdnav_t *tmp;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
121 int i;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
122
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
123 pthread_mutex_lock(&self->lock);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
124 self->freeing = 1;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
125 for (i = 0; i < READ_CACHE_CHUNKS; i++)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
126 if (self->chunk[i].cache_buffer && self->chunk[i].usage_count == 0) {
74
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
127 free(self->chunk[i].cache_buffer_base);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
128 self->chunk[i].cache_buffer = NULL;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
129 }
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
130 pthread_mutex_unlock(&self->lock);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
131
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
132 for (i = 0; i < READ_CACHE_CHUNKS; i++)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
133 if (self->chunk[i].cache_buffer) return;
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
134
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
135 /* all buffers returned, free everything */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
136 tmp = self->dvd_self;
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
137 pthread_mutex_destroy(&self->lock);
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
138 free(self);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
139 free(tmp);
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
140 }
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
141
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
142 /* This function MUST be called whenever self->file changes. */
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
143 void dvdnav_read_cache_clear(read_cache_t *self) {
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
144 int i;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
145
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
146 if(!self)
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
147 return;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
148
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
149 pthread_mutex_lock(&self->lock);
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
150 for (i = 0; i < READ_CACHE_CHUNKS; i++)
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
151 self->chunk[i].cache_valid = 0;
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
152 pthread_mutex_unlock(&self->lock);
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
153 }
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
154
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
155 /* This function is called just after reading the NAV packet. */
34
1f29402ef2ef 'Objectified' the read-ahead cache in preparation to implement a 'proper' threaded cache a-la that recommended in the DVD Demystified book.
richwareham
parents: 3
diff changeset
156 void dvdnav_pre_cache_blocks(read_cache_t *self, int sector, size_t block_count) {
103
8905d8de7e91 changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents: 76
diff changeset
157 int i, use;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
158
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
159 if(!self)
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
160 return;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
161
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
162 if(!self->dvd_self->use_read_ahead)
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
163 return;
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
164
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
165 pthread_mutex_lock(&self->lock);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
166
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
167 /* find a free cache chunk that best fits the required size */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
168 use = -1;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
169 for (i = 0; i < READ_CACHE_CHUNKS; i++)
61
6b7520caf9a1 fix stupid bug: test if the buffer is there before using it
mroi
parents: 60
diff changeset
170 if (self->chunk[i].usage_count == 0 && self->chunk[i].cache_buffer &&
6b7520caf9a1 fix stupid bug: test if the buffer is there before using it
mroi
parents: 60
diff changeset
171 self->chunk[i].cache_malloc_size >= block_count &&
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
172 (use == -1 || self->chunk[use].cache_malloc_size > self->chunk[i].cache_malloc_size))
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
173 use = i;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
174
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
175 if (use == -1) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
176 /* we haven't found a cache chunk, so we try to reallocate an existing one */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
177 for (i = 0; i < READ_CACHE_CHUNKS; i++)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
178 if (self->chunk[i].usage_count == 0 && self->chunk[i].cache_buffer &&
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
179 (use == -1 || self->chunk[use].cache_malloc_size < self->chunk[i].cache_malloc_size))
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
180 use = i;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
181 if (use >= 0) {
74
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
182 self->chunk[use].cache_buffer_base = realloc(self->chunk[use].cache_buffer_base,
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
183 block_count * DVD_VIDEO_LB_LEN + ALIGNMENT);
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
184 self->chunk[use].cache_buffer =
242
f794e1c17947 porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents: 230
diff changeset
185 (uint8_t *)(((uintptr_t)self->chunk[use].cache_buffer_base & ~((uintptr_t)(ALIGNMENT - 1))) + ALIGNMENT);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
186 dprintf("pre_cache DVD read realloc happened\n");
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
187 self->chunk[use].cache_malloc_size = block_count;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
188 } else {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
189 /* we still haven't found a cache chunk, let's allocate a new one */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
190 for (i = 0; i < READ_CACHE_CHUNKS; i++)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
191 if (!self->chunk[i].cache_buffer) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
192 use = i;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
193 break;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
194 }
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
195 if (use >= 0) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
196 /* We start with a sensible figure for the first malloc of 500 blocks.
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
197 * Some DVDs I have seen venture to 450 blocks.
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
198 * This is so that fewer realloc's happen if at all.
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
199 */
74
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
200 self->chunk[i].cache_buffer_base =
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
201 malloc((block_count > 500 ? block_count : 500) * DVD_VIDEO_LB_LEN + ALIGNMENT);
bf89c194f781 align read cache chunks in memory to allow use of raw devices
mroi
parents: 65
diff changeset
202 self->chunk[i].cache_buffer =
242
f794e1c17947 porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents: 230
diff changeset
203 (uint8_t *)(((uintptr_t)self->chunk[i].cache_buffer_base & ~((uintptr_t)(ALIGNMENT - 1))) + ALIGNMENT);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
204 self->chunk[i].cache_malloc_size = block_count > 500 ? block_count : 500;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
205 dprintf("pre_cache DVD read malloc %d blocks\n",
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
206 (block_count > 500 ? block_count : 500 ));
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
207 }
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
208 }
48
9030797161f0 Compile why don't you. :-(
jcdutton
parents: 47
diff changeset
209 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
210
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
211 if (use >= 0) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
212 self->chunk[use].cache_start_sector = sector;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
213 self->chunk[use].cache_block_count = block_count;
103
8905d8de7e91 changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents: 76
diff changeset
214 self->chunk[use].cache_read_count = 0;
8905d8de7e91 changes to read cache behaviour inspired by Thibaut Mattern:
mroi
parents: 76
diff changeset
215 self->chunk[use].cache_valid = 1;
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
216 self->current = use;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
217 } else {
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
218 dprintf("pre_caching was impossible, no cache chunk available\n");
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
219 }
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
220 pthread_mutex_unlock(&self->lock);
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
221 }
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
222
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
223 int dvdnav_read_cache_block(read_cache_t *self, int sector, size_t block_count, uint8_t **buf) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
224 int i, use;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
225 int start;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
226 int size;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
227 int incr;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
228 uint8_t *read_ahead_buf;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
229 int32_t res;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
230
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
231 if(!self)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
232 return 0;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
233
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
234 use = -1;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
235
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
236 if(self->dvd_self->use_read_ahead) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
237 /* first check, if sector is in current chunk */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
238 read_cache_chunk_t cur = self->chunk[self->current];
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
239 if (cur.cache_valid && sector >= cur.cache_start_sector &&
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
240 sector <= (cur.cache_start_sector + cur.cache_read_count) &&
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
241 sector + block_count <= cur.cache_start_sector + cur.cache_block_count)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
242 use = self->current;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
243 else
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
244 for (i = 0; i < READ_CACHE_CHUNKS; i++)
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
245 if (self->chunk[i].cache_valid &&
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
246 sector >= self->chunk[i].cache_start_sector &&
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
247 sector <= (self->chunk[i].cache_start_sector + self->chunk[i].cache_read_count) &&
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
248 sector + block_count <= self->chunk[i].cache_start_sector + self->chunk[i].cache_block_count)
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
249 use = i;
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
250 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
251
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
252 if (use >= 0) {
108
293bb1798e86 Remove C++ism.
f1rmb
parents: 107
diff changeset
253 read_cache_chunk_t *chunk;
293bb1798e86 Remove C++ism.
f1rmb
parents: 107
diff changeset
254
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
255 /* Increment read-ahead size if sector follows the last sector */
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
256 if (sector == (self->last_sector + 1)) {
225
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
257 if (self->read_ahead_incr < READ_AHEAD_SIZE_MAX)
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
258 self->read_ahead_incr++;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
259 } else {
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
260 self->read_ahead_size = READ_AHEAD_SIZE_MIN;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
261 self->read_ahead_incr = 0;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
262 }
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
263 self->last_sector = sector;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
264
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
265 /* The following resources need to be protected by a mutex :
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
266 * self->chunk[*].cache_buffer
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
267 * self->chunk[*].cache_malloc_size
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
268 * self->chunk[*].usage_count
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
269 */
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
270 pthread_mutex_lock(&self->lock);
108
293bb1798e86 Remove C++ism.
f1rmb
parents: 107
diff changeset
271 chunk = &self->chunk[use];
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
272 read_ahead_buf = chunk->cache_buffer + chunk->cache_read_count * DVD_VIDEO_LB_LEN;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
273 *buf = chunk->cache_buffer + (sector - chunk->cache_start_sector) * DVD_VIDEO_LB_LEN;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
274 chunk->usage_count++;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
275 pthread_mutex_unlock(&self->lock);
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
276
225
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
277 dprintf("libdvdnav: sector=%d, start_sector=%d, last_sector=%d\n", sector, chunk->cache_start_sector, chunk->cache_start_sector + chunk->cache_block_count);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
278
225
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
279 /* read_ahead_size */
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
280 incr = self->read_ahead_incr >> 1;
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
281 if ((self->read_ahead_size + incr) > READ_AHEAD_SIZE_MAX) {
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
282 self->read_ahead_size = READ_AHEAD_SIZE_MAX;
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
283 } else {
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
284 self->read_ahead_size += incr;
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
285 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
286
225
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
287 /* real read size */
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
288 start = chunk->cache_start_sector + chunk->cache_read_count;
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
289 if (chunk->cache_read_count + self->read_ahead_size > chunk->cache_block_count) {
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
290 size = chunk->cache_block_count - chunk->cache_read_count;
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
291 } else {
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
292 size = self->read_ahead_size;
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
293 /* ensure that the sector we want will be read */
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
294 if (sector >= chunk->cache_start_sector + chunk->cache_read_count + size)
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
295 size = sector - chunk->cache_start_sector - chunk->cache_read_count;
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
296 }
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
297 dprintf("libdvdnav: read_ahead_size=%d, size=%d\n", self->read_ahead_size, size);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
298
225
9b1b740e3fc9 big build system changes
mroi
parents: 211
diff changeset
299 if (size)
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
300 chunk->cache_read_count += DVDReadBlocks(self->dvd_self->file,
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
301 start,
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
302 size,
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
303 read_ahead_buf);
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
304
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
305 res = DVD_VIDEO_LB_LEN * block_count;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
306
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
307 } else {
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
308
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
309 if (self->dvd_self->use_read_ahead)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
310 dprintf("cache miss on sector %d\n", sector);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
311
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
312 res = DVDReadBlocks(self->dvd_self->file,
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
313 sector,
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
314 block_count,
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
315 *buf) * DVD_VIDEO_LB_LEN;
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
316 }
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
317
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
318 return res;
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
319
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
320 }
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
321
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
322 dvdnav_status_t dvdnav_free_cache_block(dvdnav_t *self, unsigned char *buf) {
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
323 read_cache_t *cache;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
324 int i;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
325
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
326 if (!self)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
327 return DVDNAV_STATUS_ERR;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
328
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
329 cache = self->cache;
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
330 if (!cache)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
331 return DVDNAV_STATUS_ERR;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
332
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
333 pthread_mutex_lock(&cache->lock);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
334 for (i = 0; i < READ_CACHE_CHUNKS; i++) {
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
335 if (cache->chunk[i].cache_buffer && buf >= cache->chunk[i].cache_buffer &&
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
336 buf < cache->chunk[i].cache_buffer + cache->chunk[i].cache_malloc_size * DVD_VIDEO_LB_LEN) {
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
337 cache->chunk[i].usage_count--;
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
338 }
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
339 }
65
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
340 pthread_mutex_unlock(&cache->lock);
dcde6d9cea7a ensure serialized access to the cache to avoid concurrent access on the
mroi
parents: 62
diff changeset
341
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
342 if (cache->freeing)
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
343 /* when we want to dispose the cache, try freeing it now */
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
344 dvdnav_read_cache_free(cache);
105
0a0a749038ff new read cache with better performance on slow drives
mroi
parents: 103
diff changeset
345
60
30995ad032cf use new memcopy'less read ahead cache
mroi
parents: 51
diff changeset
346 return DVDNAV_STATUS_OK;
0
3ddf0eaece51 Initial revision
richwareham
parents:
diff changeset
347 }