xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XrdClRecorder.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // Copyright (c) 2011-2017 by European Organization for Nuclear Research (CERN)
3 // Author: Michal Simon <michal.simon@cern.ch>
4 //------------------------------------------------------------------------------
5 // This file is part of the XRootD software suite.
6 //
7 // XRootD is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // XRootD is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with XRootD. If not, see <http://www.gnu.org/licenses/>.
19 //
20 // In applying this licence, CERN does not waive the privileges and immunities
21 // granted to it by virtue of its status as an Intergovernmental Organization
22 // or submit itself to any jurisdiction.
23 
24 #ifndef XRDCK_RECORDER_HH_
25 #define XRDCK_RECORDER_HH_
26 
28 #include "XrdCl/XrdClConstants.hh"
29 #include "XrdCl/XrdClDefaultEnv.hh"
30 #include "XrdCl/XrdClLog.hh"
31 #include "XrdClAction.hh"
32 
33 #include <mutex>
34 #include <fcntl.h>
35 
36 namespace XrdCl
37 {
38 //------------------------------------------------------------------------------
41 //------------------------------------------------------------------------------
42 class Recorder: public FilePlugIn
43 {
44  //----------------------------------------------------------------------------
47  //----------------------------------------------------------------------------
48  class Output
49  {
50  public:
51 
52  //------------------------------------------------------------------------
55  //------------------------------------------------------------------------
56  inline static Output& Instance()
57  {
58  Output& output = Get();
59  std::unique_lock<std::mutex> lck( output.mtx );
60  if( !output.IsValid() )
61  {
62  if( !output.Open() )
63  DefaultEnv::GetLog()->Error( AppMsg, "[Recorder] Failed to create the output file." );
64  }
65  return output;
66  }
67 
68  //------------------------------------------------------------------------
70  //------------------------------------------------------------------------
71  inline static Output& Get()
72  {
73  static Output output;
74  return output;
75  }
76 
77  //------------------------------------------------------------------------
78  // Record the user action
79  // @param action : the action to be recorded
80  // @return : true if the data was successful written to disk,
81  // false otherwise
82  //------------------------------------------------------------------------
83  bool Write( std::unique_ptr<Action> action )
84  {
85  std::unique_lock<std::mutex> lck( mtx );
86  const std::string &entry = action->ToString();
87  int btsWritten = 0;
88  do
89  {
90  int rc = ::write( fd, entry.c_str(), entry.size() );
91  if( rc < 0 )
92  {
93  DefaultEnv::GetLog()->Warning( AppMsg, "[Recorder] failed to record an action: %s", strerror( errno ) );
94  return false;
95  }
96  else
97  btsWritten += rc;
98  }
99  while( size_t( btsWritten ) < entry.size() );
100  return true;;
101  }
102 
103  //------------------------------------------------------------------------
107  //------------------------------------------------------------------------
108  bool Open()
109  {
110  fd = open( path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644 );
111  if( fd < 0 )
112  DefaultEnv::GetLog()->Warning( AppMsg, "[Recorder] failed to open the output file: %s", strerror( errno ) );
113  return ( fd >= 0 );
114  }
115 
116  //------------------------------------------------------------------------
118  //------------------------------------------------------------------------
119  inline bool IsValid()
120  {
121  return ( fd > 0 );
122  }
123 
124  void SetPath( const std::string &path )
125  {
126  this->path = path;
127  }
128 
129  private:
130 
131  //------------------------------------------------------------------------
133  //------------------------------------------------------------------------
134  Output( ) : fd( -1 )
135  {
136  }
137 
138  //------------------------------------------------------------------------
139  // Deleted copy/move constructors and assignment operators
140  //------------------------------------------------------------------------
141  Output( const Output& ) = delete;
142  Output( Output&& ) = delete;
143  Output& operator=( const Output& ) = delete;
144  Output& operator=( Output&& ) = delete;
145 
146  //------------------------------------------------------------------------
148  //------------------------------------------------------------------------
150  {
151  if( fd >= 0 )
152  {
153  int rc = close( fd );
154  if( rc < 0 )
155  DefaultEnv::GetLog()->Warning( AppMsg, "[Recorder] failed to close the output file: %s", strerror( errno ) );
156  }
157  }
158 
159  std::mutex mtx; //< mutex guarding the writes
160  int fd; //< the csv file descriptor
161  std::string path; //< path to the csv file
162  };
163 
164  //----------------------------------------------------------------------------
166  //----------------------------------------------------------------------------
168  {
169  //--------------------------------------------------------------------------
174  //--------------------------------------------------------------------------
176  std::unique_ptr<Action> action,
178  output( output ),
179  action( std::move( action ) ),
180  handler( handler )
181  {
182  }
183 
184  //--------------------------------------------------------------------------
189  //--------------------------------------------------------------------------
191  AnyObject *response,
192  HostList *hostList )
193  {
194  action->RecordResult( status, response );
195  output.Write( std::move( action ) );
196  handler->HandleResponseWithHosts( status, response, hostList );
197  delete this;
198  }
199 
200  //--------------------------------------------------------------------------
204  //--------------------------------------------------------------------------
206  AnyObject *response )
207  {
208  action->RecordResult( status, response );
209  output.Write( std::move( action ) );
210  handler->HandleResponse( status, response );
211  delete this;
212  }
213 
214  Output &output; //< the object handling writes to csv file
215  std::unique_ptr<Action> action; //< user action
216  ResponseHandler *handler; //< user completion handler
217  };
218 
219 
220 public:
221 
222  //----------------------------------------------------------------------------
225  //----------------------------------------------------------------------------
226  inline static void SetOutput( const std::string &cfgpath )
227  {
228  static const std::string defaultpath = "/tmp/xrdrecord.csv";
229  const char *envpath = getenv( "XRD_RECORDERPATH" );
230  std::string path = envpath ? envpath :
231  ( !cfgpath.empty() ? cfgpath : defaultpath );
232  Output::Get().SetPath( path );
233  }
234 
235  //----------------------------------------------------------------------------
237  //----------------------------------------------------------------------------
239  file( false ),
240  output( Output::Instance() )
241  {
242  }
243 
244  //----------------------------------------------------------------------------
246  //----------------------------------------------------------------------------
247  bool IsValid() const
248  {
249  return output.IsValid();
250  }
251 
252  //----------------------------------------------------------------------------
254  //----------------------------------------------------------------------------
255  virtual ~Recorder()
256  {
257  }
258 
259  //----------------------------------------------------------------------------
261  //----------------------------------------------------------------------------
262  virtual XRootDStatus Open(const std::string& url,
263  OpenFlags::Flags flags,
264  Access::Mode mode,
265  ResponseHandler* handler,
266  uint16_t timeout)
267  {
268  std::unique_ptr<Action> ptr( new OpenAction( this, url, flags, mode, timeout ) );
269  RecordHandler *recHandler = new RecordHandler( output, std::move( ptr ), handler );
270  return file.Open( url, flags, mode, recHandler, timeout );
271  }
272 
273  //----------------------------------------------------------------------------
275  //----------------------------------------------------------------------------
277  uint16_t timeout)
278  {
279  std::unique_ptr<Action> ptr( new CloseAction( this, timeout ) );
280  RecordHandler *recHandler = new RecordHandler( output, std::move( ptr ), handler );
281  return file.Close( recHandler, timeout );
282  }
283 
284  //----------------------------------------------------------------------------
286  //----------------------------------------------------------------------------
287  virtual XRootDStatus Stat(bool force,
288  ResponseHandler* handler,
289  uint16_t timeout)
290  {
291  std::unique_ptr<Action> ptr( new StatAction( this, force, timeout ) );
292  RecordHandler *recHandler = new RecordHandler( output, std::move( ptr ), handler );
293  return file.Stat(force, recHandler, timeout);
294  }
295 
296 
297  //----------------------------------------------------------------------------
299  //----------------------------------------------------------------------------
300  virtual XRootDStatus Read(uint64_t offset,
301  uint32_t size,
302  void* buffer,
303  ResponseHandler* handler,
304  uint16_t timeout)
305  {
306  std::unique_ptr<Action> ptr( new ReadAction( this, offset, size, timeout ) );
307  RecordHandler *recHandler = new RecordHandler( output, std::move( ptr ), handler );
308  return file.Read( offset, size, buffer, recHandler, timeout );
309  }
310 
311  //----------------------------------------------------------------------------
313  //----------------------------------------------------------------------------
314  virtual XRootDStatus Write(uint64_t offset,
315  uint32_t size,
316  const void* buffer,
317  ResponseHandler* handler,
318  uint16_t timeout)
319  {
320  std::unique_ptr<Action> ptr( new WriteAction( this, offset, size, timeout ) );
321  RecordHandler *recHandler = new RecordHandler( output, std::move( ptr ), handler );
322  return file.Write( offset, size, buffer, recHandler, timeout );
323  }
324 
325  //------------------------------------------------------------------------
327  //------------------------------------------------------------------------
328  virtual XRootDStatus PgRead( uint64_t offset,
329  uint32_t size,
330  void *buffer,
331  ResponseHandler *handler,
332  uint16_t timeout )
333  {
334  std::unique_ptr<Action> ptr( new PgReadAction( this, offset, size, timeout ) );
335  RecordHandler *recHandler = new RecordHandler( output, std::move( ptr ), handler );
336  return file.PgRead( offset, size, buffer, recHandler, timeout );
337  }
338 
339  //------------------------------------------------------------------------
341  //------------------------------------------------------------------------
342  virtual XRootDStatus PgWrite( uint64_t offset,
343  uint32_t size,
344  const void *buffer,
345  std::vector<uint32_t> &cksums,
346  ResponseHandler *handler,
347  uint16_t timeout )
348  {
349  std::unique_ptr<Action> ptr( new PgWriteAction( this, offset, size, timeout ) );
350  RecordHandler *recHandler = new RecordHandler( output, std::move( ptr ), handler );
351  return file.PgWrite( offset, size, buffer, cksums, recHandler, timeout );
352  }
353 
354  //----------------------------------------------------------------------------
356  //----------------------------------------------------------------------------
358  uint16_t timeout)
359  {
360  std::unique_ptr<Action> ptr( new SyncAction( this, timeout ) );
361  RecordHandler *recHandler = new RecordHandler( output, std::move( ptr ), handler );
362  return file.Sync( recHandler, timeout );
363  }
364 
365  //----------------------------------------------------------------------------
367  //----------------------------------------------------------------------------
368  virtual XRootDStatus Truncate(uint64_t size,
369  ResponseHandler* handler,
370  uint16_t timeout)
371  {
372  std::unique_ptr<Action> ptr( new TruncateAction( this, size, timeout ) );
373  RecordHandler *recHandler = new RecordHandler( output, std::move( ptr ), handler );
374  return file.Truncate(size, recHandler, timeout);
375  }
376 
377  //----------------------------------------------------------------------------
379  //----------------------------------------------------------------------------
380  virtual XRootDStatus VectorRead(const ChunkList& chunks,
381  void* buffer,
382  ResponseHandler* handler,
383  uint16_t timeout)
384  {
385  std::unique_ptr<Action> ptr( new VectorReadAction( this, chunks, timeout ) );
386  RecordHandler *recHandler = new RecordHandler( output, std::move( ptr ), handler );
387  return file.VectorRead(chunks, buffer, recHandler, timeout);
388  }
389 
390  //----------------------------------------------------------------------------
392  //----------------------------------------------------------------------------
393  virtual XRootDStatus VectorWrite( const ChunkList &chunks,
394  ResponseHandler *handler,
395  uint16_t timeout )
396  {
397  std::unique_ptr<Action> ptr( new VectorWriteAction( this, chunks, timeout ) );
398  RecordHandler *recHandler = new RecordHandler( output, std::move( ptr ), handler );
399  return file.VectorWrite( chunks, recHandler, timeout );
400  }
401 
402  //----------------------------------------------------------------------------
404  //----------------------------------------------------------------------------
405  virtual XRootDStatus Fcntl(const Buffer& arg,
406  ResponseHandler* handler,
407  uint16_t timeout)
408  {
409  std::unique_ptr<Action> ptr( new FcntlAction( this, arg, timeout ) );
410  RecordHandler *recHandler = new RecordHandler( output, std::move( ptr ), handler );
411  return file.Fcntl(arg, recHandler, timeout);
412  }
413 
414  //----------------------------------------------------------------------------
416  //----------------------------------------------------------------------------
418  uint16_t timeout)
419  {
420  return file.Visa(handler, timeout);
421  }
422 
423  //----------------------------------------------------------------------------
425  //----------------------------------------------------------------------------
426  virtual bool IsOpen() const
427  {
428  return file.IsOpen();
429  }
430 
431  //----------------------------------------------------------------------------
433  //----------------------------------------------------------------------------
434  virtual bool SetProperty(const std::string& name,
435  const std::string& value)
436  {
437  return file.SetProperty(name, value);
438  }
439 
440  //----------------------------------------------------------------------------
442  //----------------------------------------------------------------------------
443  virtual bool GetProperty(const std::string& name,
444  std::string& value) const
445  {
446  return file.GetProperty(name, value);
447  }
448 
449 private:
450 
451  File file; //< The file object that performs the actual operation
452  Output &output; //< The object for writing the recorded actions
453 };
454 
455 } // namespace XrdCl
456 
457 #endif /* XRDCK_RECORDER_HH_ */
virtual ~Recorder()
Destructor.
Definition: XrdClRecorder.hh:255
RecordHandler(Output &output, std::unique_ptr< Action > action, ResponseHandler *handler)
Definition: XrdClRecorder.hh:175
Definition: XrdClAnyObject.hh:32
Output & operator=(const Output &)=delete
XRootDStatus VectorRead(const ChunkList &chunks, void *buffer, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
std::vector< ChunkInfo > ChunkList
List of chunks.
Definition: XrdClXRootDResponses.hh:1055
virtual bool IsOpen() const
IsOpen.
Definition: XrdClRecorder.hh:426
virtual XRootDStatus Open(const std::string &url, OpenFlags::Flags flags, Access::Mode mode, ResponseHandler *handler, uint16_t timeout)
Open.
Definition: XrdClRecorder.hh:262
void HandleResponseWithHosts(XRootDStatus *status, AnyObject *response, HostList *hostList)
Definition: XrdClRecorder.hh:190
Definition: XrdClRecorder.hh:42
void Warning(uint64_t topic, const char *format,...)
Report a warning.
XRootDStatus Sync(ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
Write action.
Definition: XrdClAction.hh:278
Open action.
Definition: XrdClAction.hh:141
bool IsValid()
Definition: XrdClRecorder.hh:119
virtual XRootDStatus Truncate(uint64_t size, ResponseHandler *handler, uint16_t timeout)
Truncate.
Definition: XrdClRecorder.hh:368
Sync action.
Definition: XrdClAction.hh:320
bool Write(std::unique_ptr< Action > action)
Definition: XrdClRecorder.hh:83
XRootDStatus Write(uint64_t offset, uint32_t size, const void *buffer, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
bool SetProperty(const std::string &name, const std::string &value)
Recorder()
Constructor.
Definition: XrdClRecorder.hh:238
virtual XRootDStatus VectorWrite(const ChunkList &chunks, ResponseHandler *handler, uint16_t timeout)
VectorRead.
Definition: XrdClRecorder.hh:393
~Output()
Destructor.
Definition: XrdClRecorder.hh:149
void HandleResponse(XRootDStatus *status, AnyObject *response)
Definition: XrdClRecorder.hh:205
XRootDStatus Open(const std::string &url, OpenFlags::Flags flags, Access::Mode mode, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus Visa(ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
virtual XRootDStatus Write(uint64_t offset, uint32_t size, const void *buffer, ResponseHandler *handler, uint16_t timeout)
Write.
Definition: XrdClRecorder.hh:314
virtual XRootDStatus Close(ResponseHandler *handler, uint16_t timeout)
Close.
Definition: XrdClRecorder.hh:276
Stat action.
Definition: XrdClAction.hh:186
static Output & Instance()
Definition: XrdClRecorder.hh:56
virtual XRootDStatus Stat(bool force, ResponseHandler *handler, uint16_t timeout)
Stat.
Definition: XrdClRecorder.hh:287
static Log * GetLog()
Get default log.
Definition: XrdClAction.hh:249
Output & output
Definition: XrdClRecorder.hh:214
static Output & Get()
Definition: XrdClRecorder.hh:71
An interface for file plug-ins.
Definition: XrdClPlugInInterface.hh:38
XRootDStatus Read(uint64_t offset, uint32_t size, void *buffer, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
void SetPath(const std::string &path)
Definition: XrdClRecorder.hh:124
A file.
Definition: XrdClFile.hh:45
XRootDStatus Close(ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
std::vector< HostInfo > HostList
Definition: XrdClXRootDResponses.hh:1120
bool IsOpen() const
Check if the file is open.
#define write(a, b, c)
Definition: XrdPosix.hh:110
virtual XRootDStatus PgWrite(uint64_t offset, uint32_t size, const void *buffer, std::vector< uint32_t > &cksums, ResponseHandler *handler, uint16_t timeout)
Definition: XrdClRecorder.hh:342
std::string path
Definition: XrdClRecorder.hh:161
Vector Write action.
Definition: XrdClAction.hh:394
virtual bool GetProperty(const std::string &name, std::string &value) const
GetProperty.
Definition: XrdClRecorder.hh:443
ResponseHandler * handler
Definition: XrdClRecorder.hh:216
Request status.
Definition: XrdClXRootDResponses.hh:218
bool Open()
Definition: XrdClRecorder.hh:108
virtual XRootDStatus PgRead(uint64_t offset, uint32_t size, void *buffer, ResponseHandler *handler, uint16_t timeout)
Definition: XrdClRecorder.hh:328
virtual XRootDStatus Read(uint64_t offset, uint32_t size, void *buffer, ResponseHandler *handler, uint16_t timeout)
Read.
Definition: XrdClRecorder.hh:300
static void SetOutput(const std::string &cfgpath)
Definition: XrdClRecorder.hh:226
virtual XRootDStatus Sync(ResponseHandler *handler, uint16_t timeout)
Sync.
Definition: XrdClRecorder.hh:357
virtual XRootDStatus Fcntl(const Buffer &arg, ResponseHandler *handler, uint16_t timeout)
Fcntl.
Definition: XrdClRecorder.hh:405
VectorRead action.
Definition: XrdClAction.hh:353
XRootDStatus Truncate(uint64_t size, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus VectorWrite(const ChunkList &chunks, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus PgWrite(uint64_t offset, uint32_t size, const void *buffer, std::vector< uint32_t > &cksums, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
virtual XRootDStatus VectorRead(const ChunkList &chunks, void *buffer, ResponseHandler *handler, uint16_t timeout)
VectorRead.
Definition: XrdClRecorder.hh:380
Truncate action.
Definition: XrdClAction.hh:335
Handle an async response.
Definition: XrdClXRootDResponses.hh:1125
virtual XRootDStatus Visa(ResponseHandler *handler, uint16_t timeout)
Visa.
Definition: XrdClRecorder.hh:417
virtual void HandleResponse(XRootDStatus *status, AnyObject *response)
Definition: XrdClXRootDResponses.hh:1155
virtual bool SetProperty(const std::string &name, const std::string &value)
SetProperty.
Definition: XrdClRecorder.hh:434
#define close(a)
Definition: XrdPosix.hh:43
void Error(uint64_t topic, const char *format,...)
Report an error.
XRootDStatus Stat(bool force, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
virtual void HandleResponseWithHosts(XRootDStatus *status, AnyObject *response, HostList *hostList)
Definition: XrdClXRootDResponses.hh:1139
bool GetProperty(const std::string &name, std::string &value) const
Mode
Access mode.
Definition: XrdClFileSystem.hh:121
Close action.
Definition: XrdClAction.hh:171
Read action.
Definition: XrdClAction.hh:223
#define open
Definition: XrdPosix.hh:71
std::mutex mtx
Definition: XrdClRecorder.hh:159
Definition: XrdClAction.hh:295
Completion handler recording user action / server response.
Definition: XrdClRecorder.hh:167
File file
Definition: XrdClRecorder.hh:451
Definition: XrdClRecorder.hh:48
Flags
Open flags, may be or&#39;d when appropriate.
Definition: XrdClFileSystem.hh:75
bool IsValid() const
Definition: XrdClRecorder.hh:247
const uint64_t AppMsg
Definition: XrdClConstants.hh:32
Output & output
Definition: XrdClRecorder.hh:452
XRootDStatus PgRead(uint64_t offset, uint32_t size, void *buffer, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
Fcntl action.
Definition: XrdClAction.hh:421
Output()
Constructor.
Definition: XrdClRecorder.hh:134
XRootDStatus Fcntl(const Buffer &arg, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
Binary blob representation.
Definition: XrdClBuffer.hh:33
std::unique_ptr< Action > action
Definition: XrdClRecorder.hh:215
int fd
Definition: XrdClRecorder.hh:160