XRootD
XrdClAction.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 SRC_XRDAPPS_RECORDPLUGIN_XRDCLACTION_HH_
25 #define SRC_XRDAPPS_RECORDPLUGIN_XRDCLACTION_HH_
26 
27 #include <sstream>
28 #include <chrono>
29 #include <ctime>
30 #include <iomanip>
32 
33 namespace XrdCl
34 {
35 //----------------------------------------------------------------------------
37 //----------------------------------------------------------------------------
38 struct Action
39 {
40  //--------------------------------------------------------------------------
41  // Constructor
42  // @param file : pointer to the file plug-in that recorded the action
43  // (to be used as an ID)
44  // @param timeout : operation timeout (common for every operation)
45  //--------------------------------------------------------------------------
46  Action(void* file, uint16_t timeout)
47  : id(reinterpret_cast<uint64_t>(file))
48  , timeout(timeout)
49  , start(std::chrono::system_clock::now()) // register the action start time
50  {
51  }
52 
53  //--------------------------------------------------------------------------
55  //--------------------------------------------------------------------------
56  inline void RecordResult(XRootDStatus* st, AnyObject* rsp)
57  {
58  stop = std::chrono::system_clock::now(); // register the response time
59  status = *st;
60  Serialize(rsp);
61  }
62 
63  //--------------------------------------------------------------------------
65  //--------------------------------------------------------------------------
66  static inline double time(
67  std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> tp)
68  {
69  auto secs = std::chrono::time_point_cast<std::chrono::seconds>(tp);
70  auto ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(tp)
71  - std::chrono::time_point_cast<std::chrono::nanoseconds>(secs);
72  return secs.time_since_epoch().count() + ns.count() / 1000000000.0;
73  }
74 
75  //--------------------------------------------------------------------------
77  //--------------------------------------------------------------------------
78  static inline double timeNow()
79  {
80  auto now = std::chrono::system_clock::now();
81  return time(now);
82  }
83 
84 
85  //--------------------------------------------------------------------------
87  //--------------------------------------------------------------------------
88  inline std::string ToString()
89  {
90  std::stringstream ss;
91  ss << "\"" << id << "\"" << ',';
92  ss << "\"" << Name() << "\"" << ',';
93 
94  double tstart = time(start);
95  double tstop = time(stop);
96  ss << "\"" << std::fixed << std::setprecision(9) << tstart << "\"" << ",";
97  std::string argstr = ArgStr();
98  if (!argstr.empty())
99  argstr += ';';
100  ss << "\"" << argstr << timeout << "\"" << ',';
101  ss << "\"" << std::fixed << std::setprecision(9) << tstop << "\"" << ",";
102  auto ststr = status.ToString();
103  while (ststr.back() == ' ')
104  ststr.pop_back();
105  ss << "\"" << ststr << "\"" << ',';
106  ss << "\"" << serialrsp << "\"" << '\n';
107  return ss.str();
108  }
109 
110  //--------------------------------------------------------------------------
112  //--------------------------------------------------------------------------
113  virtual ~Action() {}
114 
115  //--------------------------------------------------------------------------
117  //--------------------------------------------------------------------------
118  virtual std::string Name() = 0;
119 
120  //--------------------------------------------------------------------------
122  //--------------------------------------------------------------------------
123  virtual std::string ArgStr() = 0;
124 
125  //--------------------------------------------------------------------------
127  //--------------------------------------------------------------------------
128  virtual void Serialize(AnyObject* response) {}
129 
130  uint64_t id; //> File object ID
131  uint16_t timeout; //> operation timeout
132  std::chrono::system_clock::time_point start; //> start time
133  XRootDStatus status; //> operation status
134  std::string serialrsp; //> serialized response
135  std::chrono::system_clock::time_point stop; //> response time
136 };
137 
138 //----------------------------------------------------------------------------
140 //----------------------------------------------------------------------------
141 struct OpenAction : public Action
142 {
144  void* file, const std::string& url, OpenFlags::Flags flags, Access::Mode mode, uint16_t timeout)
145  : Action(file, timeout)
146  , url(url)
147  , flags(flags)
148  , mode(mode)
149  {
150  }
151 
152  std::string Name() { return "Open"; }
153 
154  std::string ArgStr()
155  {
156  std::stringstream ss;
157  ss << url << ';';
158  ss << flags << ';';
159  ss << mode;
160  return ss.str();
161  }
162 
163  const std::string url;
166 };
167 
168 //----------------------------------------------------------------------------
170 //----------------------------------------------------------------------------
171 struct CloseAction : public Action
172 {
173  CloseAction(void* file, uint16_t timeout)
174  : Action(file, timeout)
175  {
176  }
177 
178  std::string Name() { return "Close"; }
179 
180  std::string ArgStr() { return {}; }
181 };
182 
183 //----------------------------------------------------------------------------
185 //----------------------------------------------------------------------------
186 struct StatAction : public Action
187 {
188  StatAction(void* file, bool force, uint16_t timeout)
189  : Action(file, timeout)
190  , force(force)
191  {
192  }
193 
194  std::string Name() { return "Stat"; }
195 
196  std::string ArgStr() { return force ? "true" : "false"; }
197 
198  void Serialize(AnyObject* response)
199  {
200  if (!response)
201  return;
202  StatInfo* info = nullptr;
203  response->Get(info);
204  if (!info)
205  return;
206  std::stringstream ss;
207  ss << std::to_string(info->GetSize()) << ';';
208  ss << std::to_string(info->GetFlags()) << ';';
209  ss << info->GetModTime() << ';';
210  ss << info->GetChangeTime() << ';';
211  ss << info->GetAccessTime() << ';';
212  ss << info->GetModeAsOctString() << ';';
213  ss << info->GetOwner() << ';';
214  ss << info->GetGroup() << ';';
215  ss << info->GetChecksum();
216  serialrsp = ss.str();
217  }
218 
219  bool force;
220 };
221 
222 //----------------------------------------------------------------------------
224 //----------------------------------------------------------------------------
225 struct ReadAction : public Action
226 {
227  ReadAction(void* file, uint64_t offset, uint32_t size, uint16_t timeout)
228  : Action(file, timeout)
229  , offset(offset)
230  , size(size)
231  {
232  }
233 
234  std::string Name() { return "Read"; }
235 
236  std::string ArgStr() { return std::to_string(offset) + ';' + std::to_string(size); }
237 
238  void Serialize(AnyObject* response)
239  {
240  if (!response)
241  return;
242  ChunkInfo* ptr = nullptr;
243  response->Get(ptr);
244  serialrsp = std::to_string(ptr ? ptr->length : 0);
245  }
246 
247  uint64_t offset;
248  uint32_t size;
249 };
250 
251 struct PgReadAction : public Action
252 {
253  PgReadAction(void* file, uint64_t offset, uint32_t size, uint16_t timeout)
254  : Action(file, timeout)
255  , offset(offset)
256  , size(size)
257  {
258  }
259 
260  std::string Name() { return "PgRead"; }
261 
262  std::string ArgStr() { return std::to_string(offset) + ';' + std::to_string(size); }
263 
264  void Serialize(AnyObject* response)
265  {
266  if (!response)
267  return;
268  PageInfo* ptr = nullptr;
269  response->Get(ptr);
270  if (ptr)
271  serialrsp = std::to_string(ptr->GetLength()) + ';' + std::to_string(ptr->GetNbRepair());
272  else
273  serialrsp = "0;0";
274  }
275 
276  uint64_t offset;
277  uint32_t size;
278 };
279 
280 //----------------------------------------------------------------------------
282 //----------------------------------------------------------------------------
283 struct WriteAction : public Action
284 {
285  WriteAction(void* file, uint64_t offset, uint32_t size, uint16_t timeout)
286  : Action(file, timeout)
287  , offset(offset)
288  , size(size)
289  {
290  }
291 
292  std::string Name() { return "Write"; }
293 
294  std::string ArgStr() { return std::to_string(offset) + ';' + std::to_string(size); }
295 
296  uint64_t offset;
297  uint32_t size;
298 };
299 
300 struct PgWriteAction : public Action
301 {
302  PgWriteAction(void* file, uint64_t offset, uint32_t size, uint16_t timeout)
303  : Action(file, timeout)
304  , offset(offset)
305  , size(size)
306  {
307  }
308 
309  std::string Name() { return "PgWrite"; }
310 
311  std::string ArgStr()
312  {
313  std::stringstream ss;
314  ss << std::to_string(offset) << ';' << std::to_string(size);
315  return ss.str();
316  }
317 
318  uint64_t offset;
319  uint32_t size;
320 };
321 
322 //----------------------------------------------------------------------------
324 //----------------------------------------------------------------------------
325 struct SyncAction : public Action
326 {
327  SyncAction(void* file, uint16_t timeout)
328  : Action(file, timeout)
329  {
330  }
331 
332  std::string Name() { return "Sync"; }
333 
334  std::string ArgStr() { return {}; }
335 };
336 
337 //----------------------------------------------------------------------------
339 //----------------------------------------------------------------------------
340 struct TruncateAction : public Action
341 {
342  TruncateAction(void* file, uint64_t size, uint16_t timeout)
343  : Action(file, timeout)
344  , size(size)
345  {
346  }
347 
348  std::string Name() { return "Truncate"; }
349 
350  std::string ArgStr() { return std::to_string(size); }
351 
352  uint32_t size;
353 };
354 
355 //----------------------------------------------------------------------------
357 //----------------------------------------------------------------------------
358 struct VectorReadAction : public Action
359 {
360  VectorReadAction(void* file, const ChunkList& chunks, uint16_t timeout)
361  : Action(file, timeout)
362  , req(chunks)
363  {
364  }
365 
366  std::string Name() { return "VectorRead"; }
367 
368  std::string ArgStr()
369  {
370  if (req.empty())
371  return {};
372  std::stringstream ss;
373  ss << req[0].offset << ";" << req[0].length;
374  for (size_t i = 1; i < req.size(); ++i)
375  ss << ";" << req[i].offset << ";" << req[i].length;
376  return ss.str();
377  }
378 
379  void Serialize(AnyObject* response)
380  {
381  if (!response)
382  return;
383  VectorReadInfo* ptr = nullptr;
384  response->Get(ptr);
385  if (!ptr)
386  return;
387  std::stringstream ss;
388  ss << ptr->GetSize();
389  auto& chunks = ptr->GetChunks();
390  for (auto& ch : chunks)
391  ss << ';' << ch.offset << ';' << ch.length;
392  serialrsp = ss.str();
393  }
394 
396 };
397 
398 //----------------------------------------------------------------------------
400 //----------------------------------------------------------------------------
401 struct VectorWriteAction : public Action
402 {
403  VectorWriteAction(void* file, const ChunkList& chunks, uint16_t timeout)
404  : Action(file, timeout)
405  , req(chunks)
406  {
407  }
408 
409  std::string Name() { return "VectorWrite"; }
410 
411  std::string ArgStr()
412  {
413  if (req.empty())
414  return {};
415  std::stringstream ss;
416  ss << req[0].offset << ";" << req[0].length;
417  for (size_t i = 1; i < req.size(); ++i)
418  ss << ";" << req[i].offset << ";" << req[i].length;
419  return ss.str();
420  }
421 
423 };
424 
425 //----------------------------------------------------------------------------
427 //----------------------------------------------------------------------------
429 {
430  FcntlAction(void* file, const Buffer& arg, uint16_t timeout)
431  : Action(file, timeout)
432  , req(arg.GetSize())
433  {
434  }
435 
436  std::string Name() { return "Fcntl"; }
437 
438  std::string ArgStr() { return std::to_string(req); }
439 
440  void Serialize(AnyObject* response)
441  {
442  if (!response)
443  return;
444  Buffer* ptr = nullptr;
445  response->Get(ptr);
446  serialrsp = std::to_string(ptr ? ptr->GetSize() : 0);
447  }
448 
449  uint32_t req;
450 };
451 
452 } /* namespace XrdCl */
453 
454 #endif /* SRC_XRDAPPS_RECORDPLUGIN_XRDCLACTION_HH_ */
void Get(Type &object)
Retrieve the object being held.
Binary blob representation.
Definition: XrdClBuffer.hh:34
uint32_t GetSize() const
Get the size of the message.
Definition: XrdClBuffer.hh:132
Object stat info.
uint64_t GetChangeTime() const
Get change time (in seconds since epoch)
uint64_t GetSize() const
Get size (in bytes)
const std::string GetModeAsOctString() const
Get mode.
const std::string & GetOwner() const
Get owner.
uint32_t GetFlags() const
Get flags.
const std::string & GetGroup() const
Get group.
uint64_t GetModTime() const
Get modification time (in seconds since epoch)
uint64_t GetAccessTime() const
Get change time (in seconds since epoch)
const std::string & GetChecksum() const
Get checksum.
uint32_t GetSize() const
Get Size.
ChunkList & GetChunks()
Get chunks.
std::vector< ChunkInfo > ChunkList
List of chunks.
Mode
Access mode.
std::string serialrsp
Definition: XrdClAction.hh:134
virtual std::string Name()=0
Action name.
uint16_t timeout
Definition: XrdClAction.hh:131
virtual std::string ArgStr()=0
Convert operation arguments into a string.
std::chrono::system_clock::time_point start
Definition: XrdClAction.hh:132
XRootDStatus status
Definition: XrdClAction.hh:133
static double time(std::chrono::time_point< std::chrono::system_clock, std::chrono::nanoseconds > tp)
Convert timpoint to unix timestamp with ns.
Definition: XrdClAction.hh:66
std::chrono::system_clock::time_point stop
Definition: XrdClAction.hh:135
virtual ~Action()
Destructor.
Definition: XrdClAction.hh:113
static double timeNow()
Get curretn unix time in ns precision as a double.
Definition: XrdClAction.hh:78
virtual void Serialize(AnyObject *response)
Serialize server response.
Definition: XrdClAction.hh:128
void RecordResult(XRootDStatus *st, AnyObject *rsp)
Record the server response / error / timeout.
Definition: XrdClAction.hh:56
std::string ToString()
Convert the action / response data into csv row.
Definition: XrdClAction.hh:88
Action(void *file, uint16_t timeout)
Definition: XrdClAction.hh:46
Describe a data chunk for vector read.
uint32_t length
offset in the file
Close action.
Definition: XrdClAction.hh:172
std::string Name()
Action name.
Definition: XrdClAction.hh:178
std::string ArgStr()
Convert operation arguments into a string.
Definition: XrdClAction.hh:180
CloseAction(void *file, uint16_t timeout)
Definition: XrdClAction.hh:173
Fcntl action.
Definition: XrdClAction.hh:429
FcntlAction(void *file, const Buffer &arg, uint16_t timeout)
Definition: XrdClAction.hh:430
std::string Name()
Action name.
Definition: XrdClAction.hh:436
void Serialize(AnyObject *response)
Serialize server response.
Definition: XrdClAction.hh:440
std::string ArgStr()
Convert operation arguments into a string.
Definition: XrdClAction.hh:438
Open action.
Definition: XrdClAction.hh:142
const std::string url
Definition: XrdClAction.hh:163
OpenFlags::Flags flags
Definition: XrdClAction.hh:164
OpenAction(void *file, const std::string &url, OpenFlags::Flags flags, Access::Mode mode, uint16_t timeout)
Definition: XrdClAction.hh:143
Access::Mode mode
Definition: XrdClAction.hh:165
std::string Name()
Action name.
Definition: XrdClAction.hh:152
std::string ArgStr()
Convert operation arguments into a string.
Definition: XrdClAction.hh:154
Flags
Open flags, may be or'd when appropriate.
size_t GetNbRepair()
Get number of repaired pages.
uint32_t GetLength() const
Get the data length.
std::string ArgStr()
Convert operation arguments into a string.
Definition: XrdClAction.hh:262
void Serialize(AnyObject *response)
Serialize server response.
Definition: XrdClAction.hh:264
std::string Name()
Action name.
Definition: XrdClAction.hh:260
PgReadAction(void *file, uint64_t offset, uint32_t size, uint16_t timeout)
Definition: XrdClAction.hh:253
PgWriteAction(void *file, uint64_t offset, uint32_t size, uint16_t timeout)
Definition: XrdClAction.hh:302
std::string Name()
Action name.
Definition: XrdClAction.hh:309
std::string ArgStr()
Convert operation arguments into a string.
Definition: XrdClAction.hh:311
Read action.
Definition: XrdClAction.hh:226
ReadAction(void *file, uint64_t offset, uint32_t size, uint16_t timeout)
Definition: XrdClAction.hh:227
std::string Name()
Action name.
Definition: XrdClAction.hh:234
void Serialize(AnyObject *response)
Serialize server response.
Definition: XrdClAction.hh:238
std::string ArgStr()
Convert operation arguments into a string.
Definition: XrdClAction.hh:236
Stat action.
Definition: XrdClAction.hh:187
std::string Name()
Action name.
Definition: XrdClAction.hh:194
void Serialize(AnyObject *response)
Serialize server response.
Definition: XrdClAction.hh:198
std::string ArgStr()
Convert operation arguments into a string.
Definition: XrdClAction.hh:196
StatAction(void *file, bool force, uint16_t timeout)
Definition: XrdClAction.hh:188
std::string ToString() const
Create a string representation.
Definition: XrdClStatus.cc:97
Sync action.
Definition: XrdClAction.hh:326
std::string Name()
Action name.
Definition: XrdClAction.hh:332
std::string ArgStr()
Convert operation arguments into a string.
Definition: XrdClAction.hh:334
SyncAction(void *file, uint16_t timeout)
Definition: XrdClAction.hh:327
Truncate action.
Definition: XrdClAction.hh:341
std::string ArgStr()
Convert operation arguments into a string.
Definition: XrdClAction.hh:350
TruncateAction(void *file, uint64_t size, uint16_t timeout)
Definition: XrdClAction.hh:342
std::string Name()
Action name.
Definition: XrdClAction.hh:348
VectorRead action.
Definition: XrdClAction.hh:359
std::string Name()
Action name.
Definition: XrdClAction.hh:366
VectorReadAction(void *file, const ChunkList &chunks, uint16_t timeout)
Definition: XrdClAction.hh:360
std::string ArgStr()
Convert operation arguments into a string.
Definition: XrdClAction.hh:368
void Serialize(AnyObject *response)
Serialize server response.
Definition: XrdClAction.hh:379
Vector Write action.
Definition: XrdClAction.hh:402
std::string ArgStr()
Convert operation arguments into a string.
Definition: XrdClAction.hh:411
std::string Name()
Action name.
Definition: XrdClAction.hh:409
VectorWriteAction(void *file, const ChunkList &chunks, uint16_t timeout)
Definition: XrdClAction.hh:403
Write action.
Definition: XrdClAction.hh:284
std::string ArgStr()
Convert operation arguments into a string.
Definition: XrdClAction.hh:294
std::string Name()
Action name.
Definition: XrdClAction.hh:292
WriteAction(void *file, uint64_t offset, uint32_t size, uint16_t timeout)
Definition: XrdClAction.hh:285