common.pattern 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (C) 2009 Red Hat, Inc.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. do_is_allocated() {
  19. local start=$1
  20. local size=$2
  21. local step=$3
  22. local count=$4
  23. for ((i=1;i<=$count;i++)); do
  24. echo "alloc $(( start + (i - 1) * step )) $size"
  25. done
  26. }
  27. is_allocated() {
  28. do_is_allocated "$@" | $QEMU_IO "$TEST_IMG" | _filter_qemu_io
  29. }
  30. do_io() {
  31. local op=$1
  32. local start=$2
  33. local size=$3
  34. local step=$4
  35. local count=$5
  36. local pattern=$6
  37. echo "=== IO: pattern $pattern" >&2
  38. for ((i=1;i<=$count;i++)); do
  39. echo "$op -P $pattern $(( start + (i - 1) * step )) $size"
  40. done
  41. }
  42. io_pattern() {
  43. do_io "$@" | $QEMU_IO "$TEST_IMG" | _filter_qemu_io
  44. }
  45. io() {
  46. local start=$2
  47. local pattern=$(( (start >> 9) % 256 ))
  48. do_io "$@" $pattern | $QEMU_IO "$TEST_IMG" | _filter_qemu_io
  49. }
  50. io_zero() {
  51. do_io "$@" 0 | $QEMU_IO "$TEST_IMG" | _filter_qemu_io
  52. }
  53. io_test() {
  54. local op=$1
  55. local offset=$2
  56. local cluster_size=$3
  57. local num_large=$4
  58. local num_medium=$((num_large * num_large))
  59. local num_small=$((4 * num_medium))
  60. local half_cluster=$((cluster_size / 2))
  61. local quarter_cluster=$((cluster_size / 4))
  62. local l2_size=$((cluster_size * cluster_size / 8))
  63. # Complete clusters
  64. io "$op" $offset $cluster_size $cluster_size $num_small
  65. offset=$((offset + num_small * $cluster_size))
  66. # From somewhere in the middle to the end of a cluster
  67. io "$op" $((offset + $half_cluster)) $half_cluster $cluster_size $num_small
  68. offset=$((offset + num_small * $cluster_size))
  69. # From the start to somewhere in the middle of a cluster
  70. io "$op" $offset $half_cluster $cluster_size $num_small
  71. offset=$((offset + num_small * $cluster_size))
  72. # Completely misaligned (and small)
  73. io "$op" $((offset + $quarter_cluster)) $half_cluster $cluster_size $num_small
  74. offset=$((offset + num_small * $cluster_size))
  75. # Spanning multiple clusters
  76. io "$op" $((offset + $half_cluster)) $((cluster_size * 2)) $((cluster_size * 3)) $num_medium
  77. offset=$((offset + num_medium * 3 * $cluster_size))
  78. # Spanning multiple L2 tables
  79. # L2 table size: 512 clusters of 4k = 2M
  80. offset=$(( ((offset + l2_size - 1) & ~(l2_size - 1)) - (3 * half_cluster) ))
  81. io "$op" $offset $((6 * half_cluster)) $(( l2_size + half_cluster )) $num_large
  82. offset=$((offset + num_large * ( l2_size + half_cluster )))
  83. }
  84. io_test2() {
  85. local orig_offset=$1
  86. local cluster_size=$2
  87. local num=$3
  88. # Pattern (repeat after 9 clusters):
  89. # used - used - free - used - compressed - compressed -
  90. # free - free - compressed
  91. # Write the clusters to be compressed
  92. echo '=== Clusters to be compressed [1]'
  93. io_pattern writev $((offset + 4 * $cluster_size)) $cluster_size $((9 * $cluster_size)) $num 165
  94. echo '=== Clusters to be compressed [2]'
  95. io_pattern writev $((offset + 5 * $cluster_size)) $cluster_size $((9 * $cluster_size)) $num 165
  96. echo '=== Clusters to be compressed [3]'
  97. io_pattern writev $((offset + 8 * $cluster_size)) $cluster_size $((9 * $cluster_size)) $num 165
  98. mv "$TEST_IMG" "$TEST_IMG.orig"
  99. $QEMU_IMG convert -f $IMGFMT -O $IMGFMT -c "$TEST_IMG.orig" "$TEST_IMG"
  100. # Write the used clusters
  101. echo '=== Used clusters [1]'
  102. io_pattern writev $((offset + 0 * $cluster_size)) $cluster_size $((9 * $cluster_size)) $num 165
  103. echo '=== Used clusters [2]'
  104. io_pattern writev $((offset + 1 * $cluster_size)) $cluster_size $((9 * $cluster_size)) $num 165
  105. echo '=== Used clusters [3]'
  106. io_pattern writev $((offset + 3 * $cluster_size)) $cluster_size $((9 * $cluster_size)) $num 165
  107. # Read them
  108. echo '=== Read used/compressed clusters'
  109. io_pattern readv $((offset + 0 * $cluster_size)) $((2 * $cluster_size)) $((9 * $cluster_size)) $num 165
  110. io_pattern readv $((offset + 3 * $cluster_size)) $((3 * $cluster_size)) $((9 * $cluster_size)) $num 165
  111. io_pattern readv $((offset + 8 * $cluster_size)) $((1 * $cluster_size)) $((9 * $cluster_size)) $num 165
  112. echo '=== Read zeros'
  113. io_zero readv $((offset + 2 * $cluster_size)) $((1 * $cluster_size)) $((9 * $cluster_size)) $num
  114. io_zero readv $((offset + 6 * $cluster_size)) $((2 * $cluster_size)) $((9 * $cluster_size)) $num
  115. }