inspect: Add support for Linux Mint and Mandriva.
[libguestfs.git] / contrib / visualize-alignment / qemu-0.13-trace-block-device-access.patch
1 From e04ef476fd330485e5a88c7018d29c55cf411fe2 Mon Sep 17 00:00:00 2001
2 From: Richard W.M. Jones <rjones@redhat.com>
3 Date: Tue, 5 Oct 2010 09:54:10 +0100
4 Subject: [PATCH] Trace reads and writes to qemu block devices.
5
6 NB: This patch is not suitable for nor intended to go upstream in
7 its current form.
8
9 When qemu opens a block device, this patch creates a trace file
10 in /tmp with a name related to the block device.  Reads and writes
11 to the device cause lines to be written to the trace file:
12
13   S <total_sectors>
14   W <sector> <nb_sectors>
15   R <sector> <nb_sectors>
16
17 'S' is the summary line, printed first which just tells you how
18 many sectors are on the device.
19
20 'W' and 'R' are writes and reads, for the range <sector> through
21 to <sector> + <nb_sectors> - 1.
22 ---
23  block.c     |   29 +++++++++++++++++++++++++++++
24  block_int.h |    3 +++
25  2 files changed, 32 insertions(+), 0 deletions(-)
26
27 diff --git a/block.c b/block.c
28 index ebbc376..26ead5b 100644
29 --- a/block.c
30 +++ b/block.c
31 @@ -474,6 +474,23 @@ static int bdrv_open_common(BlockDriverState *bs, const char *filename,
32          goto free_and_fail;
33      }
34  
35 +    /* Open trace file in /tmp based on filename. XXX */
36 +    size_t len = strlen (filename);
37 +    char *trace_file = qemu_malloc (10 + len);
38 +    snprintf (trace_file, 10 + len, "/tmp/%s.qtr", filename);
39 +    size_t i;
40 +    for (i = 5; i < 5 + len; ++i) {
41 +        if (trace_file[i] == '/')
42 +            trace_file[i] = '_';
43 +    }
44 +    bs->trace_fp = fopen (trace_file, "w");
45 +    if (bs->trace_fp) {
46 +        setlinebuf (bs->trace_fp);
47 +        fprintf (bs->trace_fp, "S %" PRIi64 "\n", bs->total_sectors);
48 +    } else {
49 +        perror (trace_file);
50 +    }
51 +
52  #ifndef _WIN32
53      if (bs->is_temporary) {
54          unlink(filename);
55 @@ -665,6 +682,10 @@ void bdrv_close(BlockDriverState *bs)
56              bdrv_close(bs->file);
57          }
58  
59 +        if (bs->trace_fp)
60 +            fclose (bs->trace_fp);
61 +        bs->trace_fp = NULL;
62 +
63          /* call the change callback */
64          bs->media_changed = 1;
65          if (bs->change_cb)
66 @@ -1995,6 +2016,10 @@ BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
67         /* Update stats even though technically transfer has not happened. */
68         bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
69         bs->rd_ops ++;
70 +
71 +        if (bs->trace_fp)
72 +            fprintf (bs->trace_fp,
73 +                     "R %" PRIi64 " %d\n", sector_num, nb_sectors);
74      }
75  
76      return ret;
77 @@ -2028,6 +2053,10 @@ BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
78          if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
79              bs->wr_highest_sector = sector_num + nb_sectors - 1;
80          }
81 +
82 +        if (bs->trace_fp)
83 +            fprintf (bs->trace_fp,
84 +                     "W %" PRIi64 " %d\n", sector_num, nb_sectors);
85      }
86  
87      return ret;
88 diff --git a/block_int.h b/block_int.h
89 index e8e7156..03e7c9b 100644
90 --- a/block_int.h
91 +++ b/block_int.h
92 @@ -178,6 +178,9 @@ struct BlockDriverState {
93      uint64_t wr_ops;
94      uint64_t wr_highest_sector;
95  
96 +    /* Trace to file. */
97 +    FILE *trace_fp;
98 +
99      /* Whether the disk can expand beyond total_sectors */
100      int growable;
101  
102 -- 
103 1.7.3.1
104