void log_tags_global_deinit(void) { gint i; g_static_mutex_lock(&log_tags_lock); g_hash_table_destroy(log_tags_hash); stats_lock(); StatsClusterKey sc_key; for (i = 0; i < log_tags_num; i++) { stats_cluster_logpipe_key_set(&sc_key, SCS_TAG, log_tags_list[i].name, NULL ); stats_unregister_counter(&sc_key, SC_TYPE_PROCESSED, &log_tags_list[i].counter); g_free(log_tags_list[i].name); } stats_unlock(); log_tags_num = 0; g_free(log_tags_list); log_tags_list = NULL; log_tags_hash = NULL; g_static_mutex_unlock(&log_tags_lock); }
/* * log_tags_get_by_name * * Lookup a tag id by it's name. If the tag is seen for the first time * the next tag id is assigned and the tag is added to the list. * * The function returns the tag id associated with the name. * * @name: the name of the tag * */ LogTagId log_tags_get_by_name(const gchar *name) { /* If log_tags_hash() is NULL, this unit is already deinitialized but other thread may refer the tag structure. If name is empty, it is an extremal element. In both cases the return value is 0. */ guint id; g_assert(log_tags_hash != NULL); g_static_mutex_lock(&log_tags_lock); id = GPOINTER_TO_UINT(g_hash_table_lookup(log_tags_hash, name)) - 1; if (id == 0xffffffff) { if (log_tags_num < LOG_TAGS_MAX - 1) { id = log_tags_num++; if (id == log_tags_list_size) { log_tags_list_size *= 2; log_tags_list = g_renew(LogTag, log_tags_list, log_tags_list_size); } log_tags_list[id].id = id; log_tags_list[id].name = g_strdup(name); log_tags_list[id].counter = NULL; /* NOTE: stats-level may not be set for calls that happen during * config file parsing, those get fixed up by * log_tags_reinit_stats() below */ stats_lock(); StatsClusterKey sc_key; stats_cluster_logpipe_key_set(&sc_key, SCS_TAG, name, NULL ); stats_register_counter(3, &sc_key, SC_TYPE_PROCESSED, &log_tags_list[id].counter); stats_unlock(); g_hash_table_insert(log_tags_hash, log_tags_list[id].name, GUINT_TO_POINTER(log_tags_list[id].id + 1)); } else id = 0; } g_static_mutex_unlock(&log_tags_lock); return id; }
/* * NOTE: this is called at cfg_init() time to update the set of counters we * have. If stats-level is decreased, we should unregister everything we * had earlier. If increased we need to register them again. * * log_tags_get_by_name() will also try to register the counter for calls * that are _after_ cfg_init(). Early calls to log_tags_get_by_name() will * not see a proper stats-level() in the global variable here. Those will * get handled by this function. */ void log_tags_reinit_stats(void) { gint id; stats_lock(); for (id = 0; id < log_tags_num; id++) { const gchar *name = log_tags_list[id].name; StatsClusterKey sc_key; stats_cluster_logpipe_key_set(&sc_key, SCS_TAG, name, NULL ); if (stats_check_level(3)) stats_register_counter(3, &sc_key, SC_TYPE_PROCESSED, &log_tags_list[id].counter); else stats_unregister_counter(&sc_key, SC_TYPE_PROCESSED, &log_tags_list[id].counter); } stats_unlock(); }
Test(logqueue, test_zero_diskbuf_and_normal_acks) { LogQueue *q; gint i; q = log_queue_fifo_new(OVERFLOW_SIZE, NULL); StatsClusterKey sc_key; stats_lock(); stats_cluster_logpipe_key_set(&sc_key, SCS_DESTINATION, q->persist_name, NULL ); stats_register_counter(0, &sc_key, SC_TYPE_QUEUED, &q->queued_messages); stats_register_counter(1, &sc_key, SC_TYPE_MEMORY_USAGE, &q->memory_usage); stats_unlock(); log_queue_set_use_backlog(q, TRUE); cr_assert_eq(atomic_gssize_racy_get(&q->queued_messages->value), 0); fed_messages = 0; acked_messages = 0; feed_some_messages(q, 1); cr_assert_eq(stats_counter_get(q->queued_messages), 1); cr_assert_neq(stats_counter_get(q->memory_usage), 0); gint size_when_single_msg = stats_counter_get(q->memory_usage); for (i = 0; i < 10; i++) feed_some_messages(q, 10); cr_assert_eq(stats_counter_get(q->queued_messages), 101); cr_assert_eq(stats_counter_get(q->memory_usage), 101*size_when_single_msg); send_some_messages(q, fed_messages); log_queue_ack_backlog(q, fed_messages); cr_assert_eq(fed_messages, acked_messages, "did not receive enough acknowledgements: fed_messages=%d, acked_messages=%d", fed_messages, acked_messages); log_queue_unref(q); }
Test(control_cmds, test_stats) { StatsCounterItem *counter = NULL; gchar **stats_result; const gchar *response; stats_init(); stats_lock(); StatsClusterKey sc_key; stats_cluster_logpipe_key_set(&sc_key, SCS_CENTER, "id", "received" ); stats_register_counter(0, &sc_key, SC_TYPE_PROCESSED, &counter); stats_unlock(); _run_command("STATS", &response); stats_result = g_strsplit(response, "\n", 2); cr_assert_str_eq(stats_result[0], "SourceName;SourceId;SourceInstance;State;Type;Number", "Bad reply"); g_strfreev(stats_result); stats_destroy(); }
Test(control_cmds, test_reset_stats) { StatsCounterItem *counter = NULL; const gchar *response; stats_init(); stats_lock(); StatsClusterKey sc_key; stats_cluster_logpipe_key_set(&sc_key, SCS_CENTER, "id", "received" ); stats_register_counter(0, &sc_key, SC_TYPE_PROCESSED, &counter); stats_counter_set(counter, 666); stats_unlock(); _run_command("RESET_STATS", &response); cr_assert(first_line_eq(response, "OK The statistics of syslog-ng have been reset to 0."), "Bad reply"); _run_command("STATS", &response); cr_assert_str_eq(response, "SourceName;SourceId;SourceInstance;State;Type;Number\ncenter;id;received;a;processed;0\n.\n", "Bad reply"); stats_destroy(); }
Test(logqueue, log_queue_fifo_rewind_all_and_memory_usage) { LogQueue *q = log_queue_fifo_new(OVERFLOW_SIZE, NULL); log_queue_set_use_backlog(q, TRUE); StatsClusterKey sc_key; stats_lock(); stats_cluster_logpipe_key_set(&sc_key, SCS_DESTINATION, q->persist_name, NULL ); stats_register_counter(1, &sc_key, SC_TYPE_MEMORY_USAGE, &q->memory_usage); stats_unlock(); feed_some_messages(q, 1); gint size_when_single_msg = stats_counter_get(q->memory_usage); feed_some_messages(q, 9); cr_assert_eq(stats_counter_get(q->memory_usage), 10*size_when_single_msg); send_some_messages(q, 10); cr_assert_eq(stats_counter_get(q->memory_usage), 0); log_queue_rewind_backlog_all(q); cr_assert_eq(stats_counter_get(q->memory_usage), 10*size_when_single_msg); log_queue_unref(q); }