trefTest.c 4.6 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
  int     refNum;
  int     steps;
  int     rsetId;
17
  int64_t*rid;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
18
  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;

24
  void *p = taosIterateRef(rsetId, 0);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
25 26 27
  while (p) {
    // process P
    count++;
28
    p = taosIterateRef(rsetId, (int64_t) 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
  int id;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
37 38

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

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

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

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

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

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

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

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

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
97 98
  if (pSpace->rsetId < 0) {
    printf("failed to open ref, reson:%s\n", tstrerror(pSpace->rsetId));
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
99 100 101 102
    return NULL;
  } 

  pSpace->p = (void **) calloc(sizeof(void *), pSpace->refNum);
103
  pSpace->rid = calloc(pSpace->refNum, sizeof(int64_t));
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
104 105 106 107 108 109

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

  pthread_t thread1, thread2, thread3;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
110 111 112
  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) 已提交
113 114 115 116 117 118

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

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

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

  uInfo("rsetId:%d main thread exit", pSpace->rsetId);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
125 126 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
  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) 已提交
154
      printf("  [-t]: number of rsetIds running in parallel, default: %d\n", threads);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
155 156 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
      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;
}