1341
|
1
|
9380
|
2 // gcc cache2.c ../osdep/shmem.o -o cache2
|
1341
|
3
|
|
4 // Initial draft of my new cache system...
|
|
5 // includes some simulation code, using usleep() to emulate limited bandwith
|
|
6 // TODO: seeking, data consistency checking
|
|
7
|
|
8 #define READ_SPEED 20
|
|
9 #define FILL_SPEED 10
|
|
10
|
|
11 #include <stdio.h>
|
|
12 #include <stdlib.h>
|
|
13 #include <string.h>
|
9380
|
14 #include "../osdep/shmem.h"
|
1341
|
15
|
|
16 typedef struct {
|
|
17 // constats:
|
|
18 void *buffer; // base pointer of the alllocated buffer memory
|
|
19 int buffer_size; // size of the alllocated buffer memory
|
|
20 int sector_size; // size of a single sector (2048/2324)
|
|
21 // Note: buffer_size should be N*sector_size, where N is integer...
|
|
22 int back_size; // we should keep back_size amount of old bytes for backward seek
|
|
23 int fill_limit; // we should fill buffer if space>fill_limit
|
|
24 // reader's pointers:
|
|
25 int read_filepos;
|
|
26 // filler's pointers:
|
|
27 int min_filepos; // buffer contain only a part of the file, from min-max pos
|
|
28 int max_filepos;
|
|
29 int offset; // filepos <-> bufferpos offset value (filepos of the buffer's first byte)
|
|
30 // commands/locking:
|
|
31 int cmd_lock; // 1 if we will seek/reset buffer, 2 if we are ready for cmd
|
|
32 int fifo_flag; // 1 if we should use FIFO to notice cache about buffer reads.
|
|
33 } cache_vars_t;
|
|
34
|
|
35 int min_fill=0;
|
|
36 int sleep_flag=0;
|
|
37
|
|
38 void cache_stats(cache_vars_t* s){
|
|
39 int newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
|
|
40 printf("0x%06X [0x%06X] 0x%06X ",s->min_filepos,s->read_filepos,s->max_filepos);
|
|
41 printf("%3d %% (%3d%%)\n",100*newb/s->buffer_size,100*min_fill/s->buffer_size);
|
|
42 }
|
|
43
|
|
44 int cache_read(cache_vars_t* s,int size){
|
|
45 int total=0;
|
|
46 while(size>0){
|
|
47 int pos,newb,len;
|
|
48
|
|
49 pos=s->read_filepos - s->offset;
|
|
50 if(pos<0) pos+=s->buffer_size; else
|
|
51 if(pos>=s->buffer_size) pos-=s->buffer_size;
|
|
52
|
|
53 newb=s->max_filepos-s->read_filepos; // new bytes in the buffer
|
|
54
|
|
55 if(newb<min_fill) min_fill=newb; // statistics...
|
|
56
|
|
57 if(newb<=0){
|
|
58 // waiting for buffer fill...
|
|
59 usleep(10000); // 10ms
|
|
60 continue;
|
|
61 }
|
|
62
|
|
63 // printf("*** newb: %d bytes ***\n",newb);
|
|
64
|
|
65 if(newb>s->buffer_size-pos) newb=s->buffer_size-pos; // handle wrap...
|
|
66 if(newb>size) newb=size;
|
|
67
|
|
68 // len=write(mem,newb)
|
|
69 printf("Buffer read: %d bytes\n",newb);
|
|
70 len=newb;usleep(len*READ_SPEED*sleep_flag);
|
|
71 // ...
|
|
72
|
|
73 s->read_filepos+=len;
|
|
74 size-=len;
|
|
75 total+=len;
|
|
76
|
|
77 }
|
|
78 return total;
|
|
79 }
|
|
80
|
|
81 int cache_fill(cache_vars_t* s){
|
|
82 int read,back,newb,space,len,pos,endpos;
|
|
83
|
|
84 read=s->read_filepos;
|
|
85
|
|
86 // calc number of back-bytes:
|
|
87 back=read - s->min_filepos;
|
|
88 if(back<0) back=0; // strange...
|
|
89 if(back>s->back_size) back=s->back_size;
|
|
90
|
|
91 // calc number of new bytes:
|
|
92 newb=s->max_filepos - read;
|
|
93 if(newb<0) newb=0; // strange...
|
|
94
|
|
95 // calc free buffer space:
|
|
96 space=s->buffer_size - (newb+back);
|
|
97
|
|
98 // calc bufferpos:
|
|
99 pos=s->max_filepos - s->offset;
|
|
100 if(pos>=s->buffer_size) pos-=s->buffer_size; // wrap-around
|
|
101
|
|
102 if(space<s->fill_limit){
|
|
103 // printf("Buffer is full (%d bytes free, limit: %d)\n",space,s->fill_limit);
|
|
104 return 0; // no fill...
|
|
105 }
|
|
106
|
|
107 // printf("### read=0x%X back=%d newb=%d space=%d pos=%d\n",read,back,newb,space,pos);
|
|
108
|
|
109 // reduce space if needed:
|
|
110 if(space>s->buffer_size-pos) space=s->buffer_size-pos;
|
|
111
|
|
112 if(space>32768) space=32768; // limit one-time block size
|
|
113
|
|
114 s->min_filepos=read-back; // avoid seeking-back to temp area...
|
|
115
|
|
116 // ....
|
|
117 printf("Buffer fill: %d bytes of %d\n",space,s->buffer_size);
|
|
118 len=space; usleep(len*FILL_SPEED*sleep_flag);
|
|
119 // ....
|
|
120
|
|
121 s->max_filepos+=len;
|
|
122 if(pos+len>=s->buffer_size){
|
|
123 // wrap...
|
|
124 s->offset+=s->buffer_size;
|
|
125 }
|
|
126
|
|
127 return len;
|
|
128
|
|
129 }
|
|
130
|
|
131 cache_vars_t* cache_init(int size,int sector){
|
|
132 int num;
|
|
133 cache_vars_t* s=shmem_alloc(sizeof(cache_vars_t));
|
|
134 memset(s,0,sizeof(cache_vars_t));
|
|
135 num=size/sector;
|
|
136 s->buffer_size=num*sector;
|
|
137 s->sector_size=sector;
|
|
138 s->buffer=shmem_alloc(s->buffer_size);
|
|
139 s->fill_limit=8*sector;
|
|
140 s->back_size=size/2;
|
|
141 return s;
|
|
142 }
|
|
143
|
|
144 int main(){
|
|
145
|
|
146 cache_vars_t* s=cache_init(1024*1024,2048);
|
|
147
|
|
148 // while(cache_fill(s)){ } // fill buffer:
|
|
149
|
|
150 min_fill=s->buffer_size;
|
|
151 sleep_flag=1; // start simulation
|
|
152
|
|
153 if(fork()){
|
|
154 while(1){
|
|
155 if(!cache_fill(s)) usleep(10000); // wait 10ms for buffer space
|
|
156 //cache_stats(s);
|
|
157 }
|
|
158 } else {
|
|
159 srand(12345);
|
|
160 while(1){
|
|
161 int len=10+rand()&8191;
|
|
162 cache_stats(s);
|
|
163 cache_read(s,len);
|
|
164 }
|
|
165 }
|
|
166
|
|
167 }
|
|
168
|