Esempio n. 1
0
void
TestServiceByFileOfWords(char *queryFile)
{
  const int maxWordSize = 64 + 1;  // A word is 64 characters plus a null byte
  FILE *file = fopen(queryFile, "r");

  if (file == NULL) {
    perror("fopen");
    fprintf(stderr, "Can't open query file %s\n",queryFile);
    exit(EXIT_FAILURE);
  }

  char line[maxWordSize];
  while (fgets(line, maxWordSize, file) != NULL) {
    int len = strlen(line);
    if (len > 0) {
	  /* Strip the \n character */
	  if (line[len-1] == '\n') line[len-1] = 0;
	  QueryWord(line,diskIndex, stdout);
    }
  }
  rewind(file);

  /* Do timing without printf */
  int64_t startTime = Debug_GetTimeInMicrosecs();
  while (fgets(line, maxWordSize, file) != NULL) {
    QueryWord(line,diskIndex, NULL);
  }
  int64_t endTime = Debug_GetTimeInMicrosecs();

  if (!quietFlag) {
    printf("QueryFile %s took %f microseconds\n",
           queryFile, (double)(endTime - startTime));
  }

  fclose(file);
}
Esempio n. 2
0
/**
 * Perform either a read or a write to disk.  Return number of bytes read or
 * written, or -1 on error.
 *
 * Since we declared this function as ALWAYS_INLINE, the compiler will make two
 * copies, one for read() and one for write(), and will optimize away our
 * |if(read)| statements in each copy.
 */
static ALWAYS_INLINE int
disksim_perform_operation(int fd, int sectorNum, void* buf, bool do_read)
{
  int simulateDisk = (diskLatency > 0);

  off_t offset = sectorNum * DISKIMG_SECTOR_SIZE;

  pthread_mutex_lock(&disk_mutex);

  int64_t startTime = 0;

  if (simulateDisk) {
    startTime = Debug_GetTimeInMicrosecs();
  }

int error;
  if (error = lseek(fd, offset, SEEK_SET) == (off_t) -1) {
    pthread_mutex_unlock(&disk_mutex);
    return -1;
  }

  ssize_t bytes;
  if (do_read) {
    bytes = read(fd, buf, DISKIMG_SECTOR_SIZE);
    numreads++;
  }
  else {
    bytes = write(fd, buf, DISKIMG_SECTOR_SIZE);
    numwrites++;
  }
  pthread_mutex_unlock(&disk_mutex);

  if (simulateDisk) {
    SimulateDiskLatency(startTime);
  }

  return bytes;
}