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 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 40
  setThreadName("addRef");

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

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

59 60
  setThreadName("removeRef");

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

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

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

80 81
  setThreadName("acquireRelease");

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

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

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

103 104
  setThreadName("openRefSpace");

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

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
108 109
  if (pSpace->rsetId < 0) {
    printf("failed to open ref, reson:%s\n", tstrerror(pSpace->rsetId));
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
110 111 112 113 114 115 116 117 118 119
    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) 已提交
120 121 122
  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) 已提交
123 124 125 126 127 128

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

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

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

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