//------------------------------------------------ // Discover device's minimum direct IO op size. // static uint64_t discover_min_op_bytes(int fd, const char* name) { uint8_t* buf = act_valloc(HI_IO_MIN_SIZE); if (! buf) { fprintf(stdout, "ERROR: IO min size buffer act_valloc()\n"); return 0; } size_t read_sz = LO_IO_MIN_SIZE; while (read_sz <= HI_IO_MIN_SIZE) { if (pread_all(fd, (void*)buf, read_sz, 0)) { free(buf); return read_sz; } read_sz <<= 1; // LO_IO_MIN_SIZE and HI_IO_MIN_SIZE are powers of 2 } fprintf(stdout, "ERROR: %s read failed at all sizes from %u to %u bytes\n", name, LO_IO_MIN_SIZE, HI_IO_MIN_SIZE); free(buf); return 0; }
/** * Read sector data from the offset. * * @fd file descriptor to read. * @offset offset in sectors. * @sect sectors data to be filled. * * RETURN: * true in success, or false. */ bool sector_read(int fd, u64 offset, struct sector_data *sect) { ASSERT(fd > 0); ASSERT_SECTOR_DATA(sect); return pread_all(fd, sect->data, sect->size, offset * sect->size); }
static int reread_data(Chunk *chunk, SpillControl *spillage) { assert(chunk->len > 0 && chunk->len <= CHUNK_SZ); chunk->data = malloc(chunk->len); if (NULL == chunk->data) { perror("reread_data"); return -1; } if (0 != pread_all(chunk->spill->fd, chunk->data, chunk->len, chunk->spill_offset)) { return -1; } spillage->alloced += CHUNK_SZ; if (spillage->alloced > spillage->max_alloced) { spillage->max_alloced = spillage->alloced; } if (verbosity > 2) { if (chunk->spill->is_tmp) { fprintf(stderr, "%.3f Re-read %zd bytes (%sB) from spill file #%d\n", get_time(), chunk->len, human_size(chunk->len), chunk->spill->is_tmp); } else { fprintf(stderr, "%.3f Re-read %zd bytes (%sB) from input file\n", get_time(), chunk->len, human_size(chunk->len)); } } return 0; }
/** * Read to sector data in units of logical block. * Logical block size is 512 bytes. * * @fd file descriptor to write. * @offset_lb offset [logical block]. * @sect sector data to be partially read. * @idx_lb start index [logical block]. * @n_lb number of logical blocks to read. * * RETURN: * true in success, or false. */ bool sector_read_lb( int fd, u64 offset_lb, struct sector_data *sect, unsigned int idx_lb, unsigned int n_lb) { const size_t count = n_lb * LOGICAL_BLOCK_SIZE; const off_t buf_off = idx_lb * LOGICAL_BLOCK_SIZE; const off_t file_off = offset_lb * LOGICAL_BLOCK_SIZE; ASSERT(fd > 0); ASSERT_SECTOR_DATA(sect); ASSERT(capacity_pb(sect->size, idx_lb + n_lb) == 1); return pread_all(fd, sect->data + buf_off, count, file_off); }
//------------------------------------------------ // Do one device read operation. // static uint64_t read_from_device(device* dev, uint64_t offset, uint32_t size, uint8_t* buf) { int fd = fd_get(dev); if (fd == -1) { return -1; } if (! pread_all(fd, buf, size, offset)) { close(fd); fprintf(stdout, "ERROR: reading %s: %d '%s'\n", dev->name, errno, act_strerror(errno)); return -1; } uint64_t stop_ns = get_ns(); fd_put(dev, fd); return stop_ns; }