Commit dcbb77d4 authored by Alejandro Homs Puron's avatar Alejandro Homs Puron Committed by operator for beamline
Browse files

Remove Model processing threads, move packet RX to Camera::AcqThread

parent 83021557
...@@ -186,7 +186,6 @@ private: ...@@ -186,7 +186,6 @@ private:
public: public:
AcqThread(Camera *cam); AcqThread(Camera *cam);
void queueFinishedFrame(FrameType frame);
virtual void start(AutoMutex& l); virtual void start(AutoMutex& l);
void stop(AutoMutex& l, bool wait); void stop(AutoMutex& l, bool wait);
...@@ -208,6 +207,7 @@ private: ...@@ -208,6 +207,7 @@ private:
AutoMutex& m_lock; AutoMutex& m_lock;
}; };
DetFrameImagePackets readRecvPackets(FrameType frame);
Status newFrameReady(FrameType frame); Status newFrameReady(FrameType frame);
void startAcq(); void startAcq();
void stopAcq(); void stopAcq();
...@@ -216,7 +216,8 @@ private: ...@@ -216,7 +216,8 @@ private:
Camera *m_cam; Camera *m_cam;
Cond& m_cond; Cond& m_cond;
AcqState& m_state; AcqState& m_state;
FrameQueue m_frame_queue; FramePacketMap m_frame_packet_map;
bool m_acq_stopped;
}; };
friend class BufferMgr; friend class BufferMgr;
...@@ -248,8 +249,6 @@ private: ...@@ -248,8 +249,6 @@ private:
void removeSharedMem(); void removeSharedMem();
void createReceivers(); void createReceivers();
DetFrameImagePackets readRecvPackets();
void publishFrame(FrameType frame);
void assemblePackets(DetFrameImagePackets&& det_frame_packets); void assemblePackets(DetFrameImagePackets&& det_frame_packets);
bool checkLostPackets(); bool checkLostPackets();
...@@ -317,7 +316,6 @@ private: ...@@ -317,7 +316,6 @@ private:
PixelDepthCPUAffinityMap m_cpu_affinity_map; PixelDepthCPUAffinityMap m_cpu_affinity_map;
GlobalCPUAffinityMgr m_global_cpu_affinity_mgr; GlobalCPUAffinityMgr m_global_cpu_affinity_mgr;
AutoPtr<AcqThread> m_acq_thread; AutoPtr<AcqThread> m_acq_thread;
FramePacketMap m_frame_packet_map;
}; };
} // namespace SlsDetector } // namespace SlsDetector
......
...@@ -268,8 +268,6 @@ class Eiger : public Model ...@@ -268,8 +268,6 @@ class Eiger : public Model
virtual int getNbFrameMapItems(); virtual int getNbFrameMapItems();
virtual void updateFrameMapItems(FrameMap *map); virtual void updateFrameMapItems(FrameMap *map);
virtual void setThreadCPUAffinity(const CPUAffinityList& aff_list);
virtual void updateImageSize(); virtual void updateImageSize();
virtual bool checkSettings(Settings settings); virtual bool checkSettings(Settings settings);
...@@ -290,57 +288,6 @@ class Eiger : public Model ...@@ -290,57 +288,6 @@ class Eiger : public Model
typedef std::vector<Receiver *> RecvList; typedef std::vector<Receiver *> RecvList;
class Thread : public lima::Thread
{
DEB_CLASS_NAMESPC(DebModCamera, "Eiger::Thread", "SlsDetector");
public:
enum State {
Init, Ready, Running, Stopping, Quitting, End,
};
Thread(Eiger *eiger, int idx);
virtual ~Thread();
void setCPUAffinity(CPUAffinity aff);
void prepareAcq();
void startAcq()
{ setState(Running); }
void stopAcq()
{
setState(Stopping);
AutoMutex l = lock();
while (m_state != Ready)
wait();
}
protected:
virtual void threadFunction();
private:
friend class Eiger;
AutoMutex lock()
{ return m_eiger->lock(); }
void wait()
{ m_eiger->wait(); }
void broadcast()
{ m_eiger->broadcast(); }
void setState(State state)
{
AutoMutex l = lock();
m_state = state;
broadcast();
}
Eiger *m_eiger;
int m_idx;
State m_state;
};
typedef std::vector<AutoPtr<Thread> > ThreadList;
class CorrBase class CorrBase
{ {
DEB_CLASS_NAMESPC(DebModCamera, "Eiger::CorrBase", DEB_CLASS_NAMESPC(DebModCamera, "Eiger::CorrBase",
...@@ -532,21 +479,8 @@ class Eiger : public Model ...@@ -532,21 +479,8 @@ class Eiger : public Model
const FrameDim& getRecvFrameDim() const FrameDim& getRecvFrameDim()
{ return m_geom.m_recv_frame_dim; } { return m_geom.m_recv_frame_dim; }
AutoMutex lock()
{ return AutoMutex(m_cond.mutex()); }
void wait()
{ m_cond.wait(); }
void broadcast()
{ m_cond.broadcast(); }
bool allFramesAcquired()
{ return m_next_frame == m_nb_frames; }
int getNbRecvs(); int getNbRecvs();
int getNbProcessingThreads();
void setNbProcessingThreads(int nb_proc_threads);
CorrBase *createBadRecvFrameCorr(); CorrBase *createBadRecvFrameCorr();
CorrBase *createChipBorderCorr(ImageType image_type); CorrBase *createChipBorderCorr(ImageType image_type);
CorrBase *createInterModGapCorr(); CorrBase *createInterModGapCorr();
...@@ -586,14 +520,11 @@ class Eiger : public Model ...@@ -586,14 +520,11 @@ class Eiger : public Model
static const unsigned long BebFpgaReadPtrAddr; static const unsigned long BebFpgaReadPtrAddr;
static const unsigned long BebFpgaPtrRange; static const unsigned long BebFpgaPtrRange;
Cond m_cond;
BebList m_beb_list; BebList m_beb_list;
Geometry m_geom; Geometry m_geom;
CorrList m_corr_list; CorrList m_corr_list;
RecvList m_recv_list; RecvList m_recv_list;
ModelReconstruction *m_reconstruction; ModelReconstruction *m_reconstruction;
FrameType m_nb_frames;
ThreadList m_thread_list;
bool m_fixed_clock_div; bool m_fixed_clock_div;
ClockDiv m_clock_div; ClockDiv m_clock_div;
}; };
......
...@@ -199,8 +199,6 @@ class Jungfrau : public Model ...@@ -199,8 +199,6 @@ class Jungfrau : public Model
virtual int getNbFrameMapItems(); virtual int getNbFrameMapItems();
virtual void updateFrameMapItems(FrameMap *map); virtual void updateFrameMapItems(FrameMap *map);
virtual void setThreadCPUAffinity(const CPUAffinityList& aff_list);
virtual void updateImageSize(); virtual void updateImageSize();
virtual bool checkSettings(Settings settings); virtual bool checkSettings(Settings settings);
...@@ -214,57 +212,6 @@ class Jungfrau : public Model ...@@ -214,57 +212,6 @@ class Jungfrau : public Model
typedef std::vector<Receiver *> RecvList; typedef std::vector<Receiver *> RecvList;
class Thread : public lima::Thread
{
DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::Thread", "SlsDetector");
public:
enum State {
Init, Ready, Running, Stopping, Quitting, End,
};
Thread(Jungfrau *jungfrau, int idx);
virtual ~Thread();
void setCPUAffinity(CPUAffinity aff);
void prepareAcq();
void startAcq()
{ setState(Running); }
void stopAcq()
{
setState(Stopping);
AutoMutex l = lock();
while (m_state != Ready)
wait();
}
protected:
virtual void threadFunction();
private:
friend class Jungfrau;
AutoMutex lock()
{ return m_jungfrau->lock(); }
void wait()
{ m_jungfrau->wait(); }
void broadcast()
{ m_jungfrau->broadcast(); }
void setState(State state)
{
AutoMutex l = lock();
m_state = state;
broadcast();
}
Jungfrau *m_jungfrau;
int m_idx;
State m_state;
};
typedef std::vector<AutoPtr<Thread> > ThreadList;
class ImgProcBase class ImgProcBase
{ {
DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::ImgProcBase", DEB_CLASS_NAMESPC(DebModCamera, "Jungfrau::ImgProcBase",
...@@ -593,22 +540,8 @@ class Jungfrau : public Model ...@@ -593,22 +540,8 @@ class Jungfrau : public Model
FrameDim getModuleFrameDim(int idx, bool raw); FrameDim getModuleFrameDim(int idx, bool raw);
int getModuleDataOffset(int idx, bool raw); int getModuleDataOffset(int idx, bool raw);
AutoMutex lock()
{ return AutoMutex(m_cond.mutex()); }
void wait()
{ m_cond.wait(); }
void broadcast()
{ m_cond.broadcast(); }
bool allFramesAcquired()
{ return m_next_frame == m_nb_frames; }
int getNbRecvs(); int getNbRecvs();
int getNbProcessingThreads();
void setNbProcessingThreads(int nb_proc_threads);
Cond m_cond;
AutoPtr<GainPedImgProc> m_gain_ped_img_proc; AutoPtr<GainPedImgProc> m_gain_ped_img_proc;
AutoPtr<GainADCMapImgProc> m_gain_adc_map_img_proc; AutoPtr<GainADCMapImgProc> m_gain_adc_map_img_proc;
AutoPtr<AveImgProc> m_ave_img_proc; AutoPtr<AveImgProc> m_ave_img_proc;
...@@ -617,8 +550,6 @@ class Jungfrau : public Model ...@@ -617,8 +550,6 @@ class Jungfrau : public Model
ImgSrc m_img_src; ImgSrc m_img_src;
ModelReconstruction *m_reconstruction; ModelReconstruction *m_reconstruction;
RecvList m_recv_list; RecvList m_recv_list;
FrameType m_nb_frames;
ThreadList m_thread_list;
}; };
std::ostream& operator <<(std::ostream& os, Jungfrau::GainPed::MapType map_type); std::ostream& operator <<(std::ostream& os, Jungfrau::GainPed::MapType map_type);
......
...@@ -47,8 +47,6 @@ class Model ...@@ -47,8 +47,6 @@ class Model
public: public:
typedef Defs::Settings Settings; typedef Defs::Settings Settings;
typedef FrameMap::FinishInfo FinishInfo;
Model(Camera *cam, Type type); Model(Camera *cam, Type type);
virtual ~Model(); virtual ~Model();
...@@ -86,12 +84,8 @@ class Model ...@@ -86,12 +84,8 @@ class Model
virtual bool isAcqActive(); virtual bool isAcqActive();
virtual bool isXferActive() = 0; virtual bool isXferActive() = 0;
virtual void setThreadCPUAffinity(const CPUAffinityList& aff_list) = 0;
virtual Reconstruction *getReconstruction(); virtual Reconstruction *getReconstruction();
void processPackets();
protected: protected:
void updateCameraModel(); void updateCameraModel();
void updateCameraImageSize(); void updateCameraImageSize();
...@@ -110,8 +104,6 @@ class Model ...@@ -110,8 +104,6 @@ class Model
virtual void startAcq() = 0; virtual void startAcq() = 0;
virtual void stopAcq() = 0; virtual void stopAcq() = 0;
void processFinishInfo(const FinishInfo& finfo);
BufferMgr *getBuffer(); BufferMgr *getBuffer();
private: private:
...@@ -125,8 +117,6 @@ class Model ...@@ -125,8 +117,6 @@ class Model
protected: protected:
AutoPtr<sls::Detector> m_det; AutoPtr<sls::Detector> m_det;
FrameType m_next_frame;
FrameType m_last_frame;
}; };
......
...@@ -103,9 +103,6 @@ class Eiger : public SlsDetector::Model ...@@ -103,9 +103,6 @@ class Eiger : public SlsDetector::Model
virtual bool checkSettings(SlsDetector::Defs::Settings settings); virtual bool checkSettings(SlsDetector::Defs::Settings settings);
virtual void setThreadCPUAffinity(const SlsDetector::CPUAffinityList&
det_thread_aff_list);
virtual void prepareAcq(); virtual void prepareAcq();
virtual void startAcq(); virtual void startAcq();
virtual void stopAcq(); virtual void stopAcq();
......
...@@ -201,9 +201,6 @@ class Jungfrau : public SlsDetector::Model ...@@ -201,9 +201,6 @@ class Jungfrau : public SlsDetector::Model
virtual bool checkSettings(SlsDetector::Defs::Settings settings); virtual bool checkSettings(SlsDetector::Defs::Settings settings);
virtual void setThreadCPUAffinity(const SlsDetector::CPUAffinityList&
det_thread_aff_list);
virtual void prepareAcq(); virtual void prepareAcq();
virtual void startAcq(); virtual void startAcq();
virtual void stopAcq(); virtual void stopAcq();
......
...@@ -34,9 +34,6 @@ public: ...@@ -34,9 +34,6 @@ public:
Model(SlsDetector::Camera *cam, SlsDetector::Type type); Model(SlsDetector::Camera *cam, SlsDetector::Type type);
virtual ~Model(); virtual ~Model();
virtual void setThreadCPUAffinity(const SlsDetector::CPUAffinityList&
det_thread_aff_list) = 0;
virtual void getFrameDim(FrameDim& frame_dim /Out/, virtual void getFrameDim(FrameDim& frame_dim /Out/,
bool raw = false) = 0; bool raw = false) = 0;
virtual void getAcqFrameDim(FrameDim& frame_dim /Out/, virtual void getAcqFrameDim(FrameDim& frame_dim /Out/,
......
...@@ -1618,8 +1618,6 @@ void GlobalCPUAffinityMgr::setModelAffinity( ...@@ -1618,8 +1618,6 @@ void GlobalCPUAffinityMgr::setModelAffinity(
if (model_affinity_list == m_curr.model_threads) if (model_affinity_list == m_curr.model_threads)
return; return;
m_cam->m_model->setThreadCPUAffinity(model_affinity_list);
CPUAffinity buffer_affinity = CPUAffinityList_all(model_affinity_list); CPUAffinity buffer_affinity = CPUAffinityList_all(model_affinity_list);
m_cam->m_buffer.setAcqBufferCPUAffinity(buffer_affinity); m_cam->m_buffer.setAcqBufferCPUAffinity(buffer_affinity);
......
...@@ -103,7 +103,8 @@ Camera::AcqThread::ExceptionCleanUp::~ExceptionCleanUp() ...@@ -103,7 +103,8 @@ Camera::AcqThread::ExceptionCleanUp::~ExceptionCleanUp()
} }
Camera::AcqThread::AcqThread(Camera *cam) Camera::AcqThread::AcqThread(Camera *cam)
: m_cam(cam), m_cond(m_cam->m_cond), m_state(m_cam->m_state) : m_cam(cam), m_cond(m_cam->m_cond), m_state(m_cam->m_state),
m_acq_stopped(false)
{ {
DEB_CONSTRUCTOR(); DEB_CONSTRUCTOR();
} }
...@@ -136,10 +137,29 @@ void Camera::AcqThread::stop(AutoMutex& l, bool wait) ...@@ -136,10 +137,29 @@ void Camera::AcqThread::stop(AutoMutex& l, bool wait)
m_state = StopReq; m_state = StopReq;
m_cond.broadcast(); m_cond.broadcast();
{
AutoMutexUnlock u(l);
stopAcq();
}
while (wait && (m_state != Stopped) && (m_state != Idle)) while (wait && (m_state != Stopped) && (m_state != Idle))
m_cond.wait(); m_cond.wait();
} }
inline
Camera::AcqThread::Status Camera::AcqThread::newFrameReady(FrameType frame)
{
DEB_MEMBER_FUNCT();
HwFrameInfoType frame_info;
frame_info.acq_frame_nb = frame;
StdBufferCbMgr *cb_mgr = m_cam->m_buffer.getBufferCbMgr(LimaBuffer);
bool cont_acq = cb_mgr->newFrameReady(frame_info);
bool acq_end = (frame == m_cam->m_lima_nb_frames - 1);
cont_acq &= !acq_end;
return Status(cont_acq, acq_end);
}
void Camera::AcqThread::threadFunction() void Camera::AcqThread::threadFunction()
{ {
DEB_MEMBER_FUNCT(); DEB_MEMBER_FUNCT();
...@@ -160,43 +180,39 @@ void Camera::AcqThread::threadFunction() ...@@ -160,43 +180,39 @@ void Camera::AcqThread::threadFunction()
DEB_TRACE() << DEB_VAR1(m_state); DEB_TRACE() << DEB_VAR1(m_state);
m_cond.broadcast(); m_cond.broadcast();
Reconstruction *reconstruct = m_cam->m_model->getReconstruction();
SeqFilter seq_filter; SeqFilter seq_filter;
bool had_frames = false; bool had_frames = false;
bool cont_acq = true; bool cont_acq = true;
bool acq_end = false; bool acq_end = false;
do { FrameType next_frame = 0;
while ((m_state != StopReq) && m_frame_queue.empty()) {
if (!m_cond.wait(m_cam->m_new_frame_timeout)) { auto get_next_frame = [&]() {
DetFrameImagePackets packets = readRecvPackets(next_frame++);
FrameType frame = packets.first;
reconstruct->addFramePackets(std::move(packets));
return frame;
};
while ((m_state != StopReq) && cont_acq &&
(next_frame < m_cam->m_lima_nb_frames)) {
FrameType frame;
{
AutoMutexUnlock u(l); AutoMutexUnlock u(l);
try { frame = get_next_frame();
m_cam->checkLostPackets(); DEB_TRACE() << DEB_VAR2(next_frame, frame);
} catch (Exception& e) {
string name = ("Camera::AcqThread: "
"checkLostPackets");
m_cam->reportException(e, name);
}
}
} }
if (!m_frame_queue.empty()) { m_cam->m_buffer.waitLimaFrame(frame, l);
FrameType frame = m_frame_queue.front(); {
m_frame_queue.pop();
DEB_TRACE() << DEB_VAR1(frame);
seq_filter.addVal(frame);
SeqFilter::Range frames = seq_filter.getSeqRange();
if (frames.nb > 0) {
int f = frames.first;
do {
m_cam->m_buffer.waitLimaFrame(f, l);
AutoMutexUnlock u(l); AutoMutexUnlock u(l);
DEB_TRACE() << DEB_VAR1(f); Status status = newFrameReady(frame);
Status status = newFrameReady(f);
cont_acq = status.first; cont_acq = status.first;
acq_end = status.second; acq_end = status.second;
had_frames = true; had_frames = true;
} while ((++f != frames.end()) && cont_acq);
} }
} }
} while ((m_state != StopReq) && cont_acq);
AcqState prev_state = m_state; AcqState prev_state = m_state;
if (acq_end && m_cam->m_skip_frame_freq) { if (acq_end && m_cam->m_skip_frame_freq) {
...@@ -238,13 +254,47 @@ void Camera::AcqThread::threadFunction() ...@@ -238,13 +254,47 @@ void Camera::AcqThread::threadFunction()
m_cond.broadcast(); m_cond.broadcast();
} }
void Camera::AcqThread::queueFinishedFrame(FrameType frame) DetFrameImagePackets Camera::AcqThread::readRecvPackets(FrameType frame)
{ {
DEB_MEMBER_FUNCT(); DEB_MEMBER_FUNCT();
DEB_PARAM() << DEB_VAR1(frame);
AutoMutex l = m_cam->lock(); DetFrameImagePackets det_frame_packets{frame, {}};
m_frame_queue.push(frame); DetImagePackets& det_packets = det_frame_packets.second;
m_cond.broadcast(); FramePacketMap::iterator it = m_frame_packet_map.find(frame);
if (it != m_frame_packet_map.end()) {
det_packets = std::move(it->second);
m_frame_packet_map.erase(it);
}
auto stopped = [&]() { return (m_cam->getAcqState() == StopReq); };
int nb_recvs = m_cam->getNbRecvs();
for (int i = 0; i < nb_recvs; ++i) {
while ((det_packets.find(i) == det_packets.end()) &&
!stopped()) {
Receiver::ImagePackets *image_packets;
Receiver *recv = m_cam->m_recv_list[i];
image_packets = recv->readImagePackets();
if (!image_packets)
break;
typedef DetImagePackets::value_type MapEntry;
FrameType f = image_packets->frame;
if (f == frame) {
det_packets.emplace(MapEntry(i, image_packets));
break;
} else {
DetImagePackets& other = m_frame_packet_map[f];
other.emplace(MapEntry(i, image_packets));
}
}
}
if (stopped()) {
det_frame_packets.second.clear();
m_frame_packet_map.clear();
}
return det_frame_packets;
} }
void Camera::AcqThread::cleanUp(AutoMutex& l)