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

Move definitions in Camera to Defs, Model and CPUAffinity modules

parent 208df25a
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2011
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//###########################################################################
#ifndef __SLS_DETECTOR_CPU_AFFINITY_H
#define __SLS_DETECTOR_CPU_AFFINITY_H
#include "SlsDetectorDefs.h"
#include "lima/CtControl.h"
#include "lima/SimplePipe.h"
namespace lima
{
namespace SlsDetector
{
class CPUAffinity
{
DEB_CLASS_NAMESPC(DebModCamera, "CPUAffinity", "SlsDetector");
public:
CPUAffinity(uint64_t m = 0) : m_mask(m)
{}
static bool UseSudo;
static int getNbCPUs(bool max_nb = false);
static uint64_t allCPUs(bool max_nb = false)
{ return (uint64_t(1) << getNbCPUs(max_nb)) - 1; }
void initCPUSet(cpu_set_t& cpu_set) const;
void applyToTask(pid_t task, bool incl_threads = true,
bool use_taskset = true) const;
operator uint64_t() const
{ return m_mask ? m_mask : allCPUs(); }
CPUAffinity& operator =(uint64_t m)
{ m_mask = m; return *this; }
bool isDefault() const
{ return !m_mask || (m_mask == allCPUs()); }
static std::string getProcDir(bool local_threads);
static std::string getTaskProcDir(pid_t task, bool is_thread);
private:
void applyWithTaskset(pid_t task, bool incl_threads) const;
void applyWithSetAffinity(pid_t task, bool incl_threads) const;
static int findNbCPUs();
static int findMaxNbCPUs();
uint64_t m_mask;
};
inline
bool operator ==(const CPUAffinity& a, const CPUAffinity& b)
{
uint64_t mask = CPUAffinity::allCPUs();
return (uint64_t(a) & mask) == (uint64_t(b) & mask);
}
inline
bool operator !=(const CPUAffinity& a, const CPUAffinity& b)
{
return !(a == b);
}
class ProcCPUAffinityMgr
{
DEB_CLASS_NAMESPC(DebModCamera, "ProcCPUAffinityMgr", "SlsDetector");
public:
enum Filter {
All, MatchAffinity, NoMatchAffinity, ThisProc=0x10,
};
ProcCPUAffinityMgr();
~ProcCPUAffinityMgr();
static ProcList getProcList(Filter filter = All,
CPUAffinity cpu_affinity = 0);
static ProcList getThreadList(Filter filter = All,
CPUAffinity cpu_affinity = 0);
void setOtherCPUAffinity(CPUAffinity cpu_affinity);
private:
class WatchDog
{
DEB_CLASS_NAMESPC(DebModCamera, "WatchDog",
"SlsDetector::ProcCPUAffinityMgr");
public:
WatchDog();
~WatchDog();
bool childEnded();
void setOtherCPUAffinity(CPUAffinity cpu_affinity);
private:
enum Cmd {
Init, SetAffinity, CleanUp, Ok,
};
typedef uint64_t Arg;
struct Packet {
Cmd cmd;
Arg arg;
};
static void sigTermHandler(int signo);
void childFunction();
void affinitySetter(CPUAffinity cpu_affinity);
ProcList getOtherProcList(CPUAffinity cpu_affinity);
void sendChildCmd(Cmd cmd, Arg arg = 0);
Packet readParentCmd();
void ackParentCmd();
Pipe m_cmd_pipe;
Pipe m_res_pipe;
pid_t m_lima_pid;
pid_t m_child_pid;
};
AutoPtr<WatchDog> m_watchdog;
};
struct SystemCPUAffinity {
CPUAffinity recv;
CPUAffinity lima;
CPUAffinity other;
};
typedef std::map<PixelDepth, SystemCPUAffinity> PixelDepthCPUAffinityMap;
class SystemCPUAffinityMgr
{
DEB_CLASS_NAMESPC(DebModCamera, "SystemCPUAffinityMgr",
"SlsDetector");
public:
class ProcessingFinishedEvent
{
DEB_CLASS_NAMESPC(DebModCamera, "ProcessingFinishedEvent",
"SlsDetector::SystemCPUAffinityMgr");
public:
ProcessingFinishedEvent(SystemCPUAffinityMgr *mgr);
~ProcessingFinishedEvent();
void processingFinished();
void registerStatusCallback(CtControl *ct_control);
private:
friend class SystemCPUAffinityMgr;
class ImageStatusCallback :
public CtControl::ImageStatusCallback
{
public:
ImageStatusCallback(
ProcessingFinishedEvent *proc_finished)
: m_proc_finished(proc_finished)
{}
protected:
virtual void imageStatusChanged(
const CtControl::ImageStatus& status)
{ m_proc_finished->imageStatusChanged(status); }
private:
ProcessingFinishedEvent *m_proc_finished;
};
void prepareAcq();
void stopAcq();
void limitUpdateRate();
void updateLastCallbackTimestamp();
Timestamp getLastCallbackTimestamp();
void imageStatusChanged(const CtControl::ImageStatus& status);
SystemCPUAffinityMgr *m_mgr;
ImageStatusCallback m_cb;
CtControl *m_ct;
int m_nb_frames;
bool m_cnt_act;
bool m_saving_act;
bool m_stopped;
Timestamp m_last_cb_ts;
};
SystemCPUAffinityMgr(Camera *cam = NULL);
~SystemCPUAffinityMgr();
void applyAndSet(const SystemCPUAffinity& o);
void updateRecvRestart();
ProcessingFinishedEvent *getProcessingFinishedEvent();
void prepareAcq();
void startAcq();
void stopAcq();
void recvFinished();
void limaFinished();
void waitLimaFinished();
private:
friend class ProcessingFinishedEvent;
enum State {
Ready, Acquiring, Changing, Processing, Restoring,
};
void setLimaAffinity(CPUAffinity lima_affinity);
void setRecvAffinity(CPUAffinity recv_affinity);
AutoMutex lock()
{ return AutoMutex(m_cond.mutex()); }
Camera *m_cam;
ProcList m_lima_tids;
SystemCPUAffinity m_curr;
SystemCPUAffinity m_set;
AutoPtr<ProcCPUAffinityMgr> m_proc_mgr;
Cond m_cond;
State m_state;
ProcessingFinishedEvent *m_proc_finished;
double m_lima_finished_timeout;
};
std::ostream& operator <<(std::ostream& os, const CPUAffinity& a);
std::ostream& operator <<(std::ostream& os, const SystemCPUAffinity& a);
std::ostream& operator <<(std::ostream& os, const PixelDepthCPUAffinityMap& m);
} // namespace SlsDetector
} // namespace lima
#endif // __SLS_DETECTOR_CPU_AFFINITY_H
......@@ -24,19 +24,16 @@
#define __SLS_DETECTOR_CAMERA_H
#include "SlsDetectorArgs.h"
#include "SlsDetectorDefs.h"
#include "SlsDetectorModel.h"
#include "SlsDetectorCPUAffinity.h"
#include "multiSlsDetector.h"
#include "slsReceiverUsers.h"
#include "lima/RegExUtils.h"
#include "lima/HwBufferMgr.h"
#include "lima/HwMaxImageSizeCallback.h"
#include "lima/Event.h"
#include "lima/SimplePipe.h"
#include "lima/CtControl.h"
#include <set>
#include <queue>
namespace lima
......@@ -45,49 +42,6 @@ namespace lima
namespace SlsDetector
{
template <class T>
class PrettyList
{
public:
typedef typename T::const_iterator const_iterator;
PrettyList(const T& l) : begin(l.begin()), end(l.end()) {}
PrettyList(const_iterator b, const_iterator e) : begin(b), end(e) {}
ostream& print(ostream& os) const
{
os << "[";
int prev;
bool in_seq = false;
bool first = true;
for (const_iterator it = begin; it != end; ++it) {
int val = *it;
bool seq = (!first && (val == prev + 1));
if (!seq) {
if (in_seq)
os << "-" << prev;
os << (first ? "" : ",") << val;
}
prev = val;
in_seq = seq;
first = false;
}
if (in_seq)
os << "-" << prev;
return os << "]";
}
private:
const_iterator begin, end;
};
template <class T>
std::ostream& operator <<(std::ostream& os, const PrettyList<T>& pl)
{
return pl.print(os);
}
class Camera : public HwMaxImageSizeCallbackGen, public EventCallbackGen
{
DEB_CLASS_NAMESPC(DebModCamera, "Camera", "SlsDetector");
......@@ -101,555 +55,6 @@ public:
typedef Defs::ReadoutFlags ReadoutFlags;
typedef Defs::DetStatus DetStatus;
enum State {
Idle, Init, Starting, Running, StopReq, Stopping, Stopped,
};
enum Type {
UnknownDet, GenericDet, EigerDet, JungfrauDet,
};
enum PixelDepth {
PixelDepth4 = 4,
PixelDepth8 = 8,
PixelDepth16 = 16,
PixelDepth32 = 32,
};
typedef uint64_t FrameType;
typedef std::vector<std::string> StringList;
typedef StringList NameList;
typedef std::vector<int> IntList;
typedef std::vector<double> FloatList;
typedef std::set<int> SortedIntList;
typedef std::vector<FrameType> FrameArray;
typedef IntList ProcList;
struct TimeRanges {
TimeRanges() :
min_exp_time(-1.),
max_exp_time(-1.),
min_lat_time(-1.),
max_lat_time(-1.),
min_frame_period(-1.),
max_frame_period(-1.)
{}
double min_exp_time;
double max_exp_time;
double min_lat_time;
double max_lat_time;
double min_frame_period;
double max_frame_period;
};
class TimeRangesChangedCallback {
DEB_CLASS_NAMESPC(DebModCamera, "TimeRangesChangedCallback",
"SlsDetector::Camera");
public:
TimeRangesChangedCallback();
virtual ~TimeRangesChangedCallback();
protected:
virtual void timeRangesChanged(TimeRanges time_ranges) = 0;
private:
friend class Camera;
Camera *m_cam;
};
class Glob
{
DEB_CLASS_NAMESPC(DebModCamera, "Glob", "SlsDetector::Camera");
public:
Glob(std::string pattern = "");
Glob& operator =(std::string pattern);
static StringList find(std::string pattern);
static StringList split(std::string path);
int getNbEntries() const
{ return m_found_list.size(); }
StringList getPathList() const
{ return m_found_list; }
StringList getSubPathList(int idx) const;
private:
std::string m_pattern;
StringList m_found_list;
};
class NumericGlob
{
DEB_CLASS_NAMESPC(DebModCamera, "NumericGlob",
"SlsDetector::Camera");
public:
typedef std::pair<int, std::string> IntString;
typedef std::vector<IntString> IntStringList;
NumericGlob(std::string pattern_prefix,
std::string pattern_suffix = "");
int getNbEntries() const
{ return m_glob.getNbEntries(); }
IntStringList getIntPathList() const;
private:
int m_nb_idx;
int m_prefix_len;
int m_suffix_len;
Glob m_glob;
};
class CPUAffinity
{
DEB_CLASS_NAMESPC(DebModCamera, "CPUAffinity",
"SlsDetector::Camera");
public:
CPUAffinity(uint64_t m = 0) : m_mask(m)
{}
static bool UseSudo;
static int getNbCPUs(bool max_nb = false);
static uint64_t allCPUs(bool max_nb = false)
{ return (uint64_t(1) << getNbCPUs(max_nb)) - 1; }
void initCPUSet(cpu_set_t& cpu_set) const;
void applyToTask(pid_t task, bool incl_threads = true,
bool use_taskset = true) const;
operator uint64_t() const
{ return m_mask ? m_mask : allCPUs(); }
CPUAffinity& operator =(uint64_t m)
{ m_mask = m; return *this; }
bool isDefault() const
{ return !m_mask || (m_mask == allCPUs()); }
static std::string getProcDir(bool local_threads);
static std::string getTaskProcDir(pid_t task, bool is_thread);
private:
void applyWithTaskset(pid_t task, bool incl_threads) const;
void applyWithSetAffinity(pid_t task, bool incl_threads) const;
static int findNbCPUs();
static int findMaxNbCPUs();
uint64_t m_mask;
};
class ProcCPUAffinityMgr
{
DEB_CLASS_NAMESPC(DebModCamera, "ProcCPUAffinityMgr",
"SlsDetector::Camera");
public:
enum Filter {
All, MatchAffinity, NoMatchAffinity, ThisProc=0x10,
};
ProcCPUAffinityMgr();
~ProcCPUAffinityMgr();
static ProcList getProcList(Filter filter = All,
CPUAffinity cpu_affinity = 0);
static ProcList getThreadList(Filter filter = All,
CPUAffinity cpu_affinity = 0);
void setOtherCPUAffinity(CPUAffinity cpu_affinity);
private:
class WatchDog
{
DEB_CLASS_NAMESPC(DebModCamera, "WatchDog",
"SlsDetector::"
"Camera::ProcCPUAffinityMgr");
public:
WatchDog();
~WatchDog();
bool childEnded();
void setOtherCPUAffinity(CPUAffinity cpu_affinity);
private:
enum Cmd {
Init, SetAffinity, CleanUp, Ok,
};
typedef uint64_t Arg;
struct Packet {
Cmd cmd;
Arg arg;
};
static void sigTermHandler(int signo);
void childFunction();
void affinitySetter(CPUAffinity cpu_affinity);
ProcList getOtherProcList(CPUAffinity cpu_affinity);
void sendChildCmd(Cmd cmd, Arg arg = 0);
Packet readParentCmd();
void ackParentCmd();
Pipe m_cmd_pipe;
Pipe m_res_pipe;
pid_t m_lima_pid;
pid_t m_child_pid;
};
AutoPtr<WatchDog> m_watchdog;
};
struct SystemCPUAffinity {
CPUAffinity recv;
CPUAffinity lima;
CPUAffinity other;
};
typedef std::map<PixelDepth, SystemCPUAffinity>
PixelDepthCPUAffinityMap;
class SystemCPUAffinityMgr
{
DEB_CLASS_NAMESPC(DebModCamera, "SystemCPUAffinityMgr",
"SlsDetector::Camera");
public:
class ProcessingFinishedEvent
{
DEB_CLASS_NAMESPC(DebModCamera, "ProcessingFinishedEvent",
"SlsDetector::Camera::SystemCPUAffinityMgr");
public:
ProcessingFinishedEvent(SystemCPUAffinityMgr *mgr);
~ProcessingFinishedEvent();
void processingFinished();
void registerStatusCallback(CtControl *ct_control);
private:
friend class SystemCPUAffinityMgr;
class ImageStatusCallback :
public CtControl::ImageStatusCallback
{
public:
ImageStatusCallback(
ProcessingFinishedEvent *proc_finished)
: m_proc_finished(proc_finished)
{}
protected:
virtual void imageStatusChanged(
const CtControl::ImageStatus& status)
{ m_proc_finished->imageStatusChanged(status); }
private:
ProcessingFinishedEvent *m_proc_finished;
};
void prepareAcq();
void stopAcq();
void limitUpdateRate();
void updateLastCallbackTimestamp();
Timestamp getLastCallbackTimestamp();
void imageStatusChanged(
const CtControl::ImageStatus& status);
SystemCPUAffinityMgr *m_mgr;
ImageStatusCallback m_cb;
CtControl *m_ct;
int m_nb_frames;
bool m_cnt_act;
bool m_saving_act;
bool m_stopped;
Timestamp m_last_cb_ts;
};
SystemCPUAffinityMgr(Camera *cam = NULL);
~SystemCPUAffinityMgr();
void applyAndSet(const SystemCPUAffinity& o);
void updateRecvRestart();
ProcessingFinishedEvent *getProcessingFinishedEvent();
void prepareAcq();
void startAcq();