1 | #include "globals.h"
|
---|
2 |
|
---|
3 | #ifdef WITH_LB
|
---|
4 | #include "module-stat.h"
|
---|
5 | #include "module-cccam.h"
|
---|
6 |
|
---|
7 | #define UNDEF_AVG_TIME 80000
|
---|
8 | #define MAX_ECM_SEND_CACHE 16
|
---|
9 |
|
---|
10 | #define LB_REOPEN_MODE_STANDARD 0
|
---|
11 | #define LB_REOPEN_MODE_FAST 1
|
---|
12 |
|
---|
13 | static int32_t stat_load_save;
|
---|
14 | static struct timeb nulltime;
|
---|
15 | static time_t last_housekeeping = 0;
|
---|
16 |
|
---|
17 | void init_stat()
|
---|
18 | {
|
---|
19 | cs_ftime(&nulltime);
|
---|
20 | stat_load_save = -100;
|
---|
21 |
|
---|
22 | //checking config
|
---|
23 | if (cfg.lb_nbest_readers < 2)
|
---|
24 | cfg.lb_nbest_readers = DEFAULT_NBEST;
|
---|
25 | if (cfg.lb_nfb_readers < 2)
|
---|
26 | cfg.lb_nfb_readers = DEFAULT_NFB;
|
---|
27 | if (cfg.lb_min_ecmcount < 2)
|
---|
28 | cfg.lb_min_ecmcount = DEFAULT_MIN_ECM_COUNT;
|
---|
29 | if (cfg.lb_max_ecmcount < 3)
|
---|
30 | cfg.lb_max_ecmcount = DEFAULT_MAX_ECM_COUNT;
|
---|
31 | if (cfg.lb_reopen_seconds < 10)
|
---|
32 | cfg.lb_reopen_seconds = DEFAULT_REOPEN_SECONDS;
|
---|
33 | if (cfg.lb_retrylimit <= 0)
|
---|
34 | cfg.lb_retrylimit = DEFAULT_RETRYLIMIT;
|
---|
35 | if (cfg.lb_stat_cleanup <= 0)
|
---|
36 | cfg.lb_stat_cleanup = DEFAULT_LB_STAT_CLEANUP;
|
---|
37 | }
|
---|
38 |
|
---|
39 | #define LINESIZE 1024
|
---|
40 |
|
---|
41 | void load_stat_from_file()
|
---|
42 | {
|
---|
43 | stat_load_save = 0;
|
---|
44 | char buf[256];
|
---|
45 | char *line;
|
---|
46 | char *fname;
|
---|
47 | FILE *file;
|
---|
48 | if (!cfg.lb_savepath || !cfg.lb_savepath[0]) {
|
---|
49 | snprintf(buf, sizeof(buf), "%s/stat", get_tmp_dir());
|
---|
50 | fname = buf;
|
---|
51 | }
|
---|
52 | else
|
---|
53 | fname = cfg.lb_savepath;
|
---|
54 |
|
---|
55 | file = fopen(fname, "r");
|
---|
56 |
|
---|
57 | if (!file) {
|
---|
58 | cs_log("loadbalancer: can't read from file %s", fname);
|
---|
59 | return;
|
---|
60 | }
|
---|
61 | cs_debug_mask(D_TRACE, "loadbalancer: load statistics from %s", fname);
|
---|
62 |
|
---|
63 | struct timeb ts, te;
|
---|
64 | cs_ftime(&ts);
|
---|
65 |
|
---|
66 | struct s_reader *rdr = NULL;
|
---|
67 | READER_STAT *stat, *dup=NULL;
|
---|
68 | line = cs_malloc(&line, LINESIZE, 0);
|
---|
69 |
|
---|
70 | int32_t i=1;
|
---|
71 | int32_t valid=0;
|
---|
72 | int32_t count=0;
|
---|
73 | int32_t type=0;
|
---|
74 | char *ptr, *saveptr1 = NULL;
|
---|
75 | char *split[10];
|
---|
76 |
|
---|
77 | while (fgets(line, LINESIZE, file))
|
---|
78 | {
|
---|
79 | if (!line[0] || line[0] == '#' || line[0] == ';')
|
---|
80 | continue;
|
---|
81 |
|
---|
82 | stat = malloc(sizeof(READER_STAT));
|
---|
83 | memset(stat, 0, sizeof(READER_STAT));
|
---|
84 |
|
---|
85 | //get type by evaluating first line:
|
---|
86 | if (type==0) {
|
---|
87 | if (strstr(line, " rc ")) type = 2;
|
---|
88 | else type = 1;
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (type==1) { //New format - faster parsing:
|
---|
92 | for (i = 0, ptr = strtok_r(line, ",", &saveptr1); ptr && i<10 ; ptr = strtok_r(NULL, ",", &saveptr1), i++)
|
---|
93 | split[i] = ptr;
|
---|
94 | valid = (i==10);
|
---|
95 | if (valid) {
|
---|
96 | strncpy(buf, split[0], sizeof(buf)-1);
|
---|
97 | stat->rc = atoi(split[1]);
|
---|
98 | stat->caid = a2i(split[2], 4);
|
---|
99 | stat->prid = a2i(split[3], 6);
|
---|
100 | stat->srvid = a2i(split[4], 4);
|
---|
101 | stat->time_avg = atoi(split[5]);
|
---|
102 | stat->ecm_count = atoi(split[6]);
|
---|
103 | stat->last_received = atol(split[7]);
|
---|
104 | stat->fail_factor = atoi(split[8]);
|
---|
105 | stat->ecmlen = a2i(split[9], 2);
|
---|
106 | }
|
---|
107 | } else { //Old format - keep for compatibility:
|
---|
108 | i = sscanf(line, "%s rc %d caid %04hX prid %06X srvid %04hX time avg %dms ecms %d last %ld fail %d len %02hX\n",
|
---|
109 | buf, &stat->rc, &stat->caid, &stat->prid, &stat->srvid,
|
---|
110 | &stat->time_avg, &stat->ecm_count, &stat->last_received, &stat->fail_factor, &stat->ecmlen);
|
---|
111 | valid = i>5;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (valid) {
|
---|
115 | if (rdr == NULL || strcmp(buf, rdr->label) != 0) {
|
---|
116 | LL_ITER itr = ll_iter_create(configured_readers);
|
---|
117 | while ((rdr=ll_iter_next(&itr))) {
|
---|
118 | if (strcmp(rdr->label, buf) == 0) {
|
---|
119 | break;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (rdr != NULL && strcmp(buf, rdr->label) == 0) {
|
---|
125 | if (!rdr->lb_stat)
|
---|
126 | rdr->lb_stat = ll_create();
|
---|
127 |
|
---|
128 | //Duplicate check:
|
---|
129 | if (cs_dblevel == 0xFF) //Only with full debug for faster reading...
|
---|
130 | dup = get_stat(rdr, stat->caid, stat->prid, stat->srvid, stat->ecmlen);
|
---|
131 |
|
---|
132 | if (dup)
|
---|
133 | free(stat); //already loaded
|
---|
134 | else {
|
---|
135 | ll_append(rdr->lb_stat, stat);
|
---|
136 | count++;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | else
|
---|
140 | {
|
---|
141 | cs_log("loadbalancer: statistics could not be loaded for %s", buf);
|
---|
142 | free(stat);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | else
|
---|
146 | {
|
---|
147 | cs_debug_mask(D_TRACE, "loadbalancer: statistics ERROR: %s rc=%d i=%d", buf, stat->rc, i);
|
---|
148 | free(stat);
|
---|
149 | }
|
---|
150 | }
|
---|
151 | fclose(file);
|
---|
152 | free(line);
|
---|
153 |
|
---|
154 | cs_ftime(&te);
|
---|
155 | #ifdef WITH_DEBUG
|
---|
156 | int32_t time = 1000*(te.time-ts.time)+te.millitm-ts.millitm;
|
---|
157 |
|
---|
158 | cs_debug_mask(D_TRACE, "loadbalancer: statistics loaded %d records in %dms", count, time);
|
---|
159 | #endif
|
---|
160 | }
|
---|
161 |
|
---|
162 | static uint32_t get_prid(uint16_t caid, uint32_t prid)
|
---|
163 | {
|
---|
164 | int32_t i;
|
---|
165 | for (i=0;i<CS_MAXCAIDTAB;i++) {
|
---|
166 | uint16_t tcaid = cfg.lb_noproviderforcaid.caid[i];
|
---|
167 | if (!tcaid) break;
|
---|
168 | if (tcaid == caid) {
|
---|
169 | prid = 0;
|
---|
170 | break;
|
---|
171 | }
|
---|
172 | if (tcaid < 0x0100 && (caid >> 8) == tcaid) {
|
---|
173 | prid = 0;
|
---|
174 | break;
|
---|
175 | }
|
---|
176 |
|
---|
177 | }
|
---|
178 | return prid;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * get statistic values for reader ridx and caid/prid/srvid/ecmlen
|
---|
183 | */
|
---|
184 | READER_STAT *get_stat(struct s_reader *rdr, uint16_t caid, uint32_t prid, uint16_t srvid, int16_t ecmlen)
|
---|
185 | {
|
---|
186 | if (!rdr->lb_stat)
|
---|
187 | rdr->lb_stat = ll_create();
|
---|
188 |
|
---|
189 | prid = get_prid(caid, prid);
|
---|
190 |
|
---|
191 | LL_ITER it = ll_iter_create(rdr->lb_stat);
|
---|
192 | READER_STAT *stat = NULL;
|
---|
193 | int32_t i = 0;
|
---|
194 | while ((stat = ll_iter_next(&it))) {
|
---|
195 | i++;
|
---|
196 | if (stat->caid==caid && stat->prid==prid && stat->srvid==srvid) {
|
---|
197 | if (stat->ecmlen == ecmlen)
|
---|
198 | break;
|
---|
199 | if (!stat->ecmlen) {
|
---|
200 | stat->ecmlen = ecmlen;
|
---|
201 | break;
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | //Move stat to list start for faster access:
|
---|
207 | if (i > 10 && stat)
|
---|
208 | ll_iter_move_first(&it);
|
---|
209 |
|
---|
210 | return stat;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * removes caid/prid/srvid/ecmlen from stat-list of reader ridx
|
---|
215 | */
|
---|
216 | int32_t remove_stat(struct s_reader *rdr, uint16_t caid, uint32_t prid, uint16_t srvid, int16_t ecmlen)
|
---|
217 | {
|
---|
218 | if (!rdr->lb_stat)
|
---|
219 | return 0;
|
---|
220 |
|
---|
221 | int32_t c = 0;
|
---|
222 | LL_ITER it = ll_iter_create(rdr->lb_stat);
|
---|
223 | READER_STAT *stat;
|
---|
224 | while ((stat = ll_iter_next(&it))) {
|
---|
225 | if (stat->caid==caid && stat->prid==prid && stat->srvid==srvid) {
|
---|
226 | if (!stat->ecmlen || stat->ecmlen == ecmlen) {
|
---|
227 | ll_iter_remove_data(&it);
|
---|
228 | c++;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 | return c;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Calculates average time
|
---|
237 | */
|
---|
238 | void calc_stat(READER_STAT *stat)
|
---|
239 | {
|
---|
240 | int32_t i, c=0, t = 0;
|
---|
241 | for (i = 0; i < LB_MAX_STAT_TIME; i++) {
|
---|
242 | if (stat->time_stat[i] > 0) {
|
---|
243 | t += (int32_t)stat->time_stat[i];
|
---|
244 | c++;
|
---|
245 | }
|
---|
246 | }
|
---|
247 | if (!c)
|
---|
248 | stat->time_avg = UNDEF_AVG_TIME;
|
---|
249 | else
|
---|
250 | stat->time_avg = t / c;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /**
|
---|
254 | * Saves statistik to /tmp/.oscam/stat.n where n is reader-index
|
---|
255 | */
|
---|
256 | void save_stat_to_file_thread()
|
---|
257 | {
|
---|
258 | stat_load_save = 0;
|
---|
259 | char buf[256];
|
---|
260 | char *fname;
|
---|
261 | if (!cfg.lb_savepath || !cfg.lb_savepath[0]) {
|
---|
262 | snprintf(buf, sizeof(buf), "%s/stat", get_tmp_dir());
|
---|
263 | fname = buf;
|
---|
264 | }
|
---|
265 | else
|
---|
266 | fname = cfg.lb_savepath;
|
---|
267 |
|
---|
268 | FILE *file = fopen(fname, "w");
|
---|
269 |
|
---|
270 | if (!file) {
|
---|
271 | cs_log("can't write to file %s", fname);
|
---|
272 | return;
|
---|
273 | }
|
---|
274 |
|
---|
275 | struct timeb ts, te;
|
---|
276 | cs_ftime(&ts);
|
---|
277 |
|
---|
278 | time_t cleanup_time = time(NULL) - (cfg.lb_stat_cleanup*60*60);
|
---|
279 |
|
---|
280 | int32_t count=0;
|
---|
281 | struct s_reader *rdr;
|
---|
282 | LL_ITER itr = ll_iter_create(configured_readers);
|
---|
283 | while ((rdr=ll_iter_next(&itr))) {
|
---|
284 |
|
---|
285 | if (rdr->lb_stat) {
|
---|
286 | LL_ITER it = ll_iter_create(rdr->lb_stat);
|
---|
287 | READER_STAT *stat;
|
---|
288 | while ((stat = ll_iter_next(&it))) {
|
---|
289 |
|
---|
290 | if (stat->last_received < cleanup_time) { //cleanup old stats
|
---|
291 | ll_iter_remove_data(&it);
|
---|
292 | continue;
|
---|
293 | }
|
---|
294 |
|
---|
295 | //Old version, too slow to parse:
|
---|
296 | //fprintf(file, "%s rc %d caid %04hX prid %06X srvid %04hX time avg %dms ecms %d last %ld fail %d len %02hX\n",
|
---|
297 | // rdr->label, stat->rc, stat->caid, stat->prid,
|
---|
298 | // stat->srvid, stat->time_avg, stat->ecm_count, stat->last_received, stat->fail_factor, stat->ecmlen);
|
---|
299 |
|
---|
300 | //New version:
|
---|
301 | fprintf(file, "%s,%d,%04hX,%06X,%04hX,%d,%d,%ld,%d,%02hX\n",
|
---|
302 | rdr->label, stat->rc, stat->caid, stat->prid,
|
---|
303 | stat->srvid, stat->time_avg, stat->ecm_count, stat->last_received, stat->fail_factor, stat->ecmlen);
|
---|
304 | count++;
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | fclose(file);
|
---|
310 |
|
---|
311 | cs_ftime(&te);
|
---|
312 | int32_t time = 1000*(te.time-ts.time)+te.millitm-ts.millitm;
|
---|
313 |
|
---|
314 |
|
---|
315 | cs_log("loadbalancer: statistic saved %d records to %s in %dms", count, fname, time);
|
---|
316 | }
|
---|
317 |
|
---|
318 | void save_stat_to_file(int32_t thread)
|
---|
319 | {
|
---|
320 | stat_load_save = 0;
|
---|
321 | if (thread)
|
---|
322 | start_thread((void*)&save_stat_to_file_thread, "save lb stats");
|
---|
323 | else
|
---|
324 | save_stat_to_file_thread();
|
---|
325 | }
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * fail_factor is multiplied to the reopen_time. This function increases the fail_factor
|
---|
329 | **/
|
---|
330 | void inc_fail(READER_STAT *stat)
|
---|
331 | {
|
---|
332 | if (stat->fail_factor <= 0)
|
---|
333 | stat->fail_factor = 1;
|
---|
334 | else
|
---|
335 | stat->fail_factor *= 2;
|
---|
336 | }
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Adds caid/prid/srvid/ecmlen to stat-list for reader ridx with time/rc
|
---|
340 | */
|
---|
341 | void add_stat(struct s_reader *rdr, ECM_REQUEST *er, int32_t ecm_time, int32_t rc)
|
---|
342 | {
|
---|
343 | if (!rdr || !er || !cfg.lb_mode || !rdr->client)
|
---|
344 | return;
|
---|
345 |
|
---|
346 | uint32_t prid = get_prid(er->caid, er->prid);
|
---|
347 |
|
---|
348 | READER_STAT *stat = get_stat(rdr, er->caid, prid, er->srvid, er->l);
|
---|
349 | if (!stat) {
|
---|
350 | stat = malloc(sizeof(READER_STAT));
|
---|
351 | memset(stat, 0, sizeof(READER_STAT));
|
---|
352 | stat->caid = er->caid;
|
---|
353 | stat->prid = prid;
|
---|
354 | stat->srvid = er->srvid;
|
---|
355 | stat->ecmlen = er->l;
|
---|
356 | stat->time_avg = UNDEF_AVG_TIME; //dummy placeholder
|
---|
357 | ll_append(rdr->lb_stat, stat);
|
---|
358 | }
|
---|
359 |
|
---|
360 | //inc ecm_count if found, drop to 0 if not found:
|
---|
361 | // rc codes:
|
---|
362 | // 0 = found +
|
---|
363 | // 1 = cache1 #
|
---|
364 | // 2 = cache2 #
|
---|
365 | // 3 = emu +
|
---|
366 | // 4 = not found -
|
---|
367 | // 5 = timeout -2
|
---|
368 | // 6 = sleeping #
|
---|
369 | // 7 = fake #
|
---|
370 | // 8 = invalid #
|
---|
371 | // 9 = corrupt #
|
---|
372 | // 10= no card #
|
---|
373 | // 11= expdate #
|
---|
374 | // 12= disabled #
|
---|
375 | // 13= stopped #
|
---|
376 | // 100= unhandled #
|
---|
377 | // + = adds statistic values
|
---|
378 | // # = ignored because of duplicate values, temporary failures or softblocks
|
---|
379 | // - = causes loadbalancer to block this reader for this caid/prov/sid
|
---|
380 | // -2 = causes loadbalancer to block if happens too often
|
---|
381 |
|
---|
382 | if (stat->ecm_count < 0)
|
---|
383 | stat->ecm_count=0;
|
---|
384 |
|
---|
385 | time_t ctime = time(NULL);
|
---|
386 |
|
---|
387 | if (rc == 0) { //found
|
---|
388 | stat->rc = 0;
|
---|
389 | stat->ecm_count++;
|
---|
390 | stat->last_received = ctime;
|
---|
391 | stat->fail_factor = 0;
|
---|
392 |
|
---|
393 | //If answering reader is a fallback reader, decrement answer time by fallback timeout:
|
---|
394 | struct s_reader *r;
|
---|
395 | LL_ITER it = ll_iter_create(er->matching_rdr);
|
---|
396 | int8_t is_fallback = 0;
|
---|
397 | while ((r=ll_iter_next(&it))) {
|
---|
398 | if (it.cur == er->fallback) is_fallback = 1;
|
---|
399 | if (r == rdr) {
|
---|
400 | if (is_fallback && (uint32_t)ecm_time >= cfg.ftimeout)
|
---|
401 | ecm_time -= cfg.ftimeout;
|
---|
402 | break;
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | //FASTEST READER:
|
---|
407 | stat->time_idx++;
|
---|
408 | if (stat->time_idx >= LB_MAX_STAT_TIME)
|
---|
409 | stat->time_idx = 0;
|
---|
410 | stat->time_stat[stat->time_idx] = ecm_time;
|
---|
411 | calc_stat(stat);
|
---|
412 |
|
---|
413 | //OLDEST READER now set by get best reader!
|
---|
414 |
|
---|
415 |
|
---|
416 | //USAGELEVEL:
|
---|
417 | int32_t ule = rdr->lb_usagelevel_ecmcount;
|
---|
418 | if (ule > 0 && ((ule / cfg.lb_min_ecmcount) > 0)) //update every MIN_ECM_COUNT usagelevel:
|
---|
419 | {
|
---|
420 | time_t t = (ctime-rdr->lb_usagelevel_time);
|
---|
421 | rdr->lb_usagelevel = 1000/(t<1?1:t);
|
---|
422 | ule = 0;
|
---|
423 | }
|
---|
424 | if (ule == 0)
|
---|
425 | rdr->lb_usagelevel_time = ctime;
|
---|
426 | rdr->lb_usagelevel_ecmcount = ule+1;
|
---|
427 | }
|
---|
428 | else if (rc == 1 || rc == 2) { //cache
|
---|
429 | //no increase of statistics here, cachetime is not real time
|
---|
430 | stat->last_received = ctime;
|
---|
431 | }
|
---|
432 | else if (rc == 4) { //not found
|
---|
433 | //CCcam card can't decode, 0x28=NOK1, 0x29=NOK2
|
---|
434 | //CCcam loop detection = E2_CCCAM_LOOP
|
---|
435 | if (er->rcEx == E2_CCCAM_NOK1 || er->rcEx == E2_CCCAM_NOK2 || er->rcEx == E2_CCCAM_LOOP) {
|
---|
436 | stat->last_received = ctime; //to avoid timeouts
|
---|
437 | return;
|
---|
438 | }
|
---|
439 |
|
---|
440 | stat->rc = rc;
|
---|
441 | inc_fail(stat);
|
---|
442 | stat->last_received = ctime;
|
---|
443 |
|
---|
444 | //reduce ecm_count step by step
|
---|
445 | if (!cfg.lb_reopen_mode)
|
---|
446 | stat->ecm_count /= 10;
|
---|
447 | }
|
---|
448 | else if (rc == 5) { //timeout
|
---|
449 | //catch suddenly occuring timeouts and block reader:
|
---|
450 | if ((int)(ctime-stat->last_received) < (int)(5*cfg.ctimeout) &&
|
---|
451 | stat->rc == 0 &&
|
---|
452 | stat->ecm_count > 0) {
|
---|
453 | stat->rc = 5;
|
---|
454 | //inc_fail(stat); //do not inc fail factor in this case
|
---|
455 | }
|
---|
456 | else if ((rdr->client->login+(int)(2*cfg.ctimeout/1000)) < ctime && rdr->client->pending < 5) { //reader is longer than 5s connected && not more then 5 pending ecms
|
---|
457 | stat->rc = 5;
|
---|
458 | inc_fail(stat);
|
---|
459 | }
|
---|
460 |
|
---|
461 | stat->last_received = ctime;
|
---|
462 |
|
---|
463 | if (!cfg.lb_reopen_mode)
|
---|
464 | stat->ecm_count /= 2;
|
---|
465 |
|
---|
466 | //add timeout to stat:
|
---|
467 | if (ecm_time<=0 || ecm_time > (int)cfg.ctimeout)
|
---|
468 | ecm_time = cfg.ctimeout;
|
---|
469 | stat->time_idx++;
|
---|
470 | if (stat->time_idx >= LB_MAX_STAT_TIME)
|
---|
471 | stat->time_idx = 0;
|
---|
472 | stat->time_stat[stat->time_idx] = ecm_time;
|
---|
473 | calc_stat(stat);
|
---|
474 | }
|
---|
475 | else
|
---|
476 | {
|
---|
477 | if (rc >= 0)
|
---|
478 | cs_debug_mask(D_TRACE, "loadbalancer: not handled stat for reader %s: rc %d %04hX&%06lX/%04hX/%02hX time %dms fail %d",
|
---|
479 | rdr->label, rc, er->caid, prid, er->srvid, er->l, ecm_time, stat->fail_factor);
|
---|
480 |
|
---|
481 | return;
|
---|
482 | }
|
---|
483 |
|
---|
484 | housekeeping_stat(0);
|
---|
485 |
|
---|
486 | cs_debug_mask(D_TRACE, "loadbalancer: adding stat for reader %s: rc %d %04hX&%06lX/%04hX/%02hX time %dms fail %d",
|
---|
487 | rdr->label, rc, er->caid, prid, er->srvid, er->l, ecm_time, stat->fail_factor);
|
---|
488 |
|
---|
489 | if (cfg.lb_save) {
|
---|
490 | stat_load_save++;
|
---|
491 | if (stat_load_save > cfg.lb_save)
|
---|
492 | save_stat_to_file(1);
|
---|
493 | }
|
---|
494 | }
|
---|
495 |
|
---|
496 | void reset_stat(uint16_t caid, uint32_t prid, uint16_t srvid, int16_t ecmlen)
|
---|
497 | {
|
---|
498 | //cs_debug_mask(D_TRACE, "loadbalance: resetting ecm count");
|
---|
499 | struct s_reader *rdr;
|
---|
500 | for (rdr=first_active_reader; rdr ; rdr=rdr->next) {
|
---|
501 | if (rdr->lb_stat && rdr->client) {
|
---|
502 | READER_STAT *stat = get_stat(rdr, caid, prid, srvid, ecmlen);
|
---|
503 | if (stat) {
|
---|
504 | if (stat->ecm_count > 0)
|
---|
505 | stat->ecm_count = 1; //not zero, so we know it's decodeable
|
---|
506 | stat->rc = 0;
|
---|
507 | stat->fail_factor = 0;
|
---|
508 | }
|
---|
509 | }
|
---|
510 | }
|
---|
511 | }
|
---|
512 |
|
---|
513 | int32_t has_ident(FTAB *ftab, ECM_REQUEST *er) {
|
---|
514 |
|
---|
515 | if (!ftab || !ftab->filts)
|
---|
516 | return 0;
|
---|
517 |
|
---|
518 | int32_t j, k;
|
---|
519 |
|
---|
520 | for (j = 0; j < ftab->nfilts; j++) {
|
---|
521 | if (ftab->filts[j].caid) {
|
---|
522 | if (ftab->filts[j].caid==er->caid) { //caid matches!
|
---|
523 | int32_t nprids = ftab->filts[j].nprids;
|
---|
524 | if (!nprids) // No Provider ->Ok
|
---|
525 | return 1;
|
---|
526 |
|
---|
527 | for (k = 0; k < nprids; k++) {
|
---|
528 | uint32_t prid = ftab->filts[j].prids[k];
|
---|
529 | if (prid == er->prid) { //Provider matches
|
---|
530 | return 1;
|
---|
531 | }
|
---|
532 | }
|
---|
533 | }
|
---|
534 | }
|
---|
535 | }
|
---|
536 | return 0; //No match!
|
---|
537 | }
|
---|
538 |
|
---|
539 | struct stat_value {
|
---|
540 | struct s_reader *rdr;
|
---|
541 | int32_t value;
|
---|
542 | int32_t time;
|
---|
543 | };
|
---|
544 |
|
---|
545 | static struct stat_value *crt_cur(struct s_reader *rdr, int32_t value, int32_t time) {
|
---|
546 | struct stat_value *v = malloc(sizeof(struct stat_value));
|
---|
547 | v->rdr = rdr;
|
---|
548 | v->value = value;
|
---|
549 | v->time = time;
|
---|
550 | return v;
|
---|
551 | }
|
---|
552 |
|
---|
553 | #ifdef WITH_DEBUG
|
---|
554 | static char *strend(char *c) {
|
---|
555 | while (c && *c) c++;
|
---|
556 | return c;
|
---|
557 | }
|
---|
558 | #endif
|
---|
559 |
|
---|
560 | static int32_t get_retrylimit(ECM_REQUEST *er) {
|
---|
561 | int32_t i;
|
---|
562 | for (i = 0; i < cfg.lb_retrylimittab.n; i++) {
|
---|
563 | if (cfg.lb_retrylimittab.caid[i] == er->caid)
|
---|
564 | return cfg.lb_retrylimittab.value[i];
|
---|
565 | }
|
---|
566 | return cfg.lb_retrylimit;
|
---|
567 | }
|
---|
568 |
|
---|
569 | static int32_t get_nbest_readers(ECM_REQUEST *er) {
|
---|
570 | int32_t i;
|
---|
571 | for (i = 0; i < cfg.lb_nbest_readers_tab.n; i++) {
|
---|
572 | if (cfg.lb_nbest_readers_tab.caid[i] == er->caid)
|
---|
573 | return cfg.lb_nbest_readers_tab.value[i];
|
---|
574 | }
|
---|
575 | return cfg.lb_nbest_readers;
|
---|
576 | }
|
---|
577 |
|
---|
578 | static int32_t get_reopen_seconds(READER_STAT *stat)
|
---|
579 | {
|
---|
580 | int32_t max = (INT_MAX / cfg.lb_reopen_seconds);
|
---|
581 | if (stat->fail_factor > max)
|
---|
582 | stat->fail_factor = max;
|
---|
583 | if (!stat->fail_factor)
|
---|
584 | return cfg.lb_reopen_seconds;
|
---|
585 | return stat->fail_factor * cfg.lb_reopen_seconds;
|
---|
586 | }
|
---|
587 |
|
---|
588 | ushort get_betatunnel_caid_to(ushort caid)
|
---|
589 | {
|
---|
590 | if (caid == 0x1801) return 0x1722;
|
---|
591 | if (caid == 0x1833) return 0x1702;
|
---|
592 | if (caid == 0x1834) return 0x1722;
|
---|
593 | if (caid == 0x1835) return 0x1722;
|
---|
594 | return 0;
|
---|
595 | }
|
---|
596 |
|
---|
597 | void convert_to_beta_int(ECM_REQUEST *er, uint16_t caid_to)
|
---|
598 | {
|
---|
599 | convert_to_beta(er->client, er, caid_to);
|
---|
600 | // update ecmd5 for store ECM in cache
|
---|
601 | memcpy(er->ecmd5, MD5(er->ecm+13, er->l-13, er->client->dump), CS_ECMSTORESIZE);
|
---|
602 | }
|
---|
603 |
|
---|
604 | /**
|
---|
605 | * Gets best reader for caid/prid/srvid/ecmlen.
|
---|
606 | * Best reader is evaluated by lowest avg time but only if ecm_count > cfg.lb_min_ecmcount (5)
|
---|
607 | * Also the reader is asked if he is "available"
|
---|
608 | * returns ridx when found or -1 when not found
|
---|
609 | */
|
---|
610 | int32_t get_best_reader(ECM_REQUEST *er)
|
---|
611 | {
|
---|
612 | if (!cfg.lb_mode || cfg.lb_mode==LB_LOG_ONLY)
|
---|
613 | return 0;
|
---|
614 |
|
---|
615 | LL_ITER it;
|
---|
616 | struct s_reader *rdr;
|
---|
617 |
|
---|
618 | #ifdef MODULE_CCCAM
|
---|
619 | //preferred card forwarding (CCcam client):
|
---|
620 | if (cfg.cc_forward_origin_card && er->origin_card) {
|
---|
621 |
|
---|
622 | struct cc_card *card = er->origin_card;
|
---|
623 |
|
---|
624 | it = ll_iter_create(er->matching_rdr);
|
---|
625 | while ((rdr=ll_iter_next(&it))) {
|
---|
626 | if (card->origin_reader == rdr)
|
---|
627 | break;
|
---|
628 | }
|
---|
629 | if (rdr) {
|
---|
630 | cs_debug_mask(D_TRACE, "loadbalancer: forward card: forced by card %d to reader %s", card->id, rdr->label);
|
---|
631 | ll_clear(er->matching_rdr);
|
---|
632 | ll_append(er->matching_rdr, rdr);
|
---|
633 | return 1;
|
---|
634 | }
|
---|
635 | }
|
---|
636 | #endif
|
---|
637 |
|
---|
638 | uint32_t prid = get_prid(er->caid, er->prid);
|
---|
639 |
|
---|
640 | //auto-betatunnel: The trick is: "let the loadbalancer decide"!
|
---|
641 | if (cfg.lb_auto_betatunnel && er->caid >> 8 == 0x18) { //nagra
|
---|
642 | ushort caid_to = get_betatunnel_caid_to(er->caid);
|
---|
643 | if (caid_to) {
|
---|
644 | int8_t needs_stats_nagra = 0, needs_stats_beta = 0;
|
---|
645 |
|
---|
646 | int32_t time_nagra = 0;
|
---|
647 | int32_t time_beta = 0;
|
---|
648 | int32_t weight;
|
---|
649 | int32_t time;
|
---|
650 |
|
---|
651 | READER_STAT *stat_nagra;
|
---|
652 | READER_STAT *stat_beta;
|
---|
653 |
|
---|
654 | //What is faster? nagra or beta?
|
---|
655 | it = ll_iter_create(er->matching_rdr);
|
---|
656 | while ((rdr=ll_iter_next(&it)) && !needs_stats_nagra && !needs_stats_beta) {
|
---|
657 | weight = rdr->lb_weight;
|
---|
658 | if (weight <= 0) weight = 1;
|
---|
659 |
|
---|
660 | stat_nagra = get_stat(rdr, er->caid, prid, er->srvid, er->l);
|
---|
661 | stat_beta = get_stat(rdr, caid_to, prid, er->srvid, er->l+10);
|
---|
662 |
|
---|
663 | if (stat_nagra && stat_nagra->rc == 0) {
|
---|
664 | time = stat_nagra->time_avg*100/weight;
|
---|
665 | if (!time_nagra || time < time_nagra)
|
---|
666 | time_nagra = time;
|
---|
667 | }
|
---|
668 |
|
---|
669 | if (stat_beta && stat_beta->rc == 0) {
|
---|
670 | time = stat_beta->time_avg*100/weight;
|
---|
671 | if (!time_beta || time < time_beta)
|
---|
672 | time_beta = time;
|
---|
673 | }
|
---|
674 |
|
---|
675 | //Uncomplete reader evaluation, we need more stats!
|
---|
676 | if (!stat_nagra)
|
---|
677 | needs_stats_nagra = 1;
|
---|
678 | if (!stat_beta)
|
---|
679 | needs_stats_beta = 1;
|
---|
680 | }
|
---|
681 |
|
---|
682 | //if we needs stats, we send 2 ecm requests: 18xx and 17xx:
|
---|
683 | if (needs_stats_nagra || needs_stats_beta) {
|
---|
684 | cs_debug_mask(D_TRACE, "loadbalancer-betatunnel %04X:%04X needs more statistics...", er->caid, caid_to);
|
---|
685 | if (needs_stats_beta)
|
---|
686 | convert_to_beta_int(er, caid_to);
|
---|
687 | }
|
---|
688 | else if (time_beta && (!time_nagra || time_beta <= time_nagra)) {
|
---|
689 | cs_debug_mask(D_TRACE, "loadbalancer-betatunnel %04X:%04X selected beta: n%dms > b%dms", er->caid, caid_to, time_nagra, time_beta);
|
---|
690 | convert_to_beta_int(er, caid_to);
|
---|
691 | }
|
---|
692 | else {
|
---|
693 | cs_debug_mask(D_TRACE, "loadbalancer-betatunnel %04X:%04X selected nagra: n%dms < b%dms", er->caid, caid_to, time_nagra, time_beta);
|
---|
694 | }
|
---|
695 | // else nagra is faster or no beta, so continue unmodified
|
---|
696 | }
|
---|
697 | }
|
---|
698 |
|
---|
699 | LLIST * result = ll_create();
|
---|
700 | LLIST * selected = ll_create();
|
---|
701 | LLIST * timeout_services = ll_create();
|
---|
702 |
|
---|
703 | struct timeb new_nulltime;
|
---|
704 | memset(&new_nulltime, 0, sizeof(new_nulltime));
|
---|
705 | time_t current_time = time(NULL);
|
---|
706 | int32_t current = -1;
|
---|
707 | READER_STAT *stat = NULL;
|
---|
708 | int32_t retrylimit = get_retrylimit(er);
|
---|
709 |
|
---|
710 | int32_t new_stats = 0;
|
---|
711 | int32_t nlocal_readers = 0;
|
---|
712 | int32_t nbest_readers = get_nbest_readers(er);
|
---|
713 | int32_t nfb_readers = cfg.lb_nfb_readers;
|
---|
714 | int32_t nreaders = cfg.lb_max_readers;
|
---|
715 | if (!nreaders) nreaders = -1;
|
---|
716 |
|
---|
717 | #ifdef WITH_DEBUG
|
---|
718 | if (cs_dblevel & 0x01) {
|
---|
719 | //loadbalancer debug output:
|
---|
720 | int32_t size = 1;
|
---|
721 | int32_t nr = 0;
|
---|
722 | it = ll_iter_create(er->matching_rdr);
|
---|
723 | while ((rdr=ll_iter_next(&it))) {
|
---|
724 | if (nr > 5) {
|
---|
725 | size+=20;
|
---|
726 | break;
|
---|
727 | }
|
---|
728 | size += strlen(rdr->label)+1;
|
---|
729 | nr++;
|
---|
730 | }
|
---|
731 | ll_iter_reset(&it);
|
---|
732 | char *rdrs = cs_malloc(&rdrs, size, 1);
|
---|
733 | char *rptr = rdrs;
|
---|
734 | *rptr = 0;
|
---|
735 | nr = 0;
|
---|
736 | while ((rdr=ll_iter_next(&it))) {
|
---|
737 | if (nr > 5) {
|
---|
738 | snprintf(rptr, size, "...(%d more)", ll_count(er->matching_rdr)-nr);
|
---|
739 | break;
|
---|
740 | }
|
---|
741 | snprintf(rptr, size, "%s ", rdr->label);
|
---|
742 | rptr = strend(rptr);
|
---|
743 | nr++;
|
---|
744 | }
|
---|
745 |
|
---|
746 | cs_debug_mask(D_TRACE, "loadbalancer: client %s for %04X&%06X/%04X/%02hX: n=%d valid readers: %s",
|
---|
747 | username(er->client), er->caid, prid, er->srvid, er->l, ll_count(er->matching_rdr), rdrs);
|
---|
748 |
|
---|
749 | free(rdrs);
|
---|
750 | }
|
---|
751 | #endif
|
---|
752 |
|
---|
753 | it = ll_iter_create(er->matching_rdr);
|
---|
754 | while ((rdr=ll_iter_next(&it)) && nreaders) {
|
---|
755 |
|
---|
756 | int32_t weight = rdr->lb_weight <= 0?100:rdr->lb_weight;
|
---|
757 |
|
---|
758 | stat = get_stat(rdr, er->caid, prid, er->srvid, er->l);
|
---|
759 | if (!stat) {
|
---|
760 | cs_debug_mask(D_TRACE, "loadbalancer: starting statistics for reader %s", rdr->label);
|
---|
761 | add_stat(rdr, er, 1, -1);
|
---|
762 | ll_append(result, rdr); //no statistics, this reader is active (now) but we need statistics first!
|
---|
763 | nreaders--;
|
---|
764 | new_stats = 1;
|
---|
765 | continue;
|
---|
766 | }
|
---|
767 |
|
---|
768 | if (stat->ecm_count < 0||(stat->ecm_count > cfg.lb_max_ecmcount && stat->time_avg > retrylimit)) {
|
---|
769 | cs_debug_mask(D_TRACE, "loadbalancer: max ecms (%d) reached by reader %s, resetting statistics", cfg.lb_max_ecmcount, rdr->label);
|
---|
770 | reset_stat(er->caid, prid, er->srvid, er->l);
|
---|
771 | ll_append(result, rdr); //max ecm reached, get new statistics
|
---|
772 | nreaders--;
|
---|
773 | continue;
|
---|
774 | }
|
---|
775 |
|
---|
776 | int32_t hassrvid = has_srvid(rdr->client, er) || has_ident(&rdr->ftab, er);
|
---|
777 |
|
---|
778 | if (stat->rc == 0 && stat->ecm_count < cfg.lb_min_ecmcount) {
|
---|
779 | cs_debug_mask(D_TRACE, "loadbalancer: reader %s needs more statistics", rdr->label);
|
---|
780 | ll_append(result, rdr); //need more statistics!
|
---|
781 | nreaders--;
|
---|
782 | new_stats = 1;
|
---|
783 | continue;
|
---|
784 | }
|
---|
785 |
|
---|
786 | //Reader can decode this service (rc==0) and has lb_min_ecmcount ecms:
|
---|
787 | if (stat->rc == 0 || hassrvid) {
|
---|
788 | if (cfg.preferlocalcards && !(rdr->typ & R_IS_NETWORK))
|
---|
789 | nlocal_readers++; //Prefer local readers!
|
---|
790 |
|
---|
791 | if (stat->rc >= 5)
|
---|
792 | ll_prepend(timeout_services, rdr);
|
---|
793 | //just add another reader if best reader is nonresponding but has services
|
---|
794 |
|
---|
795 | switch (cfg.lb_mode) {
|
---|
796 | default:
|
---|
797 | case LB_NONE:
|
---|
798 | case LB_LOG_ONLY:
|
---|
799 | //cs_debug_mask(D_TRACE, "loadbalance disabled");
|
---|
800 | ll_append(result, rdr);
|
---|
801 | nreaders--;
|
---|
802 | continue;
|
---|
803 |
|
---|
804 | case LB_FASTEST_READER_FIRST:
|
---|
805 | current = stat->time_avg * 100 / weight;
|
---|
806 | break;
|
---|
807 |
|
---|
808 | case LB_OLDEST_READER_FIRST:
|
---|
809 | if (!rdr->lb_last.time)
|
---|
810 | rdr->lb_last = nulltime;
|
---|
811 | current = (1000*(rdr->lb_last.time-nulltime.time)+
|
---|
812 | rdr->lb_last.millitm-nulltime.millitm);
|
---|
813 | if (!new_nulltime.time || (1000*(rdr->lb_last.time-new_nulltime.time)+
|
---|
814 | rdr->lb_last.millitm-new_nulltime.millitm) < 0)
|
---|
815 | new_nulltime = rdr->lb_last;
|
---|
816 | break;
|
---|
817 |
|
---|
818 | case LB_LOWEST_USAGELEVEL:
|
---|
819 | current = rdr->lb_usagelevel * 100 / weight;
|
---|
820 | break;
|
---|
821 | }
|
---|
822 | #ifdef WEBIF
|
---|
823 | rdr->lbvalue = current;
|
---|
824 | #endif
|
---|
825 | if (rdr->ph.c_available
|
---|
826 | && !rdr->ph.c_available(rdr,
|
---|
827 | AVAIL_CHECK_LOADBALANCE)) {
|
---|
828 | current=current*2;
|
---|
829 | }
|
---|
830 |
|
---|
831 | if (rdr->client->pending)
|
---|
832 | current=current*rdr->client->pending;
|
---|
833 |
|
---|
834 | if (current < 1)
|
---|
835 | current=1;
|
---|
836 | ll_append(selected, crt_cur(rdr, current, stat->time_avg));
|
---|
837 | }
|
---|
838 | }
|
---|
839 |
|
---|
840 | if (nlocal_readers > nbest_readers) { //if we have local readers, we prefer them!
|
---|
841 | nlocal_readers = nbest_readers;
|
---|
842 | nbest_readers = 0;
|
---|
843 | }
|
---|
844 | else
|
---|
845 | nbest_readers = nbest_readers-nlocal_readers;
|
---|
846 |
|
---|
847 | struct stat_value *stv;
|
---|
848 | it = ll_iter_create(selected);
|
---|
849 |
|
---|
850 | struct s_reader *best_rdr = NULL;
|
---|
851 | struct s_reader *best_rdri = NULL;
|
---|
852 | int32_t best_time = 0;
|
---|
853 | LL_NODE *fallback = NULL;
|
---|
854 |
|
---|
855 | int32_t n=0;
|
---|
856 | while (nreaders) {
|
---|
857 | struct stat_value *best = NULL;
|
---|
858 |
|
---|
859 | ll_iter_reset(&it);
|
---|
860 | while ((stv=ll_iter_next(&it))) {
|
---|
861 | if (nlocal_readers && (stv->rdr->typ & R_IS_NETWORK))
|
---|
862 | continue;
|
---|
863 |
|
---|
864 | if (stv->value && (!best || stv->value < best->value))
|
---|
865 | best=stv;
|
---|
866 | }
|
---|
867 | if (!best)
|
---|
868 | break;
|
---|
869 |
|
---|
870 | n++;
|
---|
871 | best_rdri = best->rdr;
|
---|
872 | if (!best_rdr) {
|
---|
873 | best_rdr = best_rdri;
|
---|
874 | best_time = best->time;
|
---|
875 | }
|
---|
876 | best->value = 0;
|
---|
877 |
|
---|
878 | if (nlocal_readers) {//primary readers, local
|
---|
879 | if (!ll_contains(timeout_services, best_rdri))
|
---|
880 | nlocal_readers--;
|
---|
881 | ll_append(result, best_rdri);
|
---|
882 | nreaders--;
|
---|
883 | //OLDEST_READER:
|
---|
884 | cs_ftime(&best_rdri->lb_last);
|
---|
885 | }
|
---|
886 | else if (nbest_readers) {//primary readers, other
|
---|
887 | if (!ll_contains(timeout_services, best_rdri))
|
---|
888 | nbest_readers--;
|
---|
889 | ll_append(result, best_rdri);
|
---|
890 | nreaders--;
|
---|
891 | //OLDEST_READER:
|
---|
892 | cs_ftime(&best_rdri->lb_last);
|
---|
893 | }
|
---|
894 | else if (nfb_readers) { //fallbacks:
|
---|
895 | if (!ll_contains(timeout_services, best_rdri))
|
---|
896 | nfb_readers--;
|
---|
897 | LL_NODE *node = ll_append(result, best_rdri);
|
---|
898 | if (!fallback)
|
---|
899 | fallback = node;
|
---|
900 | }
|
---|
901 | else
|
---|
902 | break;
|
---|
903 | }
|
---|
904 | ll_destroy_data(selected);
|
---|
905 | ll_destroy(timeout_services);
|
---|
906 |
|
---|
907 | if (!new_stats && ll_count(result) < ll_count(er->matching_rdr)) {
|
---|
908 | if (!n) //no best reader found? reopen if we have ecm_count>0
|
---|
909 | {
|
---|
910 | cs_debug_mask(D_TRACE, "loadbalancer: NO MATCHING READER FOUND, reopen last valid:");
|
---|
911 | it = ll_iter_create(er->matching_rdr);
|
---|
912 | while ((rdr=ll_iter_next(&it))) {
|
---|
913 | stat = get_stat(rdr, er->caid, prid, er->srvid, er->l);
|
---|
914 | if (stat && stat->ecm_count>0 && stat->last_received+get_reopen_seconds(stat) < current_time) {
|
---|
915 | if (!ll_contains(result, rdr) && nreaders) {
|
---|
916 | ll_append(result, rdr);
|
---|
917 | nreaders--;
|
---|
918 | }
|
---|
919 | n++;
|
---|
920 | cs_debug_mask(D_TRACE, "loadbalancer: reopened reader %s", rdr->label);
|
---|
921 | }
|
---|
922 | }
|
---|
923 | cs_debug_mask(D_TRACE, "loadbalancer: reopened %d readers", n);
|
---|
924 | }
|
---|
925 |
|
---|
926 | //algo for reopen other reader only if responsetime>retrylimit:
|
---|
927 | int32_t reopen = !best_rdr || (best_time && (best_time > retrylimit));
|
---|
928 | if (reopen) {
|
---|
929 | #ifdef WITH_DEBUG
|
---|
930 | if (best_rdr)
|
---|
931 | cs_debug_mask(D_TRACE, "loadbalancer: reader %s reached retrylimit (%dms), reopening other readers", best_rdr->label, best_time);
|
---|
932 | else
|
---|
933 | cs_debug_mask(D_TRACE, "loadbalancer: no best reader found, reopening other readers");
|
---|
934 | #endif
|
---|
935 | it = ll_iter_create(er->matching_rdr);
|
---|
936 | while ((rdr=ll_iter_next(&it)) && nreaders) {
|
---|
937 | stat = get_stat(rdr, er->caid, prid, er->srvid, er->l);
|
---|
938 |
|
---|
939 | if (stat && stat->rc != 0) { //retrylimit reached:
|
---|
940 | if (cfg.lb_reopen_mode || stat->last_received+get_reopen_seconds(stat) < current_time) { //Retrying reader every (900/conf) seconds
|
---|
941 | stat->last_received = current_time;
|
---|
942 | nreaders += ll_remove(result, rdr);
|
---|
943 | ll_prepend(result, rdr);
|
---|
944 | nreaders--;
|
---|
945 | cs_debug_mask(D_TRACE, "loadbalancer: retrying reader %s (fail %d)", rdr->label, stat->fail_factor);
|
---|
946 | }
|
---|
947 | }
|
---|
948 | }
|
---|
949 | }
|
---|
950 | }
|
---|
951 |
|
---|
952 | //Setting return values:
|
---|
953 | ll_destroy(er->matching_rdr);
|
---|
954 | er->matching_rdr = result;
|
---|
955 | er->fallback = fallback;
|
---|
956 |
|
---|
957 | if (new_nulltime.time)
|
---|
958 | nulltime = new_nulltime;
|
---|
959 |
|
---|
960 | #ifdef WITH_DEBUG
|
---|
961 | if (cs_dblevel & 0x01) {
|
---|
962 | //loadbalancer debug output:
|
---|
963 | int32_t size = 3;
|
---|
964 | int32_t nr = 0;
|
---|
965 | it = ll_iter_create(result);
|
---|
966 | while ((rdr=ll_iter_next(&it))) {
|
---|
967 | if (nr > 5) {
|
---|
968 | size+=20;
|
---|
969 | break;
|
---|
970 | }
|
---|
971 | size += strlen(rdr->label)+1;
|
---|
972 | nr++;
|
---|
973 | }
|
---|
974 | ll_iter_reset(&it);
|
---|
975 | char *rdrs = cs_malloc(&rdrs, size, 1);
|
---|
976 | char *rptr = rdrs;
|
---|
977 | *rptr = 0;
|
---|
978 | nr = 0;
|
---|
979 | while ((rdr=ll_iter_next(&it))) {
|
---|
980 | if (fallback && it.cur == fallback) {
|
---|
981 | snprintf(rptr, size, "[");
|
---|
982 | rptr = strend(rptr);
|
---|
983 | }
|
---|
984 | if (nr > 5) {
|
---|
985 | snprintf(rptr, size, "...(%d more)", ll_count(result)-nr);
|
---|
986 | rptr = strend(rptr);
|
---|
987 | break;
|
---|
988 | }
|
---|
989 | snprintf(rptr, size, "%s ", rdr->label);
|
---|
990 | rptr = strend(rptr);
|
---|
991 |
|
---|
992 | nr++;
|
---|
993 | }
|
---|
994 | if (fallback) {
|
---|
995 | rptr--;
|
---|
996 | *rptr=']';
|
---|
997 | }
|
---|
998 |
|
---|
999 | cs_debug_mask(D_TRACE, "loadbalancer: client %s for %04X&%06X/%04X:%02hX: n=%d selected readers: %s",
|
---|
1000 | username(er->client), er->caid, prid, er->srvid, er->l, ll_count(result), rdrs);
|
---|
1001 |
|
---|
1002 | free(rdrs);
|
---|
1003 | }
|
---|
1004 | #endif
|
---|
1005 |
|
---|
1006 |
|
---|
1007 | return 1;
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | /**
|
---|
1011 | * clears statistic of reader ridx.
|
---|
1012 | **/
|
---|
1013 | void clear_reader_stat(struct s_reader *rdr)
|
---|
1014 | {
|
---|
1015 | if (!rdr->lb_stat)
|
---|
1016 | return;
|
---|
1017 |
|
---|
1018 | ll_clear_data(rdr->lb_stat);
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | void clear_all_stat()
|
---|
1022 | {
|
---|
1023 | struct s_reader *rdr;
|
---|
1024 | LL_ITER itr = ll_iter_create(configured_readers);
|
---|
1025 | while ((rdr = ll_iter_next(&itr))) {
|
---|
1026 | clear_reader_stat(rdr);
|
---|
1027 | }
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | void housekeeping_stat_thread()
|
---|
1031 | {
|
---|
1032 | time_t cleanup_time = time(NULL) - (cfg.lb_stat_cleanup*60*60);
|
---|
1033 | int32_t cleaned = 0;
|
---|
1034 | struct s_reader *rdr;
|
---|
1035 | LL_ITER itr = ll_iter_create(configured_readers);
|
---|
1036 | while ((rdr = ll_iter_next(&itr))) {
|
---|
1037 | if (rdr->lb_stat) {
|
---|
1038 | LL_ITER it = ll_iter_create(rdr->lb_stat);
|
---|
1039 | READER_STAT *stat;
|
---|
1040 | while ((stat=ll_iter_next(&it))) {
|
---|
1041 |
|
---|
1042 | if (stat->last_received < cleanup_time) {
|
---|
1043 | ll_iter_remove_data(&it);
|
---|
1044 | cleaned++;
|
---|
1045 | }
|
---|
1046 | }
|
---|
1047 | }
|
---|
1048 | }
|
---|
1049 | cs_debug_mask(D_TRACE, "loadbalancer cleanup: removed %d entries", cleaned);
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | void housekeeping_stat(int32_t force)
|
---|
1053 | {
|
---|
1054 | time_t now = time(NULL);
|
---|
1055 | if (!force && now/60/60 == last_housekeeping/60/60) //only clean once in an hour
|
---|
1056 | return;
|
---|
1057 |
|
---|
1058 | last_housekeeping = now;
|
---|
1059 | start_thread((void*)&housekeeping_stat_thread, "housekeeping lb stats");
|
---|
1060 | }
|
---|
1061 |
|
---|
1062 | static int compare_stat(READER_STAT **ps1, READER_STAT **ps2) {
|
---|
1063 | READER_STAT *s1 = (*ps1), *s2 = (*ps2);
|
---|
1064 | int res = s1->rc - s2->rc;
|
---|
1065 | if (res) return res;
|
---|
1066 | res = s1->caid - s2->caid;
|
---|
1067 | if (res) return res;
|
---|
1068 | res = s1->prid - s2->prid;
|
---|
1069 | if (res) return res;
|
---|
1070 | res = s1->srvid - s2->srvid;
|
---|
1071 | if (res) return res;
|
---|
1072 | res = s1->ecmlen - s2->ecmlen;
|
---|
1073 | if (res) return res;
|
---|
1074 | res = s1->last_received - s2->last_received;
|
---|
1075 | return res;
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | static int compare_stat_r(READER_STAT **ps1, READER_STAT **ps2) {
|
---|
1079 | return -compare_stat(ps1, ps2);
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 | void sort_stat(struct s_reader *rdr, int32_t reverse)
|
---|
1083 | {
|
---|
1084 | if (reverse)
|
---|
1085 | ll_sort(rdr->lb_stat, compare_stat_r);
|
---|
1086 | else
|
---|
1087 | ll_sort(rdr->lb_stat, compare_stat);
|
---|
1088 | }
|
---|
1089 |
|
---|
1090 | #endif
|
---|