xrootd
Loading...
Searching...
No Matches
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
30#include "XrdCl/XrdClLog.hh"
31#include "XrdClAction.hh"
32
33#include <mutex>
34#include <fcntl.h>
35
36namespace XrdCl
37{
38//------------------------------------------------------------------------------
41//------------------------------------------------------------------------------
42class 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 ) ),
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
220public:
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
449private:
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_ */
#define close(a)
Definition XrdPosix.hh:43
#define write(a, b, c)
Definition XrdPosix.hh:110
#define open
Definition XrdPosix.hh:71
Definition XrdClAnyObject.hh:33
Binary blob representation.
Definition XrdClBuffer.hh:34
static Log * GetLog()
Get default log.
An interface for file plug-ins.
Definition XrdClPlugInInterface.hh:39
A file.
Definition XrdClFile.hh:46
XRootDStatus Read(uint64_t offset, uint32_t size, void *buffer, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus Close(ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
bool IsOpen() const
Check if the file is open.
XRootDStatus Truncate(uint64_t size, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus VectorRead(const ChunkList &chunks, void *buffer, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus Fcntl(const Buffer &arg, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus Open(const std::string &url, OpenFlags::Flags flags, Access::Mode mode, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
bool GetProperty(const std::string &name, std::string &value) const
XRootDStatus Visa(ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus Write(uint64_t offset, uint32_t size, const void *buffer, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus PgRead(uint64_t offset, uint32_t size, void *buffer, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
XRootDStatus Stat(bool force, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
bool SetProperty(const std::string &name, const std::string &value)
XRootDStatus Sync(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
XRootDStatus VectorWrite(const ChunkList &chunks, ResponseHandler *handler, uint16_t timeout=0) XRD_WARN_UNUSED_RESULT
void Error(uint64_t topic, const char *format,...)
Report an error.
void Warning(uint64_t topic, const char *format,...)
Report a warning.
Definition XrdClRecorder.hh:49
std::string path
Definition XrdClRecorder.hh:161
Output()
Constructor.
Definition XrdClRecorder.hh:134
void SetPath(const std::string &path)
Definition XrdClRecorder.hh:124
bool Write(std::unique_ptr< Action > action)
Definition XrdClRecorder.hh:83
bool Open()
Definition XrdClRecorder.hh:108
~Output()
Destructor.
Definition XrdClRecorder.hh:149
static Output & Instance()
Definition XrdClRecorder.hh:56
Output(Output &&)=delete
Output(const Output &)=delete
Output & operator=(const Output &)=delete
std::mutex mtx
Definition XrdClRecorder.hh:159
int fd
Definition XrdClRecorder.hh:160
bool IsValid()
Definition XrdClRecorder.hh:119
Output & operator=(Output &&)=delete
static Output & Get()
Definition XrdClRecorder.hh:71
Definition XrdClRecorder.hh:43
bool IsValid() const
Definition XrdClRecorder.hh:247
File file
Definition XrdClRecorder.hh:451
virtual XRootDStatus Open(const std::string &url, OpenFlags::Flags flags, Access::Mode mode, ResponseHandler *handler, uint16_t timeout)
Open.
Definition XrdClRecorder.hh:262
virtual XRootDStatus Fcntl(const Buffer &arg, ResponseHandler *handler, uint16_t timeout)
Fcntl.
Definition XrdClRecorder.hh:405
virtual XRootDStatus Sync(ResponseHandler *handler, uint16_t timeout)
Sync.
Definition XrdClRecorder.hh:357
virtual XRootDStatus PgRead(uint64_t offset, uint32_t size, void *buffer, ResponseHandler *handler, uint16_t timeout)
Definition XrdClRecorder.hh:328
virtual XRootDStatus Stat(bool force, ResponseHandler *handler, uint16_t timeout)
Stat.
Definition XrdClRecorder.hh:287
virtual bool SetProperty(const std::string &name, const std::string &value)
SetProperty.
Definition XrdClRecorder.hh:434
Output & output
Definition XrdClRecorder.hh:452
virtual bool IsOpen() const
IsOpen.
Definition XrdClRecorder.hh:426
static void SetOutput(const std::string &cfgpath)
Definition XrdClRecorder.hh:226
virtual XRootDStatus VectorWrite(const ChunkList &chunks, ResponseHandler *handler, uint16_t timeout)
VectorRead.
Definition XrdClRecorder.hh:393
virtual XRootDStatus Truncate(uint64_t size, ResponseHandler *handler, uint16_t timeout)
Truncate.
Definition XrdClRecorder.hh:368
virtual XRootDStatus Close(ResponseHandler *handler, uint16_t timeout)
Close.
Definition XrdClRecorder.hh:276
virtual ~Recorder()
Destructor.
Definition XrdClRecorder.hh:255
virtual bool GetProperty(const std::string &name, std::string &value) const
GetProperty.
Definition XrdClRecorder.hh:443
virtual XRootDStatus Visa(ResponseHandler *handler, uint16_t timeout)
Visa.
Definition XrdClRecorder.hh:417
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
virtual XRootDStatus Read(uint64_t offset, uint32_t size, void *buffer, ResponseHandler *handler, uint16_t timeout)
Read.
Definition XrdClRecorder.hh:300
Recorder()
Constructor.
Definition XrdClRecorder.hh:238
virtual XRootDStatus Write(uint64_t offset, uint32_t size, const void *buffer, ResponseHandler *handler, uint16_t timeout)
Write.
Definition XrdClRecorder.hh:314
virtual XRootDStatus VectorRead(const ChunkList &chunks, void *buffer, ResponseHandler *handler, uint16_t timeout)
VectorRead.
Definition XrdClRecorder.hh:380
Handle an async response.
Definition XrdClXRootDResponses.hh:1126
virtual void HandleResponseWithHosts(XRootDStatus *status, AnyObject *response, HostList *hostList)
Definition XrdClXRootDResponses.hh:1139
virtual void HandleResponse(XRootDStatus *status, AnyObject *response)
Definition XrdClXRootDResponses.hh:1155
Request status.
Definition XrdClXRootDResponses.hh:219
Definition XrdClAction.hh:34
const uint64_t AppMsg
Definition XrdClConstants.hh:32
std::vector< HostInfo > HostList
Definition XrdClXRootDResponses.hh:1120
std::vector< ChunkInfo > ChunkList
List of chunks.
Definition XrdClXRootDResponses.hh:1055
Definition XrdOucJson.hh:4517
Mode
Access mode.
Definition XrdClFileSystem.hh:122
Close action.
Definition XrdClAction.hh:172
Fcntl action.
Definition XrdClAction.hh:422
Open action.
Definition XrdClAction.hh:142
Flags
Open flags, may be or'd when appropriate.
Definition XrdClFileSystem.hh:76
Definition XrdClAction.hh:250
Definition XrdClAction.hh:296
Read action.
Definition XrdClAction.hh:224
Completion handler recording user action / server response.
Definition XrdClRecorder.hh:168
void HandleResponse(XRootDStatus *status, AnyObject *response)
Definition XrdClRecorder.hh:205
void HandleResponseWithHosts(XRootDStatus *status, AnyObject *response, HostList *hostList)
Definition XrdClRecorder.hh:190
Output & output
Definition XrdClRecorder.hh:214
ResponseHandler * handler
Definition XrdClRecorder.hh:216
RecordHandler(Output &output, std::unique_ptr< Action > action, ResponseHandler *handler)
Definition XrdClRecorder.hh:175
std::unique_ptr< Action > action
Definition XrdClRecorder.hh:215
Stat action.
Definition XrdClAction.hh:187
Sync action.
Definition XrdClAction.hh:321
Truncate action.
Definition XrdClAction.hh:336
VectorRead action.
Definition XrdClAction.hh:354
Vector Write action.
Definition XrdClAction.hh:395
Write action.
Definition XrdClAction.hh:279