From a44b60593ab559f66ac3e984eb89133b8ca1e260 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 5 Sep 2011 23:30:20 +0000 Subject: Improve logging with multiple threads and processes. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1165450 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/kernel/fstream.hpp | 23 ++++++++++++++++++++--- sca-cpp/trunk/kernel/parallel-test.cpp | 20 ++++++++------------ sca-cpp/trunk/kernel/parallel.hpp | 27 +++++++++++++++++++-------- 3 files changed, 47 insertions(+), 23 deletions(-) (limited to 'sca-cpp') diff --git a/sca-cpp/trunk/kernel/fstream.hpp b/sca-cpp/trunk/kernel/fstream.hpp index 6888e7d14d..89d1909f15 100644 --- a/sca-cpp/trunk/kernel/fstream.hpp +++ b/sca-cpp/trunk/kernel/fstream.hpp @@ -29,6 +29,11 @@ #include #include #include +#include +#include +#ifdef WANT_THREADS +#include +#endif #include "string.hpp" #include "stream.hpp" @@ -149,10 +154,14 @@ ifstream cin(stdin); * Format the current time. */ const string logTime() { - const time_t t = ::time(NULL); + struct timeval tv; + gettimeofday(&tv, NULL); + const time_t t = tv.tv_sec; const tm* lt = localtime(&t); char ft[32]; - strftime(ft, 31, "%a %b %d %H:%M:%S %Y", lt); + strftime(ft, 20, "%a %b %d %H:%M:%S", lt); + sprintf(ft + 19, ".%06lu ", (unsigned long)tv.tv_usec); + strftime(ft + 27, 5, "%Y", lt); return ft; } @@ -203,11 +212,19 @@ private: bool owner; bool head; + const unsigned long tid() const { +#ifdef WANT_THREADS + return (unsigned long)pthread_self(); +#else + return 0; +#endif + } + logfstream& whead() { if (head) return *this; head = true; - *this << "[" << logTime() << "] [" << type << "] "; + *this << "[" << logTime() << "] [" << type << "] [pid " << (unsigned long)getpid() << ":tid " << tid() << "] "; return *this; } }; diff --git a/sca-cpp/trunk/kernel/parallel-test.cpp b/sca-cpp/trunk/kernel/parallel-test.cpp index f883d19f69..59cfd81978 100644 --- a/sca-cpp/trunk/kernel/parallel-test.cpp +++ b/sca-cpp/trunk/kernel/parallel-test.cpp @@ -136,15 +136,15 @@ bool checkSquareResults(const list > r, int i) { return true; } -const gc_ptr tlsvc() { - gc_ptr i = new (gc_new()) long int(); +const gc_ptr tlsvc() { + gc_ptr i = new (gc_new()) unsigned long(); *i = 0l; return i; } -const perthread_ptr tlsv(tlsvc); +const perthread_ptr tlsv(tlsvc); const long int tlsset(gc_ptr> wq, gc_ptr> xq) { - const long int v = *tlsv; + const unsigned long v = *tlsv; *tlsv = threadId(); enqueue(*xq, true); dequeue(*wq); @@ -183,8 +183,7 @@ bool checkTLSSets(const list > s) { if (isNil(s)) return true; assert(car(s) == 0); - checkTLSSets(cdr(s)); - return true; + return checkTLSSets(cdr(s)); } const list > submitTLSChecks(worker& w, wqueue& wq, wqueue& xq, const int max, const int i) { @@ -198,23 +197,21 @@ bool checkTLSResults(const list > r) { if (isNil(r)) return true; assert(car(r) == true); - checkTLSResults(cdr(r)); - return true; + return checkTLSResults(cdr(r)); } bool testWorker() { - worker w(20); + const int max = 100; + worker w(max); { const lambda func = curry(lambda (mtsquare), 2); assert(submit(w, func) == 4); } { - const int max = 20; const list > r(submitSquares(w, max, 0)); checkSquareResults(r, 0); } { - const int max = 20; wqueue wq(max); unblockWorkers(wq, max); waitForWorkers(wq, max); @@ -222,7 +219,6 @@ bool testWorker() { waitForWorkers(wq, max); } { - const int max = 20; wqueue wq(max); wqueue xq(max); const list > s(submitTLSSets(w, wq, xq, max, 0)); diff --git a/sca-cpp/trunk/kernel/parallel.hpp b/sca-cpp/trunk/kernel/parallel.hpp index 648cd60cfc..57754a33e6 100644 --- a/sca-cpp/trunk/kernel/parallel.hpp +++ b/sca-cpp/trunk/kernel/parallel.hpp @@ -26,10 +26,9 @@ * Simple parallel work execution functions. */ +#include #ifdef WANT_THREADS #include -#include -#include #endif #include "function.hpp" @@ -37,17 +36,20 @@ namespace tuscany { +/** + * Returns the current process id. + */ +unsigned long processId() { + return (unsigned long)getpid(); +} + #ifdef WANT_THREADS /** * Returns the current thread id. */ -long int threadId() { -#ifdef IS_DARWIN - return syscall(SYS_thread_selfid); -#else - return syscall(SYS_gettid); -#endif +unsigned long threadId() { + return (unsigned long)pthread_self(); } /** @@ -317,6 +319,15 @@ const bool cancel(worker& w) { return true; } +#else + +/** + * Returns the current thread id. + */ +unsigned long threadId() { + return 0; +} + #endif /** -- cgit v1.2.3