From 906916e83fa2b19829e8a26b78b61714c1c1c7ee Mon Sep 17 00:00:00 2001 From: alexhudson Date: Tue, 24 Nov 2009 19:56:54 +0000 Subject: [PATCH] Slightly untested initial ringlog. --- src/agents/store/CMakeLists.txt | 1 + src/agents/store/command.c | 10 ++++- src/agents/store/ringlog.c | 67 +++++++++++++++++++++++++++++++++ src/agents/store/stored.c | 24 ++++++++++-- src/agents/store/stored.h | 6 +++ 5 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 src/agents/store/ringlog.c diff --git a/src/agents/store/CMakeLists.txt b/src/agents/store/CMakeLists.txt index ad30068..4a607d6 100644 --- a/src/agents/store/CMakeLists.txt +++ b/src/agents/store/CMakeLists.txt @@ -22,6 +22,7 @@ add_executable(bongostore properties.c query-builder.c query-parser.c + ringlog.c search.c store.c stored.c diff --git a/src/agents/store/command.c b/src/agents/store/command.c index 8cef2cc..3510cd4 100644 --- a/src/agents/store/command.c +++ b/src/agents/store/command.c @@ -249,7 +249,10 @@ StoreCommandLoop(StoreClient *client) if (-1 == ccode || ccode >= CONN_BUFSIZE) { break; } - + char command_message[256]; + snprintf(command_message, 255, "-> %s", client->buffer); + Ringlog(command_message); + memset(tokens, 0, sizeof(tokens)); n = CommandSplit(client->buffer, tokens, TOK_ARR_SZ); if (0 == n) { @@ -1318,8 +1321,13 @@ StoreCommandLoop(StoreClient *client) if (ccode >= 0) { ccode = ConnFlush(client->conn); } + + command_message[0] = '<'; + command_message[1] = '-'; + Ringlog(command_message); } + return ccode; } diff --git a/src/agents/store/ringlog.c b/src/agents/store/ringlog.c new file mode 100644 index 0000000..dd6d15b --- /dev/null +++ b/src/agents/store/ringlog.c @@ -0,0 +1,67 @@ +#include "stored.h" +#include "messages.h" +#include "lock.h" + +/* ring log for debugging purposes */ + +typedef struct { + time_t timestamp; + char message[256]; +} RingLogItem; + +#define RINGLOG_SIZE 200 + +static RingLogItem ringlog[RINGLOG_SIZE]; +// ringlog_pos points to the next "free" ringlog entry. +static int ringlog_pos = 0; +static XplMutex ringlog_lock; + +void +RinglogInit() +{ + // initialise lock; this must be held to access the ringlog. + XplMutexInit(ringlog_lock); + + // clear the ringlog. + memset(ringlog, 0, sizeof(ringlog)); +} + +void +Ringlog(char *message) +{ + XplMutexLock(ringlog_lock); + + // find the next ring log entry and write into it + ringlog[ringlog_pos].timestamp = time(NULL); + strncpy(ringlog[ringlog_pos].message, message, 255); + + // advance the ring pointer, and wrap around if necessary + ringlog_pos++; + if (ringlog_pos == RINGLOG_SIZE) ringlog_pos = 0; + + XplMutexUnlock(ringlog_lock); +} + +void +RinglogDumpFilehandle(int fh) +{ + XplMutexLock(ringlog_lock); + + for (int i = ringlog_pos; i < ringlog_pos + RINGLOG_SIZE; i++) { + // print the next ring log entry - wraps by cunning use of modulo + char message[300]; + size_t len = snprintf(message, 299, "%20s %s\n", + ctime(&(ringlog[i % RINGLOG_SIZE].timestamp)), + ringlog[i % RINGLOG_SIZE].message); + write(fh, message, len); + } + + XplMutexUnlock(ringlog_lock); +} + +void +RinglogDumpConsole() +{ + // hope we still have STDOUT around?! + RinglogDumpFilehandle(1); +} diff --git a/src/agents/store/stored.c b/src/agents/store/stored.c index 945f813..52e9f44 100644 --- a/src/agents/store/stored.c +++ b/src/agents/store/stored.c @@ -188,7 +188,18 @@ int _NonAppCheckUnload(void) static void SignalHandler(int sigtype) { - BongoAgentHandleSignal(&StoreAgent.agent, sigtype); + if (sigtype == SIGSEGV) { + char path[XPL_MAX_PATH + 1]; + int boomfile; + + snprintf(path, XPL_MAX_PATH, "%s/store-ringlog-%d", XPL_DEFAULT_WORK_DIR, (int)time(NULL)); + boomfile = open(path, O_CREAT | O_WRONLY); + if (boomfile != -1) { + RinglogDumpFilehandle(boomfile); + close(boomfile); + } + } + BongoAgentHandleSignal(&StoreAgent.agent, sigtype); } @@ -207,6 +218,8 @@ _XplServiceMain(int argc, char *argv[]) int startupOpts; LogStart(); + RinglogInit(); + Ringlog("Started store daemon"); if (XplSetEffectiveUser(MsgGetUnprivilegedUser()) < 0) { Log(LOG_ERROR, "Could not drop to unprivileged user '%s'", MsgGetUnprivilegedUser()); @@ -229,7 +242,9 @@ _XplServiceMain(int argc, char *argv[]) Log(LOG_FATAL, "Couldn't initialize Bongo libraries"); return -1; } - + + Ringlog("Initialising libraries"); + MsgInit(); cal_success = BongoCalInit(MsgGetDir(MSGAPI_DIR_DBF, NULL, 0)); @@ -263,7 +278,7 @@ _XplServiceMain(int argc, char *argv[]) } /* Create a thread pool for managing connections */ - + Ringlog("Initialising threads"); BongoQueueAgentGetThreadPoolParameters (&StoreAgent.agent, &minThreads, &maxThreads, &minSleep); @@ -298,12 +313,15 @@ _XplServiceMain(int argc, char *argv[]) /* Start the server thread */ MsgSetRecoveryFlag("store"); + Ringlog("Starting main thread"); XplStartMainThread(AGENT_NAME, &id, StoreServer, 8192, NULL, ccode); + Ringlog("Stopping main thread"); XplUnloadApp(XplGetThreadID()); MsgClearRecoveryFlag("store"); g_mime_shutdown(); + Ringlog("Shutdown complete"); return 0; } diff --git a/src/agents/store/stored.h b/src/agents/store/stored.h index 88aad4b..6358921 100644 --- a/src/agents/store/stored.h +++ b/src/agents/store/stored.h @@ -412,6 +412,12 @@ int MimeGetGuid(StoreClient *client, uint64_t guid, MimeReport **outReport); /** config.c **/ BOOL StoreAgentReadConfiguration(BOOL *recover); +/** ringlog.c **/ +void RinglogInit(); +void Ringlog(char *message); +void RinglogDumpConsole(); +void RinglogDumpFilehandle(int fh); + /** locking.c **/ typedef enum { LLOCK_NONE,