trefTest.c 4.5 KB
Newer Older
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include "os.h"
#include "tref.h"
#include "tlog.h"
#include "tglobal.h"
#include "taoserror.h"
#include "tulog.h"

typedef struct {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
14 15 16 17 18
  int     refNum;
  int     steps;
  int     rsetId;
  int64_t rid;
  void  **p;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
19 20
} SRefSpace;

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
21
void iterateRefs(int rsetId) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
22 23
  int  count = 0;

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
24
  void *p = taosIterateRef(rsetId, NULL);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
25 26 27
  while (p) {
    // process P
    count++;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
28
    p = taosIterateRef(rsetId, p);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
29 30 31 32 33
  }    

  printf(" %d ", count); 
}

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
34
void *addRef(void *param) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
35
  SRefSpace *pSpace = (SRefSpace *)param;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
36 37
  int id;
  int64_t rid;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
38 39

  for (int i=0; i < pSpace->steps; ++i) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
40
    printf("a");
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
41
    id = random() % pSpace->refNum; 
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
42 43 44
    if (pSpace->rid[id] <= 0) {
      pSpace->p[id] = malloc(128);
      pSpace->rid[id] = taosAddRef(pSpace->rsetId, pSpace->p[id]);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
45
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
46 47
    usleep(100);
  }  
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
48

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
49 50 51 52 53 54 55 56 57 58
  return NULL;
}
       
void *removeRef(void *param) {
  SRefSpace *pSpace = (SRefSpace *)param;
  int id;
  int64_t rid;

  for (int i=0; i < pSpace->steps; ++i) {
    printf("d");
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
59
    id = random() % pSpace->refNum; 
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
60 61 62 63
    if (pSpace->rid[id] > 0) {
      code = taosRemoveRef(pSpace->rsetId, pSpace->rid[id]);
      if (code == 0) pSpace->rid[id] = 0;
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
64

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78
    usleep(100);
  }  

  return NULL;
}
       
void *acquireRelease(void *param) {
  SRefSpace *pSpace = (SRefSpace *)param;
  int id;
  int64_t rid;

  for (int i=0; i < pSpace->steps; ++i) {
    printf("a");
    
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
79
    id = random() % pSpace->refNum; 
80 81
    void *p = taosAcquireRef(pSpace->rsetId, pSpace->p[id]);
    if (p) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
82
      usleep(id % 5 + 1);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
83
      taosReleaseRef(pSpace->rsetId, pSpace->p[id]);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
84 85 86 87 88 89 90
    }
  }  

  return NULL;
}
       
void myfree(void *p) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
91
  free(p);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
92 93 94 95 96 97
}

void *openRefSpace(void *param) {
  SRefSpace *pSpace = (SRefSpace *)param;

  printf("c");
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
98
  pSpace->rsetId = taosOpenRef(50, myfree);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
99

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
100 101
  if (pSpace->rsetId < 0) {
    printf("failed to open ref, reson:%s\n", tstrerror(pSpace->rsetId));
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
102 103 104 105 106 107 108 109 110 111
    return NULL;
  } 

  pSpace->p = (void **) calloc(sizeof(void *), pSpace->refNum);

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);
  pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);

  pthread_t thread1, thread2, thread3;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
112 113 114
  pthread_create(&(thread1), &thattr, addRef, (void *)(pSpace));
  pthread_create(&(thread2), &thattr, removeRef, (void *)(pSpace));
  pthread_create(&(thread3), &thattr, acquireRelease, (void *)(pSpace));
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
115 116 117 118 119 120

  pthread_join(thread1, NULL);
  pthread_join(thread2, NULL);
  pthread_join(thread3, NULL);

  for (int i=0; i<pSpace->refNum; ++i) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
121
    taosRemoveRef(pSpace->rsetId, pSpace->rid[i]);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
122 123
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
124 125 126
  taosCloseRef(pSpace->rsetId);

  uInfo("rsetId:%d main thread exit", pSpace->rsetId);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  free(pSpace->p);
  pSpace->p = NULL;

  return NULL;
}

int main(int argc, char *argv[]) {
  int refNum = 100;
  int threads = 10;
  int steps = 10000;  
  int loops = 1;

  uDebugFlag = 143;

  for (int i=1; i<argc; ++i) {
    if (strcmp(argv[i], "-n")==0 && i < argc-1) {
      refNum = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-s")==0 && i < argc-1) {
      steps = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-t")==0 && i < argc-1) {
      threads = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-l")==0 && i < argc-1) {
      loops = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-d")==0 && i < argc-1) {
      uDebugFlag = atoi(argv[i]);
    } else {
      printf("\nusage: %s [options] \n", argv[0]);
      printf("  [-n]: number of references, default: %d\n", refNum);
      printf("  [-s]: steps to run for each reference, default: %d\n", steps);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
156
      printf("  [-t]: number of rsetIds running in parallel, default: %d\n", threads);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
      printf("  [-l]: number of loops, default: %d\n", loops);
      printf("  [-d]: debugFlag, default: %d\n", uDebugFlag);
      exit(0);
    }
  }

  taosInitLog("tref.log", 5000000, 10);

  SRefSpace *pSpaceList = (SRefSpace *) calloc(sizeof(SRefSpace), threads);
  pthread_t *pThreadList = (pthread_t *) calloc(sizeof(pthread_t), threads);

  pthread_attr_t thattr;
  pthread_attr_init(&thattr);
  pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);

  for (int i=0; i<loops; ++i) {
    printf("\nloop: %d\n", i);
    for (int j=0; j<threads; ++j) {
      pSpaceList[j].steps = steps;
      pSpaceList[j].refNum = refNum;
      pthread_create(&(pThreadList[j]), &thattr, openRefSpace, (void *)(pSpaceList+j));
    }

    for (int j=0; j<threads; ++j) {
      pthread_join(pThreadList[j], NULL);
    }
  }

  int num = taosListRef();
  printf("\nnumber of references:%d\n", num);

  free(pSpaceList);
  free(pThreadList);

  taosCloseLog();

  return num;
}