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
|
/* virt-df
* Copyright (C) 2013 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.
*/
/**
* This file is used by C<virt-df> and some of the other tools when
* they need to run multiple parallel libguestfs instances to operate
* on a large number of libvirt domains efficiently.
*
* It implements a multithreaded work queue. In addition it reorders
* the output so the output still appears in the same order as the
* input (ie. still ordered alphabetically).
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libintl.h>
#include <errno.h>
#include <error.h>
#include <pthread.h>
#ifdef HAVE_LIBVIRT
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
#endif
#include "ignore-value.h"
#include "getprogname.h"
#include "guestfs.h"
#include "guestfs-utils.h"
#include "options.h"
#include "domains.h"
#include "estimate-max-threads.h"
#include "parallel.h"
#if defined(HAVE_LIBVIRT)
/* Maximum number of threads we would ever run. Note this should not
* be > 20, unless libvirt is modified to increase the maximum number
* of clients.
*/
#define MAX_THREADS 12
/* The worker threads take domains off the 'domains' global list until
* 'next_domain_to_take' is 'nr_threads'.
*
* The worker threads retire domains in numerical order, using the
* 'next_domain_to_retire' number.
*
* 'next_domain_to_take' is protected just by a mutex.
* 'next_domain_to_retire' is protected by a mutex and condition.
*/
static size_t next_domain_to_take = 0;
static pthread_mutex_t take_mutex = PTHREAD_MUTEX_INITIALIZER;
static size_t next_domain_to_retire = 0;
static pthread_mutex_t retire_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t retire_cond = PTHREAD_COND_INITIALIZER;
static void thread_failure (const char *fn, int err);
static void *worker_thread (void *arg);
struct thread_data {
size_t thread_num; /* Thread number. */
int trace, verbose; /* Flags from the options_handle. */
work_fn work;
int r; /* Used to store the error status. */
};
/**
* Run the threads and work through the global list of libvirt
* domains.
*
* C<option_P> is whatever the user passed in the I<-P> option, or
* C<0> if the user didn't use the I<-P> option (in which case the
* number of threads is chosen heuristically).
*
* C<options_handle> (which may be C<NULL>) is the global guestfs
* handle created by the options mini-library.
*
* The work function (C<work>) should do the work (inspecting the
* domain, etc.) on domain index C<i>. However it I<must not> print
* out any result directly. Instead it prints anything it needs to
* the supplied C<FILE *>. The work function should return C<0> on
* success or C<-1> on error.
*
* The C<start_threads> function returns C<0> if all work items
* completed successfully, or C<-1> if there was an error.
*/
int
start_threads (size_t option_P, guestfs_h *options_handle, work_fn work)
{
const int trace = options_handle ? guestfs_get_trace (options_handle) : 0;
const int verbose = options_handle ? guestfs_get_verbose (options_handle) : 0;
size_t i, nr_threads;
int err, errors;
void *status;
CLEANUP_FREE struct thread_data *thread_data = NULL;
CLEANUP_FREE pthread_t *threads = NULL;
if (nr_domains == 0) /* Nothing to do. */
return 0;
/* If the user selected the -P option, then we use up to that many threads. */
if (option_P > 0)
nr_threads = MIN (nr_domains, option_P);
else
nr_threads = MIN (nr_domains, MIN (MAX_THREADS, estimate_max_threads ()));
if (verbose)
fprintf (stderr, "parallel: creating %zu threads\n", nr_threads);
thread_data = malloc (sizeof (struct thread_data) * nr_threads);
threads = malloc (sizeof (pthread_t) * nr_threads);
if (thread_data == NULL || threads == NULL)
error (EXIT_FAILURE, errno, "malloc");
for (i = 0; i < nr_threads; ++i) {
thread_data[i].thread_num = i;
thread_data[i].trace = trace;
thread_data[i].verbose = verbose;
thread_data[i].work = work;
}
/* Start the worker threads. */
for (i = 0; i < nr_threads; ++i) {
err = pthread_create (&threads[i], NULL, worker_thread, &thread_data[i]);
if (err != 0)
error (EXIT_FAILURE, err, "pthread_create [%zu]", i);
}
/* Wait for the threads to exit. */
errors = 0;
for (i = 0; i < nr_threads; ++i) {
err = pthread_join (threads[i], &status);
if (err != 0) {
error (0, err, "pthread_join [%zu]", i);
errors++;
}
if (*(int *)status == -1)
errors++;
}
return errors == 0 ? 0 : -1;
}
static void *
worker_thread (void *thread_data_vp)
{
struct thread_data *thread_data = thread_data_vp;
thread_data->r = 0;
if (thread_data->verbose)
fprintf (stderr, "parallel: thread %zu starting\n",
thread_data->thread_num);
while (1) {
size_t i; /* The current domain we're working on. */
FILE *fp;
CLEANUP_FREE char *output = NULL;
size_t output_len = 0;
guestfs_h *g;
int err;
char id[64];
/* Take the next domain from the list. */
if (thread_data->verbose)
fprintf (stderr, "parallel: thread %zu waiting to get work\n",
thread_data->thread_num);
err = pthread_mutex_lock (&take_mutex);
if (err != 0) {
thread_failure ("pthread_mutex_lock", err);
thread_data->r = -1;
return &thread_data->r;
}
i = next_domain_to_take++;
err = pthread_mutex_unlock (&take_mutex);
if (err != 0) {
thread_failure ("pthread_mutex_unlock", err);
thread_data->r = -1;
return &thread_data->r;
}
if (i >= nr_domains) /* Work finished. */
break;
if (thread_data->verbose)
fprintf (stderr, "parallel: thread %zu taking domain %zu\n",
thread_data->thread_num, i);
fp = open_memstream (&output, &output_len);
if (fp == NULL) {
perror ("open_memstream");
thread_data->r = -1;
return &thread_data->r;
}
/* Create a guestfs handle. */
g = guestfs_create ();
if (g == NULL) {
perror ("guestfs_create");
thread_data->r = -1;
return &thread_data->r;
}
/* Set the handle identifier so we can tell threads apart. */
snprintf (id, sizeof id, "thread_%zu_domain_%zu",
thread_data->thread_num, i);
guestfs_set_identifier (g, id);
/* Copy some settings from the options guestfs handle. */
guestfs_set_trace (g, thread_data->trace);
guestfs_set_verbose (g, thread_data->verbose);
/* Do work. */
if (thread_data->work (g, i, fp) == -1) {
thread_data->r = -1;
if (thread_data->verbose)
fprintf (stderr,
"parallel: thread %zu work function returned an error\n",
thread_data->thread_num);
}
fclose (fp);
guestfs_close (g);
/* Retire this domain. We have to retire domains in order, which
* may mean waiting for another thread to finish here.
*/
if (thread_data->verbose)
fprintf (stderr, "parallel: thread %zu waiting to retire domain %zu\n",
thread_data->thread_num, i);
err = pthread_mutex_lock (&retire_mutex);
if (err != 0) {
thread_failure ("pthread_mutex_lock", err);
thread_data->r = -1;
return &thread_data->r;
}
while (next_domain_to_retire != i) {
err = pthread_cond_wait (&retire_cond, &retire_mutex);
if (err != 0) {
thread_failure ("pthread_cond_wait", err);
thread_data->r = -1;
ignore_value (pthread_mutex_unlock (&retire_mutex));
return &thread_data->r;
}
}
if (thread_data->verbose)
fprintf (stderr, "parallel: thread %zu retiring domain %zu\n",
thread_data->thread_num, i);
/* Retire domain. */
printf ("%s", output);
/* Update next_domain_to_retire and tell other threads. */
next_domain_to_retire = i+1;
pthread_cond_broadcast (&retire_cond);
err = pthread_mutex_unlock (&retire_mutex);
if (err != 0) {
thread_failure ("pthread_mutex_unlock", err);
thread_data->r = -1;
return &thread_data->r;
}
}
if (thread_data->verbose)
fprintf (stderr, "parallel: thread %zu exiting (r = %d)\n",
thread_data->thread_num, thread_data->r);
return &thread_data->r;
}
static void
thread_failure (const char *fn, int err)
{
fprintf (stderr, "%s: %s: %s\n",
getprogname (), fn, strerror (err));
}
#endif /* HAVE_LIBVIRT */
|