Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / qemu-io-sim.c
blobb1a5685b9911eee8a818e1ff4620ea56cf496c21
1 /*
2 * Copyright (c) 2010-2011 IBM
4 * Authors:
5 * Chunqiang Tang <ctang@us.ibm.com>
7 * This work is licensed under the terms of the GNU GPL, version 2.
8 * See the COPYING file in the top-level directory.
9 */
11 /*=============================================================================
12 * qemu-io-sim works with qemu-io to perform simulated testing. The 'sim'
13 * command allows the user to control the order of disk I/O and callback
14 * activities in order to test rare race conditions. Note that once 'sim
15 * enable' is done, it can only test aio_read and aio_write. See block/sim.c
16 * for the simulated block device driver.
17 *============================================================================*/
19 #include "qemu/osdep.h"
20 #include "block/blksim.h"
22 void fvd_init_prefetch (BlockDriverState * bs);
23 static void sim_start_prefetch(BlockDriverState *bs)
25 if (!bs->drv->format_name || !strncmp (bs->drv->format_name, "fvd", 3)) {
26 printf ("This image does not support prefetching.\n");
27 return;
29 fvd_init_prefetch (bs);
30 printf ("Prefetching started\n");
33 static void sim_help (void)
35 printf ("\n"
36 " sim enable\t\tenable simulation\n"
37 " sim list\t\tlist all simulation tasks\n"
38 " sim <#task> [#ret]\trun a simulation task, optionally uing #ret as the return value of a read/write operation\n"
39 " sim all [#ret]\t\trun all tasks, optionally using #ret as the return value of read/write tasks\n"
40 " sim prefetch\t\tstart prefetching\n");
43 static int sim_f(BlockDriverState *bs, int argc, char **argv)
45 int ret = 0;
47 if (argc == 3) {
48 ret = atoi (argv[2]);
50 else if (argc != 2) {
51 sim_help ();
52 return 0;
55 if (strcmp (argv[1], "enable") == 0) {
56 if (bs) {
57 printf ("Please close the image first. \"sim enable\" must be done before the\n"
58 "image is opened so that the image is opened with simulation support.\n");
60 else {
61 enable_block_sim(1/*print*/, 0 /*no random time*/);
62 printf ("Block device simulation is enabled.\n");
64 return 0;
67 if (!bs) {
68 fprintf(stderr, "no file open, try 'help open'\n");
69 return 0;
72 if (!bdrv_find_format("blksim")) {
73 printf ("\"sim enable\" must be done before invoking any other sim commands.\n");
74 return 0;
77 if (strcmp (argv[1], "list") == 0) {
78 sim_list_tasks ();
80 else if (strcmp (argv[1], "prefetch") == 0) {
81 sim_start_prefetch(bs);
83 else if (strcmp (argv[1], "all") == 0) {
84 sim_set_disk_io_return_code (ret);
85 int n = sim_all_tasks ();
86 sim_set_disk_io_return_code (0);
87 printf ("Executed %d tasks.\n", n);
89 else {
90 sim_set_disk_io_return_code (ret);
91 sim_task_by_uuid (atoll (argv[1]));
92 sim_set_disk_io_return_code (0);
95 return 0;
98 static const cmdinfo_t sim_cmd = {
99 .name = "sim",
100 .altname = "s",
101 .cfunc = sim_f,
102 .argmin = 1,
103 .argmax = 2,
104 .args = "",
105 .oneline = "use simulation to control the order of disk I/Os and callbacks",
106 .flags = CMD_NOFILE_OK,
107 .help = sim_help,