Permalink
Browse files

Added telemetry class, telemetry messages to stream monitor.

  • Loading branch information...
1 parent e5936c5 commit b9515a209672ee7399ebbc6fa16a5b11ffd1e8cd @millenomi committed Jul 25, 2010
Showing with 281 additions and 22 deletions.
  1. +41 −19 ILStreamMonitor.cpp
  2. +5 −3 ILStreamMonitor.h
  3. +65 −0 ILTelemetry.cpp
  4. +60 −0 ILTelemetry.h
  5. +36 −0 ILTelemetryStdoutLogger.cpp
  6. +38 −0 ILTelemetryStdoutLogger.h
  7. +36 −0 MoverCore.xcodeproj/project.pbxproj
View
@@ -10,6 +10,7 @@
#include "ILStreamMonitor.h"
#include "ILMessageHub.h"
+#include "ILTelemetry.h"
ILUniqueConstant(kILStreamReadyForReadingMessage);
ILUniqueConstant(kILStreamReadyForWritingMessage);
@@ -30,6 +31,7 @@ struct ILStreamMonitorImpl {
uint64_t currentThreadID;
bool hasPendingReadEvent, hasPendingWriteEvent, hasPendingErrorEvent;
+ bool hasActiveReadEvent, hasActiveWriteEvent, hasActiveErrorEvent;
void init(ILStreamMonitor* me, ILStream* s) {
self = me;
@@ -42,6 +44,7 @@ struct ILStreamMonitorImpl {
mutex = ILRetain(new ILMutex());
hasPendingReadEvent = hasPendingWriteEvent = hasPendingErrorEvent = false;
+ hasActiveReadEvent = hasActiveWriteEvent = hasActiveErrorEvent = false;
currentThreadID = 0;
}
@@ -135,6 +138,8 @@ void ILStreamMonitoringThread(ILObject* o) {
ILStreamMonitor* self = arguments->at<ILStreamMonitor>(kILStreamMonitorObjectKey);
uint64_t threadID = arguments->at<ILNumber>(kILStreamMonitorThreadIDKey)->unsignedIntegerValue();
+ ILCEvent(ILStr("Will start monitoring with stream monitor %p, stream %p (thread ID %llu)"), self, self->stream(), (unsigned long long) threadID);
+
int fd = self->_i->stream->fileDescriptor();
if (fd < 0)
return;
@@ -157,15 +162,23 @@ void ILStreamMonitoringThread(ILObject* o) {
tv.tv_usec = 0;
int result = select(fd + 1, &readingSet, &writingSet, &errorSet, &tv);
- if (result < 0)
+ if (result < 0) {
+ ILCEvent(ILStr("select() had an error, interrupting monitoring."));
+ self->_i->signalEvents(threadID, false, false, true);
return;
+ }
- bool canRead = FD_ISSET(fd, &readingSet),
+ bool canRead = result > 0 && FD_ISSET(fd, &readingSet),
canWrite = FD_ISSET(fd, &writingSet),
hasError = FD_ISSET(fd, &errorSet);
- if (!self->_i->signalEvents(threadID, canRead, canWrite, hasError))
+ ILCEvent(ILStr("Has spun once for select() with results read = %d, write = %d, error = %d"), canRead, canWrite, hasError);
+
+ if (!self->_i->signalEvents(threadID, canRead, canWrite, hasError)) {
+ ILCEvent(ILStr("Check-in reports we have to stop monitoring, returning"));
return;
+ } else
+ ILCEvent(ILStr("Checked-in successfully, spinning again."));
}
}
@@ -175,9 +188,9 @@ bool ILStreamMonitorImpl::signalEvents(uint64_t threadID, bool read, bool write,
if (threadID != currentThreadID)
return false;
- hasPendingReadEvent = hasPendingReadEvent || read;
- hasPendingWriteEvent = hasPendingWriteEvent || write;
- hasPendingErrorEvent = hasPendingErrorEvent ||error;
+ hasPendingReadEvent = read;
+ hasPendingWriteEvent = write;
+ hasPendingErrorEvent = error;
ILRunLoop* r = self->runLoop();
if (r)
@@ -191,31 +204,34 @@ void ILStreamMonitor::spin() {
ILRunLoop* r = this->runLoop();
if (_i->hasPendingReadEvent) {
- _i->hasPendingReadEvent = false;
-
- if (r) {
+ if (r && !_i->hasActiveReadEvent) {
+ ILEvent(kILStreamMonitorTelemetrySource, ILStr("Will send ready-for-reading message."));
+ _i->hasActiveReadEvent = true;
ILMessage* m = new ILMessage(kILStreamReadyForReadingMessage, this, NULL);
r->currentMessageHub()->deliverMessage(m);
}
- }
+ } else
+ _i->hasActiveReadEvent = false;
if (_i->hasPendingWriteEvent) {
- _i->hasPendingWriteEvent = false;
-
- if (r) {
+ if (r && !_i->hasActiveWriteEvent) {
+ ILEvent(kILStreamMonitorTelemetrySource, ILStr("Will send ready-for-writing message."));
+ _i->hasActiveWriteEvent = true;
ILMessage* m = new ILMessage(kILStreamReadyForWritingMessage, this, NULL);
r->currentMessageHub()->deliverMessage(m);
}
- }
-
+ } else
+ _i->hasActiveWriteEvent = false;
+
if (_i->hasPendingErrorEvent) {
- _i->hasPendingErrorEvent = false;
-
- if (r) {
+ if (r && !_i->hasActiveErrorEvent) {
+ ILEvent(kILStreamMonitorTelemetrySource, ILStr("Will send error-occurred message."));
+ _i->hasActiveErrorEvent = true;
ILMessage* m = new ILMessage(kILStreamErrorOccurredMessage, this, NULL);
r->currentMessageHub()->deliverMessage(m);
}
- }
+ } else
+ _i->hasActiveErrorEvent = false;
}
ILRunLoop* ILStreamMonitor::runLoop() {
@@ -227,3 +243,9 @@ void ILStreamMonitor::setRunLoop(ILRunLoop* r) {
ILAcquiredMutex m(_i->mutex);
ILSource::setRunLoop(r);
}
+
+ILUniqueConstant(kILStreamMonitorClassIdentity);
+
+void* ILStreamMonitor::classIdentity() {
+ return kILStreamMonitorClassIdentity;
+}
View
@@ -17,7 +17,7 @@
struct ILStreamMonitorImpl;
-
+#define kILStreamMonitorTelemetrySource ILStr("ILStreamMonitor")
// ~ ILStreamMonitor ~
@@ -40,13 +40,15 @@ class ILStreamMonitor : public ILSource {
virtual ILRunLoop* runLoop();
virtual void setRunLoop(ILRunLoop* r);
+ virtual void* classIdentity();
+
private:
friend void ILStreamMonitoringThread(ILObject* o);
friend class ILStreamMonitorImpl;
ILStreamMonitorImpl* _i;
};
+extern void* kILStreamMonitorClassIdentity;
-
-#endif // #ifndef ILStreamMonitor_H
+#endif // #ifndef ILStreamMonitor_H
View
@@ -0,0 +1,65 @@
+/*
+ * ILTelemetry.cpp
+ * MoverCore
+ *
+ * Created by ∞ on 25/07/10.
+ * Copyright 2010 __MyCompanyName__. All rights reserved.
+ *
+ */
+
+#include "ILTelemetry.h"
+#include "ILTelemetryStdoutLogger.h"
+
+#include <cstring>
+
+struct ILTelemetryImpl {
+ ILTelemetryDelegate* delegate;
+ bool on;
+};
+
+ILTelemetry::ILTelemetry() : ILObject() {
+ _i = new ILTelemetryImpl;
+ _i->delegate = NULL;
+ _i->on = false;
+
+ setDelegate(new ILTelemetryStdoutLogger());
+
+ char* isOn = getenv("ILTelemetryIsOn");
+ if (isOn && strcmp(isOn, "YES") == 0)
+ setOn(true);
+}
+
+ILTelemetry::~ILTelemetry() {
+ ILRelease(_i->delegate);
+
+ delete _i;
+}
+
+ILUniqueConstant(kILTelemetryClassIdentity);
+
+void* ILTelemetry::classIdentity() {
+ return kILTelemetryClassIdentity;
+}
+
+ILRetainGetterSetterImpl(ILTelemetry,
+ ILTelemetryDelegate*, _i->delegate,
+ delegate, setDelegate)
+ILAssignGetterSetterImpl(ILTelemetry,
+ bool, _i->on,
+ on, setOn)
+
+void ILTelemetry::log(ILString* content, ILString* eventSourceKind, ILObject* source) {
+ if (delegate() && on())
+ delegate()->telemetryDidLogEvent(content, eventSourceKind, source);
+}
+
+ILTelemetry* ILTelemetry::sharedTelemetry() {
+ static ILTelemetry* me = NULL; if (!me)
+ me = ILRetain(new ILTelemetry());
+
+ return me;
+}
+
+
+ILTelemetryDelegate::ILTelemetryDelegate() {}
+ILTelemetryDelegate::~ILTelemetryDelegate() {}
View
@@ -0,0 +1,60 @@
+/*
+ * ILTelemetry.h
+ * MoverCore
+ *
+ * Created by ∞ on 25/07/10.
+ * Copyright 2010 __MyCompanyName__. All rights reserved.
+ *
+ */
+
+#ifndef ILTelemetry_H
+#define ILTelemetry_H 1
+
+#include "PlatformCore.h"
+
+struct ILTelemetryImpl;
+
+#define ILEvent(s, x, ...) do { \
+ ILTelemetry::sharedTelemetry()->log(ILString::stringWithFormat(x, ## __VA_ARGS__), s, this); \
+ } while (0)
+
+#define ILCEvent(x, ...) do { \
+ ILTelemetry::sharedTelemetry()->log(ILString::stringWithFormat(x, ## __VA_ARGS__), ILStr(__PRETTY_FUNCTION__), NULL); \
+ } while (0)
+
+
+
+// ~ ILTelemetry ~
+
+class ILTelemetryDelegate : public ILObject {
+public:
+ ILTelemetryDelegate(); virtual ~ILTelemetryDelegate();
+ virtual void telemetryDidLogEvent(ILString* content, ILString* eventSourceKind, ILObject* source) = 0;
+};
+
+
+class ILTelemetry : public ILObject {
+public:
+ ILTelemetry(); virtual ~ILTelemetry();
+
+ static ILTelemetry* sharedTelemetry();
+
+ // ---
+
+ ILProperty(ILTelemetryDelegate*, delegate, setDelegate);
+ ILProperty(bool, on, setOn);
+ void log(ILString* content, ILString* eventSourceKind, ILObject* source);
+
+ // ---
+
+ virtual void* classIdentity();
+
+private:
+ friend class ILTelemetryImpl;
+ ILTelemetryImpl* _i;
+};
+
+extern void* kILTelemetryClassIdentity;
+
+
+#endif // #ifndef ILTelemetry_H
@@ -0,0 +1,36 @@
+/*
+ * ILTelemetryStdoutLogger.cpp
+ * MoverCore
+ *
+ * Created by ∞ on 25/07/10.
+ * Copyright 2010 __MyCompanyName__. All rights reserved.
+ *
+ */
+
+#include "ILTelemetryStdoutLogger.h"
+
+
+struct ILTelemetryStdoutLoggerImpl {
+};
+
+ILTelemetryStdoutLogger::ILTelemetryStdoutLogger() : ILTelemetryDelegate() {
+ _i = new ILTelemetryStdoutLoggerImpl;
+}
+
+ILTelemetryStdoutLogger::~ILTelemetryStdoutLogger() {
+ delete _i;
+}
+
+ILUniqueConstant(kILTelemetryStdoutLoggerClassIdentity);
+
+void* ILTelemetryStdoutLogger::classIdentity() {
+ return kILTelemetryStdoutLoggerClassIdentity;
+}
+
+
+void ILTelemetryStdoutLogger::telemetryDidLogEvent(ILString* content, ILString* eventSourceKind, ILObject* source) {
+
+ fprintf(stdout, "%s - %p:\n\t%s\n", eventSourceKind->UTF8String(), source, content->UTF8String());
+ fflush(stdout);
+
+}
@@ -0,0 +1,38 @@
+/*
+ * ILTelemetryStdoutLogger.h
+ * MoverCore
+ *
+ * Created by ∞ on 25/07/10.
+ * Copyright 2010 __MyCompanyName__. All rights reserved.
+ *
+ */
+
+#ifndef ILTelemetryStdoutLogger_H
+#define ILTelemetryStdoutLogger_H 1
+
+#include "PlatformCore.h"
+#include "ILTelemetry.h"
+struct ILTelemetryStdoutLoggerImpl;
+
+
+
+// ~ ILTelemetryStdoutLogger ~
+
+class ILTelemetryStdoutLogger : public ILTelemetryDelegate {
+public:
+ ILTelemetryStdoutLogger(); virtual ~ILTelemetryStdoutLogger();
+
+
+ virtual void telemetryDidLogEvent(ILString* content, ILString* eventSourceKind, ILObject* source);
+
+ virtual void* classIdentity();
+
+private:
+ friend class ILTelemetryStdoutLoggerImpl;
+ ILTelemetryStdoutLoggerImpl* _i;
+};
+
+extern void* kILTelemetryStdoutLoggerClassIdentity;
+
+
+#endif // #ifndef ILTelemetryStdoutLogger_H
Oops, something went wrong.

0 comments on commit b9515a2

Please sign in to comment.