Changeset 3972
- Timestamp:
- 11/23/10 18:36:18 (13 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/module-stat.c
r3893 r3972 1 //FIXME Not checked on threadsafety yet; not necessary if only 1 stat running; after checking please remove this line2 1 #include "module-stat.h" 3 2 … … 8 7 static struct timeb nulltime; 9 8 10 int ecm_send_cache_idx = 0;11 typedef struct s_ecm_send_cache {12 ushort caid;13 uint64 grp;14 uchar ecmd5[CS_ECMSTORESIZE];15 int readers[CS_MAXREADER];16 struct s_reader * best_rdr;17 } ECM_SEND_CACHE;18 ECM_SEND_CACHE *ecm_send_cache;19 20 9 void init_stat() 21 10 { 22 ecm_send_cache = malloc(sizeof(ECM_SEND_CACHE)*MAX_ECM_SEND_CACHE);23 memset(ecm_send_cache, 0, sizeof(ECM_SEND_CACHE)*MAX_ECM_SEND_CACHE);24 11 cs_ftime(&nulltime); 25 12 stat_load_save = -100; … … 36 23 if (cfg->lb_reopen_seconds < 10) 37 24 cfg->lb_reopen_seconds = DEFAULT_REOPEN_SECONDS; 38 39 //init mutex lock with recusive attribute:40 pthread_mutexattr_t mta;41 pthread_mutexattr_init(&mta);42 pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);43 pthread_mutex_init(&stat_busy, &mta);44 }45 46 int chk_send_cache(int caid, uchar *ecmd5, uint64 grp)47 {48 int i;49 for (i=0; i<MAX_ECM_SEND_CACHE; i++) {50 if (ecm_send_cache[i].caid == caid && (grp&ecm_send_cache[i].grp) &&51 memcmp(ecm_send_cache[i].ecmd5, ecmd5, sizeof(uchar)*CS_ECMSTORESIZE) == 0)52 return i;53 }54 return -1;55 }56 57 void add_send_cache(int caid, uchar *ecmd5, int *readers, struct s_reader *best_rdr, uint64 grp)58 {59 ecm_send_cache[ecm_send_cache_idx].caid = caid;60 memcpy(ecm_send_cache[ecm_send_cache_idx].ecmd5, ecmd5, sizeof(uchar)*CS_ECMSTORESIZE);61 memcpy(ecm_send_cache[ecm_send_cache_idx].readers, readers, sizeof(int)*CS_MAXREADER);62 ecm_send_cache[ecm_send_cache_idx].best_rdr = best_rdr;63 ecm_send_cache[ecm_send_cache_idx].grp = grp;64 ecm_send_cache_idx++;65 if (ecm_send_cache_idx >= MAX_ECM_SEND_CACHE)66 ecm_send_cache_idx = 0;67 }68 69 void clear_from_cache(int caid)70 {71 int i;72 for (i=0; i<MAX_ECM_SEND_CACHE; i++) {73 if (ecm_send_cache[i].caid == caid)74 ecm_send_cache[i].caid = 0;75 }76 25 } 77 26 … … 136 85 READER_STAT *get_stat(struct s_reader *rdr, ushort caid, ulong prid, ushort srvid) 137 86 { 138 pthread_mutex_lock(&stat_busy);139 87 if (stat_load_save < 0 && cfg->lb_save) 140 88 load_stat_from_file(); … … 150 98 } 151 99 ll_iter_release(it); 152 pthread_mutex_unlock(&stat_busy);153 100 return stat; 154 101 } … … 162 109 return 0; 163 110 164 pthread_mutex_lock(&stat_busy);165 111 int c = 0; 166 112 LL_ITER *it = ll_iter_create(rdr->lb_stat); … … 173 119 } 174 120 ll_iter_release(it); 175 clear_from_cache(caid);176 pthread_mutex_unlock(&stat_busy);177 121 return c; 178 122 } … … 203 147 void save_stat_to_file() 204 148 { 205 pthread_mutex_lock(&stat_busy);206 149 stat_load_save = 0; 207 150 char buf[256]; … … 211 154 if (!file) { 212 155 cs_log("can't write to file %s", buf); 213 pthread_mutex_unlock(&stat_busy);214 156 return; 215 157 } … … 234 176 235 177 fclose(file); 236 pthread_mutex_unlock(&stat_busy);237 178 cs_log("loadbalacer statistic saved %d records", count); 238 179 } … … 245 186 if (!rdr) 246 187 return; 247 pthread_mutex_lock(&stat_busy);248 188 READER_STAT *stat = get_stat(rdr, caid, prid, srvid); 249 189 if (!stat) { … … 312 252 stat->rc = rc; 313 253 //stat->ecm_count = 0; Keep ecm_count! 314 clear_from_cache(caid);315 254 } 316 255 else if (rc == 5) { //timeout … … 328 267 } 329 268 } 330 pthread_mutex_unlock(&stat_busy);331 269 } 332 270 333 271 void reset_stat(ushort caid, ulong prid, ushort srvid) 334 272 { 335 pthread_mutex_lock(&stat_busy);336 273 //cs_debug_mask(D_TRACE, "loadbalance: resetting ecm count"); 337 274 struct s_reader *rdr; … … 347 284 } 348 285 } 349 pthread_mutex_unlock(&stat_busy);350 286 } 351 287 … … 359 295 int get_best_reader(ECM_REQUEST *er) 360 296 { 361 pthread_mutex_lock(&stat_busy);362 297 int i; 363 i = chk_send_cache(er->caid, er->ecmd5, er->client->grp);364 if (i >= 0) { //Found in cache, return same reader because he has the cached cws!365 memcpy(er->matching_rdr, ecm_send_cache[i].readers, sizeof(int)*CS_MAXREADER);366 struct s_reader * best_rdr = ecm_send_cache[i].best_rdr;367 cs_debug_mask(D_TRACE, "loadbalancer: client %s for %04X/%06X/%04X: %s readers: %d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d (cache)",368 username(er->client), er->caid, er->prid, er->srvid,369 best_rdr?best_rdr->label:"NONE",370 er->matching_rdr[0], er->matching_rdr[1], er->matching_rdr[2], er->matching_rdr[3],371 er->matching_rdr[4], er->matching_rdr[5], er->matching_rdr[6], er->matching_rdr[7],372 er->matching_rdr[8], er->matching_rdr[9], er->matching_rdr[10], er->matching_rdr[11],373 er->matching_rdr[12], er->matching_rdr[13], er->matching_rdr[14], er->matching_rdr[15]);374 375 pthread_mutex_unlock(&stat_busy);376 return 0;377 }378 379 298 int result[CS_MAXREADER]; 380 299 memset(result, 0, sizeof(result)); … … 549 468 } 550 469 551 add_send_cache(er->caid, er->ecmd5, er->matching_rdr, best_rdr, er->client->grp); //add to cache552 553 470 if (new_nulltime.time) 554 471 nulltime = new_nulltime; 555 472 556 pthread_mutex_unlock(&stat_busy);557 558 473 return 1; 559 474 } … … 567 482 return; 568 483 569 pthread_mutex_lock(&stat_busy); 570 ll_destroy_data(rdr->lb_stat); 571 rdr->lb_stat = ll_create(); 572 pthread_mutex_unlock(&stat_busy); 573 } 484 ll_clear_data(rdr->lb_stat); 485 } -
trunk/module-stat.h
r3812 r3972 1 1 #include "globals.h" 2 //#include "module-datastruct-llist.h"3 2 #include "module-datastruct-llist.h" 4 5 static pthread_mutex_t stat_busy;6 3 7 4 void init_stat();
Note:
See TracChangeset
for help on using the changeset viewer.