2 * UCW Library -- Main Loop: Record I/O
4 * (c) 2011 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 #include "ucw/mainloop.h"
29 rec_io_timer_expired(struct main_timer *tm)
31 struct main_rec_io *rio = tm->data;
32 timer_del(&rio->timer);
33 rio->notify_handler(rio, RIO_ERR_TIMEOUT);
36 static int rec_io_deferred_start_read(struct main_hook *ho);
39 rec_io_add(struct main_rec_io *rio, int fd)
43 rio->timer.handler = rec_io_timer_expired;
44 rio->timer.data = rio;
45 rio->start_read_hook.handler = rec_io_deferred_start_read;
46 rio->start_read_hook.data = rio;
47 clist_init(&rio->idle_write_buffers);
48 clist_init(&rio->busy_write_buffers);
52 rec_io_del(struct main_rec_io *rio)
54 timer_del(&rio->timer);
55 if (clist_is_linked(&rio->start_read_hook.n))
56 hook_del(&rio->start_read_hook);
61 DBG("RIO: Freeing read buffer");
67 while ((b = clist_remove_head(&rio->idle_write_buffers)) || (b = clist_remove_head(&rio->busy_write_buffers)))
69 DBG("RIO: Freeing write buffer");
75 rec_io_process_read_buf(struct main_rec_io *rio)
78 while (rio->read_running && (got = rio->read_handler(rio)))
80 DBG("RIO READ: Ate %u bytes", got);
83 rio->read_rec_start += got;
84 rio->read_avail -= got;
85 rio->read_prev_avail = 0;
88 DBG("RIO READ: Resetting buffer");
89 rio->read_rec_start = rio->read_buf;
93 DBG("RIO READ: Want more");
94 return (rio->read_running ? HOOK_RETRY : HOOK_IDLE);
98 rec_io_read_handler(struct main_file *fi)
100 struct main_rec_io *rio = (struct main_rec_io *) fi;
102 if (rio->read_rec_max && rio->read_avail >= rio->read_rec_max)
104 rec_io_stop_read(rio);
105 rio->notify_handler(rio, RIO_ERR_RECORD_TOO_LARGE);
110 uns rec_start_pos = rio->read_rec_start - rio->read_buf;
111 uns rec_end_pos = rec_start_pos + rio->read_avail;
112 uns free_space = rio->read_buf_size - rec_end_pos;
113 DBG("RIO READ: rec_start=%u avail=%u prev_avail=%u free=%u/%u",
114 rec_start_pos, rio->read_avail, rio->read_prev_avail,
115 free_space, rio->read_buf_size);
117 if (free_space <= rio->read_buf_size/8)
119 if (rec_start_pos && rec_start_pos >= rio->read_buf_size/2)
121 // Moving the partial record to the start of the buffer
122 DBG("RIO READ: Moving partial record to start");
123 memmove(rio->read_buf, rio->read_rec_start, rio->read_avail);
124 rio->read_rec_start = rio->read_buf;
128 DBG("RIO READ: Resizing buffer");
129 rio->read_buf_size *= 2;
130 rio->read_buf = xrealloc(rio->read_buf, rio->read_buf_size);
131 rio->read_rec_start = rio->read_buf + rec_start_pos;
136 int l = read(fi->fd, rio->read_buf + rec_end_pos, free_space);
137 DBG("RIO READ: Read %d bytes", l);
140 if (errno != EINTR && errno != EAGAIN)
142 DBG("RIO READ: Signalling error");
143 rec_io_stop_read(rio);
144 rio->notify_handler(rio, RIO_ERR_READ);
150 DBG("RIO READ: Signalling EOF");
151 rec_io_stop_read(rio);
152 rio->notify_handler(rio, RIO_EVENT_EOF);
155 rio->read_prev_avail = rio->read_avail;
156 rio->read_avail += l;
157 DBG("RIO READ: Available: %u bytes", rio->read_avail);
159 return rec_io_process_read_buf(rio);
163 rec_io_deferred_start_read(struct main_hook *ho)
165 struct main_rec_io *rio = ho->data;
167 DBG("RIO: Starting reading");
170 if (!rio->read_buf_size)
171 rio->read_buf_size = 256;
172 rio->read_buf = xmalloc(rio->read_buf_size);
173 DBG("RIO: Created read buffer (%u bytes)", rio->read_buf_size);
174 rio->read_rec_start = rio->read_buf;
177 rio->file.read_handler = rec_io_read_handler;
178 file_chg(&rio->file);
180 rio->read_running = 1;
182 rio->read_prev_avail = 0;
183 return rec_io_process_read_buf(rio);
187 rec_io_recalc_read(struct main_rec_io *rio)
189 uns flow = !rio->write_throttle_read || rio->write_watermark < rio->write_throttle_read;
190 uns run = rio->read_started && flow;
191 DBG("RIO: Recalc read (flow=%u, start=%u) -> %u", flow, rio->read_started, run);
192 if (run != rio->read_running)
197 * Since we need to rescan the read buffer for leftover records and we
198 * can be deep in the call stack at this moment, we better defer most
199 * of the work to a main_hook, which will be called in the next iteration
202 if (!clist_is_linked(&rio->start_read_hook.n))
204 DBG("RIO: Scheduling start of reading");
205 hook_add(&rio->start_read_hook);
210 if (clist_is_linked(&rio->start_read_hook.n))
212 DBG("RIO: Descheduling start of reading");
213 hook_del(&rio->start_read_hook);
215 rio->file.read_handler = NULL;
216 file_chg(&rio->file);
217 DBG("RIO: Reading stopped");
218 rio->read_running = 0;
224 rec_io_start_read(struct main_rec_io *rio)
226 ASSERT(clist_is_linked(&rio->file.n));
227 rio->read_started = 1;
228 rec_io_recalc_read(rio);
232 rec_io_stop_read(struct main_rec_io *rio)
234 ASSERT(clist_is_linked(&rio->file.n));
235 rio->read_started = 0;
236 rec_io_recalc_read(rio);
240 rec_io_stop_write(struct main_rec_io *rio)
242 DBG("RIO WRITE: Stopping write");
243 ASSERT(!rio->write_watermark);
244 rio->file.write_handler = NULL;
245 file_chg(&rio->file);
249 rec_io_write_handler(struct main_file *fi)
251 struct main_rec_io *rio = (struct main_rec_io *) fi;
252 struct rio_buffer *b = clist_head(&rio->busy_write_buffers);
255 rec_io_stop_write(rio);
259 int l = write(fi->fd, b->buf + b->written, b->full - b->written);
260 DBG("RIO WRITE: Written %d bytes", l);
263 if (errno != EINTR && errno != EAGAIN)
265 rec_io_stop_write(rio);
266 rio->notify_handler(rio, RIO_ERR_WRITE);
271 if (b->written == b->full)
273 DBG("RIO WRITE: Written full buffer");
275 clist_add_tail(&rio->idle_write_buffers, &b->n);
278 rio->write_watermark -= l;
279 int ret = HOOK_RETRY;
280 if (!rio->write_watermark)
283 rec_io_stop_write(rio);
285 rec_io_recalc_read(rio);
287 // Call the hook, but carefully, because it can delete the RIO structure
288 if (rio->notify_handler(rio, rio->write_watermark ? RIO_EVENT_PART_WRITTEN : RIO_EVENT_ALL_WRITTEN) == HOOK_IDLE)
293 static struct rio_buffer *
294 rec_io_get_buffer(struct main_rec_io *rio)
296 struct rio_buffer *b = clist_remove_tail(&rio->idle_write_buffers);
298 DBG("RIO WRITE: Recycled old buffer");
301 if (!rio->write_buf_size)
302 rio->write_buf_size = 1024;
303 b = xmalloc(sizeof(struct rio_buffer) + rio->write_buf_size);
304 DBG("RIO WRITE: Allocated new buffer");
306 b->full = b->written = 0;
311 rec_io_write(struct main_rec_io *rio, void *data, uns len)
314 ASSERT(clist_is_linked(&rio->file.n));
320 struct rio_buffer *b = clist_tail(&rio->busy_write_buffers);
321 if (!b || b->full >= rio->write_buf_size)
323 b = rec_io_get_buffer(rio);
324 clist_add_tail(&rio->busy_write_buffers, &b->n);
326 uns l = MIN(len, rio->write_buf_size - b->full);
327 memcpy(b->buf + b->full, bdata, l);
331 rio->write_watermark += l;
332 DBG("RIO WRITE: Buffered %u bytes of data (total %u)", l, rio->write_watermark);
333 rec_io_recalc_read(rio);
336 if (!rio->file.write_handler)
338 DBG("RIO WRITE: Starting write");
339 rio->file.write_handler = rec_io_write_handler;
340 file_chg(&rio->file);
345 rec_io_set_timeout(struct main_rec_io *rio, timestamp_t expires_delta)
347 DBG("RIO: Setting timeout %u", (uns) expires_delta);
349 timer_del(&rio->timer);
351 timer_add_rel(&rio->timer, expires_delta);
355 rec_io_parse_line(struct main_rec_io *rio)
357 for (uns i = rio->read_prev_avail; i < rio->read_avail; i++)
358 if (rio->read_rec_start[i] == '\n')
365 static uns rhand(struct main_rec_io *rio)
367 uns r = rec_io_parse_line(rio);
370 rio->read_rec_start[r-1] = 0;
371 printf("Read <%s>\n", rio->read_rec_start);
372 if (rio->read_rec_start[0] == '!')
378 rec_io_set_timeout(rio, 10000);
379 rio->read_rec_start[r-1] = '\n';
380 rec_io_write(rio, rio->read_rec_start, r);
385 static int ehand(struct main_rec_io *rio, int cause)
387 if (cause < 0 || cause == RIO_EVENT_EOF)
389 msg(L_ERROR, "Error %d", cause);
396 msg(L_INFO, "Event %d", cause);
407 struct main_rec_io rio = {};
408 rio.read_buf_size = 4;
409 rio.read_handler = rhand;
410 rio.notify_handler = ehand;
411 // rio.read_rec_max = 40;
412 rio.write_buf_size = 4;
413 rio.write_throttle_read = 6;
415 rec_io_start_read(&rio);
416 rec_io_set_timeout(&rio, 10000);
421 msg(L_INFO, "Finished.");
423 if (clist_is_linked(&rio.file.n))