Exemple #1
0
/* executing queries at each thread */
static void* queries_exec(void *param)
{
  /* create a memcached structure */
  memcached_st *memc;
  memc = memc_new();

  struct timeval tv_s, tv_e;

  thread_param* p = (thread_param*) param;

  pthread_mutex_lock (&printmutex);
  printf("start benching using thread%"PRIu64"\n", p->tid);
  pthread_mutex_unlock (&printmutex);

  query* queries = p->queries;
  p->time = 0;

  while (p->time < duration) {
    gettimeofday(&tv_s, NULL);  // start timing
    for (size_t i = 0 ; i < p->num_ops; i++) {
      enum query_types type = queries[i].type;
      char *key = queries[i].hashed_key;
      char buf[val_len];

      if (type == query_put) {
        memc_put(memc, key, buf);
        p->num_puts++;
      } else if (type == query_get) {
        char *val = memc_get(memc, key);
        p->num_gets++;
        if (val == NULL) {
          // cache miss, put something (gabage) in cache
          p->num_miss++;
          memc_put(memc, key, buf);
        } else {
          free(val);
          p->num_hits++;
        }
      } else {
        fprintf(stderr, "unknown query type\n");
      }
    }
    gettimeofday(&tv_e, NULL);  // stop timing
    p->time += timeval_diff(&tv_s, &tv_e);
  }

  size_t nops = p->num_gets + p->num_puts;
  p->tput = nops / p->time;

  pthread_mutex_lock (&printmutex);
  printf("thread%"PRIu64" gets %"PRIu64" items in %.2f sec \n",
         p->tid, nops, p->time);
  printf("#put = %zu, #get = %zu\n", p->num_puts, p->num_gets);
  printf("#miss = %zu, #hits = %zu\n", p->num_miss, p->num_hits);
  printf("hitratio = %.4f\n",   (float) p->num_hits / p->num_gets);
  printf("tput = %.2f\n",  p->tput);
  printf("\n");
  pthread_mutex_unlock (&printmutex);

  memcached_free(memc);

  printf("queries_exec...done\n");
  pthread_exit(NULL);
}
/* executing requests at each thread */
static void* requests_exec(void *param)
{
  /* create a memcached structure */
  memcached_st *memc;
  memc = memc_new();

  size_t unicItems = 0;
  char queryfile[1024];
  snprintf(queryfile, sizeof(queryfile), "%s/%s_query", outputfolder, workingset);
  
  FILE *fquery = fopen(queryfile, "w+");
  if (fquery == NULL){
      printf("Can't open query file: %s", queryfile);
      exit(-1);
  }
  fwrite(&key_len, sizeof(size_t), 1, fquery);
    fwrite(&val_len, sizeof(size_t), 1, fquery);
    fwrite(&unicItems, sizeof(size_t), 1, fquery);

//  char outputfile[1024];
//  snprintf(outputfile, sizeof(outputfile), "./benchmarks/%s.bench", workingset);

  /*
  FILE *fout = fopen(outputfile, "w");
  fprintf(fout, "key_len : %ld\n", key_len);
  fprintf(fout, "num_requests : %ld\n", num_requests);
  */
  struct timeval tv_s, tv_e;

  thread_param* p = (thread_param*) param;

  pthread_mutex_lock (&printmutex);
  printf("start benching using thread%"PRIu64"\n", p->tid);
  pthread_mutex_unlock (&printmutex);

  request* requests = p->requests;
  p->time = 0;
//  while (p->time < duration) {
//    gettimeofday(&tv_s, NULL);  // start timing

  request q;
  q.type = request_get;
  q.delta = 1;
      for (size_t i = 0 ; i < p->num_ops; i++) {
          enum request_types type = requests[i].type;
          char *key = requests[i].hashed_key;
          uint8_t delta = requests[i].delta;
          uint64_t value;

          int ret;

          /*
          if (type == request_init) {
              ret = memc_init_inc(memc, key, 0, 0, &value);
              if (ret == 0) p->num_puts++;
          } else
          */
          if (type == request_inc) {
              ret = memc_init_inc(memc, key, delta, delta, &value);
              if (ret == 0) {
                  p->num_gets++;
//                  printf("A:%ld", value);
                  if (value == delta){
                      memcpy(q.hashed_key, key, key_len);
                      fwrite(&q, sizeof(q), 1, fquery);
                      unicItems++;
//                      printf("B:%d", delta);
                  }
              }
          }
          /*
          else if (type == request_dec) {
              ret = memc_dec(memc, key, delta, &value);
              if (ret == 0) p->num_gets++;
          } else if (type == request_get) {
              ret = memc_inc(memc, key, 0, &value);
              //fwrite(key, NKEY, 1, fout);
              if (ret == 1) {
                  printf("X");
                  //printf("%s", memcached_strerror(memc, ret));
                  fprintf(fout, "%ld\n", (uint64_t)0);
              } else {
                  p->num_gets++;
                  fprintf(fout, "%ld\n", value);
              }
          }
          */
          else {
              fprintf(stderr, "unknown request type");
          }
      }
//      fclose(fquery);

      //     FILE *fquery2 = fopen(queryfile, "w+");
      fseek(fquery, 2*sizeof(size_t), SEEK_SET);
      fwrite(&unicItems, sizeof(size_t), 1, fquery);
      fclose(fquery);
      
  size_t nops = p->num_gets + p->num_puts;
  p->tput = nops / p->time;

  pthread_mutex_lock (&printmutex);
  printf("thread%"PRIu64" gets %"PRIu64" items in %.2f sec \n",
         p->tid, nops, p->time);
  printf("#put = %zu, #get = %zu\n", p->num_puts, p->num_gets);
  printf("#miss = %zu, #hits = %zu\n", p->num_miss, p->num_hits);
  printf("hitratio = %.4f\n",   (float) p->num_hits / p->num_gets);
  printf("tput = %.2f\n",  p->tput);
  printf("\n");
  pthread_mutex_unlock (&printmutex);

  memcached_free(memc);

  printf("requests_exec...done\n");
  pthread_exit(NULL);
}