1 | #include "module-stat.h"
|
---|
2 |
|
---|
3 | #define UNDEF_AVG_TIME 80000
|
---|
4 | #define MAX_ECM_SEND_CACHE 8
|
---|
5 | #define REOPEN_SECONDS 900
|
---|
6 |
|
---|
7 | //
|
---|
8 | // WARNING! call all functions only from MASTER PROCESS (s)!
|
---|
9 | //
|
---|
10 |
|
---|
11 | struct timeb nulltime;
|
---|
12 |
|
---|
13 | int ecm_send_cache_idx = 0;
|
---|
14 | typedef struct s_ecm_send_cache {
|
---|
15 | ushort caid;
|
---|
16 | uchar ecmd5[CS_ECMSTORESIZE];
|
---|
17 | int readers[CS_MAXREADER];
|
---|
18 | int best_reader;
|
---|
19 | } ECM_SEND_CACHE;
|
---|
20 | ECM_SEND_CACHE *ecm_send_cache;
|
---|
21 |
|
---|
22 | void init_stat()
|
---|
23 | {
|
---|
24 | memset(reader_stat, 0, sizeof(reader_stat));
|
---|
25 | ecm_send_cache = malloc(sizeof(ECM_SEND_CACHE)*MAX_ECM_SEND_CACHE);
|
---|
26 | memset(ecm_send_cache, 0, sizeof(ECM_SEND_CACHE)*MAX_ECM_SEND_CACHE);
|
---|
27 | cs_ftime(&nulltime);
|
---|
28 | }
|
---|
29 |
|
---|
30 | int chk_send_cache(int caid, uchar *ecmd5)
|
---|
31 | {
|
---|
32 | int i;
|
---|
33 | for (i=0; i<MAX_ECM_SEND_CACHE; i++) {
|
---|
34 | if (ecm_send_cache[i].caid == caid &&
|
---|
35 | memcmp(ecm_send_cache[i].ecmd5, ecmd5, sizeof(uchar)*CS_ECMSTORESIZE) == 0)
|
---|
36 | return i;
|
---|
37 | }
|
---|
38 | return -1;
|
---|
39 | }
|
---|
40 |
|
---|
41 | void add_send_cache(int caid, uchar *ecmd5, int *readers, int best_reader)
|
---|
42 | {
|
---|
43 | ecm_send_cache[ecm_send_cache_idx].caid = caid;
|
---|
44 | memcpy(ecm_send_cache[ecm_send_cache_idx].ecmd5, ecmd5, sizeof(uchar)*CS_ECMSTORESIZE);
|
---|
45 | memcpy(ecm_send_cache[ecm_send_cache_idx].readers, readers, sizeof(int)*CS_MAXREADER);
|
---|
46 | ecm_send_cache[ecm_send_cache_idx].best_reader = best_reader;
|
---|
47 |
|
---|
48 | ecm_send_cache_idx++;
|
---|
49 | if (ecm_send_cache_idx >= MAX_ECM_SEND_CACHE)
|
---|
50 | ecm_send_cache_idx = 0;
|
---|
51 | }
|
---|
52 |
|
---|
53 | void load_stat_from_file(int ridx)
|
---|
54 | {
|
---|
55 | char fname[40];
|
---|
56 | sprintf(fname, "%s/stat.%d", get_tmp_dir(), ridx);
|
---|
57 | FILE *file = fopen(fname, "r");
|
---|
58 | if (!file)
|
---|
59 | return;
|
---|
60 |
|
---|
61 | int i = 0;
|
---|
62 | do
|
---|
63 | {
|
---|
64 | READER_STAT *stat = malloc(sizeof(READER_STAT));
|
---|
65 | memset(stat, 0, sizeof(READER_STAT));
|
---|
66 | i = fscanf(file, "rc %d caid %04hX prid %06lX srvid %04hX time avg %dms ecms %d last %ld\n",
|
---|
67 | &stat->rc, &stat->caid, &stat->prid, &stat->srvid, &stat->time_avg, &stat->ecm_count, &stat->last_received);
|
---|
68 | if (i > 4) {
|
---|
69 | llist_append(reader_stat[ridx], stat);
|
---|
70 | }
|
---|
71 | else
|
---|
72 | free(stat);
|
---|
73 | } while(i != EOF && i > 0);
|
---|
74 | fclose(file);
|
---|
75 | }
|
---|
76 | /**
|
---|
77 | * get statistic values for reader ridx and caid/prid/srvid
|
---|
78 | */
|
---|
79 | READER_STAT *get_stat(int ridx, ushort caid, ulong prid, ushort srvid)
|
---|
80 | {
|
---|
81 | if (!reader_stat[ridx]) {
|
---|
82 | reader_stat[ridx] = llist_create();
|
---|
83 | if (cfg->reader_auto_loadbalance_save)
|
---|
84 | load_stat_from_file(ridx);
|
---|
85 | }
|
---|
86 |
|
---|
87 | LLIST_ITR itr;
|
---|
88 | READER_STAT *stat = llist_itr_init(reader_stat[ridx], &itr);
|
---|
89 | while (stat) {
|
---|
90 | if (stat->caid==caid && stat->prid==prid && stat->srvid==srvid)
|
---|
91 | return stat;
|
---|
92 |
|
---|
93 | stat = llist_itr_next(&itr);
|
---|
94 | }
|
---|
95 | return NULL;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * removes caid/prid/srvid from stat-list of reader ridx
|
---|
100 | */
|
---|
101 | int remove_stat(int ridx, ushort caid, ulong prid, ushort srvid)
|
---|
102 | {
|
---|
103 | if (!reader_stat[ridx])
|
---|
104 | return 0;
|
---|
105 |
|
---|
106 | int c = 0;
|
---|
107 | LLIST_ITR itr;
|
---|
108 | READER_STAT *stat = llist_itr_init(reader_stat[ridx], &itr);
|
---|
109 | while (stat) {
|
---|
110 | if (stat->caid==caid && stat->prid==prid && stat->srvid==srvid) {
|
---|
111 | free(stat);
|
---|
112 | stat = llist_itr_remove(&itr);
|
---|
113 | c++;
|
---|
114 | }
|
---|
115 | else
|
---|
116 | stat = llist_itr_next(&itr);
|
---|
117 | }
|
---|
118 | return c;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Calculates average time
|
---|
123 | */
|
---|
124 | void calc_stat(READER_STAT *stat)
|
---|
125 | {
|
---|
126 | int i;
|
---|
127 | int c=0;
|
---|
128 | int t = 0;
|
---|
129 | for (i = 0; i < MAX_STAT_TIME; i++) {
|
---|
130 | if (stat->time_stat[i] > 0) {
|
---|
131 | t += stat->time_stat[i];
|
---|
132 | c++;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | if (!c)
|
---|
136 | stat->time_avg = UNDEF_AVG_TIME;
|
---|
137 | else
|
---|
138 | stat->time_avg = t / c;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Saves statistik to /tmp/.oscam/stat.n where n is reader-index
|
---|
143 | */
|
---|
144 | void save_stat_to_file(int ridx)
|
---|
145 | {
|
---|
146 | char fname[40];
|
---|
147 | sprintf(fname, "%s/stat.%d", get_tmp_dir(), ridx);
|
---|
148 | if (!reader_stat[ridx] || !llist_count(reader_stat[ridx])) {
|
---|
149 | remove(fname);
|
---|
150 | return;
|
---|
151 | }
|
---|
152 |
|
---|
153 | LLIST_ITR itr;
|
---|
154 | READER_STAT *stat = llist_itr_init(reader_stat[ridx], &itr);
|
---|
155 |
|
---|
156 | if (!stat) {
|
---|
157 | remove(fname);
|
---|
158 | return;
|
---|
159 | }
|
---|
160 |
|
---|
161 | FILE *file = fopen(fname, "w");
|
---|
162 | if (!file)
|
---|
163 | return;
|
---|
164 |
|
---|
165 | while (stat) {
|
---|
166 | fprintf(file, "rc %d caid %04hX prid %06lX srvid %04hX time avg %dms ecms %d last %ld\n",
|
---|
167 | stat->rc, stat->caid, stat->prid, stat->srvid, stat->time_avg, stat->ecm_count, stat->last_received);
|
---|
168 | stat = llist_itr_next(&itr);
|
---|
169 | }
|
---|
170 | fclose(file);
|
---|
171 | }
|
---|
172 |
|
---|
173 | void save_all_stat_to_file()
|
---|
174 | {
|
---|
175 | int i;
|
---|
176 | for (i = 0; i < CS_MAXREADER; i++)
|
---|
177 | save_stat_to_file(i);
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Adds caid/prid/srvid to stat-list for reader ridx with time/rc
|
---|
182 | */
|
---|
183 | void add_stat(int ridx, ushort caid, ulong prid, ushort srvid, int ecm_time, int rc)
|
---|
184 | {
|
---|
185 | READER_STAT *stat = get_stat(ridx, caid, prid, srvid);
|
---|
186 | if (!stat) {
|
---|
187 | stat = malloc(sizeof(READER_STAT));
|
---|
188 | memset(stat, 0, sizeof(READER_STAT));
|
---|
189 | stat->caid = caid;
|
---|
190 | stat->prid = prid;
|
---|
191 | stat->srvid = srvid;
|
---|
192 | stat->time_avg = UNDEF_AVG_TIME; //dummy placeholder
|
---|
193 | llist_append(reader_stat[ridx], stat);
|
---|
194 | }
|
---|
195 |
|
---|
196 | //inc ecm_count if found, drop to 0 if not found:
|
---|
197 | if (rc == 0) {
|
---|
198 | stat->rc = rc;
|
---|
199 | stat->ecm_count++;
|
---|
200 | stat->time_idx++;
|
---|
201 | stat->last_received = time(NULL);
|
---|
202 |
|
---|
203 | //FASTEST READER:
|
---|
204 | if (stat->time_idx >= MAX_STAT_TIME)
|
---|
205 | stat->time_idx = 0;
|
---|
206 | stat->time_stat[stat->time_idx] = ecm_time;
|
---|
207 | calc_stat(stat);
|
---|
208 |
|
---|
209 | //OLDEST READER now set by get best reader!
|
---|
210 |
|
---|
211 |
|
---|
212 | //USAGELEVEL:
|
---|
213 | int ule = reader[ridx].lb_usagelevel_ecmcount;
|
---|
214 | if (ule > 0 && ((ule / MIN_ECM_COUNT) > 0)) //update every MIN_ECM_COUNT usagelevel:
|
---|
215 | {
|
---|
216 | time_t t = (time(NULL)-reader[ridx].lb_usagelevel_time);
|
---|
217 | reader[ridx].lb_usagelevel = 1000/(t<1?1:t);
|
---|
218 | ule = 0;
|
---|
219 | }
|
---|
220 | if (ule == 0)
|
---|
221 | reader[ridx].lb_usagelevel_time = time(NULL);
|
---|
222 | reader[ridx].lb_usagelevel_ecmcount = ule+1;
|
---|
223 | }
|
---|
224 | else if (rc >= 4 && rc < 100) { //not found+timeout+etc
|
---|
225 | stat->rc = rc;
|
---|
226 | //stat->ecm_count = 0; Keep ecm_count!
|
---|
227 | }
|
---|
228 |
|
---|
229 | //cs_debug_mask(D_TRACE, "adding stat for reader %s (%d): rc %d caid %04hX prid %06lX srvid %04hX time %dms usagelevel %d",
|
---|
230 | // reader[ridx].label, ridx, rc, caid, prid, srvid, ecm_time, reader[ridx].lb_usagelevel);
|
---|
231 |
|
---|
232 | //debug only:
|
---|
233 | if (cfg->reader_auto_loadbalance_save) {
|
---|
234 | stat_load_save++;
|
---|
235 | if (stat_load_save > cfg->reader_auto_loadbalance_save) {
|
---|
236 | stat_load_save = 0;
|
---|
237 | save_all_stat_to_file();
|
---|
238 | }
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Adds to stat-list
|
---|
244 | */
|
---|
245 | void add_reader_stat(ADD_READER_STAT *stat)
|
---|
246 | {
|
---|
247 | add_stat(stat->ridx, stat->caid, stat->prid, stat->srvid, stat->time, stat->rc);
|
---|
248 | }
|
---|
249 |
|
---|
250 | void reset_stat(ushort caid, ulong prid, ushort srvid)
|
---|
251 | {
|
---|
252 | //cs_debug_mask(D_TRACE, "loadbalance: resetting ecm count");
|
---|
253 | int i;
|
---|
254 | for (i = 0; i < CS_MAXREADER; i++) {
|
---|
255 | if (reader_stat[i] && reader[i].pid && reader[i].cs_idx) {
|
---|
256 | READER_STAT *stat = get_stat(i, caid, prid, srvid);
|
---|
257 | if (stat) {
|
---|
258 | if (stat->ecm_count > 0)
|
---|
259 | stat->ecm_count = 1; //not zero, so we know it's decodeable
|
---|
260 | stat->rc = 0;
|
---|
261 | }
|
---|
262 | }
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | /**
|
---|
268 | * Gets best reader for caid/prid/srvid.
|
---|
269 | * Best reader is evaluated by lowest avg time but only if ecm_count > MIN_ECM_COUNT (5)
|
---|
270 | * Also the reader is asked if he is "available"
|
---|
271 | * returns ridx when found or -1 when not found
|
---|
272 | */
|
---|
273 | int get_best_reader(GET_READER_STAT *grs, int *result)
|
---|
274 | {
|
---|
275 | int i;
|
---|
276 | i = chk_send_cache(grs->caid, grs->ecmd5);
|
---|
277 | if (i >= 0) { //Found in cache, return same reader because he has the cached cws!
|
---|
278 | memcpy(result, ecm_send_cache[i].readers, sizeof(int)*CS_MAXREADER);
|
---|
279 | int best_ridx = ecm_send_cache[i].best_reader;
|
---|
280 | 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)",
|
---|
281 | username(grs->cidx), grs->caid, grs->prid, grs->srvid,
|
---|
282 | best_ridx<0?"NONE":reader[best_ridx].label,
|
---|
283 | result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
|
---|
284 | result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]);
|
---|
285 |
|
---|
286 | return best_ridx;
|
---|
287 | }
|
---|
288 |
|
---|
289 | //resulting readers:
|
---|
290 | memset(result, 0, sizeof(int)*CS_MAXREADER);
|
---|
291 |
|
---|
292 | struct timeb new_nulltime;
|
---|
293 | memset(&new_nulltime, 0, sizeof(new_nulltime));
|
---|
294 | time_t current_time = time(NULL);
|
---|
295 |
|
---|
296 | int best_ridx = -1, best_ridx2 = -1;
|
---|
297 | int best = 0, best2 = 0;
|
---|
298 | int current = -1;
|
---|
299 | READER_STAT *stat = NULL;
|
---|
300 | for (i = 0; i < CS_MAXREADER; i++) {
|
---|
301 | if (grs->reader_avail[i]) {
|
---|
302 | int weight = reader[i].lb_weight <= 0?100:reader[i].lb_weight;
|
---|
303 | stat = get_stat(i, grs->caid, grs->prid, grs->srvid);
|
---|
304 | if (!stat) {
|
---|
305 | cs_debug_mask(D_TRACE, "loadbalancer: starting statistics for reader %s", reader[i].label);
|
---|
306 | add_stat(i, grs->caid, grs->prid, grs->srvid, 1, -1);
|
---|
307 | result[i] = 1; //no statistics, this reader is active (now) but we need statistics first!
|
---|
308 | continue;
|
---|
309 | }
|
---|
310 |
|
---|
311 | if (stat->ecm_count > MAX_ECM_COUNT && stat->time_avg > (int)cfg->ftimeout) {
|
---|
312 | cs_debug_mask(D_TRACE, "loadbalancer: max ecms (%d) reached by reader %s, resetting statistics", MAX_ECM_COUNT, reader[i].label);
|
---|
313 | reset_stat(grs->caid, grs->prid, grs->srvid);
|
---|
314 | result[i] = 1;//max ecm reached, get new statistics
|
---|
315 | continue;
|
---|
316 | }
|
---|
317 |
|
---|
318 | if (stat->rc == 0 && stat->ecm_count < MIN_ECM_COUNT) {
|
---|
319 | cs_debug_mask(D_TRACE, "loadbalancer: reader %s needs more statistics", reader[i].label);
|
---|
320 | result[i] = 1; //need more statistics!
|
---|
321 | continue;
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 | //Reader can decode this service (rc==0) and has MIN_ECM_COUNT ecms:
|
---|
326 | if (stat->rc == 0) {
|
---|
327 | //get
|
---|
328 | switch (cfg->reader_auto_loadbalance) {
|
---|
329 | default:
|
---|
330 | case LB_NONE:
|
---|
331 | //cs_debug_mask(D_TRACE, "loadbalance disabled");
|
---|
332 | result[i] = 1;
|
---|
333 | break;
|
---|
334 | case LB_FASTEST_READER_FIRST:
|
---|
335 | current = stat->time_avg * 100 / weight;
|
---|
336 | break;
|
---|
337 | case LB_OLDEST_READER_FIRST:
|
---|
338 | if (!reader[i].lb_last.time)
|
---|
339 | reader[i].lb_last = nulltime;
|
---|
340 | current = (1000*(reader[i].lb_last.time-nulltime.time)+
|
---|
341 | reader[i].lb_last.millitm-nulltime.millitm);
|
---|
342 | if (!new_nulltime.time || (1000*(reader[i].lb_last.time-new_nulltime.time)+
|
---|
343 | reader[i].lb_last.millitm-new_nulltime.millitm) < 0)
|
---|
344 | new_nulltime = reader[i].lb_last;
|
---|
345 | break;
|
---|
346 | case LB_LOWEST_USAGELEVEL:
|
---|
347 | current = reader[i].lb_usagelevel * 100 / weight;
|
---|
348 | break;
|
---|
349 | }
|
---|
350 | #ifdef WEBIF
|
---|
351 | reader[i].lbvalue = current;
|
---|
352 | #endif
|
---|
353 |
|
---|
354 | //cs_debug_mask(D_TRACE, "loadbalance reader %s value %d", reader[i].label, current);
|
---|
355 | if (best_ridx==-1 || current < best) {
|
---|
356 | if (!reader[i].ph.c_available
|
---|
357 | || reader[i].ph.c_available(i,
|
---|
358 | AVAIL_CHECK_LOADBALANCE)) {
|
---|
359 | best_ridx = i;
|
---|
360 | best = current;
|
---|
361 | }
|
---|
362 | }
|
---|
363 | if (best_ridx2==-1 || current < best2) {
|
---|
364 | best_ridx2 = i;
|
---|
365 | best2 = current;
|
---|
366 | }
|
---|
367 | }
|
---|
368 | else if (stat->rc >= 4) {
|
---|
369 | if (stat->last_received+REOPEN_SECONDS < current_time) { //Retrying every 900 seconds
|
---|
370 | stat->last_received = current_time;
|
---|
371 | result[i] = 1;
|
---|
372 | cs_log("loadbalancer: retrying reader %s", reader[i].label);
|
---|
373 | }
|
---|
374 |
|
---|
375 | if (stat->ecm_count == 0) { //Never decodeable
|
---|
376 | if (reader[i].audisabled ||
|
---|
377 | (!client[grs->cidx].autoau && client[grs->cidx].au != i))
|
---|
378 | //au disabled or not auto/au not on this reader: never decode it
|
---|
379 | grs->reader_avail[i] = 0;
|
---|
380 | //else reader is selected as fallback.
|
---|
381 | //if no best reader could be selected, fallbackreader elevates to primary readers
|
---|
382 | //so all (au) readers ares asked if user can au
|
---|
383 | }
|
---|
384 | else
|
---|
385 | result[i] = 2;
|
---|
386 | }
|
---|
387 | }
|
---|
388 | }
|
---|
389 | if (best_ridx == -1)
|
---|
390 | best_ridx = best_ridx2;
|
---|
391 | if (best_ridx >= 0) {
|
---|
392 | //cs_debug_mask(D_TRACE, "-->loadbalance best reader %s (%d) best value %d", reader[best_ridx].label, best_ridx, best2);
|
---|
393 | result[best_ridx] = 1;
|
---|
394 |
|
---|
395 | //OLDEST_READER:
|
---|
396 | cs_ftime(&reader[best_ridx].lb_last);
|
---|
397 | }
|
---|
398 | //else
|
---|
399 | //cs_debug_mask(D_TRACE, "-->loadbalance no best reader!");
|
---|
400 |
|
---|
401 | //setting all other readers as fallbacks:
|
---|
402 | for (i=0;i<CS_MAXREADER; i++) {
|
---|
403 | if (grs->reader_avail[i] && !result[i]) {
|
---|
404 | result[i] = 2;
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | 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",
|
---|
409 | username(grs->cidx), grs->caid, grs->prid, grs->srvid,
|
---|
410 | best_ridx<0?"NONE":reader[best_ridx].label,
|
---|
411 | result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
|
---|
412 | result[8], result[9], result[10], result[11], result[12], result[13], result[14], result[15]);
|
---|
413 |
|
---|
414 | add_send_cache(grs->caid, grs->ecmd5, result, best_ridx); //add to cache
|
---|
415 |
|
---|
416 | if (new_nulltime.time)
|
---|
417 | nulltime = new_nulltime;
|
---|
418 |
|
---|
419 | return best_ridx;
|
---|
420 | }
|
---|
421 |
|
---|
422 | /**
|
---|
423 | * clears statistic of reader ridx.
|
---|
424 | **/
|
---|
425 | void clear_reader_stat(int ridx)
|
---|
426 | {
|
---|
427 | if (!reader_stat[ridx])
|
---|
428 | return;
|
---|
429 |
|
---|
430 | LLIST_ITR itr;
|
---|
431 | READER_STAT *stat = llist_itr_init(reader_stat[ridx], &itr);
|
---|
432 | while (stat) {
|
---|
433 | free(stat);
|
---|
434 | stat = llist_itr_remove(&itr);
|
---|
435 | }
|
---|
436 | llist_destroy(reader_stat[ridx]);
|
---|
437 | reader_stat[ridx] = NULL;
|
---|
438 | }
|
---|