Changeset 3972


Ignore:
Timestamp:
11/23/10 18:36:18 (13 years ago)
Author:
schlocke
Message:

loadbalancer: because we have an ecm cache, loadbalancer doesn't need a
ecm cache any longer. Also thread locking is removed, this speeds up the
loadbalancer

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 line
    21#include "module-stat.h"
    32
     
    87static struct timeb nulltime;
    98
    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 
    209void init_stat()
    2110{
    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);
    2411    cs_ftime(&nulltime);
    2512    stat_load_save = -100;
     
    3623    if (cfg->lb_reopen_seconds < 10)
    3724        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     }
    7625}
    7726
     
    13685READER_STAT *get_stat(struct s_reader *rdr, ushort caid, ulong prid, ushort srvid)
    13786{
    138     pthread_mutex_lock(&stat_busy);
    13987    if (stat_load_save < 0 && cfg->lb_save)
    14088        load_stat_from_file();
     
    15098    }
    15199    ll_iter_release(it);
    152     pthread_mutex_unlock(&stat_busy);
    153100    return stat;
    154101}
     
    162109        return 0;
    163110
    164     pthread_mutex_lock(&stat_busy);
    165111    int c = 0;
    166112    LL_ITER *it = ll_iter_create(rdr->lb_stat);
     
    173119    }
    174120    ll_iter_release(it);
    175     clear_from_cache(caid);
    176     pthread_mutex_unlock(&stat_busy);
    177121    return c;
    178122}
     
    203147void save_stat_to_file()
    204148{
    205     pthread_mutex_lock(&stat_busy);
    206149    stat_load_save = 0;
    207150    char buf[256];
     
    211154    if (!file) {
    212155        cs_log("can't write to file %s", buf);
    213         pthread_mutex_unlock(&stat_busy);
    214156        return;
    215157    }
     
    234176   
    235177    fclose(file);
    236     pthread_mutex_unlock(&stat_busy);
    237178    cs_log("loadbalacer statistic saved %d records", count);
    238179}
     
    245186    if (!rdr)
    246187        return;
    247     pthread_mutex_lock(&stat_busy);
    248188    READER_STAT *stat = get_stat(rdr, caid, prid, srvid);
    249189    if (!stat) {
     
    312252        stat->rc = rc;
    313253        //stat->ecm_count = 0; Keep ecm_count!
    314         clear_from_cache(caid);
    315254    }
    316255    else if (rc == 5) { //timeout
     
    328267        }
    329268    }
    330     pthread_mutex_unlock(&stat_busy);
    331269}
    332270
    333271void reset_stat(ushort caid, ulong prid, ushort srvid)
    334272{
    335     pthread_mutex_lock(&stat_busy);
    336273    //cs_debug_mask(D_TRACE, "loadbalance: resetting ecm count");
    337274    struct s_reader *rdr;
     
    347284        }
    348285    }
    349     pthread_mutex_unlock(&stat_busy);
    350286}
    351287
     
    359295int get_best_reader(ECM_REQUEST *er)
    360296{
    361     pthread_mutex_lock(&stat_busy);
    362297    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 
    379298    int result[CS_MAXREADER];
    380299    memset(result, 0, sizeof(result));
     
    549468    }
    550469   
    551     add_send_cache(er->caid, er->ecmd5, er->matching_rdr, best_rdr, er->client->grp); //add to cache
    552    
    553470    if (new_nulltime.time)
    554471        nulltime = new_nulltime;
    555472       
    556     pthread_mutex_unlock(&stat_busy);
    557    
    558473    return 1;
    559474}
     
    567482        return;
    568483
    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  
    11#include "globals.h"
    2 //#include "module-datastruct-llist.h"
    32#include "module-datastruct-llist.h"
    4 
    5 static pthread_mutex_t stat_busy;
    63
    74void init_stat();
Note: See TracChangeset for help on using the changeset viewer.