make the ringbuffer logging more efficient and marshall the data by writing to a...
authorRonnie Sahlberg <ronniesahlberg@gmail.com>
Wed, 18 Nov 2009 08:10:50 +0000 (19:10 +1100)
committerRonnie Sahlberg <ronniesahlberg@gmail.com>
Wed, 18 Nov 2009 08:10:50 +0000 (19:10 +1100)
common/ctdb_logging.c
include/ctdb_private.h
tools/ctdb.c

index 3183fe89311b08b5a92ca85fa32e4cef880d5430..efb47c6d9a959a7c13a8ac80703ec799b61a6760 100644 (file)
@@ -20,6 +20,7 @@
 #include "includes.h"
 #include "lib/events/events.h"
 #include "lib/tdb/include/tdb.h"
+#include "system/time.h"
 #include "../include/ctdb_private.h"
 #include "../include/ctdb.h"
 
@@ -82,49 +83,59 @@ void log_ringbuffer(const char *format, ...)
 
 static void ctdb_collect_log(struct ctdb_context *ctdb, struct ctdb_get_log_addr *log_addr)
 {
-       char *buf = talloc_size(NULL, 0);
-       struct ctdb_log_entry_wire *log_entry;
-       uint32_t old_size, len;
        TDB_DATA data;
+       FILE *f;
+       long fsize;
+       int tmp_entry;
+       int count = 0;
+       DEBUG(DEBUG_ERR,("Marshalling log entries  first:%d last:%d\n", first_entry, last_entry));
+
+       /* dump to a file, then send the file as a blob */
+       f = tmpfile();
+       if (f == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " Unable to open tmpfile - %s\n", strerror(errno)));
+               return;
+       }
 
-       DEBUG(DEBUG_INFO,("Marshalling log entries\n"));
-       while (first_entry != last_entry) {
-               int slen = strlen(log_entries[first_entry].message);
+       tmp_entry = first_entry;
+       while (tmp_entry != last_entry) {
+               struct tm *tm;
+               char tbuf[100];
 
-               if (log_entries[first_entry].level > log_addr->level) {
-                       first_entry++;
-                       if (first_entry >= MAX_LOG_ENTRIES) {
-                               first_entry = 0;
+               if (log_entries[tmp_entry].level > log_addr->level) {
+                       tmp_entry++;
+                       if (tmp_entry >= MAX_LOG_ENTRIES) {
+                               tmp_entry = 0;
                        }
-                       continue;
+                       continue;
                }
 
-               len = offsetof(struct ctdb_log_entry_wire, message) + slen + 1;
-               /* pad it to uint42 */
-               len = (len+3)&0xfffffffc;
+               tm = localtime(&log_entries[tmp_entry].t.tv_sec);
+               strftime(tbuf, sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
 
-               old_size = talloc_get_size(buf);
-               buf = talloc_realloc_size(NULL, buf, old_size + len);
-
-               log_entry = (struct ctdb_log_entry_wire *)&buf[old_size];
-               log_entry->level       = log_entries[first_entry].level;
-               log_entry->t           = log_entries[first_entry].t;
-               log_entry->message_len = slen;
-               memcpy(log_entry->message, log_entries[first_entry].message, slen);
-               log_entry->message[slen] = 0;
+               if (log_entries[tmp_entry].message) {
+                       count += fprintf(f, "%s:%s %s", tbuf, get_debug_by_level(log_entries[tmp_entry].level), log_entries[tmp_entry].message);
+               }
 
-               first_entry++;
-               if (first_entry >= MAX_LOG_ENTRIES) {
-                       first_entry = 0;
+               tmp_entry++;
+               if (tmp_entry >= MAX_LOG_ENTRIES) {
+                       tmp_entry = 0;
                }
        }
 
-       data.dptr  = (uint8_t *)buf;
-       data.dsize = talloc_get_size(buf);
-       DEBUG(DEBUG_INFO,("Marshalling log entries into a blob of %d bytes\n", (int)data.dsize));
+       fsize = ftell(f);
+       rewind(f);
+       data.dptr = talloc_size(NULL, fsize);
+       CTDB_NO_MEMORY_VOID(ctdb, data.dptr);
+       data.dsize = fread(data.dptr, 1, fsize, f);
+       fclose(f);
+
+       DEBUG(DEBUG_ERR,("Marshalling log entries into a blob of %d bytes\n", (int)data.dsize));
 
-       DEBUG(DEBUG_INFO,("Send log to %d:%d\n", (int)log_addr->pnn, (int)log_addr->srvid));
+       DEBUG(DEBUG_ERR,("Send log to %d:%d\n", (int)log_addr->pnn, (int)log_addr->srvid));
        ctdb_send_message(ctdb, log_addr->pnn, log_addr->srvid, data);
+
+       talloc_free(data.dptr);
 }
 
 int32_t ctdb_control_get_log(struct ctdb_context *ctdb, TDB_DATA addr)
index 48fb29bd0a899e67f6cb30bc5b39d5640c12b9c6..674b0aca6fd717cea076b1b2557a9dcf047b58b9 100644 (file)
@@ -1529,14 +1529,6 @@ struct ctdb_get_log_addr {
        int32_t level;
 };
 
-/* wire data for log entries, padded to uint32 */
-struct ctdb_log_entry_wire {
-       int32_t level;
-       struct timeval t;
-       int32_t message_len;
-       char message[1];
-};
-
 int32_t ctdb_control_get_log(struct ctdb_context *ctdb, TDB_DATA addr);
 int32_t ctdb_control_clear_log(struct ctdb_context *ctdb);
 
index e89640a67d0db5003b6635e09bbf6f565d3fa611..2cc7193f6fb5f7a300114a65df2787ba4796d154 100644 (file)
@@ -2357,26 +2357,11 @@ static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid, 
                             TDB_DATA data, void *private_data)
 {
-       struct ctdb_log_entry_wire *entry;
-       int size;
-       struct tm *tm;
-       char tbuf[100];
-
        DEBUG(DEBUG_ERR,("Log data received\n"));
-       while (data.dsize > 0) {
-               entry = (struct ctdb_log_entry_wire *)data.dptr;
-               size = offsetof(struct ctdb_log_entry_wire, message) + entry->message_len + 1;
-               size = (size+3)&0xfffffffc;
-
-               tm = localtime(&entry->t.tv_sec);
-
-               strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
-
-               printf("%s:%s %s", tbuf, get_debug_by_level(entry->level), entry->message);
-
-               data.dsize -= size;
-               data.dptr  += size;
+       if (data.dsize > 0) {
+               printf("%s", data.dptr);
        }
+
        exit(0);
 }