Simplify code around SYS statistics gathering.

pull/139/head
Roger A. Light 11 years ago
parent b598aec385
commit 8049c4b7f8

@ -19,14 +19,6 @@ Contributors:
#ifdef WITH_BROKER #ifdef WITH_BROKER
# include "mosquitto_broker.h" # include "mosquitto_broker.h"
# ifdef WITH_SYS_TREE
extern uint64_t g_bytes_received;
extern uint64_t g_bytes_sent;
extern unsigned long g_msgs_received;
extern unsigned long g_msgs_sent;
extern unsigned long g_pub_msgs_received;
extern unsigned long g_pub_msgs_sent;
# endif
# ifdef WITH_WEBSOCKETS # ifdef WITH_WEBSOCKETS
# include <libwebsockets.h> # include <libwebsockets.h>
# endif # endif
@ -39,6 +31,14 @@ Contributors:
#include "net_mosq.h" #include "net_mosq.h"
#include "packet_mosq.h" #include "packet_mosq.h"
#include "read_handle.h" #include "read_handle.h"
#ifdef WITH_BROKER
# include "sys_tree.h"
#else
# define G_BYTES_RECEIVED_INC(A)
# define G_BYTES_SENT_INC(A)
# define G_MSGS_SENT_INC(A)
# define G_PUB_MSGS_SENT_INC(A)
#endif
void mosquitto__packet_cleanup(struct mosquitto__packet *packet) void mosquitto__packet_cleanup(struct mosquitto__packet *packet)
{ {
@ -239,9 +239,7 @@ int mosquitto__packet_write(struct mosquitto *mosq)
while(packet->to_process > 0){ while(packet->to_process > 0){
write_length = mosquitto__net_write(mosq, &(packet->payload[packet->pos]), packet->to_process); write_length = mosquitto__net_write(mosq, &(packet->payload[packet->pos]), packet->to_process);
if(write_length > 0){ if(write_length > 0){
#if defined(WITH_BROKER) && defined(WITH_SYS_TREE) G_BYTES_SENT_INC(write_length);
g_bytes_sent += write_length;
#endif
packet->to_process -= write_length; packet->to_process -= write_length;
packet->pos += write_length; packet->pos += write_length;
}else{ }else{
@ -263,15 +261,10 @@ int mosquitto__packet_write(struct mosquitto *mosq)
} }
} }
#ifdef WITH_BROKER G_MSGS_SENT_INC(1);
# ifdef WITH_SYS_TREE
g_msgs_sent++;
if(((packet->command)&0xF6) == PUBLISH){
g_pub_msgs_sent++;
}
# endif
#else
if(((packet->command)&0xF6) == PUBLISH){ if(((packet->command)&0xF6) == PUBLISH){
G_PUB_MSGS_SENT_INC(1);
#ifndef WITH_BROKER
pthread_mutex_lock(&mosq->callback_mutex); pthread_mutex_lock(&mosq->callback_mutex);
if(mosq->on_publish){ if(mosq->on_publish){
/* This is a QoS=0 message */ /* This is a QoS=0 message */
@ -315,8 +308,8 @@ int mosquitto__packet_write(struct mosquitto *mosq)
pthread_mutex_unlock(&mosq->callback_mutex); pthread_mutex_unlock(&mosq->callback_mutex);
pthread_mutex_unlock(&mosq->current_out_packet_mutex); pthread_mutex_unlock(&mosq->current_out_packet_mutex);
return MOSQ_ERR_SUCCESS; return MOSQ_ERR_SUCCESS;
}
#endif #endif
}
/* Free data and reset values */ /* Free data and reset values */
pthread_mutex_lock(&mosq->out_packet_mutex); pthread_mutex_lock(&mosq->out_packet_mutex);
@ -376,9 +369,7 @@ int mosquitto__packet_read(struct mosquitto *mosq)
if(read_length == 1){ if(read_length == 1){
mosq->in_packet.command = byte; mosq->in_packet.command = byte;
#ifdef WITH_BROKER #ifdef WITH_BROKER
# ifdef WITH_SYS_TREE G_BYTES_RECEIVED_INC(1);
g_bytes_received++;
# endif
/* Clients must send CONNECT as their first command. */ /* Clients must send CONNECT as their first command. */
if(!(mosq->bridge) && mosq->state == mosq_cs_new && (byte&0xF0) != CONNECT) return MOSQ_ERR_PROTOCOL; if(!(mosq->bridge) && mosq->state == mosq_cs_new && (byte&0xF0) != CONNECT) return MOSQ_ERR_PROTOCOL;
#endif #endif
@ -418,9 +409,7 @@ int mosquitto__packet_read(struct mosquitto *mosq)
*/ */
if(mosq->in_packet.remaining_count < -4) return MOSQ_ERR_PROTOCOL; if(mosq->in_packet.remaining_count < -4) return MOSQ_ERR_PROTOCOL;
#if defined(WITH_BROKER) && defined(WITH_SYS_TREE) G_BYTES_RECEIVED_INC(1);
g_bytes_received++;
#endif
mosq->in_packet.remaining_length += (byte & 127) * mosq->in_packet.remaining_mult; mosq->in_packet.remaining_length += (byte & 127) * mosq->in_packet.remaining_mult;
mosq->in_packet.remaining_mult *= 128; mosq->in_packet.remaining_mult *= 128;
}else{ }else{
@ -453,9 +442,7 @@ int mosquitto__packet_read(struct mosquitto *mosq)
while(mosq->in_packet.to_process>0){ while(mosq->in_packet.to_process>0){
read_length = mosquitto__net_read(mosq, &(mosq->in_packet.payload[mosq->in_packet.pos]), mosq->in_packet.to_process); read_length = mosquitto__net_read(mosq, &(mosq->in_packet.payload[mosq->in_packet.pos]), mosq->in_packet.to_process);
if(read_length > 0){ if(read_length > 0){
#if defined(WITH_BROKER) && defined(WITH_SYS_TREE) G_BYTES_RECEIVED_INC(read_length);
g_bytes_received += read_length;
#endif
mosq->in_packet.to_process -= read_length; mosq->in_packet.to_process -= read_length;
mosq->in_packet.pos += read_length; mosq->in_packet.pos += read_length;
}else{ }else{
@ -488,12 +475,10 @@ int mosquitto__packet_read(struct mosquitto *mosq)
/* All data for this packet is read. */ /* All data for this packet is read. */
mosq->in_packet.pos = 0; mosq->in_packet.pos = 0;
#ifdef WITH_BROKER #ifdef WITH_BROKER
# ifdef WITH_SYS_TREE G_MSGS_RECEIVED_INC(1);
g_msgs_received++;
if(((mosq->in_packet.command)&0xF5) == PUBLISH){ if(((mosq->in_packet.command)&0xF5) == PUBLISH){
g_pub_msgs_received++; G_PUB_MSGS_RECEIVED_INC(1);
} }
# endif
rc = mqtt3_packet_handle(db, mosq); rc = mqtt3_packet_handle(db, mosq);
#else #else
rc = mosquitto__packet_handle(mosq); rc = mosquitto__packet_handle(mosq);

@ -30,10 +30,10 @@ Contributors:
#include "util_mosq.h" #include "util_mosq.h"
#ifdef WITH_BROKER #ifdef WITH_BROKER
#include "mosquitto_broker.h" # include "mosquitto_broker.h"
# ifdef WITH_SYS_TREE # include "sys_tree.h"
extern uint64_t g_pub_bytes_sent; #else
# endif # define G_PUB_BYTES_SENT_INC(A)
#endif #endif
int mosquitto__send_pingreq(struct mosquitto *mosq) int mosquitto__send_pingreq(struct mosquitto *mosq)
@ -155,9 +155,7 @@ int mosquitto__send_publish(struct mosquitto *mosq, uint16_t mid, const char *to
mapped_topic = topic_temp; mapped_topic = topic_temp;
} }
mosquitto__log_printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBLISH to %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", mosq->id, dup, qos, retain, mid, mapped_topic, (long)payloadlen); mosquitto__log_printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBLISH to %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", mosq->id, dup, qos, retain, mid, mapped_topic, (long)payloadlen);
#ifdef WITH_SYS_TREE G_PUB_BYTES_SENT_INC(payloadlen);
g_pub_bytes_sent += payloadlen;
#endif
rc = mosquitto__send_real_publish(mosq, mid, mapped_topic, payloadlen, payload, qos, retain, dup); rc = mosquitto__send_real_publish(mosq, mid, mapped_topic, payloadlen, payload, qos, retain, dup);
mosquitto__free(mapped_topic); mosquitto__free(mapped_topic);
return rc; return rc;
@ -167,9 +165,7 @@ int mosquitto__send_publish(struct mosquitto *mosq, uint16_t mid, const char *to
} }
#endif #endif
mosquitto__log_printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBLISH to %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", mosq->id, dup, qos, retain, mid, topic, (long)payloadlen); mosquitto__log_printf(NULL, MOSQ_LOG_DEBUG, "Sending PUBLISH to %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", mosq->id, dup, qos, retain, mid, topic, (long)payloadlen);
# ifdef WITH_SYS_TREE G_PUB_BYTES_SENT_INC(payloadlen);
g_pub_bytes_sent += payloadlen;
# endif
#else #else
mosquitto__log_printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBLISH (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", mosq->id, dup, qos, retain, mid, topic, (long)payloadlen); mosquitto__log_printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending PUBLISH (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", mosq->id, dup, qos, retain, mid, topic, (long)payloadlen);
#endif #endif

@ -22,13 +22,11 @@ Contributors:
#include "mosquitto_broker.h" #include "mosquitto_broker.h"
#include "memory_mosq.h" #include "memory_mosq.h"
#include "send_mosq.h" #include "send_mosq.h"
#include "sys_tree.h"
#include "time_mosq.h" #include "time_mosq.h"
static int max_inflight = 20; static int max_inflight = 20;
static int max_queued = 100; static int max_queued = 100;
#ifdef WITH_SYS_TREE
extern unsigned long g_msgs_dropped;
#endif
int mqtt3_db_open(struct mqtt3_config *config, struct mosquitto_db *db) int mqtt3_db_open(struct mqtt3_config *config, struct mosquitto_db *db)
{ {
@ -359,16 +357,12 @@ int mqtt3_db_message_insert(struct mosquitto_db *db, struct mosquitto *context,
"Outgoing messages are being dropped for client %s.", "Outgoing messages are being dropped for client %s.",
context->id); context->id);
} }
#ifdef WITH_SYS_TREE G_MSGS_DROPPED_INC();
g_msgs_dropped++;
#endif
return 2; return 2;
} }
}else{ }else{
if(max_queued > 0 && context->msg_count12 >= max_queued){ if(max_queued > 0 && context->msg_count12 >= max_queued){
#ifdef WITH_SYS_TREE G_MSGS_DROPPED_INC();
g_msgs_dropped++;
#endif
if(context->is_dropping == false){ if(context->is_dropping == false){
context->is_dropping = true; context->is_dropping = true;
mosquitto__log_printf(NULL, MOSQ_LOG_NOTICE, mosquitto__log_printf(NULL, MOSQ_LOG_NOTICE,

@ -44,6 +44,7 @@ Contributors:
#include "memory_mosq.h" #include "memory_mosq.h"
#include "packet_mosq.h" #include "packet_mosq.h"
#include "send_mosq.h" #include "send_mosq.h"
#include "sys_tree.h"
#include "time_mosq.h" #include "time_mosq.h"
#include "util_mosq.h" #include "util_mosq.h"
@ -53,9 +54,6 @@ extern bool flag_db_backup;
#endif #endif
extern bool flag_tree_print; extern bool flag_tree_print;
extern int run; extern int run;
#ifdef WITH_SYS_TREE
extern int g_clients_expired;
#endif
static void loop_handle_errors(struct mosquitto_db *db, struct pollfd *pollfds); static void loop_handle_errors(struct mosquitto_db *db, struct pollfd *pollfds);
static void loop_handle_reads_writes(struct mosquitto_db *db, struct pollfd *pollfds); static void loop_handle_reads_writes(struct mosquitto_db *db, struct pollfd *pollfds);
@ -296,9 +294,7 @@ int mosquitto_main_loop(struct mosquitto_db *db, int *listensock, int listensock
id = "<unknown>"; id = "<unknown>";
} }
mosquitto__log_printf(NULL, MOSQ_LOG_NOTICE, "Expiring persistent client %s due to timeout.", id); mosquitto__log_printf(NULL, MOSQ_LOG_NOTICE, "Expiring persistent client %s due to timeout.", id);
#ifdef WITH_SYS_TREE G_CLIENTS_EXPIRED_INC();
g_clients_expired++;
#endif
context->clean_session = true; context->clean_session = true;
context->state = mosq_cs_expiring; context->state = mosq_cs_expiring;
do_disconnect(db, context); do_disconnect(db, context);

@ -59,9 +59,7 @@ static int tls_ex_index_context = -1;
static int tls_ex_index_listener = -1; static int tls_ex_index_listener = -1;
#endif #endif
#ifdef WITH_SYS_TREE #include "sys_tree.h"
extern unsigned int g_socket_connections;
#endif
int mqtt3_socket_accept(struct mosquitto_db *db, int listensock) int mqtt3_socket_accept(struct mosquitto_db *db, int listensock)
{ {
@ -83,9 +81,7 @@ int mqtt3_socket_accept(struct mosquitto_db *db, int listensock)
new_sock = accept(listensock, NULL, 0); new_sock = accept(listensock, NULL, 0);
if(new_sock == INVALID_SOCKET) return -1; if(new_sock == INVALID_SOCKET) return -1;
#ifdef WITH_SYS_TREE G_SOCKET_CONNECTIONS_INC();
g_socket_connections++;
#endif
if(mosquitto__socket_nonblock(new_sock)){ if(mosquitto__socket_nonblock(new_sock)){
return INVALID_SOCKET; return INVALID_SOCKET;

@ -26,12 +26,9 @@ Contributors:
#include "packet_mosq.h" #include "packet_mosq.h"
#include "read_handle.h" #include "read_handle.h"
#include "send_mosq.h" #include "send_mosq.h"
#include "sys_tree.h"
#include "util_mosq.h" #include "util_mosq.h"
#ifdef WITH_SYS_TREE
extern uint64_t g_pub_bytes_received;
#endif
int mqtt3_packet_handle(struct mosquitto_db *db, struct mosquitto *context) int mqtt3_packet_handle(struct mosquitto_db *db, struct mosquitto *context)
{ {
if(!context) return MOSQ_ERR_INVAL; if(!context) return MOSQ_ERR_INVAL;
@ -170,9 +167,7 @@ int mqtt3_handle_publish(struct mosquitto_db *db, struct mosquitto *context)
} }
payloadlen = context->in_packet.remaining_length - context->in_packet.pos; payloadlen = context->in_packet.remaining_length - context->in_packet.pos;
#ifdef WITH_SYS_TREE G_PUB_BYTES_RECEIVED_INC(payloadlen);
g_pub_bytes_received += payloadlen;
#endif
if(context->listener && context->listener->mount_point){ if(context->listener && context->listener->mount_point){
len = strlen(context->listener->mount_point) + strlen(topic) + 1; len = strlen(context->listener->mount_point) + strlen(topic) + 1;
topic_mount = mosquitto__malloc(len+1); topic_mount = mosquitto__malloc(len+1);

@ -17,13 +17,14 @@ Contributors:
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "config.h> #include "config.h"
#include "mosquitto_broker.h" #include "mosquitto_broker.h"
#include "mqtt3_protocol.h" #include "mqtt3_protocol.h"
#include "memory_mosq.h" #include "memory_mosq.h"
#include "packet_mosq.h" #include "packet_mosq.h"
#include "send_mosq.h" #include "send_mosq.h"
#include "sys_tree.h"
#include "time_mosq.h" #include "time_mosq.h"
#include "tls_mosq.h" #include "tls_mosq.h"
#include "util_mosq.h" #include "util_mosq.h"
@ -36,10 +37,6 @@ Contributors:
#include <libwebsockets.h> #include <libwebsockets.h>
#endif #endif
#ifdef WITH_SYS_TREE
extern unsigned int g_connection_count;
#endif
static char *client_id_gen(struct mosquitto_db *db) static char *client_id_gen(struct mosquitto_db *db)
{ {
char *client_id; char *client_id;
@ -101,9 +98,7 @@ int mqtt3_handle_connect(struct mosquitto_db *db, struct mosquitto *context)
X509_NAME_ENTRY *name_entry; X509_NAME_ENTRY *name_entry;
#endif #endif
#ifdef WITH_SYS_TREE G_CONNECTION_COUNT_INC();
g_connection_count++;
#endif
/* Don't accept multiple CONNECT commands. */ /* Don't accept multiple CONNECT commands. */
if(context->state != mosq_cs_new){ if(context->state != mosq_cs_new){

@ -34,19 +34,12 @@ POSSIBILITY OF SUCH DAMAGE.
#include "mosquitto_broker.h" #include "mosquitto_broker.h"
#include "mqtt3_protocol.h" #include "mqtt3_protocol.h"
#include "memory_mosq.h" #include "memory_mosq.h"
#include "sys_tree.h"
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <sys/stat.h> #include <sys/stat.h>
#ifdef WITH_SYS_TREE
extern uint64_t g_bytes_received;
extern uint64_t g_bytes_sent;
extern unsigned long g_msgs_received;
extern unsigned long g_msgs_sent;
extern unsigned long g_pub_msgs_received;
extern unsigned long g_pub_msgs_sent;
#endif
extern struct mosquitto_db int_db; extern struct mosquitto_db int_db;
static int callback_mqtt(struct libwebsocket_context *context, static int callback_mqtt(struct libwebsocket_context *context,
@ -258,9 +251,7 @@ static int callback_mqtt(struct libwebsocket_context *context,
mosq = u->mosq; mosq = u->mosq;
pos = 0; pos = 0;
buf = (uint8_t *)in; buf = (uint8_t *)in;
#ifdef WITH_SYS_TREE G_BYTES_RECEIVED_INC(len);
g_bytes_received += len;
#endif
while(pos < len){ while(pos < len){
if(!mosq->in_packet.command){ if(!mosq->in_packet.command){
mosq->in_packet.command = buf[pos]; mosq->in_packet.command = buf[pos];
@ -315,10 +306,11 @@ static int callback_mqtt(struct libwebsocket_context *context,
/* All data for this packet is read. */ /* All data for this packet is read. */
mosq->in_packet.pos = 0; mosq->in_packet.pos = 0;
#ifdef WITH_SYS_TREE #ifdef WITH_SYS_TREE
g_msgs_received++; G_MSGS_RECEIVED_INC();
if(((mosq->in_packet.command)&0xF5) == PUBLISH){ if(((mosq->in_packet.command)&0xF5) == PUBLISH){
g_pub_msgs_received++; G_PUB_MSGS_RECEIVED_INC();
} }
#endif #endif
rc = mqtt3_packet_handle(db, mosq); rc = mqtt3_packet_handle(db, mosq);

Loading…
Cancel
Save