From 608deb8e00cc81ee4c2dd2295a5a98eb0aba0dcb Mon Sep 17 00:00:00 2001
From: am0ebe <am0ebe@gmx.de>
Date: Tue, 18 Jun 2024 14:46:13 +0200
Subject: [PATCH] more threadpool rewrite, change info to printcaminfo because
 of name collisiong with iprinter::info, added iprinter print overloads, limit
 opencv threads to 1

---
 src/cam.cpp           | 25 +++++++++++++++++--------
 src/cam.h             |  9 +++------
 src/cmd/main.cpp      | 20 ++++++++++++++++++++
 src/core.cpp          |  2 +-
 src/frameobserver.cpp | 14 +++++++++-----
 src/frameobserver.h   |  2 +-
 src/iprinter.cpp      | 15 +++++++++++++++
 src/iprinter.h        |  3 +++
 8 files changed, 69 insertions(+), 21 deletions(-)

diff --git a/src/cam.cpp b/src/cam.cpp
index db56d1b..e3eb167 100644
--- a/src/cam.cpp
+++ b/src/cam.cpp
@@ -121,7 +121,7 @@ bool Cam::checkOutDir()
 	}
 
 
-	IPrinter::info("writing frames to: " + _outDir);
+	info("writing frames to: " + _outDir);
 	return true;
 }
 
@@ -156,10 +156,9 @@ void Cam::startRecording(seconds dur)
 	{
 		FeaturePtr pFeature;
 
-		g( _cam->GetFeatureByName ("PayloadSize", pFeature ));
+		g( _cam->GetFeatureByName ("PayloadSize", pFeature )); //xxx adjust: set to 7062528
 		g( pFeature->GetValue (_payloadSize));
 		print("PayloadSize",_payloadSize);
-		//XXX adjust packetSize ??
 
 		g( _cam->GetFeatureByName("PixelFormat", pFeature));
 		g( pFeature->GetValue(_pixelFormat));
@@ -230,10 +229,20 @@ void Cam::stopRecording()
 		qDebug() << __FUNCTION__ << "Exception caught: " << xx.what();
 		return;
 	}
+
+	//xxx put in info
+	print("_name",_name);
+	print("_nframes_complete",_nframes_complete);
+	print("_nframes_incomplete",_nframes_incomplete);
+	print("_nframes_toosmall",_nframes_toosmall);
+	print("_nframes_invalid",_nframes_invalid);
+	print("_nframes_unknown",_nframes_unknown);
+
+
 }
 
 // id,name,model,serial,interfaceID,state
-QString Cam::info()
+QString Cam::printCamInfo()
 {
 	QString camInfo = _name + DELIM + _ip + DELIM;
 	if( state() == Cam::disconnected )
@@ -291,12 +300,12 @@ void Cam::onChanged(UpdateTriggerType type)
 	switch (type)
 	{
 		case UpdateTriggerType::UpdateTriggerPluggedIn:
-			IPrinter::info(name()+" connected");
+			info(name()+" connected");
 			setState(closed);
 			//_cam ptr set in core
 			break;
 		case UpdateTriggerType::UpdateTriggerPluggedOut:
-			IPrinter::info(name()+" disconnected");
+			info(name()+" disconnected");
 			setState(disconnected);
 			_cam.reset();
 			break;
@@ -329,7 +338,7 @@ void Cam::saveSettings()
 	if (f(_cam->SaveSettings( file.toStdString().c_str(), &settingsStruct )))
 		error( "Could not save camera settings to '" + file + "'" );
 	else
-		IPrinter::info("Saved settings to " + file);
+		info("Saved settings to " + file);
 }
 
 //load settings.xml for current open cam
@@ -343,5 +352,5 @@ void Cam::loadSettings()
 	if(f(_cam->LoadSettings( settingsFile().toStdString().c_str())))
 		error( "Could not load camera settings from '" + settingsFile() + "'" );
 	else
-		IPrinter::info("Loaded settings from " + settingsFile());
+		info("Loaded settings from " + settingsFile());
 }
diff --git a/src/cam.h b/src/cam.h
index 52ec121..d2b1cef 100644
--- a/src/cam.h
+++ b/src/cam.h
@@ -58,7 +58,7 @@ public:
 	void close();
 	void startRecording(minutes dur);
 	void startRecording(seconds dur); //can use literals "1s, 1min"
-	QString info();
+	QString printCamInfo();
 
 	void saveSettings();
 	void loadSettings();
@@ -86,10 +86,7 @@ private:
 	QTimer* _timer;
 	QString _outDir;
 
-	VmbUint64_t _nframes_complete; //1 cam, all observers & processors, totalframes
-	VmbUint64_t _nframes_incomplete;
-	VmbUint64_t _nframes_toosmall;
-	VmbUint64_t _nframes_invalid;
-	VmbUint64_t _nframes_unknown;
+	//1 cam, all observers & processors, totalframes
+	VmbUint64_t _nframes_complete, _nframes_incomplete, _nframes_toosmall, _nframes_invalid, _nframes_unknown;
 };
 
diff --git a/src/cmd/main.cpp b/src/cmd/main.cpp
index 44996ef..31065e0 100644
--- a/src/cmd/main.cpp
+++ b/src/cmd/main.cpp
@@ -10,6 +10,8 @@
 #include <QCoreApplication>
 #include <QTimer>
 
+#include <opencv2/core.hpp>
+
 int main(int argc, char *argv[])
 {
 
@@ -20,6 +22,10 @@ int main(int argc, char *argv[])
 	qDebug() << utils::getFirstFileInAppDirEndingWith("json");
 	qDebug() << utils::getFirstFileInAppDirEndingWith("xml");
 
+	cv::setNumThreads(1); // since im only using cv in frameprocessor which is already multi-threaded.
+	//xxx test performance
+
+
 	//parse args
 	QStringList arguments = a.arguments();
 	for (int i = 1; i < arguments.size(); ++i)
@@ -56,3 +62,17 @@ int main(int argc, char *argv[])
 // unplanned termination (ctrl-c, kill)
 // std::signal(SIGINT, signalHandler);
 // std::signal(SIGTERM, signalHandler);
+
+
+// limit THREADS
+//use tools to analyze
+// gdb -p <pid>
+// or
+// info threads
+// strace -f -p <pid>
+// int idealThreadCount = QThread::idealThreadCount();
+// int threadPoolMaxCount = QThreadPool::globalInstance()->maxThreadCount();
+// QThreadPool::globalInstance()->setMaxThreadCount(idealThreadCount);
+
+// qDebug() << "Ideal Thread Count:" << idealThreadCount;
+// qDebug() << "Thread Pool Max Count:" << threadPoolMaxCount;
diff --git a/src/core.cpp b/src/core.cpp
index 4eda229..7c9ba0a 100644
--- a/src/core.cpp
+++ b/src/core.cpp
@@ -103,7 +103,7 @@ void Core::listCams()
 			camInfo += " * ";
 		else
 			camInfo += "   ";
-		camInfo += camera->info();
+		camInfo += camera->printCamInfo();
 
 		allCamInfo << camInfo;
 	}
diff --git a/src/frameobserver.cpp b/src/frameobserver.cpp
index 5642fcb..6cc9855 100644
--- a/src/frameobserver.cpp
+++ b/src/frameobserver.cpp
@@ -16,12 +16,13 @@ using namespace VmbCPP;
 FrameObserver::FrameObserver( CamPtr cam ) : IFrameObserver( cam->getCameraPtr() ),
 	_cam(cam),
 	_processor(new FrameProcessor(cam->outDir())),
-	_threadpool(QThreadPool::globalInstance())
+	_pool(QThreadPool::globalInstance())
 {
-	// _threadpool->setMaxThreadCount(QThread::idealThreadCount()); //dflt
+	// _pool->setMaxThreadCount(QThread::idealThreadCount()); //dflt
 	connect(_processor, &FrameProcessor::frameProcessed, this, &FrameObserver::queueFrame, Qt::QueuedConnection);
 }
 
+
 FrameObserver::~FrameObserver()
 {
 	qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << "";
@@ -45,6 +46,8 @@ void FrameObserver::FrameReceived ( const FramePtr pframe )
 	switch( status )
 	{
 		case VmbFrameStatusIncomplete:
+			qDebug() << "Frame incomplete";
+
 			_cam->_nframes_incomplete++;
 			queueFrame(pframe);
 			return;
@@ -60,18 +63,19 @@ void FrameObserver::FrameReceived ( const FramePtr pframe )
 			_cam->_nframes_unknown++;
 			return;
 		case VmbFrameStatusComplete:
+			qDebug() << "Frame complete";
+
 			_cam->_nframes_complete++;
 			break;
 	}
 
 	_processor->setFrame(pframe);
-	_threadpool->start(_processor); //start processing in a separate thread
+	_pool->start(_processor); //start processing in a separate thread. sa trystart()
 }
 
 void FrameObserver::queueFrame(FramePtr pframe)
 {
-	qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << "";
-
+	// qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << "";
 	_cam->getCameraPtr()->QueueFrame(pframe); // Queue the frame back to the camera
 }
 
diff --git a/src/frameobserver.h b/src/frameobserver.h
index c16c71a..1a5a484 100644
--- a/src/frameobserver.h
+++ b/src/frameobserver.h
@@ -26,6 +26,6 @@ class FrameObserver : public QObject, public IFrameObserver
 	private:
 		CamPtr _cam;
 		FrameProcessor* _processor;
-		QThreadPool* _threadpool;
+		QThreadPool* _pool;
 };
 
diff --git a/src/iprinter.cpp b/src/iprinter.cpp
index a12eb5c..34b77d3 100644
--- a/src/iprinter.cpp
+++ b/src/iprinter.cpp
@@ -49,6 +49,21 @@ void IPrinter::print(const QString& name, const std::string& val)
 		emit info(name + ": " + QString::fromStdString(val));
 }
 
+void IPrinter::print(const char* cstr)
+{
+	emit info(QString::fromLocal8Bit(cstr));
+}
+
+void IPrinter::print(const char* cstr, const VmbInt64_t& val)
+{
+	emit info(QString("%1: %2").arg(QString::fromLocal8Bit(cstr)).arg(val));
+}
+
+void IPrinter::print(const char* cstr, const QString& val)
+{
+	emit info(QString("%1: %2").arg(QString::fromLocal8Bit(cstr)).arg(val));
+}
+
 bool IPrinter::f(const VmbErrorType& ret, const QString& msg)
 {
 	// vimba function wrapper to test return type and potentially emit error message or successmsg
diff --git a/src/iprinter.h b/src/iprinter.h
index 8cc20b0..b335257 100644
--- a/src/iprinter.h
+++ b/src/iprinter.h
@@ -30,4 +30,7 @@ public:
 	void print(const QStringList&);
 	void print(const QString&, const VmbInt64_t&);
 	void print(const QString&, const std::string&);
+	void print(const char*);
+	void print(const char*, const VmbInt64_t&);
+	void print(const char*, const QString&);
 };
-- 
GitLab