diff --git a/src_main/console/piconsole.cpp b/src_main/console/piconsole.cpp index ff3e2bd6..f6d0a8f1 100644 --- a/src_main/console/piconsole.cpp +++ b/src_main/console/piconsole.cpp @@ -833,14 +833,14 @@ void PIConsole::addVariable(const PIString & name, const PISystemTime * ptr, int * */ void PIConsole::addVariable(const PIString & name, const PIDiagnostics * ptr, int col, FormatFlags format) { addString(name + " diagnostics", col, format | PIConsole::Bold); - addVariable("Received count", ptr->receiveCount_ptr(), col, format); - addVariable("Invalid count", ptr->wrongCount_ptr(), col, format); - addVariable("Sended count", ptr->sendCount_ptr(), col, format); - addVariable("Immediate Frequency, Hz", ptr->immediateFrequency_ptr(), col, format); - addVariable("Integral Frequency, Hz", ptr->integralFrequency_ptr(), col, format); - addVariable("Receive speed", ptr->receiveSpeed_ptr(), col, format); - addVariable("Send speed", ptr->sendSpeed_ptr(), col, format); - addVariable("Quality", ptr->quality_ptr(), col, format); +// addVariable("Received count", ptr->receiveCount_ptr(), col, format); +// addVariable("Invalid count", ptr->wrongCount_ptr(), col, format); +// addVariable("Sended count", ptr->sendCount_ptr(), col, format); +// addVariable("Immediate Frequency, Hz", ptr->immediateFrequency_ptr(), col, format); +// addVariable("Integral Frequency, Hz", ptr->integralFrequency_ptr(), col, format); +// addVariable("Receive speed", ptr->receiveSpeed_ptr(), col, format); +// addVariable("Send speed", ptr->sendSpeed_ptr(), col, format); +// addVariable("Quality", ptr->quality_ptr(), col, format); } void PIConsole::addVariable(const PIString & name, const PISystemMonitor * ptr, int col, FormatFlags format) { addString("monitor " + name, col, format | PIConsole::Bold); diff --git a/src_main/core/pitime.cpp b/src_main/core/pitime.cpp index 9feb21e5..b696f8b7 100755 --- a/src_main/core/pitime.cpp +++ b/src_main/core/pitime.cpp @@ -65,6 +65,7 @@ void piUSleep(int usecs) { #ifdef WINDOWS //printf("Sleep %d\n", usecs / 1000); if (usecs > 0) Sleep(usecs / 1000); + //printf("Sleep end"); #else # ifdef FREERTOS vTaskDelay(usecs / 1000 / portTICK_PERIOD_MS); diff --git a/src_main/io_utils/pibasetransfer.cpp b/src_main/io_utils/pibasetransfer.cpp index 3ac42ae1..c04fab37 100644 --- a/src_main/io_utils/pibasetransfer.cpp +++ b/src_main/io_utils/pibasetransfer.cpp @@ -26,11 +26,14 @@ PIBaseTransfer::PIBaseTransfer(): crc(standardCRC_16()), diag(false) { setPacketSize(4096); #endif randomize(); + piCout << "PIBaseTransfer" << &diag; } PIBaseTransfer::~PIBaseTransfer() { + piCout << "~PIBaseTransfer" << &diag; diag.stop(); + piCout << "~PIBaseTransfer done"; break_ = true; } diff --git a/src_main/io_utils/pidiagnostics.cpp b/src_main/io_utils/pidiagnostics.cpp index 2cfdf4dc..f6113c87 100644 --- a/src_main/io_utils/pidiagnostics.cpp +++ b/src_main/io_utils/pidiagnostics.cpp @@ -60,40 +60,56 @@ PIDiagnostics::State::State() { PIDiagnostics::PIDiagnostics(bool start_): PITimer(/*PITimer::Pool*/) { disconn_ = 0.; - setInterval(100); + piCout << "PIDiagnostics construct"; + setInterval(500); reset(); setDisconnectTimeout(3.); changeDisconnectTimeout(3.); - if (start_) start(100); + if (start_) PITimer::start(500); + piCout << "PIDiagnostics construct done"; +} + +PIDiagnostics::~PIDiagnostics() { + piCout << "~PIDiagnostics start..."; + PITimer::stop(); + piCout << "~PIDiagnostics done!"; } PIDiagnostics::State PIDiagnostics::state() const { - constLock(); + mutex_state.lock(); State ret = cur_state; - constUnlock(); + mutex_state.unlock(); + return ret; +} + +PIDiagnostics::Quality PIDiagnostics::quality() const { + PIDiagnostics::Quality ret; + mutex_state.lock(); + ret = cur_state.quality; + mutex_state.unlock(); return ret; } PIString PIDiagnostics::receiveSpeed() const { - constLock(); + mutex_state.lock(); PIString ret = cur_state.receive_speed; - constUnlock(); + mutex_state.unlock(); return ret; } PIString PIDiagnostics::sendSpeed() const { - constLock(); + mutex_state.lock(); PIString ret = cur_state.send_speed; - constUnlock(); + mutex_state.unlock(); return ret; } void PIDiagnostics::reset() { - lock(); + mutex_state.lock(); cur_state = State(); if (disconn_ != 0.) { int hist_size = history_rec.size(); @@ -102,12 +118,12 @@ void PIDiagnostics::reset() { history_rec.resize(hist_size); history_send.resize(hist_size); } - unlock(); + mutex_state.unlock(); } void PIDiagnostics::received(int size, bool correct) { - lock(); + mutex_state.lock(); Entry & e(history_rec.front()); if (correct) { e.cnt_ok++; @@ -121,24 +137,25 @@ void PIDiagnostics::received(int size, bool correct) { cur_state.received_bytes_wrong += size; } e.empty = false; - unlock(); + mutex_state.unlock(); } void PIDiagnostics::sended(int size) { - lock(); + mutex_state.lock(); Entry & e(history_send.front()); e.cnt_ok++; e.bytes_ok += size; cur_state.sended_packets++; cur_state.sended_bytes += size; e.empty = false; - unlock(); + mutex_state.unlock(); } void PIDiagnostics::tick(void * , int ) { - lock(); + mutex_state.lock(); + //piCoutObj << "lock"; int tcnt_recv = 0; int tcnt_send = 0; Entry send = calcHistory(history_send, tcnt_send); @@ -161,7 +178,7 @@ void PIDiagnostics::tick(void * , int ) { cur_state.sended_packets_per_sec = ullong(float(send.cnt_ok) / its); cur_state.sended_bytes_per_sec = ullong(double(send.bytes_ok) / its); } -// piCoutObj << "tick" << recv.cnt_ok << send.cnt_ok; + //piCoutObj << "tick" << recv.cnt_ok << send.cnt_ok; // speedRecv = PIString::readableSize(ullong(double(history_rec.front().bytes_ok) / hz)) + "/s"; // speedSend = PIString::readableSize(ullong(double(history_send.front().bytes_ok) / hz)) + "/s"; cur_state.receive_speed = PIString::readableSize(cur_state.received_bytes_per_sec) + "/s"; @@ -192,7 +209,8 @@ void PIDiagnostics::tick(void * , int ) { qualityChanged(diag, cur_state.quality); cur_state.quality = diag; } - unlock(); + mutex_state.unlock(); + //piCoutObj << "unlock"; } @@ -219,7 +237,8 @@ void PIDiagnostics::propertyChanged(const PIString &) { void PIDiagnostics::changeDisconnectTimeout(float disct) { - lock(); + //PITimer::stop(); + mutex_state.lock(); disconn_ = piMaxf(disct, interval() / 1000.f); if (interval() > 0) { int hist_size = piClampi(int(disconn_ * 1000.f / float(interval())), 1, 65536); @@ -231,15 +250,16 @@ void PIDiagnostics::changeDisconnectTimeout(float disct) { history_send.resize(1); } //piCoutObj << hist_size << disconn_ << interval(); - unlock(); + mutex_state.unlock(); + //PITimer::start(); } -void PIDiagnostics::constLock() const { - const_cast(this)->lock(); -} +//void PIDiagnostics::constLock() const { +// const_cast(this)->lock(); +//} -void PIDiagnostics::constUnlock() const { - const_cast(this)->unlock(); -} +//void PIDiagnostics::constUnlock() const { +// const_cast(this)->unlock(); +//} diff --git a/src_main/io_utils/pidiagnostics.h b/src_main/io_utils/pidiagnostics.h index b5fce1af..b44dbf73 100644 --- a/src_main/io_utils/pidiagnostics.h +++ b/src_main/io_utils/pidiagnostics.h @@ -36,7 +36,7 @@ public: //! Constructs an empty diagnostics and if "start_" start it PIDiagnostics(bool start_ = true); - virtual ~PIDiagnostics() {;} + virtual ~PIDiagnostics(); //! Connection quality enum Quality { @@ -77,6 +77,7 @@ public: //! Returns period of full disconnect in seconds and period of averaging frequency void setDisconnectTimeout(float s) {setProperty("disconnectTimeout", s);} + /* //! Returns immediate receive frequency, packets/s float immediateFrequency() const {return cur_state.immediate_freq;} @@ -112,9 +113,10 @@ public: //! Returns overall sended packets count ullong sendCount() const {return cur_state.sended_packets;} + */ //! Returns connection quality - PIDiagnostics::Quality quality() const {return cur_state.quality;} + PIDiagnostics::Quality quality() const; //! Returns receive speed in format "n {B|kB|MB|GB|TB}/s" PIString receiveSpeed() const; @@ -122,7 +124,7 @@ public: //! Returns send speed in format "n {B|kB|MB|GB|TB}/s" PIString sendSpeed() const; - + /* DEPRECATED //! Returns immediate receive frequency pointer, packets/s. Useful for output to PIConsole const float * immediateFrequency_ptr() const {return &cur_state.immediate_freq;} @@ -166,9 +168,9 @@ public: const PIString * receiveSpeed_ptr() const {return &cur_state.receive_speed;} //! Returns send speed pointer in format "n {B|kB|MB|GB|TB}/s". Useful for output to PIConsole - const PIString * sendSpeed_ptr() const {return &cur_state.send_speed;} + const PIString * sendSpeed_ptr() const {return &cur_state.send_speed;}*/ - EVENT_HANDLER0(void, start) {start(100.); changeDisconnectTimeout(disconn_);} + EVENT_HANDLER0(void, start) {PITimer::start(100.); changeDisconnectTimeout(disconn_);} EVENT_HANDLER1(void, start, double, msecs) {if (msecs > 0.) {PITimer::start(msecs); changeDisconnectTimeout(disconn_);}} EVENT_HANDLER0(void, stop) {PITimer::stop();} EVENT_HANDLER0(void, reset); @@ -220,12 +222,13 @@ private: Entry calcHistory(PIQueue & hist, int & cnt); void propertyChanged(const PIString &); void changeDisconnectTimeout(float disct); - void constLock() const; - void constUnlock() const; +// void constLock() const; +// void constUnlock() const; PIQueue history_rec, history_send; float disconn_; State cur_state; + mutable PIMutex mutex_state; }; diff --git a/src_main/thread/pithread.cpp b/src_main/thread/pithread.cpp index 65623a8f..5a172959 100755 --- a/src_main/thread/pithread.cpp +++ b/src_main/thread/pithread.cpp @@ -168,6 +168,7 @@ PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int timer_delay) terminating = running_ = lockRun = false; priority_ = piNormal; delay_ = timer_delay; + piCout << "PIThread" << this; if (startNow) start(timer_delay); } @@ -180,6 +181,7 @@ PIThread::PIThread(bool startNow, int timer_delay): PIObject() { terminating = running_ = lockRun = false; priority_ = piNormal; delay_ = timer_delay; + piCout << "PIThread" << this; if (startNow) start(timer_delay); } @@ -202,6 +204,8 @@ PIThread::~PIThread() { pthread_cancel(PRIVATE->thread); # endif # else + piCout << "terminate by ~PIThread" << this; + while(1) msleep(10); TerminateThread(PRIVATE->thread, 0); CloseHandle(PRIVATE->thread); # endif @@ -321,6 +325,10 @@ void PIThread::terminate() { //pthread_join(PRIVATE->thread, &ret); # endif #else + piCout << "terminate by terminate"; + while (1) { + msleep(10); + } TerminateThread(PRIVATE->thread, 0); CloseHandle(PRIVATE->thread); #endif @@ -438,12 +446,12 @@ void PIThread::__thread_func__(void * t) { while (!ct.terminating) { ct.maybeCallQueuedEvents(); if (ct.lockRun) ct.mutex_.lock(); - //piCout << "thread" << ct.name() << "..."; +// piCout << "thread" << ct.name() << "..." << ct.lockRun; ct.run(); - //piCout << "thread" << ct.name() << "done"; //printf("thread %p tick\n", &ct); if (ct.ret_func != 0) ct.ret_func(ct.data_); if (ct.lockRun) ct.mutex_.unlock(); +// piCout << "thread" << ct.name() << "done"; if (ct.delay_ > 0) { ct.tmr_.reset(); double sl(0.); @@ -472,6 +480,7 @@ void PIThread::__thread_func__(void * t) { //piCout << "pthread_exit" << (ct.__privateinitializer__.p)->thread; UNREGISTER_THREAD(&ct); PIINTROSPECTION_UNREGISTER_THREAD(ct.tid()); + piCout << "pthread_exit" << &ct; #ifndef WINDOWS pthread_detach((ct.__privateinitializer__.p)->thread); (ct.__privateinitializer__.p)->thread = 0; diff --git a/src_main/thread/pitimer.cpp b/src_main/thread/pitimer.cpp index a65006f1..528da0c7 100755 --- a/src_main/thread/pitimer.cpp +++ b/src_main/thread/pitimer.cpp @@ -18,6 +18,7 @@ */ #include "pitimer.h" +#include #ifdef PIP_TIMER_RT # include #endif @@ -98,14 +99,15 @@ void _PITimerBase::startDeferred(double interval_ms, double delay_ms) { bool _PITimerBase::stop(bool wait) { + piCout << "_PITimerBase::stop" << isRunning(); if (!isRunning()) return true; + piCout << "_PITimerBase::stopTimer"; running_ = !stopTimer(wait); return !running_; } - class _PITimerImp_Thread: public _PITimerBase { public: _PITimerImp_Thread(); @@ -145,7 +147,7 @@ private: class _PITimerImp_Pool: public _PITimerImp_Thread { public: _PITimerImp_Pool(); - virtual ~_PITimerImp_Pool() {stop(true);} + virtual ~_PITimerImp_Pool() {} private: class Pool: public PIThread { public: @@ -170,12 +172,14 @@ _PITimerImp_Thread::_PITimerImp_Thread() { wait_dt = 100; wait_dd = 200; wait_tick = 10; - //piCout << "new _PITimerImp_Thread"; + piCout << "new _PITimerImp_Thread" << &thread_ << this; } _PITimerImp_Thread::~_PITimerImp_Thread() { - stop(true); + piCout << "~_PITimerImp_Thread ..." << &thread_ << this; + thread_.stop(true); + piCout << "~_PITimerImp_Thread done" << &thread_ << this; } @@ -205,6 +209,7 @@ bool _PITimerImp_Thread::startTimer(double interval_ms) { bool _PITimerImp_Thread::stopTimer(bool wait) { + piCout << "stop timer..." << &thread_ << this; #ifndef FREERTOS thread_.stop(true); #else @@ -214,12 +219,19 @@ bool _PITimerImp_Thread::stopTimer(bool wait) { if (thread_.isRunning()) thread_.terminate(); #endif + piCout << "stop timer done!" << this << st_wait; return true; } bool _PITimerImp_Thread::threadFunc() { - if (!running_) return false; + //piCout << "threadFunc"; + //printf("threadFunc\n"); + if (!running_) { + //piCout << "threadFunc 1"; + //printf("threadFunc 1"); + return false; + } if (deferred_) { PISystemTime dwt; int wth(wait_dt); @@ -230,41 +242,69 @@ bool _PITimerImp_Thread::threadFunc() { dwt = st_time - PISystemTime::current(true); if (wth > 0) { if (dwt.toMilliseconds() > wth + 1.) { - piMSleep(wth); + //printf("wait 2\n"); + //piCout << "wait 2" << this << dwt; + msleep(wth); + //printf("threadFunc 2\n"); + //piCout << "threadFunc 2"; return false; } else { + //piCout << "wait 3" << this << dwt; + //printf("wait 3\n"); dwt.sleep(); deferred_ = false; st_time = PISystemTime::current(true); } } else { - if (dwt.toMilliseconds() > 0.1) + if (dwt.toMilliseconds() > 0.1) { + //piCout << "threadFunc 3"; + //printf("threadFunc 3\n"); return false; + } } } st_wait = st_time - PISystemTime::current(true); //piCout << "wait" << this << st_wait; if (st_wait.abs() > st_odt || st_wait.seconds <= -5) { adjustTimes(); + //piCout << "threadFunc 4"; + //printf("threadFunc 4\n"); return true; } if (wait_tick > 0) { if (st_wait.toMilliseconds() > wait_tick + 1.) { - piMSleep(wait_tick); + //piCout << "wait 5" << this << wait_tick; + //printf("wait 5 %d\n", wait_tick); + //fflush(stdout); + msleep(wait_tick); + //piCout << "threadFunc 5"; + //printf("threadFunc 5\n"); + //fflush(stdout); return false; } else { + //piCout << "wait 6" << this << st_wait; + //printf("wait 6 %f\n" , st_wait.toMicroseconds()); st_wait.sleep(); } } else { - if (st_wait.toMilliseconds() > 0.1) + if (st_wait.toMilliseconds() > 0.1) { + //piCout << "threadFunc 6"; + //printf("threadFunc 6\n"); return false; + } } st_time += st_inc; if (!parent->isPIObject()) { piCout << "Achtung! PITimer \"parent\" is not PIObject!"; + //piCout << "threadFunc 7"; + printf("threadFunc 7\n"); return false; } + //piCout << "timer tick"; + //printf("timer tick\n"); tfunc(parent); + //piCout << "threadFunc 8"; + //printf("threadFunc 8\n"); return true; } @@ -522,7 +562,7 @@ void PITimer::init() { void PITimer::destroy() { //piCout << "destroy" << this << imp; if (imp == 0) return; - //imp->stop(true); ///BUG: WTF FreeRTOS segfault on this! + imp->stop(true); ///BUG: WTF FreeRTOS segfault on this! delete imp; imp = 0; } diff --git a/src_main/thread/pitimer.h b/src_main/thread/pitimer.h index 1ca30055..424b8e08 100755 --- a/src_main/thread/pitimer.h +++ b/src_main/thread/pitimer.h @@ -35,7 +35,7 @@ class PIP_EXPORT _PITimerBase { friend class PITimer; public: _PITimerBase(); - virtual ~_PITimerBase() {stop(true);} + virtual ~_PITimerBase() {} double interval() const {return interval_;} void setInterval(double i); diff --git a/utils/system_daemon/main.cpp b/utils/system_daemon/main.cpp index 156e378d..873b23c7 100755 --- a/utils/system_daemon/main.cpp +++ b/utils/system_daemon/main.cpp @@ -158,15 +158,16 @@ public: } void updatePeerDiag(TileSimple * tl, const PIDiagnostics & diag) { tl->content.clear(); + PIDiagnostics::State ds = diag.state(); tl->content << TileSimple::Row(diag.name() + " diagnostics", CellFormat(PIScreenTypes::Default, PIScreenTypes::Default, PIScreenTypes::Bold)); - tl->content << TileSimple::Row("Received count: " + PIString::fromNumber(diag.receiveCount()), CellFormat()); - tl->content << TileSimple::Row("Invalid count: " + PIString::fromNumber(diag.wrongCount()), CellFormat()); - tl->content << TileSimple::Row("Sended count: " + PIString::fromNumber(diag.sendCount()), CellFormat()); - tl->content << TileSimple::Row("Immediate Frequency, Hz: " + PIString::fromNumber(diag.immediateFrequency()), CellFormat()); - tl->content << TileSimple::Row("Integral Frequency, Hz: " + PIString::fromNumber(diag.integralFrequency()), CellFormat()); - tl->content << TileSimple::Row("Receive speed: " + diag.receiveSpeed(), CellFormat()); - tl->content << TileSimple::Row("Send speed: " + diag.sendSpeed(), CellFormat()); - tl->content << TileSimple::Row("Quality: " + PIString::fromNumber((int)diag.quality()), CellFormat()); + tl->content << TileSimple::Row("Received count: " + PIString::fromNumber(ds.received_packets), CellFormat()); + tl->content << TileSimple::Row("Invalid count: " + PIString::fromNumber(ds.received_packets_wrong), CellFormat()); + tl->content << TileSimple::Row("Sended count: " + PIString::fromNumber(ds.sended_packets), CellFormat()); + tl->content << TileSimple::Row("Immediate Frequency, Hz: " + PIString::fromNumber(ds.immediate_freq), CellFormat()); + tl->content << TileSimple::Row("Integral Frequency, Hz: " + PIString::fromNumber(ds.integral_freq), CellFormat()); + tl->content << TileSimple::Row("Receive speed: " + ds.receive_speed, CellFormat()); + tl->content << TileSimple::Row("Send speed: " + ds.send_speed, CellFormat()); + tl->content << TileSimple::Row("Quality: " + PIString::fromNumber((int)ds.quality), CellFormat()); } void updatePeerInfo() { bool pm = daemon_.lockedPeers(); diff --git a/utils/udp_file_transfer/main.cpp b/utils/udp_file_transfer/main.cpp index 0e2955e6..084f52f7 100644 --- a/utils/udp_file_transfer/main.cpp +++ b/utils/udp_file_transfer/main.cpp @@ -106,9 +106,9 @@ private: << ft.diagnostic().sendSpeed() << "(" << PIString::readableSize(ft.bytesFileCur()) << "/" << PIString::readableSize(ft.bytesFileAll()) << ", " << PIString::readableSize(ft.bytesCur()) << "/" << PIString::readableSize(ft.bytesAll()) << ")" - << "ETA" << (ft.diagnostic().receiveBytesPerSec() > 0 ? + << "ETA" << (ft.diagnostic().state().received_bytes_per_sec > 0 ? PIString::fromNumber(PISystemTime::fromSeconds((ft.bytesAll() - ft.bytesCur()) / - ft.diagnostic().receiveBytesPerSec()).toSeconds()) + ft.diagnostic().state().received_bytes_per_sec).toSeconds()) : PIString("unknown")) #ifndef WINDOWS << Flush