stats.c 28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org, 
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
 *                      Karl Heyes <karl@xiph.org>
 *                      and others (see AUTHORS for details).
 */

13 14 15 16
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
17 18 19 20 21
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

Michael Smith's avatar
Michael Smith committed
22 23 24
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
Jack Moffitt's avatar
Jack Moffitt committed
25

26 27 28 29
#include "thread/thread.h"
#include "avl/avl.h"
#include "httpp/httpp.h"
#include "net/sock.h"
Jack Moffitt's avatar
Jack Moffitt committed
30 31 32

#include "connection.h"

33
#include "source.h"
Jack Moffitt's avatar
Jack Moffitt committed
34 35 36 37
#include "global.h"
#include "refbuf.h"
#include "client.h"
#include "stats.h"
38
#include "xslt.h"
39
#include "util.h"
40 41
#define CATMODULE "stats"
#include "logging.h"
Jack Moffitt's avatar
Jack Moffitt committed
42

43
#ifdef _WIN32
44
#define atoll _atoi64
45
#define vsnprintf _vsnprintf
46
#define snprintf _snprintf
47
#endif
Jack Moffitt's avatar
Jack Moffitt committed
48

49 50 51 52
#define STATS_EVENT_SET     0
#define STATS_EVENT_INC     1
#define STATS_EVENT_DEC     2
#define STATS_EVENT_ADD     3
Karl Heyes's avatar
Karl Heyes committed
53 54 55
#define STATS_EVENT_SUB     4
#define STATS_EVENT_REMOVE  5
#define STATS_EVENT_HIDDEN  6
56

57 58 59 60 61 62 63 64
typedef struct _event_queue_tag
{
    volatile stats_event_t *head;
    volatile stats_event_t **tail;
} event_queue_t;

#define event_queue_init(qp)    { (qp)->head = NULL; (qp)->tail = &(qp)->head; }

Jack Moffitt's avatar
Jack Moffitt committed
65 66
typedef struct _event_listener_tag
{
67 68
    event_queue_t queue;
    mutex_t mutex;
Jack Moffitt's avatar
Jack Moffitt committed
69

70
    struct _event_listener_tag *next;
Jack Moffitt's avatar
Jack Moffitt committed
71 72
} event_listener_t;

73
static volatile int _stats_running = 0;
74
static thread_type *_stats_thread_id;
75
static volatile int _stats_threads = 0;
Jack Moffitt's avatar
Jack Moffitt committed
76

77 78
static stats_t _stats;
static mutex_t _stats_mutex;
Jack Moffitt's avatar
Jack Moffitt committed
79

80
static event_queue_t _global_event_queue;
Jack Moffitt's avatar
Jack Moffitt committed
81 82
mutex_t _global_event_mutex;

83
static volatile event_listener_t *_event_listeners;
Jack Moffitt's avatar
Jack Moffitt committed
84 85 86 87 88 89 90


static void *_stats_thread(void *arg);
static int _compare_stats(void *a, void *b, void *arg);
static int _compare_source_stats(void *a, void *b, void *arg);
static int _free_stats(void *key);
static int _free_source_stats(void *key);
91
static void _add_event_to_queue(stats_event_t *event, event_queue_t *queue);
92 93
static stats_node_t *_find_node(avl_tree *tree, const char *name);
static stats_source_t *_find_source(avl_tree *tree, const char *source);
Jack Moffitt's avatar
Jack Moffitt committed
94
static void _free_event(stats_event_t *event);
95
static stats_event_t *_get_event_from_queue (event_queue_t *queue);
Jack Moffitt's avatar
Jack Moffitt committed
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

/* simple helper function for creating an event */
static stats_event_t *build_event (const char *source, const char *name, const char *value)
{
    stats_event_t *event;

    event = (stats_event_t *)calloc(1, sizeof(stats_event_t));
    if (event)
    {
        if (source)
            event->source = (char *)strdup(source);
        if (name)
            event->name = (char *)strdup(name);
        if (value)
            event->value = (char *)strdup(value);
        else
            event->action = STATS_EVENT_REMOVE;
    }
    return event;
}

static void queue_global_event (stats_event_t *event)
{
    thread_mutex_lock(&_global_event_mutex);
121
    _add_event_to_queue (event, &_global_event_queue);
122 123 124
    thread_mutex_unlock(&_global_event_mutex);
}

125
void stats_initialize(void)
Jack Moffitt's avatar
Jack Moffitt committed
126
{
127
    _event_listeners = NULL;
128

129 130 131
    /* set up global struct */
    _stats.global_tree = avl_tree_new(_compare_stats, NULL);
    _stats.source_tree = avl_tree_new(_compare_source_stats, NULL);
Jack Moffitt's avatar
Jack Moffitt committed
132

133 134
    /* set up global mutex */
    thread_mutex_create(&_stats_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
135

136
    /* set up stats queues */
137
    event_queue_init (&_global_event_queue);
138
    thread_mutex_create(&_global_event_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
139

140 141 142
    /* fire off the stats thread */
    _stats_running = 1;
    _stats_thread_id = thread_create("Stats Thread", _stats_thread, NULL, THREAD_ATTACHED);
Jack Moffitt's avatar
Jack Moffitt committed
143 144
}

145
void stats_shutdown(void)
Jack Moffitt's avatar
Jack Moffitt committed
146
{
147
    int n;
Jack Moffitt's avatar
Jack Moffitt committed
148

149 150 151
    if(!_stats_running) /* We can't shutdown if we're not running. */
        return;

152 153 154
    /* wait for thread to exit */
    _stats_running = 0;
    thread_join(_stats_thread_id);
Jack Moffitt's avatar
Jack Moffitt committed
155

156 157 158 159 160 161 162
    /* wait for other threads to shut down */
    do {
        thread_sleep(300000);
        thread_mutex_lock(&_stats_mutex);
        n = _stats_threads;
        thread_mutex_unlock(&_stats_mutex);
    } while (n > 0);
163
    INFO0("stats thread finished");
164

165
    /* free the queues */
Jack Moffitt's avatar
Jack Moffitt committed
166

167 168
    /* destroy the queue mutexes */
    thread_mutex_destroy(&_global_event_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
169

170 171 172
    thread_mutex_destroy(&_stats_mutex);
    avl_tree_free(_stats.source_tree, _free_source_stats);
    avl_tree_free(_stats.global_tree, _free_stats);
173

174 175 176 177
    while (1)
    {
        stats_event_t *event = _get_event_from_queue (&_global_event_queue);
        if (event == NULL) break;
178 179 180 181
        if(event->source)
            free(event->source);
        if(event->value)
            free(event->value);
182 183
        if(event->name)
            free(event->name);
184 185
        free(event);
    }
Jack Moffitt's avatar
Jack Moffitt committed
186 187
}

188
stats_t *stats_get_stats(void)
Jack Moffitt's avatar
Jack Moffitt committed
189
{
190 191 192
    /* lock global stats
    
     copy stats
Jack Moffitt's avatar
Jack Moffitt committed
193

194
     unlock global stats
Jack Moffitt's avatar
Jack Moffitt committed
195

196
     return copied stats */
Jack Moffitt's avatar
Jack Moffitt committed
197

198
    return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
199 200
}

201 202
/* simple name=tag stat create/update */
void stats_event(const char *source, const char *name, const char *value)
Jack Moffitt's avatar
Jack Moffitt committed
203
{
204 205
    stats_event_t *event;

206 207 208 209 210
    if (value && xmlCheckUTF8 ((unsigned char *)value) == 0)
    {
        WARN2 ("seen non-UTF8 data, probably incorrect metadata (%s, %s)", name, value);
        return;
    }
211 212 213
    event = build_event (source, name, value);
    if (event)
        queue_global_event (event);
Jack Moffitt's avatar
Jack Moffitt committed
214 215
}

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

/* wrapper for stats_event, this takes a charset to convert from */
void stats_event_conv(const char *mount, const char *name, const char *value, const char *charset)
{
    const char *metadata = value;
    xmlBufferPtr conv = xmlBufferCreate ();

    if (charset)
    {
        xmlCharEncodingHandlerPtr handle = xmlFindCharEncodingHandler (charset);

        if (handle)
        {
            xmlBufferPtr raw = xmlBufferCreate ();
            xmlBufferAdd (raw, (const xmlChar *)value, strlen (value));
            if (xmlCharEncInFunc (handle, conv, raw) > 0)
                metadata = (char *)xmlBufferContent (conv);
            xmlBufferFree (raw);
            xmlCharEncCloseFunc (handle);
        }
        else
            WARN1 ("No charset found for \"%s\"", charset);
    }

    stats_event (mount, name, metadata);
    xmlBufferFree (conv);
}

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
/* make stat hidden (non-zero). name can be NULL if it applies to a whole
 * source stats tree. */
void stats_event_hidden (const char *source, const char *name, int hidden)
{
    stats_event_t *event;
    const char *str = NULL;

    if (hidden)
        str = "";
    event = build_event (source, name, str);
    if (event)
    {
        event->action = STATS_EVENT_HIDDEN;
        queue_global_event (event);
    }
}
260 261 262

/* printf style formatting for stat create/update */
void stats_event_args(const char *source, char *name, char *format, ...)
Jack Moffitt's avatar
Jack Moffitt committed
263
{
264 265
    char buf[1024];
    va_list val;
266 267 268 269
    int ret;

    if (name == NULL)
        return;
270
    va_start(val, format);
271
    ret = vsnprintf(buf, 1024, format, val);
272
    va_end(val);
Jack Moffitt's avatar
Jack Moffitt committed
273

274 275 276 277 278 279
    if (ret < 0 || (unsigned int)ret >= sizeof (buf))
    {
        WARN2 ("problem with formatting %s stat %s",
                source==NULL ? "global" : source, name);
        return;
    }
280
    stats_event(source, name, buf);
Jack Moffitt's avatar
Jack Moffitt committed
281 282
}

283
static char *_get_stats(const char *source, const char *name)
Jack Moffitt's avatar
Jack Moffitt committed
284
{
285 286 287
    stats_node_t *stats = NULL;
    stats_source_t *src = NULL;
    char *value = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
288

289
    thread_mutex_lock(&_stats_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
290

291 292 293 294 295 296 297 298
    if (source == NULL) {
        stats = _find_node(_stats.global_tree, name);
    } else {
        src = _find_source(_stats.source_tree, source);
        if (src) {
            stats = _find_node(src->stats_tree, name);
        }
    }
Jack Moffitt's avatar
Jack Moffitt committed
299

300
    if (stats) value = (char *)strdup(stats->value);
Jack Moffitt's avatar
Jack Moffitt committed
301

302
    thread_mutex_unlock(&_stats_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
303

304
    return value;
Jack Moffitt's avatar
Jack Moffitt committed
305 306
}

307
char *stats_get_value(const char *source, const char *name)
308
{
309
    return(_get_stats(source, name));
310
}
311 312 313

/* increase the value in the provided stat by 1 */
void stats_event_inc(const char *source, const char *name)
Jack Moffitt's avatar
Jack Moffitt committed
314
{
315 316 317 318 319 320
    stats_event_t *event = build_event (source, name, NULL);
    /* DEBUG2("%s on %s", name, source==NULL?"global":source); */
    if (event)
    {
        event->action = STATS_EVENT_INC;
        queue_global_event (event);
321
    }
Jack Moffitt's avatar
Jack Moffitt committed
322 323
}

324
void stats_event_add(const char *source, const char *name, unsigned long value)
Jack Moffitt's avatar
Jack Moffitt committed
325
{
326 327 328 329 330 331 332 333
    stats_event_t *event = build_event (source, name, NULL);
    /* DEBUG2("%s on %s", name, source==NULL?"global":source); */
    if (event)
    {
        event->value = malloc (16);
        snprintf (event->value, 16, "%ld", value);
        event->action = STATS_EVENT_ADD;
        queue_global_event (event);
334
    }
Jack Moffitt's avatar
Jack Moffitt committed
335 336
}

Karl Heyes's avatar
Karl Heyes committed
337 338 339 340 341 342 343 344 345 346 347 348
void stats_event_sub(const char *source, const char *name, unsigned long value)
{
    stats_event_t *event = build_event (source, name, NULL);
    if (event)
    {
        event->value = malloc (16);
        snprintf (event->value, 16, "%ld", value);
        event->action = STATS_EVENT_SUB;
        queue_global_event (event);
    }
}

349 350
/* decrease the value in the provided stat by 1 */
void stats_event_dec(const char *source, const char *name)
Jack Moffitt's avatar
Jack Moffitt committed
351
{
352 353 354 355 356 357
    /* DEBUG2("%s on %s", name, source==NULL?"global":source); */
    stats_event_t *event = build_event (source, name, NULL);
    if (event)
    {
        event->action = STATS_EVENT_DEC;
        queue_global_event (event);
358
    }
Jack Moffitt's avatar
Jack Moffitt committed
359 360 361 362 363
}

/* note: you must call this function only when you have exclusive access
** to the avl_tree
*/
364
static stats_node_t *_find_node(avl_tree *stats_tree, const char *name)
Jack Moffitt's avatar
Jack Moffitt committed
365
{
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    stats_node_t *stats;
    avl_node *node;
    int cmp;

    /* get the root node */
    node = stats_tree->root->right;
    
    while (node) {
        stats = (stats_node_t *)node->key;
        cmp = strcmp(name, stats->name);
        if (cmp < 0) 
            node = node->left;
        else if (cmp > 0)
            node = node->right;
        else
            return stats;
    }
    
    /* didn't find it */
    return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
386 387 388 389 390
}

/* note: you must call this function only when you have exclusive access
** to the avl_tree
*/
391
static stats_source_t *_find_source(avl_tree *source_tree, const char *source)
Jack Moffitt's avatar
Jack Moffitt committed
392
{
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
    stats_source_t *stats;
    avl_node *node;
    int cmp;

    /* get the root node */
    node = source_tree->root->right;
    while (node) {
        stats = (stats_source_t *)node->key;
        cmp = strcmp(source, stats->source);
        if (cmp < 0)
            node = node->left;
        else if (cmp > 0)
            node = node->right;
        else
            return stats;
    }
Jack Moffitt's avatar
Jack Moffitt committed
409

410 411
    /* didn't find it */
    return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
412 413 414 415
}

static stats_event_t *_copy_event(stats_event_t *event)
{
416
    stats_event_t *copy = (stats_event_t *)calloc(1, sizeof(stats_event_t));
417 418 419 420
    if (event->source) 
        copy->source = (char *)strdup(event->source);
    else
        copy->source = NULL;
421 422
    if (event->name)
        copy->name = (char *)strdup(event->name);
423 424 425 426
    if (event->value)
        copy->value = (char *)strdup(event->value);
    else
        copy->value = NULL;
427
    copy->hidden = event->hidden;
428
    copy->next = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
429

430
    return copy;
Jack Moffitt's avatar
Jack Moffitt committed
431 432
}

433 434 435 436 437 438

/* helper to apply specialised changes to a stats node */
static void modify_node_event (stats_node_t *node, stats_event_t *event)
{
    char *str;

439 440 441 442 443 444 445 446
    if (event->action == STATS_EVENT_HIDDEN)
    {
        if (event->value)
            node->hidden = 1;
        else
            node->hidden = 0;
        return;
    }
447 448
    if (event->action != STATS_EVENT_SET)
    {
449
        int64_t value = 0;
450 451 452 453 454 455 456 457 458 459 460 461

        switch (event->action)
        {
            case STATS_EVENT_INC:
                value = atoi (node->value)+1;
                break;
            case STATS_EVENT_DEC:
                value = atoi (node->value)-1;
                break;
            case STATS_EVENT_ADD:
                value = atoi (node->value)+atoi (event->value);
                break;
462 463 464
            case STATS_EVENT_SUB:
                value = atoll (node->value) - atoll (event->value);
                break;
465
            default:
466
                WARN2 ("unhandled event (%d) for %s", event->action, event->source);
467 468 469
                break;
        }
        str = malloc (16);
470
        snprintf (str, 16, "%" PRId64, value);
471 472
        if (event->value == NULL)
            event->value = strdup (str);
473 474 475 476 477
    }
    else
        str = (char *)strdup (event->value);
    free (node->value);
    node->value = str;
478 479 480 481
    if (event->source)
        DEBUG3 ("update \"%s\" %s (%s)", event->source, node->name, node->value);
    else
        DEBUG2 ("update global %s (%s)", node->name, node->value);
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
}


static void process_global_event (stats_event_t *event)
{
    stats_node_t *node;

    /* DEBUG3("global event %s %s %d", event->name, event->value, event->action); */
    if (event->action == STATS_EVENT_REMOVE)
    {
        /* we're deleting */
        node = _find_node(_stats.global_tree, event->name);
        if (node != NULL)
            avl_delete(_stats.global_tree, (void *)node, _free_stats);
        return;
    }
    node = _find_node(_stats.global_tree, event->name);
    if (node)
    {
        modify_node_event (node, event);
    }
    else
    {
        /* add node */
        node = (stats_node_t *)calloc(1, sizeof(stats_node_t));
        node->name = (char *)strdup(event->name);
        node->value = (char *)strdup(event->value);

        avl_insert(_stats.global_tree, (void *)node);
    }
}


static void process_source_event (stats_event_t *event)
{
    stats_source_t *snode = _find_source(_stats.source_tree, event->source);
    if (snode == NULL)
    {
        if (event->action == STATS_EVENT_REMOVE)
            return;
        snode = (stats_source_t *)calloc(1,sizeof(stats_source_t));
        if (snode == NULL)
            return;
        DEBUG1 ("new source stat %s", event->source);
        snode->source = (char *)strdup(event->source);
        snode->stats_tree = avl_tree_new(_compare_stats, NULL);
528 529 530 531
        if (event->action == STATS_EVENT_HIDDEN)
            snode->hidden = 1;
        else
            snode->hidden = 0;
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548

        avl_insert(_stats.source_tree, (void *)snode);
    }
    if (event->name)
    {
        stats_node_t *node = _find_node(snode->stats_tree, event->name);
        if (node == NULL)
        {
            if (event->action == STATS_EVENT_REMOVE)
                return;
            /* adding node */
            if (event->value)
            {
                DEBUG2 ("new node %s (%s)", event->name, event->value);
                node = (stats_node_t *)calloc(1,sizeof(stats_node_t));
                node->name = (char *)strdup(event->name);
                node->value = (char *)strdup(event->value);
549
                node->hidden = snode->hidden;
550 551 552 553 554 555 556 557 558 559 560 561 562 563

                avl_insert(snode->stats_tree, (void *)node);
            }
            return;
        }
        if (event->action == STATS_EVENT_REMOVE)
        {
            DEBUG1 ("delete node %s", event->name);
            avl_delete(snode->stats_tree, (void *)node, _free_stats);
            return;
        }
        modify_node_event (node, event);
        return;
    }
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
    if (event->action == STATS_EVENT_HIDDEN)
    {
        avl_node *node = avl_get_first (snode->stats_tree);

        if (event->value)
            snode->hidden = 1;
        else
            snode->hidden = 0;
        while (node)
        {
            stats_node_t *stats = (stats_node_t*)node->key;
            stats->hidden = snode->hidden;
            node = avl_get_next (node);
        }
        return;
    }
580 581 582 583 584 585 586 587
    if (event->action == STATS_EVENT_REMOVE)
    {
        DEBUG1 ("delete source node %s", event->source);
        avl_delete(_stats.source_tree, (void *)snode, _free_source_stats);
    }
}


Karl Heyes's avatar
Karl Heyes committed
588 589 590
void stats_event_time (const char *mount, const char *name)
{
    time_t now = time(NULL);
591
    struct tm local;
Karl Heyes's avatar
Karl Heyes committed
592 593 594 595 596 597 598 599
    char buffer[100];

    localtime_r (&now, &local);
    strftime (buffer, sizeof (buffer), "%a, %d %b %Y %H:%M:%S %z", &local);
    stats_event (mount, name, buffer);
}


Karl Heyes's avatar
Karl Heyes committed
600 601
void stats_global (ice_config_t *config)
{
602
    stats_event (NULL, "server_id", config->server_id);
Karl Heyes's avatar
Karl Heyes committed
603 604 605 606 607 608
    stats_event (NULL, "host", config->hostname);
    stats_event (NULL, "location", config->location);
    stats_event (NULL, "admin", config->admin);
}


Jack Moffitt's avatar
Jack Moffitt committed
609 610
static void *_stats_thread(void *arg)
{
611 612 613 614
    stats_event_t *event;
    stats_event_t *copy;
    event_listener_t *listener;

Karl Heyes's avatar
Karl Heyes committed
615
    stats_event_time (NULL, "server_start");
616 617 618 619 620 621

    /* global currently active stats */
    stats_event (NULL, "clients", "0");
    stats_event (NULL, "connections", "0");
    stats_event (NULL, "sources", "0");
    stats_event (NULL, "stats", "0");
Karl Heyes's avatar
Karl Heyes committed
622
    stats_event (NULL, "listeners", "0");
623 624 625 626 627 628 629

    /* global accumulating stats */
    stats_event (NULL, "client_connections", "0");
    stats_event (NULL, "source_client_connections", "0");
    stats_event (NULL, "source_relay_connections", "0");
    stats_event (NULL, "source_total_connections", "0");
    stats_event (NULL, "stats_connections", "0");
630
    stats_event (NULL, "listener_connections", "0");
631 632

    INFO0 ("stats thread started");
633
    while (_stats_running) {
634
        thread_mutex_lock(&_global_event_mutex);
635
        if (_global_event_queue.head != NULL) {
636
            /* grab the next event from the queue */
637
            event = _get_event_from_queue (&_global_event_queue);
638 639
            thread_mutex_unlock(&_global_event_mutex);

640 641
            if (event == NULL)
                continue;
642 643 644
            event->next = NULL;

            thread_mutex_lock(&_stats_mutex);
645 646 647 648 649 650

            /* check if we are dealing with a global or source event */
            if (event->source == NULL)
                process_global_event (event);
            else
                process_source_event (event);
651 652 653
            
            /* now we have an event that's been processed into the running stats */
            /* this event should get copied to event listeners' queues */
654
            listener = (event_listener_t *)_event_listeners;
655 656
            while (listener) {
                copy = _copy_event(event);
657 658 659
                thread_mutex_lock (&listener->mutex);
                _add_event_to_queue (copy, &listener->queue);
                thread_mutex_unlock (&listener->mutex);
660 661 662 663 664 665 666 667

                listener = listener->next;
            }

            /* now we need to destroy the event */
            _free_event(event);

            thread_mutex_unlock(&_stats_mutex);
668
            continue;
669
        }
670 671 672 673
        else
        {
            thread_mutex_unlock(&_global_event_mutex);
        }
674

675
        thread_sleep(300000);
676 677 678
    }

    return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
679 680
}

681
/* you must have the _stats_mutex locked here */
682
static void _unregister_listener(event_listener_t *listener)
683 684 685 686 687
{
    event_listener_t **prev = (event_listener_t **)&_event_listeners,
                     *current = *prev;
    while (current)
    {
688
        if (current == listener)
689 690 691 692 693 694 695 696 697 698
        {
            *prev = current->next;
            break;
        }
        prev = &current->next;
        current = *prev;
    }
}


Jack Moffitt's avatar
Jack Moffitt committed
699 700
static stats_event_t *_make_event_from_node(stats_node_t *node, char *source)
{
701 702 703 704 705 706 707 708
    stats_event_t *event = (stats_event_t *)malloc(sizeof(stats_event_t));
    
    if (source != NULL)
        event->source = (char *)strdup(source);
    else
        event->source = NULL;
    event->name = (char *)strdup(node->name);
    event->value = (char *)strdup(node->value);
709
    event->hidden = node->hidden;
710
    event->action = STATS_EVENT_SET;
711
    event->next = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
712

713
    return event;
Jack Moffitt's avatar
Jack Moffitt committed
714 715 716
}


717 718 719 720
static void _add_event_to_queue(stats_event_t *event, event_queue_t *queue)
{
    *queue->tail = event;
    queue->tail = (volatile stats_event_t **)&event->next;
Jack Moffitt's avatar
Jack Moffitt committed
721 722 723
}


724 725 726
static stats_event_t *_get_event_from_queue (event_queue_t *queue)
{
    stats_event_t *event = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
727

728 729 730 731 732 733 734
    if (queue && queue->head)
    {
        event = (stats_event_t *)queue->head;
        queue->head = event->next;
        if (queue->head == NULL)
            queue->tail = &queue->head;
    }
Jack Moffitt's avatar
Jack Moffitt committed
735

736
    return event;
Jack Moffitt's avatar
Jack Moffitt committed
737 738
}

739
static int _send_event_to_client(stats_event_t *event, client_t *client)
Jack Moffitt's avatar
Jack Moffitt committed
740
{
741
    int len;
742
    char buf [200];
Jack Moffitt's avatar
Jack Moffitt committed
743

744
    /* send data to the client!!!! */
745
    len = snprintf (buf, sizeof (buf), "EVENT %s %s %s\n",
746 747 748
            (event->source != NULL) ? event->source : "global",
            event->name ? event->name : "null",
            event->value ? event->value : "null");
749
    if (len > 0 && len < (int)sizeof (buf))
750 751 752 753 754 755
    {
        client_send_bytes (client, buf, len);
        if (client->con->error)
            return -1;
    }
    return 0;
Jack Moffitt's avatar
Jack Moffitt committed
756 757
}

758 759

static xmlNodePtr _dump_stats_to_doc (xmlNodePtr root, const char *show_mount, int hidden)
Jack Moffitt's avatar
Jack Moffitt committed
760
{
761 762
    avl_node *avlnode;
    xmlNodePtr ret = NULL;
763 764

    thread_mutex_lock(&_stats_mutex);
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
    /* general stats first */
    avlnode = avl_get_first(_stats.global_tree);
    while (avlnode)
    {
        stats_node_t *stat = avlnode->key;
        if (stat->hidden <=  hidden)
            xmlNewTextChild (root, NULL, XMLSTR(stat->name), XMLSTR(stat->value));
        avlnode = avl_get_next (avlnode);
    }
    /* now per mount stats */
    avlnode = avl_get_first(_stats.source_tree);
    while (avlnode)
    {
        stats_source_t *source = (stats_source_t *)avlnode->key;
        if (source->hidden <= hidden &&
                (show_mount == NULL || strcmp (show_mount, source->source) == 0))
        {
            avl_node *avlnode2 = avl_get_first (source->stats_tree);
            xmlNodePtr xmlnode = xmlNewTextChild (root, NULL, XMLSTR("source"), NULL);
784

785 786 787 788 789 790 791 792 793
            xmlSetProp (xmlnode, XMLSTR("mount"), XMLSTR(source->source));
            if (ret == NULL)
                ret = xmlnode;
            while (avlnode2)
            {
                stats_node_t *stat = avlnode2->key;
                xmlNewTextChild (xmlnode, NULL, XMLSTR(stat->name), XMLSTR(stat->value));
                avlnode2 = avl_get_next (avlnode2);
            }
794
        }
795
        avlnode = avl_get_next (avlnode);
796 797
    }
    thread_mutex_unlock(&_stats_mutex);
798
    return ret;
Jack Moffitt's avatar
Jack Moffitt committed
799 800
}

801

802 803 804 805 806
/* factoring out code for stats loops
** this function copies all stats to queue, and registers 
** the queue for all new events atomically.
** note: mutex must already be created!
*/
807
static void _register_listener (event_listener_t *listener)
Jack Moffitt's avatar
Jack Moffitt committed
808
{
809 810 811 812
    avl_node *node;
    avl_node *node2;
    stats_event_t *event;
    stats_source_t *source;
Jack Moffitt's avatar
Jack Moffitt committed
813

814
    thread_mutex_lock(&_stats_mutex);
Jack Moffitt's avatar
Jack Moffitt committed
815

816 817 818 819 820 821
    /* first we fill our queue with the current stats */
    
    /* start with the global stats */
    node = avl_get_first(_stats.global_tree);
    while (node) {
        event = _make_event_from_node((stats_node_t *)node->key, NULL);
822
        _add_event_to_queue (event, &listener->queue);
Jack Moffitt's avatar
Jack Moffitt committed
823

824 825
        node = avl_get_next(node);
    }
Jack Moffitt's avatar
Jack Moffitt committed
826

827 828 829 830 831 832 833
    /* now the stats for each source */
    node = avl_get_first(_stats.source_tree);
    while (node) {
        source = (stats_source_t *)node->key;
        node2 = avl_get_first(source->stats_tree);
        while (node2) {
            event = _make_event_from_node((stats_node_t *)node2->key, source->source);
834
            _add_event_to_queue (event, &listener->queue);
835 836 837 838 839 840

            node2 = avl_get_next(node2);
        }
        
        node = avl_get_next(node);
    }
Jack Moffitt's avatar
Jack Moffitt committed
841

842
    /* now we register to receive future event notices */
843 844
    listener->next = (event_listener_t *)_event_listeners;
    _event_listeners = listener;
Jack Moffitt's avatar
Jack Moffitt committed
845

846
    thread_mutex_unlock(&_stats_mutex);
847 848 849 850
}

void *stats_connection(void *arg)
{
851
    client_t *client = (client_t *)arg;
852
    stats_event_t *event;
853
    event_listener_t listener;
854

855 856
    INFO0 ("stats client starting");

857
    event_queue_init (&listener.queue);
858 859 860
    /* increment the thread count */
    thread_mutex_lock(&_stats_mutex);
    _stats_threads++;
861
    stats_event_args (NULL, "stats", "%d", _stats_threads);
862 863
    thread_mutex_unlock(&_stats_mutex);

864
    thread_mutex_create (&(listener.mutex));
865

866
    _register_listener (&listener);
867 868

    while (_stats_running) {
869 870 871
        thread_mutex_lock (&listener.mutex);
        event = _get_event_from_queue (&listener.queue);
        thread_mutex_unlock (&listener.mutex);
872
        if (event != NULL) {
873
            if (_send_event_to_client(event, client) < 0) {
874 875 876 877 878 879
                _free_event(event);
                break;
            }
            _free_event(event);
            continue;
        }
880
        thread_sleep (500000);
881 882 883
    }

    thread_mutex_lock(&_stats_mutex);
884
    _unregister_listener (&listener);
885
    _stats_threads--;
886
    stats_event_args (NULL, "stats", "%d", _stats_threads);
887 888
    thread_mutex_unlock(&_stats_mutex);

889
    thread_mutex_destroy (&listener.mutex);
890
    client_destroy (client);
891 892
    INFO0 ("stats client finished");

893
    return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
894 895
}

896 897 898 899 900 901 902 903 904 905 906 907 908

void stats_callback (client_t *client, void *notused)
{
    if (client->con->error)
    {
        client_destroy (client);
        return;
    }
    client_set_queue (client, NULL);
    thread_create("Stats Connection", stats_connection, (void *)client, THREAD_DETACHED);
}


Jack Moffitt's avatar
Jack Moffitt committed
909
typedef struct _source_xml_tag {
910 911
    char *mount;
    xmlNodePtr node;
Jack Moffitt's avatar
Jack Moffitt committed
912

913
    struct _source_xml_tag *next;
Jack Moffitt's avatar
Jack Moffitt committed
914 915 916
} source_xml_t;


917
void stats_transform_xslt(client_t *client, const char *uri)
918 919
{
    xmlDocPtr doc;
920
    char *xslpath = util_get_path_from_normalised_uri (uri);
921
    const char *mount = httpp_get_query_param (client->parser, "mount");
922

923
    doc = stats_get_xml (0, mount);
924

925
    xslt_transform(doc, xslpath, client);
926 927

    xmlFreeDoc(doc);
928
    free (xslpath);
929 930
}

931
xmlDocPtr stats_get_xml(int show_hidden, const char *show_mount)
Jack Moffitt's avatar
Jack Moffitt committed
932
{
933
    xmlDocPtr doc;
934
    xmlNodePtr node;
935

936 937
    doc = xmlNewDoc (XMLSTR("1.0"));
    node = xmlNewDocNode (doc, NULL, XMLSTR("icestats"), NULL);
938 939
    xmlDocSetRootElement(doc, node);

940
    node = _dump_stats_to_doc (node, show_mount, show_hidden);
941

942
    return doc;
Jack Moffitt's avatar
Jack Moffitt committed
943 944
}

945

Jack Moffitt's avatar
Jack Moffitt committed
946 947
static int _compare_stats(void *arg, void *a, void *b)
{
948 949
    stats_node_t *nodea = (stats_node_t *)a;
    stats_node_t *nodeb = (stats_node_t *)b;
Jack Moffitt's avatar
Jack Moffitt committed
950

951
    return strcmp(nodea->name, nodeb->name);
Jack Moffitt's avatar
Jack Moffitt committed
952 953 954 955
}

static int _compare_source_stats(void *arg, void *a, void *b)
{
956 957
    stats_source_t *nodea = (stats_source_t *)a;
    stats_source_t *nodeb = (stats_source_t *)b;
Jack Moffitt's avatar
Jack Moffitt committed
958

959
    return strcmp(nodea->source, nodeb->source);
Jack Moffitt's avatar
Jack Moffitt committed
960 961 962 963
}

static int _free_stats(void *key)
{
964 965 966 967 968 969
    stats_node_t *node = (stats_node_t *)key;
    free(node->value);
    free(node->name);
    free(node);
    
    return 1;
Jack Moffitt's avatar
Jack Moffitt committed
970 971 972 973
}

static int _free_source_stats(void *key)
{
974 975 976
    stats_source_t *node = (stats_source_t *)key;
    avl_tree_free(node->stats_tree, _free_stats);
    free(node->source);
977
    free(node);
Jack Moffitt's avatar
Jack Moffitt committed
978

979
    return 1;
Jack Moffitt's avatar
Jack Moffitt committed
980 981 982 983
}

static void _free_event(stats_event_t *event)
{
984 985 986 987
    if (event->source) free(event->source);
    if (event->name) free(event->name);
    if (event->value) free(event->value);
    free(event);
Jack Moffitt's avatar
Jack Moffitt committed
988
}
989 990


991
refbuf_t *stats_get_streams (void)
992
{
993
#define STREAMLIST_BLKSIZE  4096
994
    avl_node *node;
995
    unsigned int remaining = STREAMLIST_BLKSIZE;
996 997
    refbuf_t *start = refbuf_new (remaining), *cur = start;
    char *buffer = cur->data;
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008

    /* now the stats for each source */
    thread_mutex_lock (&_stats_mutex);
    node = avl_get_first(_stats.source_tree);
    while (node)
    {
        int ret;
        stats_source_t *source = (stats_source_t *)node->key;

        if (source->hidden == 0)
        {
1009
            if (remaining <= strlen (source->source) + 3)
1010
            {
1011 1012 1013 1014 1015
                cur->len = STREAMLIST_BLKSIZE - remaining;
                cur->next = refbuf_new (STREAMLIST_BLKSIZE);
                remaining = STREAMLIST_BLKSIZE;
                cur = cur->next;
                buffer = cur->data;
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
            }
            ret = snprintf (buffer, remaining, "%s\r\n", source->source);
            if (ret > 0)
            {
                buffer += ret;
                remaining -= ret;
            }
        }
        node = avl_get_next(node);
    }
    thread_mutex_unlock (&_stats_mutex);
1027 1028
    cur->len = STREAMLIST_BLKSIZE - remaining;
    return start;
1029 1030
}

1031 1032


1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
/* This removes any source stats from virtual mountpoints, ie mountpoints
 * where no source_t exists. This function requires the global sources lock
 * to be held before calling.
 */
void stats_clear_virtual_mounts (void)
{
    avl_node *snode;

    thread_mutex_lock (&_stats_mutex);
    snode = avl_get_first(_stats.source_tree);
    while (snode)
    {
        stats_source_t *src = (stats_source_t *)snode->key;
        source_t *source = source_find_mount_raw (src->source);

        if (source == NULL)
        {
            /* no source_t is reserved so remove them now */
            snode = avl_get_next (snode);
            DEBUG1 ("releasing %s stats", src->source);
            avl_delete (_stats.source_tree, src, _free_source_stats);
            continue;
        }

        snode = avl_get_next (snode);
    }
    thread_mutex_unlock (&_stats_mutex);
}