libubus: add logic to reduce msgbuf data size after 16 small messages
authorAlexandru Ardelean <ardeleanalex@gmail.com>
Fri, 27 Jun 2014 16:11:41 +0000 (19:11 +0300)
committerFelix Fietkau <nbd@openwrt.org>
Thu, 3 Jul 2014 10:45:13 +0000 (12:45 +0200)
libubus-io.c
libubus.h

index 48bb72d..f38403f 100644 (file)
@@ -28,6 +28,8 @@
 
 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
 
 
 #define STATIC_IOV(_var) { .iov_base = (char *) &(_var), .iov_len = sizeof(_var) }
 
+#define UBUS_MSGBUF_REDUCTION_INTERVAL 16
+
 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
        [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
        [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
 static const struct blob_attr_info ubus_policy[UBUS_ATTR_MAX] = {
        [UBUS_ATTR_STATUS] = { .type = BLOB_ATTR_INT32 },
        [UBUS_ATTR_OBJID] = { .type = BLOB_ATTR_INT32 },
@@ -250,6 +252,17 @@ static bool get_next_msg(struct ubus_context *ctx, int *recv_fd)
 
        len = blob_raw_len(&hdrbuf.data);
        if (len > ctx->msgbuf_data_len) {
 
        len = blob_raw_len(&hdrbuf.data);
        if (len > ctx->msgbuf_data_len) {
+               ctx->msgbuf_reduction_counter = UBUS_MSGBUF_REDUCTION_INTERVAL;
+       } else if (ctx->msgbuf_data_len > UBUS_MSG_CHUNK_SIZE) {
+               if (ctx->msgbuf_reduction_counter > 0) {
+                       len = -1;
+                       --ctx->msgbuf_reduction_counter;
+               } else
+                       len = UBUS_MSG_CHUNK_SIZE;
+       } else
+               len = -1;
+
+       if (len > -1) {
                ctx->msgbuf.data = realloc(ctx->msgbuf.data, len * sizeof(char));
                if (ctx->msgbuf.data)
                        ctx->msgbuf_data_len = len;
                ctx->msgbuf.data = realloc(ctx->msgbuf.data, len * sizeof(char));
                if (ctx->msgbuf.data)
                        ctx->msgbuf_data_len = len;
index 78ffa38..a63ce31 100644 (file)
--- a/libubus.h
+++ b/libubus.h
@@ -156,6 +156,7 @@ struct ubus_context {
 
        struct ubus_msghdr_buf msgbuf;
        uint32_t msgbuf_data_len;
 
        struct ubus_msghdr_buf msgbuf;
        uint32_t msgbuf_data_len;
+       int msgbuf_reduction_counter;
 };
 
 struct ubus_object_data {
 };
 
 struct ubus_object_data {