-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdClReplay.cc
1388 lines (1298 loc) · 52.3 KB
/
XrdClReplay.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// Copyright (c) 2011-2021 by European Organization for Nuclear Research (CERN)
// Author: Michal Simon <michal.simon@cern.ch>
// Co-Author: Andreas-Joachim Peters <andreas.joachim.peters@cern.ch>
//------------------------------------------------------------------------------
// This file is part of the XRootD software suite.
//
// XRootD is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// XRootD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//
// In applying this licence, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
//------------------------------------------------------------------------------
#include "XrdCl/XrdClOperations.hh"
#include "XrdCl/XrdClUtils.hh"
#include "XrdCl/XrdClFileOperations.hh"
#include "XrdSys/XrdSysPthread.hh"
#include "XrdClAction.hh"
#include "XrdClActionMetrics.hh"
#include "XrdClReplayArgs.hh"
#include <fstream>
#include <vector>
#include <tuple>
#include <unordered_map>
#include <chrono>
#include <iostream>
#include <thread>
#include <iomanip>
#include <atomic>
#include <stdarg.h>
#include <getopt.h>
#include <map>
#include <vector>
#include <numeric>
#include <mutex>
#include <condition_variable>
namespace XrdCl
{
//------------------------------------------------------------------------------
//! Buffer pool - to limit memory consumption
//------------------------------------------------------------------------------
class BufferPool
{
public:
//--------------------------------------------------------------------------
//! Single instance access
//--------------------------------------------------------------------------
static BufferPool& Instance()
{
static BufferPool instance;
return instance;
}
//--------------------------------------------------------------------------
//! Allocate a buffer if has available memory, otherwise wait until enough
//! memory has been reclaimed
//--------------------------------------------------------------------------
std::shared_ptr<std::vector<char>> Allocate( size_t length )
{
std::unique_lock<std::mutex> lck( mtx );
cv.wait( lck, [this, length]{ return available >= length; } );
available -= length;
BufferDeleter del;
std::shared_ptr<std::vector<char>> buffer( new std::vector<char>( length, 'A' ), del );
return buffer;
}
private:
//--------------------------------------------------------------------------
//! Reclaim memory
//--------------------------------------------------------------------------
void Reclaim( size_t length )
{
std::unique_lock<std::mutex> lck;
available += length;
cv.notify_all();
}
//--------------------------------------------------------------------------
//! Custom deleter for shared_ptr (notifies the buffer-pool when memory
//! has been reclaimed)
//--------------------------------------------------------------------------
struct BufferDeleter
{
void operator()( std::vector<char> *buff )
{
BufferPool::Instance().Reclaim( buff->size() );
delete buff;
}
};
static const size_t KB = 1024;
static const size_t MB = 1024 * KB;
static const size_t GB = 1024 * MB;
//--------------------------------------------------------------------------
//! Constructor - read out the memory limit from XRD_MAXBUFFERSIZE, if not
//! set don't impose any limit
//--------------------------------------------------------------------------
BufferPool() : mtx(), cv()
{
const char *maxsize = getenv( "XRD_MAXBUFFERSIZE" );
if( maxsize )
{
size_t len = strlen( maxsize );
size_t pos;
available = std::stoul( maxsize, &pos );
std::string sufix( len != pos ? maxsize + len - 2 : "" );
std::transform( sufix.begin(), sufix.end(), sufix.begin(), ::toupper );
if( !sufix.empty() )
{
if( sufix == "KB" )
available *= KB;
else if( sufix == "MB" )
available *= MB;
else if( sufix == "GB" )
available *= GB;
}
return;
}
available = std::numeric_limits<size_t>::max();
}
BufferPool( const BufferPool& ) = delete;
BufferPool( BufferPool&& ) = delete;
BufferPool& operator=( const BufferPool& ) = delete;
BufferPool& operator=( BufferPool& ) = delete;
size_t available;
std::mutex mtx;
std::condition_variable cv;
};
//------------------------------------------------------------------------------
//! Timer helper class
//------------------------------------------------------------------------------
class mytimer_t
{
public:
//--------------------------------------------------------------------------
//! Constructor (record start time)
//--------------------------------------------------------------------------
mytimer_t()
: start(clock_t::now())
{
}
//--------------------------------------------------------------------------
//! Reset the start time
//--------------------------------------------------------------------------
void reset() { start = clock_t::now(); }
//--------------------------------------------------------------------------
//! @return : get time elapsed from start
//--------------------------------------------------------------------------
double elapsed() const
{
return (1.0
* (std::chrono::duration_cast<std::chrono::nanoseconds>(clock_t::now() - start).count())
/ 1000000000.0);
}
private:
using clock_t = std::chrono::high_resolution_clock;
std::chrono::time_point<clock_t> start; //< registered start time
};
//------------------------------------------------------------------------------
//! Barrier for synchronizing the asynchronous execution of actions
//! It is actually a wrapper around semaphore.
//------------------------------------------------------------------------------
class barrier_t
{
public:
//------------------------------------------------------------------------
//! Constructor
//! @param sem : the semaphore
//------------------------------------------------------------------------
barrier_t(XrdSysSemaphore& sem)
: sem(sem)
{
}
//------------------------------------------------------------------------
//! Destructor
//------------------------------------------------------------------------
~barrier_t() { sem.Post(); }
inline XrdSysSemaphore& get() { return sem; }
private:
XrdSysSemaphore& sem; //< the semaphore to be posted
};
//------------------------------------------------------------------------------
//! AssureFile creates input data files on the fly if required
//------------------------------------------------------------------------------
bool AssureFile(const std::string& url, uint64_t size, bool viatruncate, bool verify)
{
OpenFlags::Flags flags = OpenFlags::Read;
Access::Mode mode = Access::None;
uint16_t timeout = 60;
{
// deal with existing files
auto file = std::make_unique<XrdCl::File>(false);
XRootDStatus status = file->Open(url, flags, mode, timeout);
if (status.IsOK())
{
StatInfo* statinfo;
// file exists already, verify the size
status = file->Stat(false, statinfo, timeout);
if (status.IsOK())
{
if (statinfo->GetSize() < size)
{
std::cerr
<< "Error: file size is not sufficient, but I won't touch the file - aborting ...";
return false;
}
else
{
std::cout << "# ---> info: file exists and has sufficient size" << std::endl;
return true;
}
}
}
}
if (verify)
{
std::cerr << "Verify: file is missing or inaccesible: " << url << std::endl;
return false;
}
{
// deal with non-existing file
OpenFlags::Flags wflags = OpenFlags::New | OpenFlags::Write | OpenFlags::MakePath;
Access::Mode wmode = Access::UR | Access::UW | Access::UX;
auto file = std::make_unique<XrdCl::File>(false);
XRootDStatus status = file->Open(url, wflags, wmode, timeout);
if (status.IsOK())
{
if (viatruncate)
{
// create a file via truncation
status = file->Truncate(size, timeout);
if (!status.IsOK())
{
std::cerr << "Error: " << status.ToString() << " - empty file might be left behind!"
<< std::endl;
return false;
}
return true;
}
else
{
// create a file via writes
using buffer_t = std::vector<uint64_t>; //< data buffer
buffer_t buffer(32768);
size_t nbytes = 0;
while (nbytes < size)
{
size_t towrite = size - nbytes;
if (towrite > (buffer.size() * sizeof(uint64_t)))
towrite = buffer.size() * sizeof(uint64_t);
for (size_t i = 0; i < buffer.size(); ++i)
{
// we write the offset in this buffer
buffer[i] = nbytes / sizeof(uint64_t) + i;
}
status = file->Write(nbytes, towrite, buffer.data(), timeout);
if (!status.IsOK())
{
std::cerr << "Error: " << status.ToString() << " - failed to write file at offset "
<< nbytes << " - incomplete file might be left behind!" << std::endl;
return false;
}
nbytes += towrite;
}
}
return true;
}
else
{
std::cerr << "Error: " << status.ToString() << " - failed to create file!" << std::endl;
}
}
return false;
}
//------------------------------------------------------------------------------
//! Executes an action registered in the csv file
//------------------------------------------------------------------------------
class ActionExecutor
{
using buffer_t = std::shared_ptr<std::vector<char>>; //< data buffer
public:
//--------------------------------------------------------------------------
//! Constructor
//! @param file : the file that should be the context of the action
//! @param action : the action to be executed
//! @param args : arguments for the action
//! @param orgststr : original status
//! @param resp : original response
//! @param duration : nominal duration of this action
//--------------------------------------------------------------------------
ActionExecutor(File& file,
const std::string& action,
const std::string& args,
const std::string& orgststr,
const std::string& resp,
const double& duration)
: file(file)
, action(action)
, args(args)
, orgststr(orgststr)
, nominalduration(duration)
{
}
//--------------------------------------------------------------------------
//! Execute the action
//! @param ending : synchronization object for ending the execution
//--------------------------------------------------------------------------
void Execute(std::shared_ptr<barrier_t>& ending,
std::shared_ptr<barrier_t>& closing,
ActionMetrics& metric,
bool simulate)
{
if (action == "Open") // open action
{
std::string url;
OpenFlags::Flags flags;
Access::Mode mode;
uint16_t timeout;
std::tie(url, flags, mode, timeout) = GetOpenArgs();
std::string lmetric;
if ((flags & OpenFlags::Update) || (flags & OpenFlags::Write))
{
metric.ios["OpenW::n"]++;
}
else
{
metric.ios["OpenR::n"]++;
}
metric.ios["Open::n"]++;
mytimer_t timer;
if (!simulate)
WaitFor(Open(file, url, flags, mode, timeout) >>
[this, orgststr{ orgststr }, ending, closing, timer, &metric](XRootDStatus& s) mutable
{
metric.addIos("Open", "e", HandleStatus(s, orgststr, "Open"));
metric.addDelays("Open", "tmeas", timer.elapsed());
ending.reset();
closing.reset();
});
else
{
ending.reset();
closing.reset();
}
}
else if (action == "Close") // close action
{
uint16_t timeout = GetCloseArgs();
mytimer_t timer;
if (closing)
{
auto& sem = closing->get();
closing.reset();
sem.Wait();
}
metric.ios["Close::n"]++;
if (!simulate)
Async(Close(file, timeout) >>
[this, orgststr{ orgststr }, ending, timer, &metric](XRootDStatus& s) mutable
{
metric.addIos("Close", "e", HandleStatus(s, orgststr, "Close"));
metric.addDelays("Close", "tmeas", timer.elapsed());
ending.reset();
});
else
{
ending.reset();
}
}
else if (action == "Stat") // stat action
{
bool force;
uint16_t timeout;
std::tie(force, timeout) = GetStatArgs();
metric.ios["Stat::n"]++;
mytimer_t timer;
if (!simulate)
Async(Stat(file, force, timeout) >>
[this, orgststr{ orgststr }, ending, closing, timer, &metric](XRootDStatus& s, StatInfo& r) mutable
{
metric.addIos("Stat", "e", HandleStatus(s, orgststr, "Stat"));
metric.addDelays("Stat", "tmeas", timer.elapsed());
ending.reset();
closing.reset();
});
else
{
ending.reset();
closing.reset();
}
}
else if (action == "Read") // read action
{
uint64_t offset;
buffer_t buffer;
uint16_t timeout;
std::tie(offset, buffer, timeout) = GetReadArgs();
metric.ios["Read::n"]++;
metric.ios["Read::b"] += buffer->size();
if ((offset + buffer->size()) > metric.ios["Read::o"])
metric.ios["Read::o"] = offset + buffer->size();
mytimer_t timer;
if (!simulate)
Async(Read(file, offset, buffer->size(), buffer->data(), timeout) >>
[buffer, orgststr{ orgststr }, ending, closing, timer, &metric](XRootDStatus& s,
ChunkInfo& r) mutable
{
metric.addIos("Read", "e", HandleStatus(s, orgststr, "Read"));
metric.addDelays("Read", "tmeas", timer.elapsed());
buffer.reset();
ending.reset();
closing.reset();
});
else
{
buffer.reset();
ending.reset();
closing.reset();
}
}
else if (action == "PgRead") // pgread action
{
uint64_t offset;
buffer_t buffer;
uint16_t timeout;
std::tie(offset, buffer, timeout) = GetPgReadArgs();
metric.ios["PgRead::n"]++;
metric.ios["PgRead::b"] += buffer->size();
if ((offset + buffer->size()) > metric.ios["Read::o"])
metric.ios["Read::o"] = offset + buffer->size();
mytimer_t timer;
if (!simulate)
Async(PgRead(file, offset, buffer->size(), buffer->data(), timeout) >>
[buffer, orgststr{ orgststr }, ending, closing, timer, &metric](XRootDStatus& s,
PageInfo& r) mutable
{
metric.addIos("PgRead", "e", HandleStatus(s, orgststr, "PgRead"));
metric.addDelays("PgRead", "tmeas", timer.elapsed());
buffer.reset();
ending.reset();
closing.reset();
});
else
{
buffer.reset();
ending.reset();
closing.reset();
}
}
else if (action == "Write") // write action
{
uint64_t offset;
buffer_t buffer;
uint16_t timeout;
std::tie(offset, buffer, timeout) = GetWriteArgs();
metric.ios["Write::n"]++;
metric.ios["Write::b"] += buffer->size();
if ((offset + buffer->size()) > metric.ios["Write::o"])
metric.ios["Write::o"] = offset + buffer->size();
mytimer_t timer;
if (!simulate)
Async(
Write(file, offset, buffer->size(), buffer->data(), timeout) >>
[buffer, orgststr{ orgststr }, ending, closing, timer, &metric](XRootDStatus& s) mutable
{
metric.addIos("Write", "e", HandleStatus(s, orgststr, "Write"));
metric.addDelays("Write", "tmeas", timer.elapsed());
buffer.reset();
ending.reset();
closing.reset();
});
else
{
buffer.reset();
ending.reset();
closing.reset();
}
}
else if (action == "PgWrite") // pgwrite action
{
uint64_t offset;
buffer_t buffer;
uint16_t timeout;
std::tie(offset, buffer, timeout) = GetPgWriteArgs();
metric.ios["PgWrite::n"]++;
metric.ios["PgWrite::b"] += buffer->size();
if ((offset + buffer->size()) > metric.ios["Write::o"])
metric.ios["Write::o"] = offset + buffer->size();
mytimer_t timer;
if (!simulate)
Async(
PgWrite(file, offset, buffer->size(), buffer->data(), timeout) >>
[buffer, orgststr{ orgststr }, ending, closing, timer, &metric](XRootDStatus& s) mutable
{
metric.addIos("PgWrite", "e", HandleStatus(s, orgststr, "PgWrite"));
metric.addDelays("PgWrite", "tmeas", timer.elapsed());
buffer.reset();
ending.reset();
closing.reset();
});
else
{
buffer.reset();
ending.reset();
closing.reset();
}
}
else if (action == "Sync") // sync action
{
uint16_t timeout = GetSyncArgs();
metric.ios["Sync::n"]++;
mytimer_t timer;
if (!simulate)
Async(Sync(file, timeout) >>
[this, orgststr{ orgststr }, ending, closing, timer, &metric](XRootDStatus& s) mutable
{
metric.addIos("Sync", "e", HandleStatus(s, orgststr, "Sync"));
metric.addDelays("Sync", "tmeas", timer.elapsed());
ending.reset();
closing.reset();
});
else
{
ending.reset();
closing.reset();
}
}
else if (action == "Truncate") // truncate action
{
uint64_t size;
uint16_t timeout;
std::tie(size, timeout) = GetTruncateArgs();
metric.ios["Truncate::n"]++;
if (size > metric.ios["Truncate::o"])
metric.ios["Truncate::o"] = size;
mytimer_t timer;
if (!simulate)
Async(Truncate(file, size, timeout) >>
[this, orgststr{ orgststr }, ending, closing, timer, &metric](XRootDStatus& s) mutable
{
metric.addIos("Truncate", "e", HandleStatus(s, orgststr, "Truncate"));
metric.addDelays("Truncate", "tmeas", timer.elapsed());
ending.reset();
closing.reset();
});
else
{
ending.reset();
closing.reset();
}
}
else if (action == "VectorRead") // vector read action
{
ChunkList chunks;
uint16_t timeout;
std::vector<buffer_t> buffers;
std::tie(chunks, timeout, buffers) = GetVectorReadArgs();
metric.ios["VectorRead::n"]++;
for (auto& ch : chunks)
{
metric.ios["VectorRead::b"] += ch.GetLength();
if ((ch.GetOffset() + ch.GetLength()) > metric.ios["Read::o"])
metric.ios["Read::o"] = ch.GetOffset() + ch.GetLength();
}
mytimer_t timer;
if (!simulate)
Async(
VectorRead(file, chunks, timeout) >>
[this, orgststr{ orgststr }, buffers, ending, closing, timer, &metric](XRootDStatus& s, VectorReadInfo& r) mutable
{
metric.addIos("VectorRead", "e", HandleStatus(s, orgststr, "VectorRead"));
metric.addDelays("VectorRead", "tmeas", timer.elapsed());
buffers.clear();
ending.reset();
closing.reset();
});
else
{
buffers.clear();
ending.reset();
closing.reset();
}
}
else if (action == "VectorWrite") // vector write
{
ChunkList chunks;
uint16_t timeout;
std::vector<buffer_t> buffers;
std::tie(chunks, timeout, buffers) = GetVectorWriteArgs();
metric.ios["VectorWrite::n"]++;
for (auto& ch : chunks)
{
metric.ios["VectorWrite::b"] += ch.GetLength();
if ((ch.GetOffset() + ch.GetLength()) > metric.ios["Write::o"])
metric.ios["Write::o"] = ch.GetOffset() + ch.GetLength();
}
mytimer_t timer;
if (!simulate)
Async(VectorWrite(file, chunks, timeout) >>
[this, orgststr{ orgststr }, buffers, ending, closing, timer, &metric](XRootDStatus& s) mutable
{
metric.addIos("VectorWrite", "e", HandleStatus(s, orgststr, "VectorWrite"));
metric.addDelays("VectorWrite", "tmeas", timer.elapsed());
buffers.clear();
ending.reset();
closing.reset();
});
else
{
buffers.clear();
ending.reset();
closing.reset();
}
}
else
{
DefaultEnv::GetLog()->Warning(AppMsg, "Cannot replyt %s action.", action.c_str());
}
}
//--------------------------------------------------------------------------
//! Get nominal duration variable
//--------------------------------------------------------------------------
double NominalDuration() const { return nominalduration; }
//--------------------------------------------------------------------------
//! Get aciton name
//--------------------------------------------------------------------------
std::string Name() const { return action; }
private:
//--------------------------------------------------------------------------
//! Handle response status
//--------------------------------------------------------------------------
static bool HandleStatus(XRootDStatus& response, const std::string& orgstr, const std::string where="unknown")
{
std::string rspstr = response.ToString();
rspstr.erase(remove(rspstr.begin(), rspstr.end(), ' '), rspstr.end());
if (rspstr != orgstr)
{
DefaultEnv::GetLog()->Warning(AppMsg,
"We were expecting status: %s, but "
"received: %s from: %s",
orgstr.c_str(),
rspstr.c_str(),
where.c_str());
return true;
}
else
{
return false;
}
}
//--------------------------------------------------------------------------
//! Parse arguments for open
//--------------------------------------------------------------------------
std::tuple<std::string, OpenFlags::Flags, Access::Mode, uint16_t> GetOpenArgs()
{
std::vector<std::string> tokens;
Utils::splitString(tokens, args, ";");
if (tokens.size() != 4)
throw std::invalid_argument("Failed to parse open arguments.");
std::string url = tokens[0];
OpenFlags::Flags flags = static_cast<OpenFlags::Flags>(std::stoul(tokens[1]));
Access::Mode mode = static_cast<Access::Mode>(std::stoul(tokens[2]));
uint16_t timeout = static_cast<uint16_t>(std::stoul(tokens[3]));
return std::make_tuple(url, flags, mode, timeout);
}
//--------------------------------------------------------------------------
//! Parse arguments for close
//--------------------------------------------------------------------------
uint16_t GetCloseArgs() { return static_cast<uint16_t>(std::stoul(args)); }
std::tuple<bool, uint16_t> GetStatArgs()
{
std::vector<std::string> tokens;
Utils::splitString(tokens, args, ";");
if (tokens.size() != 2)
throw std::invalid_argument("Failed to parse stat arguments.");
bool force = (tokens[0] == "true");
uint16_t timeout = static_cast<uint16_t>(std::stoul(tokens[1]));
return std::make_tuple(force, timeout);
}
//--------------------------------------------------------------------------
//! Parse arguments for read
//--------------------------------------------------------------------------
std::tuple<uint64_t, buffer_t, uint16_t> GetReadArgs()
{
std::vector<std::string> tokens;
Utils::splitString(tokens, args, ";");
if (tokens.size() != 3)
throw std::invalid_argument("Failed to parse read arguments.");
uint64_t offset = std::stoull(tokens[0]);
uint32_t length = std::stoul(tokens[1]);
auto buffer = BufferPool::Instance().Allocate( length );
uint16_t timeout = static_cast<uint16_t>(std::stoul(tokens[2]));
return std::make_tuple(offset, buffer, timeout);
}
//--------------------------------------------------------------------------
//! Parse arguments for pgread
//--------------------------------------------------------------------------
inline std::tuple<uint64_t, buffer_t, uint16_t> GetPgReadArgs() { return GetReadArgs(); }
//--------------------------------------------------------------------------
//! Parse arguments for write
//--------------------------------------------------------------------------
inline std::tuple<uint64_t, buffer_t, uint16_t> GetWriteArgs() { return GetReadArgs(); }
//--------------------------------------------------------------------------
//! Parse arguments for pgwrite
//--------------------------------------------------------------------------
inline std::tuple<uint64_t, buffer_t, uint16_t> GetPgWriteArgs() { return GetReadArgs(); }
//--------------------------------------------------------------------------
//! Parse arguments for sync
//--------------------------------------------------------------------------
uint16_t GetSyncArgs() { return static_cast<uint16_t>(std::stoul(args)); }
//--------------------------------------------------------------------------
//! Parse arguments for truncate
//--------------------------------------------------------------------------
std::tuple<uint64_t, uint16_t> GetTruncateArgs()
{
std::vector<std::string> tokens;
Utils::splitString(tokens, args, ";");
if (tokens.size() != 2)
throw std::invalid_argument("Failed to parse truncate arguments.");
uint64_t size = std::stoull(tokens[0]);
uint16_t timeout = static_cast<uint16_t>(std::stoul(tokens[1]));
return std::make_tuple(size, timeout);
}
//--------------------------------------------------------------------------
//! Parse arguments for vector read
//--------------------------------------------------------------------------
std::tuple<ChunkList, uint16_t, std::vector<buffer_t>> GetVectorReadArgs()
{
std::vector<std::string> tokens;
Utils::splitString(tokens, args, ";");
ChunkList chunks;
chunks.reserve( tokens.size() - 1 );
std::vector<buffer_t> buffers;
buffers.reserve( tokens.size() - 1 );
for (size_t i = 0; i < tokens.size() - 1; i += 2)
{
uint64_t offset = std::stoull(tokens[i]);
uint32_t length = std::stoul(tokens[i + 1]);
auto buffer = BufferPool::Instance().Allocate( length );
chunks.emplace_back(offset, length, buffer->data());
buffers.emplace_back( std::move( buffer ) );
}
uint16_t timeout = static_cast<uint16_t>(std::stoul(tokens.back()));
return std::make_tuple(std::move(chunks), timeout, std::move(buffers));
}
//--------------------------------------------------------------------------
//! Parse arguments for vector write
//--------------------------------------------------------------------------
inline std::tuple<ChunkList, uint16_t, std::vector<buffer_t>> GetVectorWriteArgs() { return GetVectorReadArgs(); }
File& file; //< the file object
const std::string action; //< the action to be executed
const std::string args; //< arguments for the operation
std::string orgststr; //< the original response status of the action
double nominalduration; //< the original duration of execution
};
//------------------------------------------------------------------------------
//! Split a row into columns
//------------------------------------------------------------------------------
std::vector<std::string> ToColumns( const std::string &row )
{
std::vector<std::string> columns;
size_t quotecnt = 0;
size_t pos = 0;
//----------------------------------------------------------------------------
//! loop over all the columns in the row
//----------------------------------------------------------------------------
while( pos != std::string::npos && pos < row.size() )
{
if( row[pos] == '"' ) // we are handling a quoted column
{
if( quotecnt > 0 ) // this is a closing quote
{
if( pos + 1 < row.size() && row[pos + 1] != ',' ) // if it is not the last character in the row it should be followed by a comma
throw std::runtime_error( "Parsing error: missing comma" );
--quotecnt; // strip the quote
++pos; // move to the comma or end of row
continue;
}
else // this is a opening quote
{
++quotecnt;
auto b = std::next( row.begin(), pos + 1 ); // iterator to the beginning of our column
size_t posend = row.find( "\",", pos + 1 ); // position of the cursor to the end of our column
if( posend == std::string::npos && row[row.size() - 1] == '"' )
posend = row.size() - 1;
else if( posend == std::string::npos )
throw std::runtime_error( "Parsing error: missing closing quote" );
auto e = std::next( row.begin(), posend ); // iterator to the end of our column
columns.emplace_back( b, e ); // add the column to the result
pos = posend; // move to the next column
continue;
}
}
else if( row[pos] == ',' ) // we are handling a column separator
{
if( pos + 1 < row.size() && row[pos + 1] == '"' ) // check if the column is quoted
{
++pos; // if yes we will handle this with the logic reserved for quoted columns
continue;
}
auto b = std::next( row.begin(), pos + 1 ); // iterator to the beginning of our column
size_t posend = row.find( ',', pos + 1 ); // position of the cursor to the end of our column
if( posend == std::string::npos )
posend = row.size();
auto e = std::next( row.begin(), posend ); // iterator to the end of our column
columns.emplace_back( b, e ); // add the column to the result
pos = posend; // move to the next column
continue;
}
else if( pos == 0 ) // we are handling the 1st column if not quoted
{
size_t posend = row.find( ',', pos + 1 ); // position of the cursor to the end of our column
if( posend == std::string::npos )
posend = row.size();
auto end = std::next( row.begin(), posend ); // iterator to the end of our column
columns.emplace_back( row.begin(), end ); // add the column to the result
pos = posend; // move to the next column
continue;
}
else
{
throw std::runtime_error( "Parsing error: invalid input file." );
}
}
return columns;
}
//------------------------------------------------------------------------------
//! List of actions: start time - action
//------------------------------------------------------------------------------
using action_list = std::multimap<double, ActionExecutor>;
//------------------------------------------------------------------------------
//! Parse input file
//! @param path : path to the input csv file
//------------------------------------------------------------------------------
std::unordered_map<File*, action_list> ParseInput(const std::string& path,
double& t0,
double& t1,
std::unordered_map<File*, std::string>& filenames,
std::unordered_map<File*, double>& synchronicity,
std::unordered_map<File*, size_t>& responseerrors,
const std::vector<std::string>& option_regex)
{
std::unordered_map<File*, action_list> result;
std::unique_ptr<std::ifstream> fin( path.empty() ? nullptr : new std::ifstream( path, std::ifstream::in ) );
std::istream &input = path.empty() ? std::cin : *fin;
std::string line;
std::unordered_map<uint64_t, File*> files;
std::unordered_map<uint64_t, double> last_stop;
std::unordered_map<uint64_t, double> overlaps;
std::unordered_map<uint64_t, double> overlaps_cnt;
t0 = 10e99;
t1 = 0;
while (input.good())
{
std::getline(input, line);
if (line.empty())
continue;
std::vector<std::string> tokens = ToColumns( line );
if (tokens.size() == 6)
tokens.emplace_back();
if (tokens.size() != 7)
{
throw std::invalid_argument("Invalid input file format.");
}
uint64_t id = std::stoull(tokens[0]); // file object ID
std::string action = tokens[1]; // action name (e.g. Open)
double start = std::stod(tokens[2]); // start time
std::string args = tokens[3]; // operation arguments
double stop = std::stod(tokens[4]); // stop time
std::string status = tokens[5]; // operation status
std::string resp = tokens[6]; // server response
if (option_regex.size())
{
for (auto& v : option_regex)
{
std::vector<std::string> tokens;
Utils::splitString(tokens, v, ":=");
std::regex src(tokens[0]);
if (tokens.size() != 2)
{
std::cerr
<< "Error: invalid regex for argument replacement - must be format like <oldstring>:=<newstring>"
<< std::endl;
exit(EINVAL);
}
else
{
// write the results to an output iterator
args = std::regex_replace(args, src, tokens[1]);
}
}
}
if (start < t0)
t0 = start;
if (stop > t1)
t1 = stop;
if (!files.count(id))
{
files[id] = new File(false);
files[id]->SetProperty("BundledClose", "true");
filenames[files[id]] = args;
filenames[files[id]].erase(args.find(";"));
overlaps[id] = 0;
overlaps_cnt[id] = 0;
last_stop[id] = stop;
}
else
{
overlaps_cnt[id]++;
if (start > last_stop[id])
{
overlaps[id]++;
}
last_stop[id] = stop;
}
last_stop[id] = stop;
double nominal_duration = stop - start;
if (status != "[SUCCESS]")
{
responseerrors[files[id]]++;
}
else
{
result[files[id]].emplace(