ATLAS Offline Software
PerfMonStorePayloadMon.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // StorePayloadMon.cxx
8 // Implementation file for class StorePayloadMon
9 // Author: S.Binet<binet@cern.ch>
11 
12 // PerfMonComps includes
13 #include "PerfMonEvent/mallinfo.h"
14 #include "PerfMonStorePayloadMon.h"
15 
16 // C includes
17 #include <stdio.h>
18 #include <string.h>
19 #include <fcntl.h>
20 
21 // STL includes
22 #include <vector>
23 #include <sstream>
24 
25 // FrameWork includes
26 #include "Gaudi/Property.h"
27 #include "SGTools/DataProxy.h"
28 
29 // pmon edm
31 
32 namespace PerfMon {
33 
34 struct DpLoad
35 {
37  MallocStats::return_type b0; //> number of bytes before flushing proxy
38  MallocStats::return_type b1; //> number of bytes after flushing proxy
39 };
40 
41  typedef ::std::vector<DpLoad> DpLoads_t;
42 
44 // Public methods:
46 
47 // Constructors
50  ISvcLocator* pSvcLocator ) :
51  ::AthAlgorithm( name, pSvcLocator ),
52  m_store("StoreGateSvc/StoreGateSvc", name),
53  m_clidsvc("ClassIDSvc/ClassIDSvc", name),
54  m_stream(-1),
55  m_stream_name("pmon_payload.ascii")
56 {
57  //
58  // Property declaration
59  //
60  //declareProperty( "Property", m_nProperty );
61 
62  declareProperty("Store", m_store,
63  "StoreGateSvc instance to monitor for "
64  "DataProxy payload sizes");
65 
66  declareProperty("OutputName",
67  m_stream_name = "pmon_payload.ascii",
68  "Name of the output file where the monitoring data will "
69  "be stored");
70 
71  declareProperty("DisplayMallinfos",
72  m_displayMallinfos = false,
73  "display mallinfos stats after each event");
74 }
75 
76 // Destructor
79 {
80  if (m_stream>0) {
81  fflush(NULL); //> flushes all output streams
82  close(m_stream);
83  }
84 
85 }
86 
87 // Athena Algorithm's Hooks
90 {
91  // retrieve store to monitor
92  if (!m_store.retrieve().isSuccess()) {
93  ATH_MSG_ERROR("could not retrieve [" << m_store.typeAndName() << "]");
94  return StatusCode::FAILURE;
95  }
96 
97  // retrieve clidsvc
98  if (!m_clidsvc.retrieve().isSuccess()) {
99  ATH_MSG_ERROR("could not retrieve [" << m_clidsvc.typeAndName() << "]");
100  return StatusCode::FAILURE;
101  }
102 
103  // open payload-mon stream
104  m_stream = open(m_stream_name.c_str(),
105  O_WRONLY | O_CREAT,
106  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
107  if (m_stream<0) {
108  ATH_MSG_ERROR("could not open payload-mon stream file ["
109  << m_stream_name << "]");
110  return StatusCode::FAILURE;
111  }
112 
113  { // describe data format
114  const char* hdr = "#data format: [[(bytes_0, bytes_1, delta, clid, sg_key),]]\n";
115  write(m_stream, hdr, strlen(hdr));
116  const char* data_hdr = "data = [\n";
117  write(m_stream, data_hdr, strlen(data_hdr));
118  }
119 
120  return StatusCode::SUCCESS;
121 }
122 
124 {
125  {
126  const char *ftr = "] # data\n";
127  write(m_stream, ftr, strlen(ftr));
128  }
129  return StatusCode::SUCCESS;
130 }
131 
132 inline
135 {
137  return m.uordblks + m.hblkhd - m.fsmblks;
138 }
139 
140 #if 0
141 inline
142 std::string
143 diff_minfos(const PerfMon::MallocStats& ms0,
144  const PerfMon::MallocStats& ms1)
145 {
146  const struct mallinfo& m0 = ms0.m_infos;
147  const struct mallinfo& m1 = ms1.m_infos;
148 
149 #define PP_DIFF(f) m0. f << " -> " << m1. f << " => " << m1. f - m0. f
150 #define PP_TOT(m) (m.uordblks + m.hblkhd - m.fsmblks)
151 
152  std::ostringstream s;
153  s << "-------------\n"
154  << "arena : "<<PP_DIFF(arena)<< "\n"
155  << "ordblks : "<<PP_DIFF(ordblks)<< "\n" /* number of free chunks */
156  << "smblks : "<<PP_DIFF(smblks)<< "\n" /* number of fastbin blocks */
157  << "hblks : "<<PP_DIFF(hblks)<< "\n" /* number of mmapped regions */
158  << "hblkhd : "<<PP_DIFF(hblkhd)<< "\n" /* space in mmapped regions */
159  << "usmblks : "<<PP_DIFF(usmblks)<< "\n" /* maximum total allocated space */
160  << "fsmblks : "<<PP_DIFF(fsmblks)<< "\n" /* space available in freed fastbin blocks */
161  << "uordblks: "<<PP_DIFF(uordblks)<< "\n" /* total allocated space */
162  << "fordblks: "<<PP_DIFF(fordblks)<< "\n" /* total free space */
163  << "keepcost: "<<PP_DIFF(keepcost)<< "\n" /* top-most, releasable (via malloc */
164  << "==> " << PP_TOT(m0) << " -> " << PP_TOT(m1)
165  << " => " << (PP_TOT(m1) - PP_TOT(m0))
166  << "\n"
167  << std::ends;
168 
169 #undef PP_DIFF
170 #undef PP_TOT
171 
172  return s.str();
173 }
174 #endif
175 
177 {
178  std::vector<const SG::DataProxy*> proxies = m_store->proxies();
179  const std::size_t n = proxies.size();
180  DpLoads_t data(n);
181 
183  for (std::size_t i = 0; i!=n; ++i) {
184  SG::DataProxy* proxy = const_cast<SG::DataProxy*>(proxies[i]); // Blech!
185  DpLoad& mon = data[i];
186  mon.proxy = proxy;
187  mon.b0 = -1;
188  mon.b1 = -1;
189  if (proxy->clID() == 2101) { // event-info
190  // do not flush this one, as it is needed everywhere...
191  continue;
192  }
193 #ifndef __APPLE__
194  malloc_trim(0); // compact heap...
195 #endif
196  mon.b0 = pmon_get_mem();
197  m_store->clearProxyPayload(proxy);
198  mon.b1 = pmon_get_mem();
199 
200  }
202 
203  if (m_displayMallinfos) {
205  //malloc_stats();
206  }
207 
208  const char* hdr = " [ ## new-event\n";
209  write(m_stream, hdr, strlen(hdr));
210 
211 #define PMON_DP_FMT " (%10d, %10d, %10d, \"%s\", \"%s\"),\n"
212  for (std::size_t i = 0; i!=n; ++i) {
213  DpLoad& mon = data[i];
214  std::string clid = "";
215  if (!m_clidsvc->getTypeNameOfID(mon.proxy->clID(), clid).isSuccess()) {
216  std::stringstream o;
217  o << mon.proxy->clID() << std::ends;
218  clid = o.str();
219  }
220  char *buf = NULL;
221  int buf_sz = asprintf
222  (&buf,
223  PMON_DP_FMT,
224  mon.b0, mon.b1, mon.b1 - mon.b0,
225  clid.c_str(),
226  mon.proxy->name().c_str());
227  write(m_stream, buf, buf_sz);
228  free(buf);
229  }
230 
231  char *buf = NULL;
232  int buf_sz = asprintf
233  (&buf,
234  PMON_DP_FMT,
235  b0, b1, b1 - b0,
236  "StoreGateSvc", m_store.name().c_str());
237  write(m_stream, buf, buf_sz);
238  free(buf);
239 
240 #undef PMON_DP_FMT
241 
242  const char* ftr = " ],\n";
243  write(m_stream, ftr, strlen(ftr));
244 
245  ATH_MSG_INFO("flush-store: " << b0 << " -> " << b1 << " -- delta= "
246  << b1 -b0);
247 
248  return StatusCode::SUCCESS;
249 }
250 
251 
252 } //> end namespace PerfMon
PerfMon::DpLoad::b0
MallocStats::return_type b0
Definition: PerfMonStorePayloadMon.cxx:37
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
PerfMon::DpLoad::proxy
const SG::DataProxy * proxy
Definition: PerfMonStorePayloadMon.cxx:36
PerfMon::DpLoad
Definition: PerfMonStorePayloadMon.cxx:35
PerfMonStorePayloadMon.h
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
StateLessPT_NewConfig.proxy
proxy
Definition: StateLessPT_NewConfig.py:392
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
MallocStats.h
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
PerfMon::StorePayloadMon::StorePayloadMon
StorePayloadMon()
Default constructor:
PerfMon::StorePayloadMon::m_stream
int m_stream
payload-mon file descriptor
Definition: PerfMonStorePayloadMon.h:73
PerfMon::StorePayloadMon::execute
virtual StatusCode execute()
Definition: PerfMonStorePayloadMon.cxx:176
PerfMon::mallinfo
mallinfo_t mallinfo()
Definition: mallinfo.h:29
PerfMon::StorePayloadMon::~StorePayloadMon
virtual ~StorePayloadMon()
Destructor:
Definition: PerfMonStorePayloadMon.cxx:78
PerfMon::MallocStats::return_type
int return_type
Definition: MallocStats.h:36
PerfMon::pmon_get_mem
PerfMon::MallocStats::return_type pmon_get_mem()
Definition: PerfMonStorePayloadMon.cxx:134
PerfMon::MallocStats::dump
void dump(std::ostream &out=std::cout)
display malloc stats on std::cout
Definition: MallocStats.cxx:70
PerfMon
a simple malloc wrapper that keeps track of the amount of memory allocated on the heap.
Definition: CallGraphAuditor.cxx:24
python.changerun.m1
m1
Definition: changerun.py:32
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
PerfMon::DpLoads_t
::std::vector< DpLoad > DpLoads_t
Definition: PerfMonStorePayloadMon.cxx:41
lumiFormat.i
int i
Definition: lumiFormat.py:92
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
python.ByteStreamConfig.write
def write
Definition: Event/ByteStreamCnvSvc/python/ByteStreamConfig.py:248
PerfMon::StorePayloadMon::m_clidsvc
IClassIDSvc_t m_clidsvc
handle to the class id svc
Definition: PerfMonStorePayloadMon.h:70
PerfMon::mallinfo_t
struct mallinfo mallinfo_t
Definition: mallinfo.h:28
mallinfo.h
Wrappers for mallinfo.
AthAlgorithm
Definition: AthAlgorithm.h:47
PerfMon::MallocStats::m_infos
PerfMon::mallinfo_t m_infos
Definition: MallocStats.h:28
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
Trk::open
@ open
Definition: BinningType.h:40
plotBeamSpotMon.mon
mon
Definition: plotBeamSpotMon.py:67
PMON_DP_FMT
#define PMON_DP_FMT
SG::DataProxy
Definition: DataProxy.h:44
PerfMon::StorePayloadMon::finalize
virtual StatusCode finalize()
Definition: PerfMonStorePayloadMon.cxx:123
PerfMon::StorePayloadMon::m_displayMallinfos
bool m_displayMallinfos
display mallinfos after each event
Definition: PerfMonStorePayloadMon.h:79
PerfMon::StorePayloadMon::m_store
StoreGateSvc_t m_store
handle to the store instance to monitor
Definition: PerfMonStorePayloadMon.h:66
PerfMon::DpLoad::b1
MallocStats::return_type b1
Definition: PerfMonStorePayloadMon.cxx:38
PerfMon::MallocStats
Access to some useful malloc statistics.
Definition: MallocStats.h:27
PerfMon::StorePayloadMon::m_stream_name
std::string m_stream_name
Name of the output file where the monitoring data will be stored.
Definition: PerfMonStorePayloadMon.h:76
PerfMon::StorePayloadMon::initialize
virtual StatusCode initialize()
Definition: PerfMonStorePayloadMon.cxx:89
DataProxy.h