MCPcopy Create free account
hub / github.com/1a1a11a/libCacheSim / go_back_one_req

Function go_back_one_req

libCacheSim/traceReader/reader.c:349–411  ·  view source on GitHub ↗

* @brief from current line/request, go back one, the next read will * get the current request * * @param reader * @return int */

Source from the content-addressed store, hash-verified

347 * @return int
348 */
349int go_back_one_req(reader_t *const reader) {
350 switch (reader->trace_format) {
351 case TXT_TRACE_FORMAT:;
352 ssize_t curr_offset = ftell(reader->file);
353 if (curr_offset <= reader->trace_start_offset) {
354 // we are at the start of the file
355 return 1;
356 }
357
358 ssize_t max_seek_size = curr_offset - reader->trace_start_offset;
359 ssize_t seek_size = 0;
360 ssize_t total_seek_size = 0;
361 bool found = false;
362 while (!found && total_seek_size < max_seek_size) {
363 seek_size = MIN(PER_SEEK_SIZE, max_seek_size - total_seek_size);
364 total_seek_size += seek_size;
365 fseek(reader->file, -seek_size, SEEK_CUR);
366 ssize_t read_size =
367 fread(reader->line_buf, 1, seek_size - 1, reader->file);
368 reader->line_buf[read_size - 1] = 0;
369 char *last_line_end = strrchr(reader->line_buf, '\n');
370
371 if (last_line_end == NULL) {
372 // three possible cases
373 // 1. reach the trace start
374 // 2. line is too long and has not found \n
375 // 3. error case
376 if (seek_size < PER_SEEK_SIZE) {
377 // case 1 reach the trace start
378 fseek(reader->file, reader->trace_start_offset, SEEK_SET);
379
380 return 0;
381 } else if (total_seek_size < max_seek_size) {
382 // case 2 line is too long and has not found \n
383 fseek(reader->file, -PER_SEEK_SIZE, SEEK_CUR);
384
385 } else {
386 WARN("go_back_one_req cannot find the request above\n");
387 return 1;
388 }
389 } else {
390 found = true;
391 int pos = last_line_end + 2 - reader->line_buf;
392 fseek(reader->file, -(seek_size - pos), SEEK_CUR);
393 return 0;
394 }
395 }
396 break;
397 case BINARY_TRACE_FORMAT:
398 if (reader->mmap_offset >=
399 reader->trace_start_offset + reader->item_size) {
400 reader->mmap_offset -= (reader->item_size);
401 return 0;
402 } else {
403 return 1;
404 }
405 break;
406 default:

Callers 6

test_reader_more1Function · 0.85
go_back_two_reqFunction · 0.85
reader_set_read_posFunction · 0.85
read_last_reqFunction · 0.85
convert_to_oracleGeneralFunction · 0.85
convert_to_lcsFunction · 0.85

Calls

no outgoing calls

Tested by 1

test_reader_more1Function · 0.68