trefTest.c 4.7 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 39
  setThreadName("addRef");

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

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

57 58
  setThreadName("removeRef");

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

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

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

77 78
  setThreadName("acquireRelease");

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

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

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

100 101
  setThreadName("openRefSpace");

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

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

  pSpace->p = (void **) calloc(sizeof(void *), pSpace->refNum);
111
  pSpace->rid = calloc(pSpace->refNum, sizeof(int64_t));
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
112 113 114 115 116 117

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

  pthread_t thread1, thread2, thread3;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
118 119 120
  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) 已提交
121 122 123 124 125 126

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

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
130 131 132
  taosCloseRef(pSpace->rsetId);

  uInfo("rsetId:%d main thread exit", pSpace->rsetId);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  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) 已提交
162
      printf("  [-t]: number of rsetIds running in parallel, default: %d\n", threads);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
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 196 197 198 199 200 201
      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;
}