annotate TOOLS/cache2.c @ 4218:3931c41f740a

Added new syncengine thanks to a new previously undocumented feature of the em8300, this might fix playback on both slow and fast machines (more testing needed). This also requires users to get the em8300 driver from cvs until the next version is released (will probably happen this weekend) Added lots of comments, should be pretty easy to understand most of the internals now Added lots of brackets to if's for's while's etc, this is not a cosmetical thing but rather due to the fact I got some very odd bugs with else's since I didn't properly use brackets (and it's the K&R standard to have brackets everywhere) Fixed some bugs that would occur when disabling libmp1e Switched to default to the new naming scheme of device nodes, the driver will slowly switch over to this state, if it can't find devices under the new name it will try the old naming scheme I stopped opening devices in non-blocking mode, it would break the new syncengine which tries to burst data to the device (alot of times meaning it will fill the fifo pretty fast which would previously result in jerkyness on fast machines) The device now sets the initial state of the pts and speed (probably not needed, but assumption is the mother of all fuckups =) Keep the control interface open during the entire duration of the libvo device, we might need this to flush video buffers on seeking (currently not implemented, therefore seeking is broken) This is beta stuff to the driver, I will get some users to test it for me and do my best to fix seeking as soon as possible...
author mswitch
date Thu, 17 Jan 2002 10:33:47 +0000
parents 5628c341f4ff
children edfe34c5405d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1341
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
1
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
2 // gcc cache2.c ../linux/shmem.o -o cache2
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
3
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
4 // Initial draft of my new cache system...
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
5 // includes some simulation code, using usleep() to emulate limited bandwith
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
6 // TODO: seeking, data consistency checking
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
7
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
8 #define READ_SPEED 20
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
9 #define FILL_SPEED 10
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
10
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
11 #include <stdio.h>
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
12 #include <stdlib.h>
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
13 #include <string.h>
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
14 #include "../linux/shmem.h"
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
15
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
16 typedef struct {
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
17 // constats:
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
18 void *buffer; // base pointer of the alllocated buffer memory
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
19 int buffer_size; // size of the alllocated buffer memory
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
20 int sector_size; // size of a single sector (2048/2324)
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
21 // Note: buffer_size should be N*sector_size, where N is integer...
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
22 int back_size; // we should keep back_size amount of old bytes for backward seek
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
23 int fill_limit; // we should fill buffer if space>fill_limit
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
24 // reader's pointers:
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
25 int read_filepos;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
26 // filler's pointers:
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
27 int min_filepos; // buffer contain only a part of the file, from min-max pos
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
28 int max_filepos;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
29 int offset; // filepos <-> bufferpos offset value (filepos of the buffer's first byte)
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
30 // commands/locking:
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
31 int cmd_lock; // 1 if we will seek/reset buffer, 2 if we are ready for cmd
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
32 int fifo_flag; // 1 if we should use FIFO to notice cache about buffer reads.
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
33 } cache_vars_t;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
34
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
35 int min_fill=0;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
36 int sleep_flag=0;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
37
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
38 void cache_stats(cache_vars_t* s){
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
39 int newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
40 printf("0x%06X [0x%06X] 0x%06X ",s->min_filepos,s->read_filepos,s->max_filepos);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
41 printf("%3d %% (%3d%%)\n",100*newb/s->buffer_size,100*min_fill/s->buffer_size);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
42 }
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
43
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
44 int cache_read(cache_vars_t* s,int size){
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
45 int total=0;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
46 while(size>0){
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
47 int pos,newb,len;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
48
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
49 pos=s->read_filepos - s->offset;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
50 if(pos<0) pos+=s->buffer_size; else
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
51 if(pos>=s->buffer_size) pos-=s->buffer_size;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
52
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
53 newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
54
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
55 if(newb<min_fill) min_fill=newb; // statistics...
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
56
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
57 if(newb<=0){
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
58 // waiting for buffer fill...
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
59 usleep(10000); // 10ms
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
60 continue;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
61 }
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
62
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
63 // printf("*** newb: %d bytes ***\n",newb);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
64
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
65 if(newb>s->buffer_size-pos) newb=s->buffer_size-pos; // handle wrap...
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
66 if(newb>size) newb=size;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
67
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
68 // len=write(mem,newb)
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
69 printf("Buffer read: %d bytes\n",newb);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
70 len=newb;usleep(len*READ_SPEED*sleep_flag);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
71 // ...
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
72
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
73 s->read_filepos+=len;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
74 size-=len;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
75 total+=len;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
76
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
77 }
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
78 return total;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
79 }
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
80
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
81 int cache_fill(cache_vars_t* s){
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
82 int read,back,newb,space,len,pos,endpos;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
83
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
84 read=s->read_filepos;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
85
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
86 // calc number of back-bytes:
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
87 back=read - s->min_filepos;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
88 if(back<0) back=0; // strange...
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
89 if(back>s->back_size) back=s->back_size;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
90
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
91 // calc number of new bytes:
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
92 newb=s->max_filepos - read;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
93 if(newb<0) newb=0; // strange...
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
94
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
95 // calc free buffer space:
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
96 space=s->buffer_size - (newb+back);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
97
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
98 // calc bufferpos:
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
99 pos=s->max_filepos - s->offset;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
100 if(pos>=s->buffer_size) pos-=s->buffer_size; // wrap-around
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
101
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
102 if(space<s->fill_limit){
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
103 // printf("Buffer is full (%d bytes free, limit: %d)\n",space,s->fill_limit);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
104 return 0; // no fill...
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
105 }
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
106
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
107 // printf("### read=0x%X back=%d newb=%d space=%d pos=%d\n",read,back,newb,space,pos);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
108
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
109 // reduce space if needed:
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
110 if(space>s->buffer_size-pos) space=s->buffer_size-pos;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
111
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
112 if(space>32768) space=32768; // limit one-time block size
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
113
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
114 s->min_filepos=read-back; // avoid seeking-back to temp area...
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
115
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
116 // ....
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
117 printf("Buffer fill: %d bytes of %d\n",space,s->buffer_size);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
118 len=space; usleep(len*FILL_SPEED*sleep_flag);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
119 // ....
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
120
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
121 s->max_filepos+=len;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
122 if(pos+len>=s->buffer_size){
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
123 // wrap...
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
124 s->offset+=s->buffer_size;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
125 }
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
126
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
127 return len;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
128
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
129 }
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
130
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
131 cache_vars_t* cache_init(int size,int sector){
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
132 int num;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
133 cache_vars_t* s=shmem_alloc(sizeof(cache_vars_t));
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
134 memset(s,0,sizeof(cache_vars_t));
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
135 num=size/sector;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
136 s->buffer_size=num*sector;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
137 s->sector_size=sector;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
138 s->buffer=shmem_alloc(s->buffer_size);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
139 s->fill_limit=8*sector;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
140 s->back_size=size/2;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
141 return s;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
142 }
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
143
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
144 int main(){
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
145
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
146 cache_vars_t* s=cache_init(1024*1024,2048);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
147
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
148 // while(cache_fill(s)){ } // fill buffer:
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
149
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
150 min_fill=s->buffer_size;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
151 sleep_flag=1; // start simulation
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
152
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
153 if(fork()){
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
154 while(1){
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
155 if(!cache_fill(s)) usleep(10000); // wait 10ms for buffer space
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
156 //cache_stats(s);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
157 }
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
158 } else {
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
159 srand(12345);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
160 while(1){
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
161 int len=10+rand()&8191;
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
162 cache_stats(s);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
163 cache_read(s,len);
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
164 }
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
165 }
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
166
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
167 }
5628c341f4ff Initial draft of my new cache system...
arpi
parents:
diff changeset
168