DABC (Data Acquisition Backbone Core)  2.9.9
MonitorModule.cxx
Go to the documentation of this file.
1 // $Id: MonitorModule.cxx 4697 2021-02-23 14:25:09Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #include "hadaq/MonitorModule.h"
17 
18 #include "dabc/Publisher.h"
19 #include "dabc/Buffer.h"
20 #include "hadaq/Iterator.h"
21 
22 #include <cstdlib>
23 #include <cstdio>
24 #include <fstream>
25 #include <streambuf>
26 
27 hadaq::MonitorModule::MonitorModule(const std::string &name, dabc::Command cmd) :
28  dabc::ModuleAsync(name, cmd),
29  fInterval(1.),
30  fTopFolder(),
31  fAddrs(),
32  fEventId(0)
33 {
35 
36  fSubevId = Cfg("SubevId", cmd).AsUInt(0);
37  fTriggerType = Cfg("TriggerType", cmd).AsUInt(1);
38  if (fTriggerType > 0xF) fTriggerType = 0xF;
39 
40  fInterval = Cfg("Period", cmd).AsDouble(1.);
41 
42  fTopFolder = Cfg("TopFolder", cmd).AsStr(fTopFolder);
43 
44  fAddrs0 = Cfg("Addrs0", cmd).AsUIntVect();
45  fAddrs = Cfg("Addrs", cmd).AsUIntVect();
46 
47  fShellCmd = Cfg("ShellCmd", cmd).AsStr("du -d 0 .");
48 
49  fWorkerHierarchy.Create("TRBNET");
50 
51  for (unsigned ix = 0; ix < fAddrs.size(); ++ix) {
53  DOUT0("Name = %s item %p", GetItemName(fAddrs[ix]).c_str(), item());
54  item.SetField(dabc::prop_kind, "rate");
55  item.EnableHistory(100);
56  }
57 
58  if (fInterval > 0)
59  CreateTimer("TrbNetRead", fInterval);
60 
61  if (fTopFolder.empty())
62  Publish(fWorkerHierarchy, "TRBNET");
63  else
64  Publish(fWorkerHierarchy, std::string("TRBNET/") + fTopFolder);
65 }
66 
67 std::string hadaq::MonitorModule::GetItemName(unsigned addr)
68 {
69  return dabc::format("addr_%04x", addr);
70 }
71 
72 
74 {
76 
77 }
78 
79 uint32_t hadaq::MonitorModule::DoRead(uint32_t addr0, uint32_t addr)
80 {
81  std::string tmpfile = "output.log";
82 
83  std::string cmd;
84  if (addr0 > 0)
85  cmd = dabc::format(fShellCmd.c_str(), addr0, addr);
86  else
87  cmd = dabc::format(fShellCmd.c_str(), addr);
88 
89  cmd.append(dabc::format(" >%s 2<&1", tmpfile.c_str()));
90 
91  // execute command
92  auto res = std::system(cmd.c_str());
93  (void) res; // suppress compiler warnings
94 
95  std::ifstream t(tmpfile);
96  std::string str((std::istreambuf_iterator<char>(t)),
97  std::istreambuf_iterator<char>());
98 
99  // remove temporary file
100  std::remove(tmpfile.c_str());
101 
102  unsigned value1 = 0, value2 = 0;
103 
104  std::sscanf(str.c_str(), "0x%x 0x%x", &value1, &value2);
105 
106  // DOUT0("Reading addr %x result %s 0x%x", addr, str.c_str(), value2);
107 
108  return value2;
109 }
110 
111 
113 {
114  if (buf.null()) return false;
115 
117 
118  // initialzie buffer
119  int block_size = (fAddrs0.size() == 0) ? 2 : 3;
120 
121  hadaq::WriteIterator iter(buf);
122  if (!iter.NewEvent(fEventId, 0 ,fAddrs.size() * 4 * block_size + sizeof(hadaq::RawSubevent))) return false;
123  if (!iter.NewSubevent(fAddrs.size() * 4 * block_size, fEventId)) return false;
124 
125  // iter.evnt()->SetTrigTypeTrb3(0xC);
126 
127  uint32_t id = iter.evnt()->GetId();
128  iter.evnt()->SetId((id & 0xfffffff0) | (fTriggerType & 0xf));
129 
130  iter.subevnt()->SetDecodingDirect(0x11000200); // swapped mode, must be first
131  iter.subevnt()->SetTrigNr(fEventId); // assign again, otherwise swapping applyed wrong
132  iter.subevnt()->SetTrigTypeTrb3(fTriggerType); // trigger type
133  iter.subevnt()->SetId(fSubevId); // subevent id
134 
135  // write raw data
136  uint32_t *rawdata = (uint32_t *) iter.rawdata();
137  for (unsigned n=0; n < fAddrs.size(); ++n) {
138 
139  unsigned addr0 = 0;
140  if (block_size == 3) {
141  addr0 = (fAddrs0.size() >= fAddrs.size()) ? fAddrs0[n] : fAddrs0[0];
142  iter.subevnt()->SetValue(rawdata++, addr0); // write address0
143  }
144 
145  iter.subevnt()->SetValue(rawdata++, fAddrs[n]); // write address
146  // iter.subevnt()->SetValue(rawdata++, 0x1234567); // write value
147 
148  iter.subevnt()->SetValue(rawdata++, DoRead(addr0, fAddrs[n])); // write value
149  }
150 
151  // closing buffer
152  iter.FinishSubEvent(fAddrs.size() * 4 * block_size);
153  iter.FinishEvent();
154 
155  fEventId++;
156 
157  buf = iter.Close();
158 
159  return !buf.null();
160 }
161 
162 
164 {
165  if (TimerName(timer) != "TrbNetRead")
166  return;
167 
168  dabc::Buffer buf = TakeBuffer();
169 
170  if (ReadAllVariables(buf))
171  SendToAllOutputs(buf);
172 }
173 
175 {
176  if (fInterval > 0) return;
177 
178  dabc::Buffer buf = TakeBuffer();
179 
180  if (ReadAllVariables(buf))
181  SendToAllOutputs(buf);
182 }
Reference on memory from memory pool.
Definition: Buffer.h:135
void SetTypeId(unsigned tid)
Definition: Buffer.h:151
Represents command with its arguments.
Definition: Command.h:99
Represents objects hierarchy of remote (or local) DABC process.
Definition: Hierarchy.h:285
Hierarchy CreateHChild(const std::string &name, bool allowslahes=false, bool sortorder=false)
Create child item in hierarchy with specified name If allowslahes enabled, instead of subfolders item...
Definition: Hierarchy.h:392
void Create(const std::string &name, bool withmutex=false)
Create top-level object with specified name.
Definition: Hierarchy.cxx:934
void EnableHistory(unsigned length=100, bool withchilds=false)
Activate history production for selected element and its childs.
Definition: Hierarchy.cxx:837
virtual void OnThreadAssigned()
Definition: Module.cxx:79
unsigned CreateTimer(const std::string &name, double period_sec=-1., bool synchron=false)
Definition: Module.cxx:109
void EnsurePorts(unsigned numinp=0, unsigned numout=0, const std::string &poolname="")
Method ensure that at least specified number of input and output ports will be created.
Definition: Module.cxx:66
std::vector< uint64_t > AsUIntVect() const
Definition: Record.cxx:631
uint64_t AsUInt(uint64_t dflt=0) const
Definition: Record.cxx:525
std::string AsStr(const std::string &dflt="") const
Definition: Record.cxx:749
double AsDouble(double dflt=0.) const
Definition: Record.cxx:549
bool SetField(const std::string &name, const RecordField &v)
Definition: Record.h:516
bool null() const
Returns true if reference contains nullptr.
Definition: Reference.h:151
Hierarchy fWorkerHierarchy
place for publishing of worker parameters
Definition: Worker.h:168
RecordField Cfg(const std::string &name, Command cmd=nullptr) const
Returns configuration field of specified name Configuration value of specified name searched in follo...
Definition: Worker.cxx:521
virtual bool Publish(const Hierarchy &h, const std::string &path)
Definition: Worker.cxx:1075
double fInterval
Time interval for reading in seconds.
Definition: MonitorModule.h:35
std::string GetItemName(unsigned addr)
unsigned fTriggerType
trigger type
Definition: MonitorModule.h:38
std::string fTopFolder
name of top folder, which should exists also in every variable
Definition: MonitorModule.h:36
std::string fShellCmd
shell command with formats pattern
Definition: MonitorModule.h:39
std::vector< uint64_t > fAddrs
array of monitored address
Definition: MonitorModule.h:42
bool ReadAllVariables(dabc::Buffer &buf)
void ProcessTimerEvent(unsigned timer) override
Method called by framework when timer event is produced.
std::vector< uint64_t > fAddrs0
array of monitored address
Definition: MonitorModule.h:41
uint32_t DoRead(uint32_t addr0, uint32_t addr)
MonitorModule(const std::string &name, dabc::Command cmd=nullptr)
void BeforeModuleStart() override
void OnThreadAssigned() override
unsigned fSubevId
subevent id
Definition: MonitorModule.h:37
Write iterator for HADAQ events/subevents.
Definition: Iterator.h:104
hadaq::RawEvent * evnt() const
Definition: Iterator.h:133
void * rawdata() const
Definition: Iterator.h:135
bool NewEvent(uint32_t evtSeqNr=0, uint32_t runNr=0, uint32_t minsubeventsize=0)
Definition: Iterator.cxx:333
bool FinishSubEvent(uint32_t rawdatasz)
Definition: Iterator.cxx:367
dabc::Buffer Close()
Definition: Iterator.cxx:309
bool NewSubevent(uint32_t minrawsize=0, uint32_t trigger=0)
Definition: Iterator.cxx:353
hadaq::RawSubevent * subevnt() const
Definition: Iterator.h:134
#define DOUT0(args ...)
Definition: logging.h:156
Event manipulation API.
Definition: api.h:23
const char * xmlWorkPool
Definition: Object.cxx:46
std::string format(const char *fmt,...)
Definition: string.cxx:49
const char * prop_kind
Definition: Hierarchy.cxx:29
@ mbt_HadaqEvents
Definition: HadaqTypeDefs.h:24
void SetId(uint32_t id)
Definition: defines.h:209
uint32_t GetId() const
Definition: defines.h:208
void SetValue(uint32_t *member, uint32_t val)
swapsave method to set value stolen from hadtu.h
Definition: defines.h:166
void SetDecodingDirect(uint32_t dec)
Definition: defines.h:150
Hadaq subevent structure.
Definition: defines.h:262
void SetTrigNr(uint32_t trigger)
Definition: defines.h:275
void SetTrigTypeTrb3(uint8_t typ)
Definition: defines.h:280