Commit a36e69ddfe8452211bcf3ed94716c60bce5ccd8c

Authored by ths
1 parent e4bcb14c

Collecting block device statistics, by Richard W.M. Jones.


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@3760 c046a42c-6fe2-441c-8c8c-71466251a162
... ... @@ -521,8 +521,11 @@ int bdrv_read(BlockDriverState *bs, int64_t sector_num,
521 521 return ret;
522 522 else if (ret != len)
523 523 return -EINVAL;
524   - else
  524 + else {
  525 + bs->rd_bytes += (unsigned) len;
  526 + bs->rd_ops ++;
525 527 return 0;
  528 + }
526 529 } else {
527 530 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
528 531 }
... ... @@ -553,8 +556,11 @@ int bdrv_write(BlockDriverState *bs, int64_t sector_num,
553 556 return ret;
554 557 else if (ret != len)
555 558 return -EIO;
556   - else
  559 + else {
  560 + bs->wr_bytes += (unsigned) len;
  561 + bs->wr_ops ++;
557 562 return 0;
  563 + }
558 564 } else {
559 565 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
560 566 }
... ... @@ -902,6 +908,24 @@ void bdrv_info(void)
902 908 term_printf("\n");
903 909 }
904 910 }
  911 +
  912 +/* The "info blockstats" command. */
  913 +void bdrv_info_stats (void)
  914 +{
  915 + BlockDriverState *bs;
  916 +
  917 + for (bs = bdrv_first; bs != NULL; bs = bs->next) {
  918 + term_printf ("%s:"
  919 + " rd_bytes=%" PRIu64
  920 + " wr_bytes=%" PRIu64
  921 + " rd_operations=%" PRIu64
  922 + " wr_operations=%" PRIu64
  923 + "\n",
  924 + bs->device_name,
  925 + bs->rd_bytes, bs->wr_bytes,
  926 + bs->rd_ops, bs->wr_ops);
  927 + }
  928 +}
905 929 #endif
906 930  
907 931 void bdrv_get_backing_filename(BlockDriverState *bs,
... ... @@ -1064,6 +1088,7 @@ BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1064 1088 BlockDriverCompletionFunc *cb, void *opaque)
1065 1089 {
1066 1090 BlockDriver *drv = bs->drv;
  1091 + BlockDriverAIOCB *ret;
1067 1092  
1068 1093 if (!drv)
1069 1094 return NULL;
... ... @@ -1076,7 +1101,15 @@ BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1076 1101 buf += 512;
1077 1102 }
1078 1103  
1079   - return drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
  1104 + ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
  1105 +
  1106 + if (ret) {
  1107 + /* Update stats even though technically transfer has not happened. */
  1108 + bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
  1109 + bs->rd_ops ++;
  1110 + }
  1111 +
  1112 + return ret;
1080 1113 }
1081 1114  
1082 1115 BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
... ... @@ -1084,6 +1117,7 @@ BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1084 1117 BlockDriverCompletionFunc *cb, void *opaque)
1085 1118 {
1086 1119 BlockDriver *drv = bs->drv;
  1120 + BlockDriverAIOCB *ret;
1087 1121  
1088 1122 if (!drv)
1089 1123 return NULL;
... ... @@ -1093,7 +1127,15 @@ BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1093 1127 memcpy(bs->boot_sector_data, buf, 512);
1094 1128 }
1095 1129  
1096   - return drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
  1130 + ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
  1131 +
  1132 + if (ret) {
  1133 + /* Update stats even though technically transfer has not happened. */
  1134 + bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
  1135 + bs->wr_ops ++;
  1136 + }
  1137 +
  1138 + return ret;
1097 1139 }
1098 1140  
1099 1141 void bdrv_aio_cancel(BlockDriverAIOCB *acb)
... ...
... ... @@ -47,6 +47,7 @@ typedef struct QEMUSnapshotInfo {
47 47  
48 48 #ifndef QEMU_IMG
49 49 void bdrv_info(void);
  50 +void bdrv_info_stats(void);
50 51 #endif
51 52  
52 53 void bdrv_init(void);
... ...
block_int.h
... ... @@ -114,6 +114,12 @@ struct BlockDriverState {
114 114  
115 115 void *sync_aiocb;
116 116  
  117 + /* I/O stats (display with "info blockstats"). */
  118 + uint64_t rd_bytes;
  119 + uint64_t wr_bytes;
  120 + uint64_t rd_ops;
  121 + uint64_t wr_ops;
  122 +
117 123 /* NOTE: the following infos are only hints for real hardware
118 124 drivers. They are not used by the block driver */
119 125 int cyls, heads, secs, translation;
... ...
monitor.c
... ... @@ -255,6 +255,11 @@ static void do_info_block(void)
255 255 bdrv_info();
256 256 }
257 257  
  258 +static void do_info_blockstats(void)
  259 +{
  260 + bdrv_info_stats();
  261 +}
  262 +
258 263 /* get the current CPU defined by the user */
259 264 static int mon_set_cpu(int cpu_index)
260 265 {
... ... @@ -1327,6 +1332,8 @@ static term_cmd_t info_cmds[] = {
1327 1332 "", "show the network state" },
1328 1333 { "block", "", do_info_block,
1329 1334 "", "show the block devices" },
  1335 + { "blockstats", "", do_info_blockstats,
  1336 + "", "show block device statistics" },
1330 1337 { "registers", "", do_info_registers,
1331 1338 "", "show the cpu registers" },
1332 1339 { "cpus", "", do_info_cpus,
... ...