Commit 1987530fe0fbe00970d572cd2eda5a149aac3333

Authored by aliguori
1 parent a7cbfae0

qcow2 format: keep 'num_free_bytes', and show it upon 'info blockstats' (Uri Lublin)

'num_free_bytes' is the number of non-allocated bytes below highest-allocation.
It's useful, together with the highest-allocation, to figure out how
fragmented the image is, and how likely it will run out-of-space soon.

For example when the highest allocation is high (almost end-of-disk), but 
many bytes (clusters) are free, and can be re-allocated when neeeded, than
we know it's probably not going to reach end-of-disk-space soon.

Added bookkeeping to block-qcow2.c
Export it using BlockDeviceInfo
Show it upon 'info blockstats' if BlockDeviceInfo exists

Signed-off-by: Uri Lublin <uril@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6407 c046a42c-6fe2-441c-8c8c-71466251a162
block-qcow2.c
... ... @@ -145,6 +145,7 @@ typedef struct BDRVQcowState {
145 145 AES_KEY aes_decrypt_key;
146 146  
147 147 int64_t highest_alloc; /* highest cluester allocated (in clusters) */
  148 + int64_t nc_free; /* num of free clusters below highest_alloc */
148 149  
149 150 uint64_t snapshots_offset;
150 151 int snapshots_size;
... ... @@ -173,7 +174,7 @@ static void free_clusters(BlockDriverState *bs,
173 174 #ifdef DEBUG_ALLOC
174 175 static void check_refcounts(BlockDriverState *bs);
175 176 #endif
176   -static void scan_refcount(BlockDriverState *bs, int64_t *high);
  177 +static void scan_refcount(BlockDriverState *bs, int64_t *high, int64_t *free);
177 178  
178 179  
179 180 static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
... ... @@ -283,7 +284,7 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
283 284 if (refcount_init(bs) < 0)
284 285 goto fail;
285 286  
286   - scan_refcount(bs, &s->highest_alloc);
  287 + scan_refcount(bs, &s->highest_alloc, &s->nc_free);
287 288  
288 289 /* read the backing file name */
289 290 if (header.backing_file_offset != 0) {
... ... @@ -1672,6 +1673,7 @@ static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1672 1673 bdi->vm_state_offset = (int64_t)s->l1_vm_state_index <<
1673 1674 (s->cluster_bits + s->l2_bits);
1674 1675 bdi->highest_alloc = s->highest_alloc << s->cluster_bits;
  1676 + bdi->num_free_bytes = s->nc_free << s->cluster_bits;
1675 1677 return 0;
1676 1678 }
1677 1679  
... ... @@ -2214,25 +2216,35 @@ static int load_refcount_block(BlockDriverState *bs,
2214 2216 return 0;
2215 2217 }
2216 2218  
2217   -static void scan_refcount(BlockDriverState *bs, int64_t *high)
  2219 +static void scan_refcount(BlockDriverState *bs, int64_t *high, int64_t *free)
2218 2220 {
2219 2221 BDRVQcowState *s = bs->opaque;
2220   - int64_t refcnt_index, cluster_index, cluster_end, h = 0;
  2222 + int64_t refcnt_index, cluster_index, cluster_end, h = 0, f = 0;
  2223 + int64_t tail = 0; /* do not count last consecutive free entries */
2221 2224  
2222 2225 for (refcnt_index=0; refcnt_index < s->refcount_table_size; refcnt_index++){
2223 2226 if (s->refcount_table[refcnt_index] == 0) {
  2227 + f += 1 << (s->cluster_bits - REFCOUNT_SHIFT);
  2228 + tail += 1 << (s->cluster_bits - REFCOUNT_SHIFT);
2224 2229 continue;
2225 2230 }
2226 2231 cluster_index = refcnt_index << (s->cluster_bits - REFCOUNT_SHIFT);
2227 2232 cluster_end = (refcnt_index + 1) << (s->cluster_bits - REFCOUNT_SHIFT);
2228 2233 for ( ; cluster_index < cluster_end; cluster_index++) {
2229   - if (get_refcount(bs, cluster_index) == 0)
2230   - /* do nothing -- reserved for free counting */;
2231   - else
  2234 + if (get_refcount(bs, cluster_index) == 0) {
  2235 + f++;
  2236 + tail++;
  2237 + }
  2238 + else {
2232 2239 h = cluster_index;
  2240 + tail = 0;
  2241 + }
2233 2242 }
2234 2243 }
2235 2244  
  2245 + f -= tail;
  2246 + if (free)
  2247 + *free = f;
2236 2248 if (high)
2237 2249 *high = (h+1);
2238 2250 }
... ... @@ -2278,8 +2290,10 @@ retry:
2278 2290 (s->free_cluster_index - nb_clusters) << s->cluster_bits);
2279 2291 #endif
2280 2292  
2281   - if (s->highest_alloc < s->free_cluster_index)
  2293 + if (s->highest_alloc < s->free_cluster_index) {
  2294 + s->nc_free += (s->free_cluster_index - s->highest_alloc);
2282 2295 s->highest_alloc = s->free_cluster_index;
  2296 + }
2283 2297  
2284 2298 return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
2285 2299 }
... ... @@ -2456,6 +2470,12 @@ static int update_cluster_refcount(BlockDriverState *bs,
2456 2470 block_index = cluster_index &
2457 2471 ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
2458 2472 refcount = be16_to_cpu(s->refcount_block_cache[block_index]);
  2473 +
  2474 + if (refcount == 1 && addend == -1)
  2475 + s->nc_free += 1;
  2476 + else if (refcount == 0 && addend == 1)
  2477 + s->nc_free -= 1;
  2478 +
2459 2479 refcount += addend;
2460 2480 if (refcount < 0 || refcount > 0xffff)
2461 2481 return -EINVAL;
... ...
... ... @@ -1090,8 +1090,9 @@ void bdrv_info_stats (void)
1090 1090 bs->rd_bytes, bs->wr_bytes,
1091 1091 bs->rd_ops, bs->wr_ops);
1092 1092 if (bdrv_get_info(bs, &bdi) == 0)
1093   - term_printf(" high=%" PRIu64,
1094   - bdi.highest_alloc);
  1093 + term_printf(" high=%" PRId64
  1094 + " bytes_free=%" PRId64,
  1095 + bdi.highest_alloc, bdi.num_free_bytes);
1095 1096 term_printf("\n");
1096 1097 }
1097 1098 }
... ...
... ... @@ -27,6 +27,7 @@ typedef struct BlockDriverInfo {
27 27 /* offset at which the VM state can be saved (0 if not possible) */
28 28 int64_t vm_state_offset;
29 29 int64_t highest_alloc; /* highest allocated block offset (in bytes) */
  30 + int64_t num_free_bytes; /* below highest_alloc */
30 31 } BlockDriverInfo;
31 32  
32 33 typedef struct QEMUSnapshotInfo {
... ...