-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathperf-collect.cpp
942 lines (841 loc) · 30.4 KB
/
perf-collect.cpp
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/*
Simple tool for collecting tracepoints into perf.data files.
*/
#include <tracepoint/TracepointSession.h>
#include <tracepoint/TracepointSpec.h>
#include <tracepoint/PerfDataFileWriter.h>
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <vector>
#define PROGRAM_NAME "perf-collect"
#define EXIT_SIGNALS SIGTERM, SIGINT
#define EXIT_SIGNALS_STR "SIGTERM or SIGINT"
static constexpr int ExitSigs[] = { EXIT_SIGNALS };
static constexpr unsigned ExitSigsCount = sizeof(ExitSigs) / sizeof(ExitSigs[0]);
static char const* const UsageCommon = R"(
Usage: )" PROGRAM_NAME R"( [options...] TracepointSpecs...
)";
// Usage error: stderr += UsageCommon + UsageShort.
static char const* const UsageShort = R"(
Try ")" PROGRAM_NAME R"( --help" for more information.
)";
// -h or --help: stdout += UsageCommon + UsageLong.
static char const* const UsageLong = R"(
Collects tracepoint events and saves them to a perf.data file. Collection
runs until )" EXIT_SIGNALS_STR R"( is received.
Requires privileges, typically the CAP_PERFMON capability plus read access to
/sys/kernel/tracing. Pre-registration of a tracepoint requires write access to
/sys/kernel/tracing/user_events_data.
Options:
-b, --buffersize <size>
Set the size of each buffer, in kilobytes. There will be
one buffer per CPU. The default is 128, max is 2GB.
-c, --circular Use circular trace mode. Events will be collected in
circular buffers (new events overwrite old) until the
signal is received, at which point the output file will be
created, buffer contents will be written to the file, and
the tool will exit.
-C, --realtime Use realtime trace mode (default). File will be created
immediately and events will be written to the file as they
are received until the signal is received, at which point
the tool will finalize the file and exit.
-i, --input <file> Read additional TracepointSpecs from <file>. Each line in
the file is treated as a TracepointSpec. Empty lines and
lines starting with '#' are ignored.
-o, --output <file> Set the output filename. The default is "./perf.data".
-w, --wakeup <size> Set the wakeup watermark size for realtime trace mode, in
kilobytes. The default is 2. The tool will wait for a
buffer to have at least this much data before waking to
flush the buffer to the output file.
-v, --verbose Show diagnostic output.
-h, --help Show this help message and exit.
A TracepointSpec is one of the following:
* If the tracepoint is a normal (non-EventHeader) user_event that may not
already exist, use the full user_event definition,
"SystemName:EventName Fields...". If the tracepoint does not already exist,
it will be pre-registered so that it can be added to the trace session. For
example:
user_events:MyEvent u32 MyField1; struct MyStruct2 MyField2 20
" Fields..." is required. For an event with no fields, use " ;", e.g.
user_events:MySimpleEvent ;
See https://docs.kernel.org/trace/user_events.html#command-format for
details on the user_events definition syntax.
* If the tracepoint is an EventHeader user_event that may not already exist,
use the EventHeader identity, "SystemName:ProviderName_Suffix". If the
tracepoint does not already exist, it will be pre-registered so that it can
be added to the trace session. For example:
user_events:MyProvider_L5K1
* If the tracepoint is known to already be registered (e.g. a kernel event),
use the tracepoint identity with a leading colon, ":SystemName:EventName".
If the tracepoint does not already exist, it will not be added to the trace
session. For example:
:sched:sched_switch
In all cases, you may omit "SystemName:" if it is "user_events:", e.g.
MyEvent u32 MyField1;
MyProvider_L5K1Gmygroup
:MyUserEventThatAlreadyExists
For TracepointSpecs provided on the command line, use quotation marks to
ensure correct handling of spaces and semicolons in each TracepointSpec, e.g.
"user_events:MyEvent u32 MyField1; struct MyStruct2 MyField2 20"
)";
using namespace std::string_view_literals;
using namespace tracepoint_control;
using namespace tracepoint_decode;
class Tracepoint
{
std::vector<char> storage;
public:
TracepointSpec spec;
Tracepoint(Tracepoint&&) = default;
Tracepoint& operator=(Tracepoint&&) = default;
explicit
Tracepoint(std::string_view line)
{
auto const trimmedLine = TracepointSpec::Trim(line);
if (!trimmedLine.empty())
{
storage.assign(trimmedLine.begin(), trimmedLine.end());
}
spec = TracepointSpec({ storage.data(), storage.size() });
}
};
struct Options
{
char const* output = "./perf.data";
bool verbose = false;
};
// fprintf(stderr, "PROGRAM_NAME: " + format, args...).
static void
PrintStderr(const char* format, ...)
{
va_list args;
va_start(args, format);
fputs(PROGRAM_NAME ": ", stderr);
vfprintf(stderr, format, args);
va_end(args);
}
// if (condition) fprintf(stderr, "PROGRAM_NAME: " + format, args...).
static void
PrintStderrIf(bool condition, const char* format, ...)
{
if (condition)
{
va_list args;
va_start(args, format);
fputs(PROGRAM_NAME ": ", stderr);
vfprintf(stderr, format, args);
va_end(args);
}
}
static void
PushFrontDef(Options const& o, std::vector<Tracepoint>& tracepoints, Tracepoint&& tp)
{
auto const& spec = tp.spec;
auto const trimmed = spec.Trimmed;
switch (tp.spec.Kind)
{
case TracepointSpecKind::Empty:
break;
case TracepointSpecKind::Identifier:
PrintStderrIf(o.verbose, "verbose: identifier \"%.*s:%.*s\".\n",
(unsigned)spec.SystemName.size(), spec.SystemName.data(),
(unsigned)spec.EventName.size(), spec.EventName.data());
tracepoints.push_back(std::move(tp));
break;
case TracepointSpecKind::Definition:
if (spec.SystemName != UserEventsSystemName)
{
PrintStderr("error: definition system name \"%.*s\" must be 'user_events' (from \"%.*s\").\n",
(unsigned)spec.SystemName.size(), spec.SystemName.data(),
(unsigned)trimmed.size(), trimmed.data());
}
else
{
PrintStderrIf(o.verbose, "verbose: definition \"%.*s:%.*s%s%.*s%s%.*s\"\n",
(unsigned)spec.SystemName.size(), spec.SystemName.data(),
(unsigned)spec.EventName.size(), spec.EventName.data(),
spec.Flags.empty() ? "" : ":",
(unsigned)spec.Flags.size(), spec.Flags.data(),
spec.Fields.empty() ? "" : " ",
(unsigned)spec.Fields.size(), spec.Fields.data());
tracepoints.push_back(std::move(tp));
}
break;
case TracepointSpecKind::EventHeaderDefinition:
if (spec.SystemName != UserEventsSystemName)
{
PrintStderr("error: eventheader system name \"%.*s\" must be 'user_events' (from \"%.*s\").\n",
(unsigned)spec.SystemName.size(), spec.SystemName.data(),
(unsigned)trimmed.size(), trimmed.data());
}
else
{
PrintStderrIf(o.verbose, "verbose: eventheader \"%.*s:%.*s%s%.*s\".\n",
(unsigned)spec.SystemName.size(), spec.SystemName.data(),
(unsigned)spec.EventName.size(), spec.EventName.data(),
spec.Flags.empty() ? "" : ":",
(unsigned)spec.Flags.size(), spec.Flags.data());
tracepoints.push_back(std::move(tp));
}
break;
case TracepointSpecKind::ErrorIdentifierCannotHaveFields:
PrintStderr("error: identifier cannot have fields (from \"%.*s\").\n",
(unsigned)trimmed.size(), trimmed.data());
break;
case TracepointSpecKind::ErrorIdentifierCannotHaveFlags:
PrintStderr("error: identifier cannot have flags (from \"%.*s\").\n",
(unsigned)trimmed.size(), trimmed.data());
break;
case TracepointSpecKind::ErrorDefinitionCannotHaveColonAfterFlags:
PrintStderr("error: definition cannot have colon after flags (from \"%.*s\").\n",
(unsigned)trimmed.size(), trimmed.data());
break;
case TracepointSpecKind::ErrorIdentifierEventNameEmpty:
PrintStderr("error: identifier event name is empty (from \"%.*s\").\n",
(unsigned)trimmed.size(), trimmed.data());
break;
case TracepointSpecKind::ErrorDefinitionEventNameEmpty:
PrintStderr("error: definition event name is empty (from \"%.*s\").\n",
(unsigned)trimmed.size(), trimmed.data());
break;
case TracepointSpecKind::ErrorIdentifierEventNameInvalid:
PrintStderr("error: identifier event name \"%.*s\" is invalid (from \"%.*s\").\n",
(unsigned)spec.EventName.size(), spec.EventName.data(),
(unsigned)trimmed.size(), trimmed.data());
break;
case TracepointSpecKind::ErrorDefinitionEventNameInvalid:
PrintStderr("error: definition event name \"%.*s\" is invalid (from \"%.*s\").\n",
(unsigned)spec.EventName.size(), spec.EventName.data(),
(unsigned)trimmed.size(), trimmed.data());
break;
case TracepointSpecKind::ErrorEventHeaderDefinitionEventNameInvalid:
PrintStderr("error: eventheader event name \"%.*s\" is invalid (from \"%.*s\").\n",
(unsigned)spec.EventName.size(), spec.EventName.data(),
(unsigned)trimmed.size(), trimmed.data());
PrintStderr("(error) If this was meant to be the name of an existing non-eventheader event, add a leading ':'.\n");
PrintStderr("(error) If this was meant to be the definition of a non-eventheader event, a Fields... section must be provided.\n");
PrintStderr("(error) If a non-eventheader event has no fields, use \" ;\" for Fields..., e.g. \"MyEvent ;\".\n");
break;
case TracepointSpecKind::ErrorIdentifierSystemNameEmpty:
PrintStderr("error: identifier system name is empty (from \"%.*s\").\n",
(unsigned)trimmed.size(), trimmed.data());
break;
case TracepointSpecKind::ErrorDefinitionSystemNameEmpty:
PrintStderr("error: definition system name is empty (from \"%.*s\").\n",
(unsigned)trimmed.size(), trimmed.data());
break;
case TracepointSpecKind::ErrorIdentifierSystemNameInvalid:
PrintStderr("error: identifier system name \"%.*s\" is invalid (from \"%.*s\").\n",
(unsigned)spec.SystemName.size(), spec.SystemName.data(),
(unsigned)trimmed.size(), trimmed.data());
break;
case TracepointSpecKind::ErrorDefinitionSystemNameInvalid:
PrintStderr("error: definition system name \"%.*s\" is invalid (from \"%.*s\").\n",
(unsigned)spec.SystemName.size(), spec.SystemName.data(),
(unsigned)trimmed.size(), trimmed.data());
break;
}
}
static bool
PushFrontDefsFromFile(Options const& o, std::vector<Tracepoint>& tracepoints, char const* filename)
{
// CodeQL [SM01937] This is a sample/tool. Using externally-supplied path is intended behavior.
FILE* file = fopen(filename, "r");
if (file == nullptr)
{
PrintStderr("error: cannot open file \"%s\", error %u.\n",
filename, errno);
return false;
}
std::vector<char> line;
char buf[128];
while (fgets(buf, sizeof(buf), file))
{
line.insert(line.end(), buf, buf + strlen(buf));
if (line.back() == '\n')
{
PushFrontDef(o, tracepoints, Tracepoint({ line.data(), line.size() }));
line.clear();
}
}
auto const error = errno;
bool const ok = 0 == ferror(file);
fclose(file);
if (!ok)
{
fprintf(stderr, "error: failed reading file \"%s\", error %u.\n",
filename, error);
}
else
{
// Flush last line.
PushFrontDef(o, tracepoints, Tracepoint({ line.data(), line.size() }));
}
return ok;
}
static void
ArgSize(
_In_z_ char const* flagName,
unsigned maxValue,
int argi,
int argc,
_In_reads_(argc) char* argv[],
_Inout_ bool* usageError,
_Inout_ unsigned* value)
{
if (argi >= argc)
{
PrintStderr("error: missing value for flag %s.\n",
flagName);
*usageError = true;
}
else
{
auto const* const arg = argv[argi];
auto argValue = strtoul(arg, nullptr, 0);
if (argValue == 0)
{
PrintStderr("error: expected positive integer for flag %s \"%s\".\n",
flagName, arg);
*usageError = true;
}
else if (argValue > maxValue)
{
PrintStderr("error: value %lu too large (max %u) for flag %s \"%s\".\n",
argValue, maxValue, flagName, arg);
*usageError = true;
}
else
{
*value = static_cast<unsigned>(argValue);
}
}
}
static void
InitExitSigSet(sigset_t* exitSigSet)
{
sigemptyset(exitSigSet);
for (auto exitSig : ExitSigs)
{
sigaddset(exitSigSet, exitSig);
}
}
static int SignalHandled = 0;
class SignalMask
{
int m_initError;
bool m_masked;
unsigned short m_sigsInstalled;
sigset_t m_oldSigSet;
struct sigaction m_oldActs[ExitSigsCount];
public:
SignalMask(SignalMask const&) = delete;
SignalMask& operator=(SignalMask const&) = delete;
~SignalMask()
{
Restore();
}
SignalMask() noexcept
: m_initError(0)
, m_masked(false)
, m_sigsInstalled(0)
{
struct sigaction newAct = {};
newAct.sa_handler = [](int sig)
{
SignalHandled = sig;
};
InitExitSigSet(&newAct.sa_mask);
if (sigprocmask(SIG_BLOCK, &newAct.sa_mask, &m_oldSigSet))
{
m_initError = errno;
PrintStderr("error: sigprocmask error %u.\n",
m_initError);
if (m_initError == 0)
{
m_initError = EINTR;
}
return;
}
m_masked = true;
for (; m_sigsInstalled != ExitSigsCount; m_sigsInstalled += 1)
{
if (sigaction(ExitSigs[m_sigsInstalled], &newAct, &m_oldActs[m_sigsInstalled]))
{
m_initError = errno;
PrintStderr("error: sigaction error %u.\n",
m_initError);
if (m_initError == 0)
{
m_initError = EINTR;
}
return;
}
}
}
int
InitError() const noexcept
{
return m_initError;
}
sigset_t const*
OldSigSet() const noexcept
{
return &m_oldSigSet;
}
void
Restore() noexcept
{
for (; m_sigsInstalled != 0; m_sigsInstalled -= 1)
{
sigaction(ExitSigs[m_sigsInstalled - 1], &m_oldActs[m_sigsInstalled - 1], nullptr);
}
if (m_masked)
{
m_masked = false;
sigprocmask(SIG_SETMASK, &m_oldSigSet, nullptr);
if (m_initError == 0)
{
fputc('\n', stderr);
}
}
}
};
static unsigned
EnableTracepoints(
Options const& o,
std::vector<Tracepoint> const& tracepoints,
TracepointCache& cache,
TracepointSession& session)
{
unsigned enabledCount = 0;
for (auto const& tp : tracepoints)
{
int error;
if (tp.spec.Kind == TracepointSpecKind::Identifier)
{
error = cache.AddFromSystem(TracepointName(tp.spec.SystemName, tp.spec.EventName));
switch (error)
{
default:
PrintStderr("warning: Cannot load format for \"%.*s:%.*s\", error %u.\n",
(unsigned)tp.spec.SystemName.size(), tp.spec.SystemName.data(),
(unsigned)tp.spec.EventName.size(), tp.spec.EventName.data(),
error);
continue;
case 0:
PrintStderrIf(o.verbose, "verbose: Loaded format for \"%.*s:%.*s\".\n",
(unsigned)tp.spec.SystemName.size(), tp.spec.SystemName.data(),
(unsigned)tp.spec.EventName.size(), tp.spec.EventName.data());
break;
case EEXIST:
PrintStderrIf(o.verbose, "verbose: Format already loaded for \"%.*s:%.*s\".\n",
(unsigned)tp.spec.SystemName.size(), tp.spec.SystemName.data(),
(unsigned)tp.spec.EventName.size(), tp.spec.EventName.data());
break;
}
}
else
{
error = cache.PreregisterTracepointDefinition(tp.spec);
switch (error)
{
default:
PrintStderr("warning: Cannot pre-register \"%.*s:%.*s\", error %u.\n",
(unsigned)tp.spec.SystemName.size(), tp.spec.SystemName.data(),
(unsigned)tp.spec.EventName.size(), tp.spec.EventName.data(),
error);
continue;
case 0:
PrintStderrIf(o.verbose, "verbose: Pre-registered \"%.*s:%.*s\".\n",
(unsigned)tp.spec.SystemName.size(), tp.spec.SystemName.data(),
(unsigned)tp.spec.EventName.size(), tp.spec.EventName.data());
break;
case EEXIST:
PrintStderrIf(o.verbose, "verbose: Did not pre-register \"%.*s:%.*s\" (already cached).\n",
(unsigned)tp.spec.SystemName.size(), tp.spec.SystemName.data(),
(unsigned)tp.spec.EventName.size(), tp.spec.EventName.data());
break;
}
}
error = session.EnableTracepoint(TracepointName(tp.spec.SystemName, tp.spec.EventName));
if (error != 0)
{
PrintStderr("warning: Cannot enable \"%.*s:%.*s\", error %u.\n",
(unsigned)tp.spec.SystemName.size(), tp.spec.SystemName.data(),
(unsigned)tp.spec.EventName.size(), tp.spec.EventName.data(),
error);
}
else
{
enabledCount += 1;
PrintStderrIf(o.verbose, "verbose: Enabled \"%.*s:%.*s\".\n",
(unsigned)tp.spec.SystemName.size(), tp.spec.SystemName.data(),
(unsigned)tp.spec.EventName.size(), tp.spec.EventName.data());
}
}
return enabledCount;
}
static int
CollectCircular(Options const& o, TracepointSession& session)
{
int error;
int sig = 0;
sigset_t exitSigSet;
InitExitSigSet(&exitSigSet);
// Scope for signalMask.
PrintStderr("info: collecting until " EXIT_SIGNALS_STR ".\n");
{
SignalMask signalMask;
error = signalMask.InitError();
if (error != 0)
{
return error;
}
sigwait(&exitSigSet, &sig);
}
PrintStderr("info: stopping session (signal %u).\n",
sig);
error = session.SavePerfDataFile(o.output);
if (error == 0)
{
PrintStderr("info: saved buffer contents to \"%s\".\n",
o.output);
}
else
{
PrintStderr("error: failed saving to \"%s\", error %u.\n",
o.output, error);
}
return error;
}
static int
CollectRealtime(Options const& o, TracepointSession& session)
{
int error;
unsigned wakeupCount = 0;
uint64_t eventBytes = 0;
PerfDataFileWriter writer;
error = writer.Create(o.output);
if (error != 0)
{
PrintStderr("error: failed creating file \"%s\", error %u.\n",
o.output, error);
goto Done;
}
error = writer.WriteFinishedInit();
if (error != 0)
{
PrintStderr("error: failed writing FinishedInit to \"%s\", error %u.\n",
o.output, error);
unlink(o.output); // Nothing useful in the file.
goto Done;
}
{
auto const writerSessionStartPos = writer.FilePos();
auto writerRoundStartPos = writerSessionStartPos;
TracepointTimestampRange writtenRange;
assert(SignalHandled == 0);
// Scope for signalMask.
PrintStderr("info: created \"%s\", collecting until " EXIT_SIGNALS_STR ".\n",
o.output);
{
SignalMask signalMask;
error = signalMask.InitError();
if (error != 0)
{
unlink(o.output); // Nothing useful in the file.
goto Done;
}
while (SignalHandled == 0) // Not sure whether this can ever be false.
{
error = session.WaitForWakeup(nullptr, signalMask.OldSigSet());
if (error != 0)
{
signalMask.Restore();
if (error != EINTR)
{
PrintStderr("error: ppoll failed, error %u.\n",
error);
}
else
{
PrintStderrIf(o.verbose, "verbose: ppoll EINTR.\n");
}
break;
}
wakeupCount += 1;
error = session.FlushToWriter(writer, &writtenRange);
if (error != 0)
{
signalMask.Restore();
PrintStderr("error: failed flushing \"%s\", error %u.\n",
o.output, error);
goto Finalize;
}
auto const writerRoundEndPos = writer.FilePos();
eventBytes = writerRoundEndPos - writerSessionStartPos;
PrintStderrIf(o.verbose, "verbose: flushed %lu bytes.\n",
static_cast<unsigned long>(writerRoundEndPos - writerRoundStartPos));
if (writerRoundStartPos != writerRoundEndPos)
{
error = writer.WriteFinishedRound();
if (error != 0)
{
signalMask.Restore();
PrintStderr("error: failed writing FinishedRound to \"%s\", error %u.\n",
o.output, error);
goto Finalize;
}
writerRoundStartPos = writer.FilePos();
}
}
}
PrintStderr("info: stopping session (signal %u).\n",
SignalHandled);
error = session.FlushToWriter(writer, &writtenRange);
if (error != 0)
{
PrintStderr("error: failed flushing \"%s\", error %u.\n",
o.output, error);
goto Finalize;
}
auto const writerSessionEndPos = writer.FilePos();
eventBytes = writerSessionEndPos - writerSessionStartPos;
PrintStderrIf(o.verbose, "verbose: flushed %lu bytes.\n",
static_cast<unsigned long>(writerSessionEndPos - writerRoundStartPos));
error = session.SetWriterHeaders(writer, &writtenRange);
if (error != 0)
{
PrintStderr("error: failed collecting system info for \"%s\", error %u.\n",
o.output, error);
goto Finalize;
}
}
Finalize:
{
auto newError = writer.FinalizeAndClose();
if (newError == 0)
{
PrintStderr("info: woke %u times, wrote 0x%lX bytes to \"%s\".\n",
wakeupCount, static_cast<unsigned long>(eventBytes), o.output);
}
else if (error == 0)
{
error = newError;
PrintStderr("error: failed finalizing \"%s\", error %u.\n",
o.output, error);
}
}
Done:
return error;
}
int
main(int argc, char* argv[])
{
int error;
try
{
std::vector<Tracepoint> tracepoints;
Options o;
unsigned const buffersizeMax = 0x80000000 / 1024;
unsigned buffersize = 128u;
unsigned const wakeupMax = 0x80000000 / 1024;
unsigned wakeup = 2u;
bool realtime = true;
bool showHelp = false;
bool usageError = false;
for (int argi = 1; argi < argc; argi += 1)
{
auto const* const arg = argv[argi];
if (arg[0] != '-')
{
PushFrontDef(o, tracepoints, Tracepoint(arg));
}
else if (arg[1] != '-')
{
auto const flags = &arg[1];
for (unsigned flagsPos = 0; flags[flagsPos] != '\0'; flagsPos += 1)
{
auto const flag = flags[flagsPos];
switch (flag)
{
case 'b':
argi += 1;
ArgSize("-b", buffersizeMax, argi, argc, argv, &usageError, &buffersize);
break;
case 'c':
realtime = false;
break;
case 'C':
realtime = true;
break;
case 'i':
argi += 1;
if (argi < argc)
{
PushFrontDefsFromFile(o, tracepoints, argv[argi]);
}
else
{
PrintStderr("error: missing filename for flag -i.\n");
usageError = true;
}
break;
case 'o':
argi += 1;
if (argi < argc)
{
o.output = argv[argi];
}
else
{
PrintStderr("error: missing filename for flag -o.\n");
usageError = true;
}
break;
case 'w':
argi += 1;
ArgSize("-w", wakeupMax, argi, argc, argv, &usageError, &wakeup);
break;
case 'v':
o.verbose = true;
break;
case 'h':
showHelp = true;
break;
default:
PrintStderr("error: invalid flag -%c.\n",
flag);
usageError = true;
break;
}
}
}
else
{
auto const flag = &arg[2];
if (0 == strcmp(flag, "buffersize"))
{
argi += 1;
ArgSize("--buffersize", buffersizeMax, argi, argc, argv, &usageError, &buffersize);
}
else if (0 == strcmp(flag, "circular"))
{
realtime = false;
}
else if (0 == strcmp(flag, "realtime"))
{
realtime = true;
}
else if (0 == strcmp(flag, "input"))
{
argi += 1;
if (argi < argc)
{
PushFrontDefsFromFile(o, tracepoints, argv[argi]);
}
else
{
PrintStderr("error: missing filename for flag --input.\n");
usageError = true;
}
}
else if (0 == strcmp(flag, "output"))
{
argi += 1;
if (argi < argc)
{
o.output = argv[argi];
}
else
{
PrintStderr("error: missing filename for flag --output.\n");
usageError = true;
}
}
else if (0 == strcmp(flag, "wakeup"))
{
argi += 1;
ArgSize("--wakeup", wakeupMax, argi, argc, argv, &usageError, &wakeup);
}
else if (0 == strcmp(flag, "verbose"))
{
o.verbose = true;
}
else if (0 == strcmp(flag, "help"))
{
showHelp = true;
}
else
{
PrintStderr("error: invalid flag \"--%s\".\n",
flag);
usageError = true;
}
}
}
if (showHelp || usageError)
{
fputs(UsageCommon, stdout);
fputs(showHelp ? UsageLong : UsageShort, stdout);
error = EINVAL;
goto Done;
}
else if (tracepoints.empty())
{
PrintStderr("error: no tracepoints specified, exiting.\n");
error = EINVAL;
goto Done;
}
else if (realtime && wakeup >= buffersize)
{
PrintStderr("error: wakeup size %u must be less than buffersize %u.\n",
wakeup, buffersize);
error = EINVAL;
goto Done;
}
auto const mode = realtime
? TracepointSessionMode::RealTime
: TracepointSessionMode::Circular;
TracepointCache cache;
TracepointSession session(
cache,
TracepointSessionOptions(mode, buffersize * 1024)
.WakeupWatermark(wakeup * 1024));
unsigned const enabledCount = EnableTracepoints(o, tracepoints, cache, session);
if (enabledCount == 0)
{
PrintStderr("error: No tracepoints enabled, exiting.\n");
error = ENOENT;
goto Done;
}
switch (mode)
{
case TracepointSessionMode::Circular:
error = CollectCircular(o, session);
break;
case TracepointSessionMode::RealTime:
error = CollectRealtime(o, session);
break;
default:
assert(false);
break;
}
}
catch (std::exception const& ex)
{
PrintStderr("fatal error: %s.\n",
ex.what());
error = ENOMEM;
}
Done:
return error;
}