Author: Allan Jude Language: c
Description: Not specified Timestamp: 2018-04-02 02:47:57 +0000
View raw paste Child paste by: Allan Jude Reply
  1. #include <sys/param.h>
  2. #include <err.h>
  3. #include <stdalign.h>
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. #define UBERBLOCK_SHIFT         (10)                    /* Each uberblock should be padded to 1KB */
  10.  
  11. #define VDEV_UBERRING_SHIFT     (17)                    /* The entire ring of uberblocks is 128KB */
  12. #define VDEV_UBERBLOCK_RING     (128 << 10)
  13.  
  14. #define MAX_UBERBLOCK_SHIFT     (15)                    /* The largest sector size we support is 16KB. Was 13 (8KB) */
  15.  
  16.                                                         /* Calculate the uberblock shift based on ashift bounded by UBERBLOCK_SHIFT and MAX_UBERBLOCK_SHIFT */
  17. #define VDEV_UBERBLOCK_SHIFT(ashift)            \
  18.         MIN(MAX(ashift, UBERBLOCK_SHIFT),       \
  19.             MAX_UBERBLOCK_SHIFT)
  20.  
  21.                                                         /* How many sectors are available for uberblocks. This used to determine the number of uberblocks */
  22. #define VDEV_UBERBLOCK_COUNT(ashift)            \
  23.         (VDEV_UBERBLOCK_RING >> VDEV_UBERBLOCK_SHIFT(ashift))
  24.  
  25.                                                         /* How big is an uberblock, this will need to be renamed to ubersector or something */
  26. #define VDEV_UBERBLOCK_SIZE(ashift)             (1ULL << VDEV_UBERBLOCK_SHIFT(ashift))
  27.  
  28. /*
  29.  * Mask the transaction id [txg] to calculate the offset within the selected
  30.  * ubersector to write the next uberblock. This is done round-robin to avoid
  31.  * writing to the same ubersector for sequential uberblocks.
  32.  * Will be a mask of X 1 bits, shifted left by Y bits.
  33.  * X is equal to the difference in between the ashift [sector size] and
  34.  * UBERBLOCK_SHIFT [uberblock size].
  35.  * Y is the difference between VDEV_UBERRING_SHIFT [size of the uber ring] and
  36.  * VDEV_UBERBLOCK_SHIFT(ashift) [size of each ubersector]
  37.  *
  38.  * ashift=9:  X=10-10=0, Y=17-10=7: 0x0000000
  39.  * ashift=12: X=12-10=2, Y=17-12=5: 0x1100000
  40.  * ashift=14: X=14-10=4, Y=17-14=3: 0x1111000
  41.  */
  42. #define VDEV_SUBBLOCK(txg, ashift)              \
  43.         ((txg & \
  44.           ( \
  45.               ((1ULL << (VDEV_UBERBLOCK_SHIFT(ashift) - UBERBLOCK_SHIFT)) - 1) << \
  46.               (VDEV_UBERRING_SHIFT - VDEV_UBERBLOCK_SHIFT(ashift)) \
  47.           ) \
  48.         ) >> (VDEV_UBERRING_SHIFT - VDEV_UBERBLOCK_SHIFT(ashift)) \
  49.         )
  50.  
  51. int
  52. main(int argc, char *argv[])
  53. {
  54.         unsigned long long txg, n, ashift;
  55.         const char *errstr;
  56.  
  57.         txg = 0;
  58.  
  59.         if (argc > 1) {
  60.                 ashift = strtonum(argv[1], 9, 15, &errstr);
  61.                 if (errstr != NULL) {
  62.                         errx(1, "ashift is %s: %s", errstr, argv[1]);
  63.                 }
  64.         } else {
  65.                 ashift = 12;
  66.         }
  67.  
  68.         for (txg = 0; txg < 256; txg++) {
  69.                 printf("txg=%llu n=%llu\tblock=%2llu\tsubblock=%llu\n",
  70.                     txg,
  71.                     txg & (VDEV_UBERBLOCK_COUNT(UBERBLOCK_SHIFT) - 1),
  72.                     txg & (VDEV_UBERBLOCK_COUNT(ashift) - 1),
  73.                     VDEV_SUBBLOCK(txg, ashift)
  74.                 );
  75.         }
  76. }
  77.  
View raw paste Child paste by: Allan Jude Reply