Commit 95533d5ffbdf3f45e8daa64477b63cf771376618
Committed by
Anthony Liguori
1 parent
84844a20
qemu-io: add aio read/write/flush commands
Add commands to exercise asynchronous reads/writes and to flush all outstanding aio commands. Commands to exercise aio cancellations will follow in a separate patch. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing
1 changed file
with
353 additions
and
0 deletions
qemu-io.c
| @@ -752,6 +752,351 @@ static const cmdinfo_t writev_cmd = { | @@ -752,6 +752,351 @@ static const cmdinfo_t writev_cmd = { | ||
| 752 | .help = writev_help, | 752 | .help = writev_help, |
| 753 | }; | 753 | }; |
| 754 | 754 | ||
| 755 | +struct aio_ctx { | ||
| 756 | + QEMUIOVector qiov; | ||
| 757 | + int64_t offset; | ||
| 758 | + char *buf; | ||
| 759 | + int qflag; | ||
| 760 | + int vflag; | ||
| 761 | + int Cflag; | ||
| 762 | + int Pflag; | ||
| 763 | + int pattern; | ||
| 764 | + struct timeval t1; | ||
| 765 | +}; | ||
| 766 | + | ||
| 767 | +static void | ||
| 768 | +aio_write_done(void *opaque, int ret) | ||
| 769 | +{ | ||
| 770 | + struct aio_ctx *ctx = opaque; | ||
| 771 | + struct timeval t2; | ||
| 772 | + int total; | ||
| 773 | + int cnt = 1; | ||
| 774 | + | ||
| 775 | + gettimeofday(&t2, NULL); | ||
| 776 | + | ||
| 777 | + total = ctx->qiov.size; | ||
| 778 | + | ||
| 779 | + if (ret < 0) { | ||
| 780 | + printf("aio_write failed: %s\n", strerror(-ret)); | ||
| 781 | + return; | ||
| 782 | + } | ||
| 783 | + | ||
| 784 | + if (ctx->qflag) | ||
| 785 | + return; | ||
| 786 | + | ||
| 787 | + /* Finally, report back -- -C gives a parsable format */ | ||
| 788 | + t2 = tsub(t2, ctx->t1); | ||
| 789 | + print_report("wrote", &t2, ctx->offset, ctx->qiov.size, total, cnt, | ||
| 790 | + ctx->Cflag); | ||
| 791 | + | ||
| 792 | + qemu_io_free(ctx->buf); | ||
| 793 | + free(ctx); | ||
| 794 | +} | ||
| 795 | + | ||
| 796 | +static const cmdinfo_t aio_read_cmd; | ||
| 797 | + | ||
| 798 | +static void | ||
| 799 | +aio_read_done(void *opaque, int ret) | ||
| 800 | +{ | ||
| 801 | + struct aio_ctx *ctx = opaque; | ||
| 802 | + struct timeval t2; | ||
| 803 | + int total; | ||
| 804 | + int cnt = 1; | ||
| 805 | + | ||
| 806 | + gettimeofday(&t2, NULL); | ||
| 807 | + | ||
| 808 | + total = ctx->qiov.size; | ||
| 809 | + | ||
| 810 | + if (ret < 0) { | ||
| 811 | + printf("readv failed: %s\n", strerror(-ret)); | ||
| 812 | + return; | ||
| 813 | + } | ||
| 814 | + | ||
| 815 | + if (ctx->Pflag) { | ||
| 816 | + void *cmp_buf = malloc(total); | ||
| 817 | + | ||
| 818 | + memset(cmp_buf, ctx->pattern, total); | ||
| 819 | + if (memcmp(ctx->buf, cmp_buf, total)) { | ||
| 820 | + printf("Pattern verification failed at offset %lld, " | ||
| 821 | + "%d bytes\n", | ||
| 822 | + (long long) ctx->offset, total); | ||
| 823 | + } | ||
| 824 | + free(cmp_buf); | ||
| 825 | + } | ||
| 826 | + | ||
| 827 | + if (ctx->qflag) | ||
| 828 | + return; | ||
| 829 | + | ||
| 830 | + if (ctx->vflag) | ||
| 831 | + dump_buffer(ctx->buf, ctx->offset, total); | ||
| 832 | + | ||
| 833 | + /* Finally, report back -- -C gives a parsable format */ | ||
| 834 | + t2 = tsub(t2, ctx->t1); | ||
| 835 | + print_report("read", &t2, ctx->offset, ctx->qiov.size, total, cnt, | ||
| 836 | + ctx->Cflag); | ||
| 837 | + | ||
| 838 | + qemu_io_free(ctx->buf); | ||
| 839 | + free(ctx); | ||
| 840 | + | ||
| 841 | +} | ||
| 842 | + | ||
| 843 | +static void | ||
| 844 | +aio_read_help(void) | ||
| 845 | +{ | ||
| 846 | + printf( | ||
| 847 | +"\n" | ||
| 848 | +" asynchronously reads a range of bytes from the given offset\n" | ||
| 849 | +"\n" | ||
| 850 | +" Example:\n" | ||
| 851 | +" 'aio_read -v 512 1k 1k ' - dumps 2 kilobytes read from 512 bytes into the file\n" | ||
| 852 | +"\n" | ||
| 853 | +" Reads a segment of the currently open file, optionally dumping it to the\n" | ||
| 854 | +" standard output stream (with -v option) for subsequent inspection.\n" | ||
| 855 | +" The read is performed asynchronously and should the aio_flush command \n" | ||
| 856 | +" should be used to ensure all outstanding aio requests have been completed\n" | ||
| 857 | +" -C, -- report statistics in a machine parsable format\n" | ||
| 858 | +" -P, -- use a pattern to verify read data\n" | ||
| 859 | +" -v, -- dump buffer to standard output\n" | ||
| 860 | +" -q, -- quite mode, do not show I/O statistics\n" | ||
| 861 | +"\n"); | ||
| 862 | +} | ||
| 863 | + | ||
| 864 | +static int | ||
| 865 | +aio_read_f(int argc, char **argv) | ||
| 866 | +{ | ||
| 867 | + char *p; | ||
| 868 | + int count = 0; | ||
| 869 | + int nr_iov, i, c; | ||
| 870 | + struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx)); | ||
| 871 | + BlockDriverAIOCB *acb; | ||
| 872 | + | ||
| 873 | + ctx->pattern = 0xcd; | ||
| 874 | + | ||
| 875 | + while ((c = getopt(argc, argv, "CP:qv")) != EOF) { | ||
| 876 | + switch (c) { | ||
| 877 | + case 'C': | ||
| 878 | + ctx->Cflag = 1; | ||
| 879 | + break; | ||
| 880 | + case 'P': | ||
| 881 | + ctx->Pflag = 1; | ||
| 882 | + ctx->pattern = atoi(optarg); | ||
| 883 | + break; | ||
| 884 | + case 'q': | ||
| 885 | + ctx->qflag = 1; | ||
| 886 | + break; | ||
| 887 | + case 'v': | ||
| 888 | + ctx->vflag = 1; | ||
| 889 | + break; | ||
| 890 | + default: | ||
| 891 | + return command_usage(&aio_read_cmd); | ||
| 892 | + } | ||
| 893 | + } | ||
| 894 | + | ||
| 895 | + if (optind > argc - 2) | ||
| 896 | + return command_usage(&aio_read_cmd); | ||
| 897 | + | ||
| 898 | + | ||
| 899 | + ctx->offset = cvtnum(argv[optind]); | ||
| 900 | + if (ctx->offset < 0) { | ||
| 901 | + printf("non-numeric length argument -- %s\n", argv[optind]); | ||
| 902 | + return 0; | ||
| 903 | + } | ||
| 904 | + optind++; | ||
| 905 | + | ||
| 906 | + if (ctx->offset & 0x1ff) { | ||
| 907 | + printf("offset %lld is not sector aligned\n", | ||
| 908 | + (long long)ctx->offset); | ||
| 909 | + return 0; | ||
| 910 | + } | ||
| 911 | + | ||
| 912 | + if (count & 0x1ff) { | ||
| 913 | + printf("count %d is not sector aligned\n", | ||
| 914 | + count); | ||
| 915 | + return 0; | ||
| 916 | + } | ||
| 917 | + | ||
| 918 | + for (i = optind; i < argc; i++) { | ||
| 919 | + size_t len; | ||
| 920 | + | ||
| 921 | + len = cvtnum(argv[i]); | ||
| 922 | + if (len < 0) { | ||
| 923 | + printf("non-numeric length argument -- %s\n", argv[i]); | ||
| 924 | + return 0; | ||
| 925 | + } | ||
| 926 | + count += len; | ||
| 927 | + } | ||
| 928 | + | ||
| 929 | + nr_iov = argc - optind; | ||
| 930 | + qemu_iovec_init(&ctx->qiov, nr_iov); | ||
| 931 | + ctx->buf = p = qemu_io_alloc(count, 0xab); | ||
| 932 | + for (i = 0; i < nr_iov; i++) { | ||
| 933 | + size_t len; | ||
| 934 | + | ||
| 935 | + len = cvtnum(argv[optind]); | ||
| 936 | + if (len < 0) { | ||
| 937 | + printf("non-numeric length argument -- %s\n", | ||
| 938 | + argv[optind]); | ||
| 939 | + return 0; | ||
| 940 | + } | ||
| 941 | + | ||
| 942 | + qemu_iovec_add(&ctx->qiov, p, len); | ||
| 943 | + p += len; | ||
| 944 | + optind++; | ||
| 945 | + } | ||
| 946 | + | ||
| 947 | + gettimeofday(&ctx->t1, NULL); | ||
| 948 | + acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov, | ||
| 949 | + ctx->qiov.size >> 9, aio_read_done, ctx); | ||
| 950 | + if (!acb) | ||
| 951 | + return -EIO; | ||
| 952 | + | ||
| 953 | + return 0; | ||
| 954 | +} | ||
| 955 | + | ||
| 956 | +static const cmdinfo_t aio_read_cmd = { | ||
| 957 | + .name = "aio_read", | ||
| 958 | + .cfunc = aio_read_f, | ||
| 959 | + .argmin = 2, | ||
| 960 | + .argmax = -1, | ||
| 961 | + .args = "[-Cqv] [-P pattern ] off len [len..]", | ||
| 962 | + .oneline = "asynchronously reads a number of bytes", | ||
| 963 | + .help = aio_read_help, | ||
| 964 | +}; | ||
| 965 | + | ||
| 966 | +static const cmdinfo_t aio_write_cmd; | ||
| 967 | + | ||
| 968 | +static void | ||
| 969 | +aio_write_help(void) | ||
| 970 | +{ | ||
| 971 | + printf( | ||
| 972 | +"\n" | ||
| 973 | +" asynchronously writes a range of bytes from the given offset source \n" | ||
| 974 | +" from multiple buffers\n" | ||
| 975 | +"\n" | ||
| 976 | +" Example:\n" | ||
| 977 | +" 'aio_write 512 1k 1k' - writes 2 kilobytes at 512 bytes into the open file\n" | ||
| 978 | +"\n" | ||
| 979 | +" Writes into a segment of the currently open file, using a buffer\n" | ||
| 980 | +" filled with a set pattern (0xcdcdcdcd).\n" | ||
| 981 | +" The write is performed asynchronously and should the aio_flush command \n" | ||
| 982 | +" should be used to ensure all outstanding aio requests have been completed\n" | ||
| 983 | +" -P, -- use different pattern to fill file\n" | ||
| 984 | +" -C, -- report statistics in a machine parsable format\n" | ||
| 985 | +" -q, -- quite mode, do not show I/O statistics\n" | ||
| 986 | +"\n"); | ||
| 987 | +} | ||
| 988 | + | ||
| 989 | + | ||
| 990 | +static int | ||
| 991 | +aio_write_f(int argc, char **argv) | ||
| 992 | +{ | ||
| 993 | + char *p; | ||
| 994 | + int count = 0; | ||
| 995 | + int nr_iov, i, c; | ||
| 996 | + int pattern = 0xcd; | ||
| 997 | + struct aio_ctx *ctx = calloc(1, sizeof(struct aio_ctx)); | ||
| 998 | + BlockDriverAIOCB *acb; | ||
| 999 | + | ||
| 1000 | + while ((c = getopt(argc, argv, "CqP:")) != EOF) { | ||
| 1001 | + switch (c) { | ||
| 1002 | + case 'C': | ||
| 1003 | + ctx->Cflag = 1; | ||
| 1004 | + break; | ||
| 1005 | + case 'q': | ||
| 1006 | + ctx->qflag = 1; | ||
| 1007 | + break; | ||
| 1008 | + case 'P': | ||
| 1009 | + pattern = atoi(optarg); | ||
| 1010 | + break; | ||
| 1011 | + default: | ||
| 1012 | + return command_usage(&aio_write_cmd); | ||
| 1013 | + } | ||
| 1014 | + } | ||
| 1015 | + | ||
| 1016 | + if (optind > argc - 2) | ||
| 1017 | + return command_usage(&aio_write_cmd); | ||
| 1018 | + | ||
| 1019 | + ctx->offset = cvtnum(argv[optind]); | ||
| 1020 | + if (ctx->offset < 0) { | ||
| 1021 | + printf("non-numeric length argument -- %s\n", argv[optind]); | ||
| 1022 | + return 0; | ||
| 1023 | + } | ||
| 1024 | + optind++; | ||
| 1025 | + | ||
| 1026 | + if (ctx->offset & 0x1ff) { | ||
| 1027 | + printf("offset %lld is not sector aligned\n", | ||
| 1028 | + (long long)ctx->offset); | ||
| 1029 | + return 0; | ||
| 1030 | + } | ||
| 1031 | + | ||
| 1032 | + if (count & 0x1ff) { | ||
| 1033 | + printf("count %d is not sector aligned\n", | ||
| 1034 | + count); | ||
| 1035 | + return 0; | ||
| 1036 | + } | ||
| 1037 | + | ||
| 1038 | + | ||
| 1039 | + for (i = optind; i < argc; i++) { | ||
| 1040 | + size_t len; | ||
| 1041 | + | ||
| 1042 | + len = cvtnum(argv[optind]); | ||
| 1043 | + if (len < 0) { | ||
| 1044 | + printf("non-numeric length argument -- %s\n", argv[i]); | ||
| 1045 | + return 0; | ||
| 1046 | + } | ||
| 1047 | + count += len; | ||
| 1048 | + } | ||
| 1049 | + | ||
| 1050 | + nr_iov = argc - optind; | ||
| 1051 | + qemu_iovec_init(&ctx->qiov, nr_iov); | ||
| 1052 | + ctx->buf = p = qemu_io_alloc(count, pattern); | ||
| 1053 | + for (i = 0; i < nr_iov; i++) { | ||
| 1054 | + size_t len; | ||
| 1055 | + | ||
| 1056 | + len = cvtnum(argv[optind]); | ||
| 1057 | + if (len < 0) { | ||
| 1058 | + printf("non-numeric length argument -- %s\n", | ||
| 1059 | + argv[optind]); | ||
| 1060 | + return 0; | ||
| 1061 | + } | ||
| 1062 | + | ||
| 1063 | + qemu_iovec_add(&ctx->qiov, p, len); | ||
| 1064 | + p += len; | ||
| 1065 | + optind++; | ||
| 1066 | + } | ||
| 1067 | + | ||
| 1068 | + gettimeofday(&ctx->t1, NULL); | ||
| 1069 | + acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov, | ||
| 1070 | + ctx->qiov.size >> 9, aio_write_done, ctx); | ||
| 1071 | + if (!acb) | ||
| 1072 | + return -EIO; | ||
| 1073 | + | ||
| 1074 | + return 0; | ||
| 1075 | +} | ||
| 1076 | + | ||
| 1077 | +static const cmdinfo_t aio_write_cmd = { | ||
| 1078 | + .name = "aio_write", | ||
| 1079 | + .cfunc = aio_write_f, | ||
| 1080 | + .argmin = 2, | ||
| 1081 | + .argmax = -1, | ||
| 1082 | + .args = "[-Cq] [-P pattern ] off len [len..]", | ||
| 1083 | + .oneline = "asynchronously writes a number of bytes", | ||
| 1084 | + .help = aio_write_help, | ||
| 1085 | +}; | ||
| 1086 | + | ||
| 1087 | +static int | ||
| 1088 | +aio_flush_f(int argc, char **argv) | ||
| 1089 | +{ | ||
| 1090 | + qemu_aio_flush(); | ||
| 1091 | + return 0; | ||
| 1092 | +} | ||
| 1093 | + | ||
| 1094 | +static const cmdinfo_t aio_flush_cmd = { | ||
| 1095 | + .name = "aio_flush", | ||
| 1096 | + .cfunc = aio_flush_f, | ||
| 1097 | + .oneline = "completes all outstanding aio requets" | ||
| 1098 | +}; | ||
| 1099 | + | ||
| 755 | static int | 1100 | static int |
| 756 | flush_f(int argc, char **argv) | 1101 | flush_f(int argc, char **argv) |
| 757 | { | 1102 | { |
| @@ -1118,6 +1463,9 @@ int main(int argc, char **argv) | @@ -1118,6 +1463,9 @@ int main(int argc, char **argv) | ||
| 1118 | add_command(&readv_cmd); | 1463 | add_command(&readv_cmd); |
| 1119 | add_command(&write_cmd); | 1464 | add_command(&write_cmd); |
| 1120 | add_command(&writev_cmd); | 1465 | add_command(&writev_cmd); |
| 1466 | + add_command(&aio_read_cmd); | ||
| 1467 | + add_command(&aio_write_cmd); | ||
| 1468 | + add_command(&aio_flush_cmd); | ||
| 1121 | add_command(&flush_cmd); | 1469 | add_command(&flush_cmd); |
| 1122 | add_command(&truncate_cmd); | 1470 | add_command(&truncate_cmd); |
| 1123 | add_command(&length_cmd); | 1471 | add_command(&length_cmd); |
| @@ -1137,6 +1485,11 @@ int main(int argc, char **argv) | @@ -1137,6 +1485,11 @@ int main(int argc, char **argv) | ||
| 1137 | openfile(argv[optind], flags); | 1485 | openfile(argv[optind], flags); |
| 1138 | command_loop(); | 1486 | command_loop(); |
| 1139 | 1487 | ||
| 1488 | + /* | ||
| 1489 | + * Make sure all outstanding requests get flushed the program exits. | ||
| 1490 | + */ | ||
| 1491 | + qemu_aio_flush(); | ||
| 1492 | + | ||
| 1140 | if (bs) | 1493 | if (bs) |
| 1141 | bdrv_close(bs); | 1494 | bdrv_close(bs); |
| 1142 | return 0; | 1495 | return 0; |