/
boot-analysis.c
1244 lines (1077 loc) · 34.3 KB
/
boot-analysis.c
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
/* libguestfs
* Copyright (C) 2016 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* Trace and analyze the appliance boot process to find out which
* steps are taking the most time. It is not part of the standard
* tests.
*
* This needs to be run on a quiet machine, so that other processes
* disturb the timing as little as possible. The program is
* completely safe to run at any time. It doesn't read or write any
* external files, and it doesn't require root.
*
* You can run it from the build directory like this:
*
* make
* make -C tests/qemu boot-analysis
* ./run tests/qemu/boot-analysis
*
* The way it works is roughly like this:
*
* We create a libguestfs handle and register callback handlers so we
* can see appliance messages, trace events and so on.
*
* We then launch the handle and shut it down as quickly as possible.
*
* While the handle is running, events (seen by the callback handlers)
* are written verbatim into an in-memory buffer, with timestamps.
*
* Afterwards we analyze the result using regular expressions to try
* to identify a "timeline" for the handle (eg. at what time did the
* BIOS hand control to the kernel). This analysis is done in
* 'boot-analysis-timeline.c'.
*
* The whole process is repeated across a few runs, and the final
* timeline (including statistical analysis of the variation between
* runs) gets printed.
*
* The program is very sensitive to the specific messages printed by
* BIOS/kernel/supermin/userspace, so it won't work on non-x86, and it
* will require periodic adjustment of the regular expressions in
* order to keep things up to date.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <time.h>
#include <errno.h>
#include <error.h>
#include <ctype.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <math.h>
#include <pthread.h>
#include "guestfs.h"
#include "guestfs-internal-frontend.h"
#include "boot-analysis.h"
#include "boot-analysis-utils.h"
/* Activities taking longer than this % of the total time, except
* those flagged as LONG_ACTIVITY, are highlighted in red.
*/
#define WARNING_THRESHOLD 1.0
static const char *append = NULL;
static int force_colour = 0;
static int memsize = 0;
static int smp = 1;
static int verbose = 0;
static int libvirt_pipe[2] = { -1, -1 };
static ssize_t libvirt_pass = -1;
/* Because there is a separate thread which collects libvirt log data,
* we must protect the pass_data struct with a mutex. This only
* applies during the data collection passes.
*/
static pthread_mutex_t pass_data_lock = PTHREAD_MUTEX_INITIALIZER;
struct pass_data pass_data[NR_TEST_PASSES];
size_t nr_activities;
struct activity *activities;
static void run_test (void);
static struct event *add_event (struct pass_data *, uint64_t source);
static guestfs_h *create_handle (void);
static void set_up_event_handlers (guestfs_h *g, size_t pass);
static void libvirt_log_hack (int argc, char **argv);
static void start_libvirt_thread (size_t pass);
static void stop_libvirt_thread (void);
static void add_drive (guestfs_h *g);
static void check_pass_data (void);
static void dump_pass_data (void);
static void analyze_timeline (void);
static void dump_timeline (void);
static void print_analysis (void);
static void print_longest_to_shortest (void);
static void free_pass_data (void);
static void free_final_timeline (void);
static void ansi_green (void);
static void ansi_red (void);
static void ansi_blue (void);
static void ansi_magenta (void);
static void ansi_restore (void);
static void
usage (int exitcode)
{
guestfs_h *g;
int default_memsize = -1;
g = guestfs_create ();
if (g) {
default_memsize = guestfs_get_memsize (g);
guestfs_close (g);
}
fprintf (stderr,
"boot-analysis: Trace and analyze the appliance boot process.\n"
"Usage:\n"
" boot-analysis [--options]\n"
"Options:\n"
" --help Display this usage text and exit.\n"
" --append OPTS Append OPTS to kernel command line.\n"
" --colour Output colours, even if not a terminal.\n"
" -m MB\n"
" --memsize MB Set memory size in MB (default: %d).\n"
" --smp N Enable N virtual CPUs (default: 1).\n"
" -v|--verbose Verbose output, useful for debugging.\n",
default_memsize);
exit (exitcode);
}
int
main (int argc, char *argv[])
{
enum { HELP_OPTION = CHAR_MAX + 1 };
static const char *options = "m:v";
static const struct option long_options[] = {
{ "help", 0, 0, HELP_OPTION },
{ "append", 1, 0, 0 },
{ "color", 0, 0, 0 },
{ "colour", 0, 0, 0 },
{ "memsize", 1, 0, 'm' },
{ "libvirt-pipe-0", 1, 0, 0 }, /* see libvirt_log_hack */
{ "libvirt-pipe-1", 1, 0, 0 },
{ "smp", 1, 0, 0 },
{ "verbose", 0, 0, 'v' },
{ 0, 0, 0, 0 }
};
int c, option_index;
for (;;) {
c = getopt_long (argc, argv, options, long_options, &option_index);
if (c == -1) break;
switch (c) {
case 0: /* Options which are long only. */
if (STREQ (long_options[option_index].name, "append")) {
append = optarg;
break;
}
else if (STREQ (long_options[option_index].name, "color") ||
STREQ (long_options[option_index].name, "colour")) {
force_colour = 1;
break;
}
else if (STREQ (long_options[option_index].name, "libvirt-pipe-0")) {
if (sscanf (optarg, "%d", &libvirt_pipe[0]) != 1)
error (EXIT_FAILURE, 0,
"could not parse libvirt-pipe-0 parameter: %s", optarg);
break;
}
else if (STREQ (long_options[option_index].name, "libvirt-pipe-1")) {
if (sscanf (optarg, "%d", &libvirt_pipe[1]) != 1)
error (EXIT_FAILURE, 0,
"could not parse libvirt-pipe-1 parameter: %s", optarg);
break;
}
else if (STREQ (long_options[option_index].name, "smp")) {
if (sscanf (optarg, "%d", &smp) != 1)
error (EXIT_FAILURE, 0,
"could not parse smp parameter: %s", optarg);
break;
}
fprintf (stderr, "%s: unknown long option: %s (%d)\n",
guestfs_int_program_name, long_options[option_index].name, option_index);
exit (EXIT_FAILURE);
case 'm':
if (sscanf (optarg, "%d", &memsize) != 1) {
fprintf (stderr, "%s: could not parse memsize parameter: %s\n",
guestfs_int_program_name, optarg);
exit (EXIT_FAILURE);
}
break;
case 'v':
verbose = 1;
break;
case HELP_OPTION:
usage (EXIT_SUCCESS);
default:
usage (EXIT_FAILURE);
}
}
libvirt_log_hack (argc, argv);
if (STRNEQ (host_cpu, "x86_64") && STRNEQ (host_cpu, "aarch64"))
fprintf (stderr, "WARNING: host_cpu != x86_64|aarch64: This program may not work or give bogus results.\n");
run_test ();
}
static void
run_test (void)
{
guestfs_h *g;
size_t i;
printf ("Warming up the libguestfs cache ...\n");
for (i = 0; i < NR_WARMUP_PASSES; ++i) {
g = create_handle ();
add_drive (g);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);
guestfs_close (g);
}
printf ("Running the tests in %d passes ...\n", NR_TEST_PASSES);
for (i = 0; i < NR_TEST_PASSES; ++i) {
g = create_handle ();
set_up_event_handlers (g, i);
start_libvirt_thread (i);
add_drive (g);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);
guestfs_close (g);
stop_libvirt_thread ();
printf (" pass %zu: %zu events collected in %" PRIi64 " ns\n",
i+1, pass_data[i].nr_events, pass_data[i].elapsed_ns);
}
if (verbose)
dump_pass_data ();
printf ("Analyzing the results ...\n");
check_pass_data ();
construct_timeline ();
analyze_timeline ();
if (verbose)
dump_timeline ();
printf ("\n");
g = create_handle ();
test_info (g, NR_TEST_PASSES);
guestfs_close (g);
printf ("\n");
print_analysis ();
printf ("\n");
printf ("Longest activities:\n");
printf ("\n");
print_longest_to_shortest ();
free_pass_data ();
free_final_timeline ();
}
static struct event *
add_event_unlocked (struct pass_data *data, uint64_t source)
{
struct event *ret;
data->nr_events++;
data->events = realloc (data->events,
sizeof (struct event) * data->nr_events);
if (data->events == NULL)
error (EXIT_FAILURE, errno, "realloc");
ret = &data->events[data->nr_events-1];
get_time (&ret->t);
ret->source = source;
ret->message = NULL;
return ret;
}
static struct event *
add_event (struct pass_data *data, uint64_t source)
{
struct event *ret;
pthread_mutex_lock (&pass_data_lock);
ret = add_event_unlocked (data, source);
pthread_mutex_unlock (&pass_data_lock);
return ret;
}
/* Common function to create the handle and set various defaults. */
static guestfs_h *
create_handle (void)
{
guestfs_h *g;
CLEANUP_FREE char *full_append = NULL;
g = guestfs_create ();
if (!g) error (EXIT_FAILURE, errno, "guestfs_create");
if (memsize != 0)
if (guestfs_set_memsize (g, memsize) == -1)
exit (EXIT_FAILURE);
if (smp >= 2)
if (guestfs_set_smp (g, smp) == -1)
exit (EXIT_FAILURE);
/* This changes some details in appliance/init and enables a
* detailed trace of calls to initcall functions in the kernel.
*/
if (asprintf (&full_append,
"guestfs_boot_analysis=1 "
"ignore_loglevel initcall_debug "
"%s",
append != NULL ? append : "") == -1)
error (EXIT_FAILURE, errno, "asprintf");
if (guestfs_set_append (g, full_append) == -1)
exit (EXIT_FAILURE);
return g;
}
/* Common function to add the /dev/null drive. */
static void
add_drive (guestfs_h *g)
{
if (guestfs_add_drive_opts (g, "/dev/null",
GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
-1) == -1)
exit (EXIT_FAILURE);
}
/* Called when the handle is closed. Perform any cleanups required in
* the pass_data here.
*/
static void
close_callback (guestfs_h *g, void *datavp, uint64_t source,
int eh, int flags,
const char *buf, size_t buf_len,
const uint64_t *array, size_t array_len)
{
struct pass_data *data = datavp;
struct event *event;
if (!data->seen_launch)
return;
event = add_event (data, source);
event->message = strdup ("close callback");
if (event->message == NULL)
error (EXIT_FAILURE, errno, "strdup");
get_time (&data->end_t);
data->elapsed_ns = timespec_diff (&data->start_t, &data->end_t);
}
/* Called when the qemu subprocess exits.
* XXX This is never called - why?
*/
static void
subprocess_quit_callback (guestfs_h *g, void *datavp, uint64_t source,
int eh, int flags,
const char *buf, size_t buf_len,
const uint64_t *array, size_t array_len)
{
struct pass_data *data = datavp;
struct event *event;
if (!data->seen_launch)
return;
event = add_event (data, source);
event->message = strdup ("subprocess quit callback");
if (event->message == NULL)
error (EXIT_FAILURE, errno, "strdup");
}
/* Called when the launch operation is complete (the library and the
* guestfs daemon and talking to each other).
*/
static void
launch_done_callback (guestfs_h *g, void *datavp, uint64_t source,
int eh, int flags,
const char *buf, size_t buf_len,
const uint64_t *array, size_t array_len)
{
struct pass_data *data = datavp;
struct event *event;
if (!data->seen_launch)
return;
event = add_event (data, source);
event->message = strdup ("launch done callback");
if (event->message == NULL)
error (EXIT_FAILURE, errno, "strdup");
}
/* Trim \r (multiple) from the end of a string. */
static void
trim_r (char *message)
{
size_t len = strlen (message);
while (len > 0 && message[len-1] == '\r') {
message[len-1] = '\0';
len--;
}
}
/* Called when we get (possibly part of) a log message (or more than
* one log message) from the appliance (which may include qemu, the
* BIOS, kernel, etc).
*/
static void
appliance_callback (guestfs_h *g, void *datavp, uint64_t source,
int eh, int flags,
const char *buf, size_t buf_len,
const uint64_t *array, size_t array_len)
{
struct pass_data *data = datavp;
struct event *event;
size_t i, len, slen;
if (!data->seen_launch)
return;
/* If the previous log message was incomplete, but time has moved on
* a lot, record a new log message anyway, so it gets a new
* timestamp.
*/
if (data->incomplete_log_message >= 0) {
struct timespec ts;
get_time (&ts);
if (timespec_diff (&data->events[data->incomplete_log_message].t,
&ts) >= 10000000 /* 10ms */)
data->incomplete_log_message = -1;
}
/* If the previous log message was incomplete then we may need to
* append part of the current log message to a previous one.
*/
if (data->incomplete_log_message >= 0) {
len = buf_len;
for (i = 0; i < buf_len; ++i) {
if (buf[i] == '\n') {
len = i;
break;
}
}
event = &data->events[data->incomplete_log_message];
slen = strlen (event->message);
event->message = realloc (event->message, slen + len + 1);
if (event->message == NULL)
error (EXIT_FAILURE, errno, "realloc");
memcpy (event->message + slen, buf, len);
event->message[slen + len] = '\0';
trim_r (event->message);
/* Skip what we just added to the previous incomplete message. */
buf += len;
buf_len -= len;
if (buf_len == 0) /* still not complete, more to come! */
return;
/* Skip the \n in the buffer. */
buf++;
buf_len--;
data->incomplete_log_message = -1;
}
/* Add the event, or perhaps multiple events if the message
* contains \n characters.
*/
while (buf_len > 0) {
len = buf_len;
for (i = 0; i < buf_len; ++i) {
if (buf[i] == '\n') {
len = i;
break;
}
}
event = add_event (data, source);
event->message = strndup (buf, len);
if (event->message == NULL)
error (EXIT_FAILURE, errno, "strndup");
trim_r (event->message);
/* Skip what we just added to the event. */
buf += len;
buf_len -= len;
if (buf_len == 0) {
/* Event is incomplete (doesn't end with \n). We'll finish it
* in the next callback.
*/
data->incomplete_log_message = event - data->events;
return;
}
/* Skip the \n in the buffer. */
buf++;
buf_len--;
}
}
/* Called when we get a debug message from the library side. These
* are always delivered as complete messages.
*/
static void
library_callback (guestfs_h *g, void *datavp, uint64_t source,
int eh, int flags,
const char *buf, size_t buf_len,
const uint64_t *array, size_t array_len)
{
struct pass_data *data = datavp;
struct event *event;
if (!data->seen_launch)
return;
event = add_event (data, source);
event->message = strndup (buf, buf_len);
if (event->message == NULL)
error (EXIT_FAILURE, errno, "strndup");
}
/* Called when we get a call trace message (a libguestfs API function
* has been called or is returning). These are always delivered as
* complete messages.
*/
static void
trace_callback (guestfs_h *g, void *datavp, uint64_t source,
int eh, int flags,
const char *buf, size_t buf_len,
const uint64_t *array, size_t array_len)
{
struct pass_data *data = datavp;
struct event *event;
char *message;
message = strndup (buf, buf_len);
if (message == NULL)
error (EXIT_FAILURE, errno, "strndup");
if (STREQ (message, "launch"))
data->seen_launch = 1;
if (!data->seen_launch) {
free (message);
return;
}
event = add_event (data, source);
event->message = message;
}
/* Common function to set up event callbacks and record data in memory
* for a particular pass (0 <= pass < NR_TEST_PASSES).
*/
static void
set_up_event_handlers (guestfs_h *g, size_t pass)
{
struct pass_data *data;
assert (/* 0 <= pass && */ pass < NR_TEST_PASSES);
data = &pass_data[pass];
data->pass = pass;
data->nr_events = 0;
data->events = NULL;
get_time (&data->start_t);
data->incomplete_log_message = -1;
data->seen_launch = 0;
guestfs_set_event_callback (g, close_callback,
GUESTFS_EVENT_CLOSE, 0, data);
guestfs_set_event_callback (g, subprocess_quit_callback,
GUESTFS_EVENT_SUBPROCESS_QUIT, 0, data);
guestfs_set_event_callback (g, launch_done_callback,
GUESTFS_EVENT_LAUNCH_DONE, 0, data);
guestfs_set_event_callback (g, appliance_callback,
GUESTFS_EVENT_APPLIANCE, 0, data);
guestfs_set_event_callback (g, library_callback,
GUESTFS_EVENT_LIBRARY, 0, data);
guestfs_set_event_callback (g, trace_callback,
GUESTFS_EVENT_TRACE, 0, data);
guestfs_set_verbose (g, 1);
guestfs_set_trace (g, 1);
}
/* libvirt debugging sucks in a number of concrete ways:
*
* - you can't get a synchronous callback from a log message
* - you can't enable logging per handle (only globally
* by setting environment variables)
* - you can't debug the daemon easily
* - it's very complex
* - it's very complex but not in ways that are practical or useful
*
* To get log messages at all, we need to create a pipe connected to a
* second thread, and when libvirt prints something to the pipe we log
* that.
*
* However that's not sufficient. Because logging is only enabled
* when libvirt examines environment variables at the start of the
* program, we need to create the pipe and then fork+exec a new
* instance of the whole program with the pipe and environment
* variables set up.
*/
static int is_libvirt_backend (guestfs_h *g);
static void *libvirt_log_thread (void *datavp);
static void
libvirt_log_hack (int argc, char **argv)
{
guestfs_h *g;
g = guestfs_create ();
if (!is_libvirt_backend (g)) {
guestfs_close (g);
return;
}
guestfs_close (g);
/* Have we set up the pipes and environment and forked yet? If not,
* do that first.
*/
if (libvirt_pipe[0] == -1 || libvirt_pipe[1] == -1) {
char log_outputs[64];
char **new_argv;
char param1[64], param2[64];
size_t i;
pid_t pid;
int status;
/* Create the pipe. NB: do NOT use O_CLOEXEC since we want to pass
* this pipe into a child process.
*/
if (pipe (libvirt_pipe) == -1)
error (EXIT_FAILURE, 0, "pipe2");
/* Create the environment variables to enable logging in libvirt. */
setenv ("LIBVIRT_DEBUG", "1", 1);
//setenv ("LIBVIRT_LOG_FILTERS",
// "1:qemu 1:securit 3:file 3:event 3:object 1:util", 1);
snprintf (log_outputs, sizeof log_outputs,
"1:file:/dev/fd/%d", libvirt_pipe[1]);
setenv ("LIBVIRT_LOG_OUTPUTS", log_outputs, 1);
/* Run self again. */
new_argv = malloc ((argc+3) * sizeof (char *));
if (new_argv == NULL)
error (EXIT_FAILURE, errno, "malloc");
for (i = 0; i < (size_t) argc; ++i)
new_argv[i] = argv[i];
snprintf (param1, sizeof param1, "--libvirt-pipe-0=%d", libvirt_pipe[0]);
new_argv[argc] = param1;
snprintf (param2, sizeof param2, "--libvirt-pipe-1=%d", libvirt_pipe[1]);
new_argv[argc+1] = param2;
new_argv[argc+2] = NULL;
pid = fork ();
if (pid == -1)
error (EXIT_FAILURE, errno, "fork");
if (pid == 0) { /* Child process. */
execvp (argv[0], new_argv);
perror ("execvp");
_exit (EXIT_FAILURE);
}
if (waitpid (pid, &status, 0) == -1)
error (EXIT_FAILURE, errno, "waitpid");
if (WIFEXITED (status))
exit (WEXITSTATUS (status));
error (EXIT_FAILURE, 0, "unexpected exit status from process: %d", status);
}
/* If we reach this else clause, then we have forked. Now we must
* create a thread to read events from the pipe. This must be
* constantly reading from the pipe, otherwise we will deadlock.
* During the warm-up phase we end up throwing away messages.
*/
else {
pthread_t thread;
pthread_attr_t attr;
int r;
r = pthread_attr_init (&attr);
if (r != 0)
error (EXIT_FAILURE, r, "pthread_attr_init");
r = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
if (r != 0)
error (EXIT_FAILURE, r, "pthread_attr_setdetachstate");
r = pthread_create (&thread, &attr, libvirt_log_thread, NULL);
if (r != 0)
error (EXIT_FAILURE, r, "pthread_create");
pthread_attr_destroy (&attr);
}
}
static void
start_libvirt_thread (size_t pass)
{
/* In the non-libvirt case, this variable is ignored. */
pthread_mutex_lock (&pass_data_lock);
libvirt_pass = pass;
pthread_mutex_unlock (&pass_data_lock);
}
static void
stop_libvirt_thread (void)
{
/* In the non-libvirt case, this variable is ignored. */
pthread_mutex_lock (&pass_data_lock);
libvirt_pass = -1;
pthread_mutex_unlock (&pass_data_lock);
}
/* The separate "libvirt thread". It loops reading debug messages
* printed by libvirt and adds them to the pass_data.
*/
static void *
libvirt_log_thread (void *arg)
{
struct event *event;
CLEANUP_FREE char *buf = NULL;
ssize_t r;
buf = malloc (BUFSIZ);
if (buf == NULL)
error (EXIT_FAILURE, errno, "malloc");
while ((r = read (libvirt_pipe[0], buf, BUFSIZ)) > 0) {
pthread_mutex_lock (&pass_data_lock);
if (libvirt_pass == -1) goto discard;
event =
add_event_unlocked (&pass_data[libvirt_pass], GUESTFS_EVENT_LIBRARY);
event->message = strndup (buf, r);
if (event->message == NULL)
error (EXIT_FAILURE, errno, "strndup");
discard:
pthread_mutex_unlock (&pass_data_lock);
}
if (r == -1)
error (EXIT_FAILURE, errno, "libvirt_log_thread: read");
/* It's possible for the pipe to be closed (r == 0) if thread
* cancellation is delayed after the main thread exits, so just
* ignore that case and exit.
*/
pthread_exit (NULL);
}
static int
is_libvirt_backend (guestfs_h *g)
{
CLEANUP_FREE char *backend = guestfs_get_backend (g);
return backend &&
(STREQ (backend, "libvirt") || STRPREFIX (backend, "libvirt:"));
}
/* Sanity check the collected events. */
static void
check_pass_data (void)
{
size_t i, j, len;
int64_t ns;
const char *message;
for (i = 0; i < NR_TEST_PASSES; ++i) {
assert (pass_data[i].pass == i);
assert (pass_data[i].elapsed_ns > 1000);
assert (pass_data[i].nr_events > 0);
assert (pass_data[i].events != NULL);
for (j = 0; j < pass_data[i].nr_events; ++j) {
assert (pass_data[i].events[j].t.tv_sec > 0);
if (j > 0) {
ns = timespec_diff (&pass_data[i].events[j-1].t,
&pass_data[i].events[j].t);
assert (ns >= 0);
}
assert (pass_data[i].events[j].source != 0);
message = pass_data[i].events[j].message;
assert (message != NULL);
assert (pass_data[i].events[j].source != GUESTFS_EVENT_APPLIANCE ||
strchr (message, '\n') == NULL);
len = strlen (message);
assert (len == 0 || message[len-1] != '\r');
}
}
}
static void
print_escaped_string (const char *message)
{
while (*message) {
if (isprint (*message))
putchar (*message);
else
printf ("\\x%02x", (unsigned int) *message);
message++;
}
}
/* Dump the events to stdout, if verbose is set. */
static void
dump_pass_data (void)
{
size_t i, j;
for (i = 0; i < NR_TEST_PASSES; ++i) {
printf ("pass %zu\n", pass_data[i].pass);
printf (" number of events collected %zu\n", pass_data[i].nr_events);
printf (" elapsed time %" PRIi64 " ns\n", pass_data[i].elapsed_ns);
for (j = 0; j < pass_data[i].nr_events; ++j) {
int64_t ns;
CLEANUP_FREE char *event_str = NULL;
ns = timespec_diff (&pass_data[i].start_t, &pass_data[i].events[j].t);
event_str = guestfs_event_to_string (pass_data[i].events[j].source);
printf (" #%zu: +%" PRIi64 " [%s] \"", j, ns, event_str);
print_escaped_string (pass_data[i].events[j].message);
printf ("\"\n");
}
}
}
int
activity_exists (const char *name)
{
size_t i;
for (i = 0; i < nr_activities; ++i)
if (STREQ (activities[i].name, name))
return 1;
return 0;
}
/* Add an activity to the global list. */
struct activity *
add_activity (const char *name, int flags)
{
struct activity *ret;
size_t i;
/* You shouldn't have two activities with the same name. */
assert (!activity_exists (name));
nr_activities++;
activities = realloc (activities, sizeof (struct activity) * nr_activities);
if (activities == NULL)
error (EXIT_FAILURE, errno, "realloc");
ret = &activities[nr_activities-1];
ret->name = strdup (name);
if (ret->name == NULL)
error (EXIT_FAILURE, errno, "strdup");
ret->flags = flags;
for (i = 0; i < NR_TEST_PASSES; ++i)
ret->start_event[i] = ret->end_event[i] = 0;
return ret;
}
struct activity *
find_activity (const char *name)
{
size_t i;
for (i = 0; i < nr_activities; ++i)
if (STREQ (activities[i].name, name))
return &activities[i];
error (EXIT_FAILURE, 0,
"internal error: could not find activity '%s'", name);
/*NOTREACHED*/
abort ();
}
int
activity_exists_with_no_data (const char *name, size_t pass)
{
size_t i;
for (i = 0; i < nr_activities; ++i)
if (STREQ (activities[i].name, name) &&
activities[i].start_event[pass] == 0 &&
activities[i].end_event[pass] == 0)
return 1;
return 0;
}
static int
compare_activities_by_t (const void *av, const void *bv)
{
const struct activity *a = av;
const struct activity *b = bv;
return a->t - b->t;
}
/* Go through the activities, computing the start and elapsed time. */
static void
analyze_timeline (void)
{
struct activity *activity;
size_t i, j;
int64_t delta_ns;
for (j = 0; j < nr_activities; ++j) {
activity = &activities[j];
activity->t = 0;
activity->mean = 0;
for (i = 0; i < NR_TEST_PASSES; ++i) {
delta_ns =
timespec_diff (&pass_data[i].events[0].t,
&pass_data[i].events[activity->start_event[i]].t);
activity->t += delta_ns;
delta_ns =
timespec_diff (&pass_data[i].events[activity->start_event[i]].t,
&pass_data[i].events[activity->end_event[i]].t);
activity->mean += delta_ns;
}
/* Divide through to get real start time and mean of each activity. */
activity->t /= NR_TEST_PASSES;
activity->mean /= NR_TEST_PASSES;
/* Calculate the end time of this activity. It's convenient when
* drawing the timeline for one activity to finish just before the
* next activity starts, rather than having them end and start at
* the same time, hence ``- 1'' here.
*/
activity->end_t = activity->t + activity->mean - 1;
/* The above only calculated mean. Now we are able to
* calculate from the mean the variance and the standard
* deviation.
*/
activity->variance = 0;
for (i = 0; i < NR_TEST_PASSES; ++i) {
delta_ns =
timespec_diff (&pass_data[i].events[activity->start_event[i]].t,
&pass_data[i].events[activity->end_event[i]].t);
activity->variance += pow (delta_ns - activity->mean, 2);
}
activity->variance /= NR_TEST_PASSES;
activity->sd = sqrt (activity->variance);