File: boot-benchmark.c

package info (click to toggle)
libguestfs 1%3A1.34.6-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 112,152 kB
  • ctags: 48,429
  • sloc: ansic: 438,854; ml: 49,329; sh: 15,718; java: 9,326; perl: 8,954; makefile: 7,318; cs: 6,153; haskell: 5,531; python: 3,161; erlang: 2,386; xml: 1,739; ruby: 350; pascal: 248; lex: 134; yacc: 128; cpp: 10
file content (227 lines) | stat: -rw-r--r-- 5,810 bytes parent folder | download
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
/* 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.
 */

/* See instructions in boot-benchmark.1 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <getopt.h>
#include <limits.h>
#include <time.h>
#include <errno.h>
#include <error.h>
#include <assert.h>
#include <math.h>

#include "guestfs.h"
#include "guestfs-internal-frontend.h"

#include "boot-analysis-utils.h"

#include "getprogname.h"

#define NR_WARMUP_PASSES 3
#define NR_TEST_PASSES   10

static const char *append = NULL;
static int memsize = 0;
static int smp = 1;

static void run_test (void);
static guestfs_h *create_handle (void);
static void add_drive (guestfs_h *g);

static void __attribute__((noreturn))
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-benchmark: Benchmark the time taken to boot the libguestfs appliance.\n"
           "Usage:\n"
           "  boot-benchmark [--options]\n"
           "Options:\n"
           "  --help         Display this usage text and exit.\n"
           "  --append OPTS  Append OPTS to kernel command line.\n"
           "  -m MB\n"
           "  --memsize MB   Set memory size in MB (default: %d).\n"
           "  --smp N        Enable N virtual CPUs (default: 1).\n",
           default_memsize);
  exit (exitcode);
}

int
main (int argc, char *argv[])
{
  enum { HELP_OPTION = CHAR_MAX + 1 };
  static const char options[] = "m:";
  static const struct option long_options[] = {
    { "help", 0, 0, HELP_OPTION },
    { "append", 1, 0, 0 },
    { "memsize", 1, 0, 'm' },
    { "smp", 1, 0, 0 },
    { 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, "smp")) {
        if (sscanf (optarg, "%d", &smp) != 1) {
          fprintf (stderr, "%s: could not parse smp parameter: %s\n",
                   getprogname (), optarg);
          exit (EXIT_FAILURE);
        }
        break;
      }
      fprintf (stderr, "%s: unknown long option: %s (%d)\n",
               getprogname (), 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",
                 getprogname (), optarg);
        exit (EXIT_FAILURE);
      }
      break;

    case HELP_OPTION:
      usage (EXIT_SUCCESS);

    default:
      usage (EXIT_FAILURE);
    }
  }

  run_test ();
}

static void
run_test (void)
{
  guestfs_h *g;
  size_t i;
  int64_t ns[NR_TEST_PASSES];
  double mean;
  double variance;
  double sd;

  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 ...\n");
  for (i = 0; i < NR_TEST_PASSES; ++i) {
    struct timespec start_t, end_t;

    g = create_handle ();
    add_drive (g);
    get_time (&start_t);
    if (guestfs_launch (g) == -1)
      exit (EXIT_FAILURE);
    guestfs_close (g);
    get_time (&end_t);

    ns[i] = timespec_diff (&start_t, &end_t);
  }

  /* Calculate the mean. */
  mean = 0;
  for (i = 0; i < NR_TEST_PASSES; ++i)
    mean += ns[i];
  mean /= NR_TEST_PASSES;

  /* Calculate the variance and standard deviation. */
  variance = 0;
  for (i = 0; i < NR_TEST_PASSES; ++i)
    variance = pow (ns[i] - mean, 2);
  variance /= NR_TEST_PASSES;
  sd = sqrt (variance);

  /* Print the test parameters. */
  printf ("\n");
  g = create_handle ();
  test_info (g, NR_TEST_PASSES);
  guestfs_close (g);

  /* Print the result. */
  printf ("\n");
  printf ("Result: %.1fms ±%.1fms\n", mean / 1000000, sd / 1000000);
}

/* 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);

  if (append != NULL)
    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);
}