LCOV - code coverage report
Current view: top level - cmdline - status.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 258 267 96.6 %
Date: 2016-01-16 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2013 Andrea Mazzoleni
       3             :  *
       4             :  * This program is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This program is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "portable.h"
      19             : 
      20             : #include "support.h"
      21             : #include "elem.h"
      22             : #include "state.h"
      23             : #include "parity.h"
      24             : #include "handle.h"
      25             : #include "raid/raid.h"
      26             : 
      27             : /****************************************************************************/
      28             : /* status */
      29             : 
      30          24 : unsigned day_ago(time_t ref, time_t now)
      31             : {
      32          24 :         return (now - ref) / (24 * 3600);
      33             : }
      34             : 
      35             : #define GRAPH_COLUMN 70
      36             : #define GRAPH_ROW 15
      37             : 
      38          62 : static unsigned perc(uint64_t part, uint64_t total)
      39             : {
      40          62 :         if (!total)
      41           6 :                 return 0;
      42             : 
      43          56 :         return (unsigned)(part * 100 / total);
      44             : }
      45             : 
      46             : /**
      47             :  * Bit used to mark unscrubbed time info.
      48             :  */
      49             : #define TIME_NEW 1
      50             : 
      51          14 : int state_status(struct snapraid_state* state)
      52             : {
      53             :         block_off_t blockmax;
      54             :         block_off_t i;
      55             :         time_t* timemap;
      56             :         time_t now;
      57             :         block_off_t bad;
      58             :         block_off_t bad_first;
      59             :         block_off_t bad_last;
      60             :         block_off_t rehash;
      61             :         block_off_t count;
      62             :         unsigned l;
      63             :         unsigned dayoldest, daymedian, daynewest;
      64             :         unsigned bar_scrubbed[GRAPH_COLUMN];
      65             :         unsigned bar_new[GRAPH_COLUMN];
      66             :         unsigned barpos;
      67             :         unsigned barmax;
      68             :         time_t oldest, newest, median;
      69             :         unsigned x, y;
      70             :         tommy_node* node_disk;
      71             :         unsigned file_count;
      72             :         unsigned file_fragmented;
      73             :         unsigned extra_fragment;
      74             :         uint64_t file_size;
      75             :         uint64_t file_block_count;
      76             :         uint64_t file_block_free;
      77             :         block_off_t parity_block_free;
      78             :         unsigned unsynced_blocks;
      79             :         unsigned unscrubbed_blocks;
      80             :         uint64_t all_wasted;
      81             :         int free_not_zero;
      82             : 
      83             :         /* get the present time */
      84          14 :         now = time(0);
      85             : 
      86             :         /* keep track if at least a free info is available */
      87          14 :         free_not_zero = 0;
      88             : 
      89          14 :         blockmax = parity_allocated_size(state);
      90             : 
      91          14 :         log_tag("summary:block_size:%u\n", state->block_size);
      92          14 :         log_tag("summary:parity_block_count:%u\n", blockmax);
      93             : 
      94             :         /* get the minimum parity free space */
      95          14 :         parity_block_free = state->parity[0].free_blocks;
      96          98 :         for (l = 0; l < state->level; ++l) {
      97          84 :                 log_tag("summary:parity_block_total:%s:%u\n", lev_config_name(l), state->parity[l].total_blocks);
      98          84 :                 log_tag("summary:parity_block_free:%s:%u\n", lev_config_name(l), state->parity[l].free_blocks);
      99          84 :                 if (state->parity[l].free_blocks < parity_block_free)
     100           1 :                         parity_block_free = state->parity[l].free_blocks;
     101          84 :                 if (state->parity[l].free_blocks != 0)
     102          73 :                         free_not_zero = 1;
     103             :         }
     104          14 :         log_tag("summary:parity_block_free_min:%u\n", parity_block_free);
     105             : 
     106          14 :         printf("SnapRAID status report:\n");
     107          14 :         printf("\n");
     108          14 :         printf("   Files Fragmented Excess  Wasted  Used    Free  Use Name\n");
     109          14 :         printf("            Files  Fragments  GB      GB      GB\n");
     110             : 
     111             :         /* count fragments */
     112          14 :         file_count = 0;
     113          14 :         file_size = 0;
     114          14 :         file_block_count = 0;
     115          14 :         file_block_free = 0;
     116          14 :         file_fragmented = 0;
     117          14 :         extra_fragment = 0;
     118          14 :         all_wasted = 0;
     119          98 :         for (node_disk = state->disklist; node_disk != 0; node_disk = node_disk->next) {
     120          84 :                 struct snapraid_disk* disk = node_disk->data;
     121             :                 tommy_node* node;
     122             :                 block_off_t j;
     123          84 :                 unsigned disk_file_count = 0;
     124          84 :                 unsigned disk_file_fragmented = 0;
     125          84 :                 unsigned disk_extra_fragment = 0;
     126          84 :                 block_off_t disk_block_count = 0;
     127          84 :                 uint64_t disk_file_size = 0;
     128          84 :                 block_off_t disk_block_latest_used = 0;
     129             :                 block_off_t disk_block_max_by_space;
     130             :                 block_off_t disk_block_max_by_parity;
     131             :                 block_off_t disk_block_max;
     132             :                 int64_t wasted;
     133             : 
     134             :                 /* for each file in the disk */
     135          84 :                 node = disk->filelist;
     136       95690 :                 while (node) {
     137             :                         struct snapraid_file* file;
     138             : 
     139       95522 :                         file = node->data;
     140       95522 :                         node = node->next; /* next node */
     141             : 
     142             :                         /* check fragmentation */
     143       95522 :                         if (file->blockmax != 0) {
     144             :                                 block_off_t prev_pos;
     145             :                                 block_off_t last_pos;
     146             :                                 int fragmented;
     147             : 
     148       95384 :                                 fragmented = 0;
     149       95384 :                                 prev_pos = fs_file2par_get(disk, file, 0);
     150      235639 :                                 for (j = 1; j < file->blockmax; ++j) {
     151      140255 :                                         block_off_t parity_pos = fs_file2par_get(disk, file, j);
     152      140255 :                                         if (prev_pos + 1 != parity_pos) {
     153         698 :                                                 fragmented = 1;
     154         698 :                                                 ++extra_fragment;
     155         698 :                                                 ++disk_extra_fragment;
     156             :                                         }
     157      140255 :                                         prev_pos = parity_pos;
     158             :                                 }
     159             : 
     160             :                                 /* keep track of latest block used */
     161       95384 :                                 last_pos = fs_file2par_get(disk, file, file->blockmax - 1);
     162       95384 :                                 if (last_pos > disk_block_latest_used) {
     163       92527 :                                         disk_block_latest_used = last_pos;
     164             :                                 }
     165             : 
     166       95384 :                                 if (fragmented) {
     167         582 :                                         ++file_fragmented;
     168         582 :                                         ++disk_file_fragmented;
     169             :                                 }
     170             : 
     171       95384 :                                 disk_block_count += file->blockmax;
     172             :                         }
     173             : 
     174             :                         /* count files */
     175       95522 :                         ++file_count;
     176       95522 :                         ++disk_file_count;
     177       95522 :                         file_size += file->size;
     178       95522 :                         file_block_count += file->blockmax;
     179       95522 :                         disk_file_size += file->size;
     180             :                 }
     181             : 
     182          84 :                 if (disk->free_blocks != 0)
     183          48 :                         free_not_zero = 1;
     184             : 
     185             :                 /* get the free block info */
     186          84 :                 disk_block_max_by_space = disk_block_count + disk->free_blocks;
     187          84 :                 disk_block_max_by_parity = blockmax + parity_block_free;
     188             : 
     189             :                 /* the maximum usable space in a disk is limited by the smallest */
     190             :                 /* of the disk size and the parity size */
     191             :                 /* the wasted space is the space that we have to leave */
     192             :                 /* free on the data disk, when the parity is filled up */
     193          84 :                 if (disk_block_max_by_space < disk_block_max_by_parity) {
     194          65 :                         disk_block_max = disk_block_max_by_space;
     195             :                 } else {
     196          19 :                         disk_block_max = disk_block_max_by_parity;
     197             :                 }
     198             : 
     199             :                 /* wasted space is the difference of the two maximum size */
     200             :                 /* if negative, it's extra space available in parity */
     201          84 :                 wasted = (int64_t)disk_block_max_by_space - (int64_t)disk_block_max_by_parity;
     202          84 :                 wasted *= state->block_size;
     203             : 
     204          84 :                 if (wasted > 0)
     205           6 :                         all_wasted += wasted;
     206          84 :                 file_block_free += disk_block_max - disk_block_count;
     207             : 
     208          84 :                 printf("%8u", disk_file_count);
     209          84 :                 printf("%8u", disk_file_fragmented);
     210          84 :                 printf("%8u", disk_extra_fragment);
     211          84 :                 if (wasted < -100LL * GIGA) {
     212          30 :                         printf("       -");
     213             :                 } else {
     214          54 :                         printf("%8.1f", (double)wasted / GIGA);
     215             :                 }
     216          84 :                 printf("%8" PRIu64, disk_file_size / GIGA);
     217             : 
     218          84 :                 if (disk_block_max == 0 && disk_block_count == 0) {
     219             :                         /* if the disk is empty and we don't have the free space info */
     220          36 :                         printf("       -");
     221          36 :                         printf("   - ");
     222             :                 } else {
     223          48 :                         printf("%8" PRIu64, (disk_block_max - disk_block_count) * (uint64_t)state->block_size / GIGA);
     224          48 :                         printf(" %3u%%", perc(disk_block_count, disk_block_max));
     225             :                 }
     226          84 :                 printf(" %s\n", disk->name);
     227             : 
     228          84 :                 log_tag("summary:disk_file_count:%s:%u\n", disk->name, disk_file_count);
     229          84 :                 log_tag("summary:disk_block_count:%s:%u\n", disk->name, disk_block_count);
     230          84 :                 log_tag("summary:disk_fragmented_file_count:%s:%u\n", disk->name, disk_file_fragmented);
     231          84 :                 log_tag("summary:disk_excess_fragment_count:%s:%u\n", disk->name, disk_extra_fragment);
     232          84 :                 log_tag("summary:disk_file_size:%s:%" PRIu64 "\n", disk->name, disk_file_size);
     233          84 :                 log_tag("summary:disk_block_allocated:%s:%u\n", disk->name, disk_block_latest_used + 1);
     234          84 :                 log_tag("summary:disk_block_total:%s:%u\n", disk->name, disk->total_blocks);
     235          84 :                 log_tag("summary:disk_block_free:%s:%u\n", disk->name, disk->free_blocks);
     236          84 :                 log_tag("summary:disk_block_max_by_space:%s:%u\n", disk->name, disk_block_max_by_space);
     237          84 :                 log_tag("summary:disk_block_max_by_parity:%s:%u\n", disk->name, disk_block_max_by_parity);
     238          84 :                 log_tag("summary:disk_block_max:%s:%u\n", disk->name, disk_block_max);
     239          84 :                 log_tag("summary:disk_space_wasted:%s:%" PRId64 "\n", disk->name, wasted);
     240             :         }
     241             : 
     242             :         /* totals */
     243          14 :         printf(" --------------------------------------------------------------------------\n");
     244          14 :         printf("%8u", file_count);
     245          14 :         printf("%8u", file_fragmented);
     246          14 :         printf("%8u", extra_fragment);
     247          14 :         printf("%8.1f", (double)all_wasted / GIGA);
     248          14 :         printf("%8" PRIu64, file_size / GIGA);
     249          14 :         printf("%8" PRIu64, file_block_free * state->block_size / GIGA);
     250          14 :         printf(" %3u%%", perc(file_block_count, file_block_count + file_block_free));
     251          14 :         printf("\n");
     252             : 
     253             :         /* warn about invalid data free info */
     254          14 :         if (!free_not_zero)
     255           1 :                 printf("\nWARNING! Free space info will be valid after the first sync.\n");
     256             : 
     257          14 :         log_tag("summary:file_count:%u\n", file_count);
     258          14 :         log_tag("summary:file_block_count:%" PRIu64 "\n", file_block_count);
     259          14 :         log_tag("summary:fragmented_file_count:%u\n", file_fragmented);
     260          14 :         log_tag("summary:excess_fragment_count:%u\n", extra_fragment);
     261          14 :         log_tag("summary:file_size:%" PRIu64 "\n", file_size);
     262          14 :         log_tag("summary:parity_size:%" PRIu64 "\n", blockmax * (uint64_t)state->block_size);
     263          14 :         log_tag("summary:parity_size_max:%" PRIu64 "\n", (blockmax + parity_block_free) * (uint64_t)state->block_size);
     264          14 :         log_tag("summary:hash:%s\n", hash_config_name(state->hash));
     265          14 :         log_tag("summary:prev_hash:%s\n", hash_config_name(state->prevhash));
     266          14 :         log_tag("summary:best_hash:%s\n", hash_config_name(state->besthash));
     267          14 :         log_flush();
     268             : 
     269             :         /* copy the info a temp vector, and count bad/rehash/unsynced blocks */
     270          14 :         timemap = malloc_nofail(blockmax * sizeof(time_t));
     271          14 :         bad = 0;
     272          14 :         bad_first = 0;
     273          14 :         bad_last = 0;
     274          14 :         count = 0;
     275          14 :         rehash = 0;
     276          14 :         unsynced_blocks = 0;
     277          14 :         unscrubbed_blocks = 0;
     278          14 :         log_tag("block_count:%u\n", blockmax);
     279       39766 :         for (i = 0; i < blockmax; ++i) {
     280             :                 int one_invalid;
     281             :                 int one_valid;
     282             : 
     283       39752 :                 snapraid_info info = info_get(&state->infoarr, i);
     284             : 
     285             :                 /* for each disk */
     286       39752 :                 one_invalid = 0;
     287       39752 :                 one_valid = 0;
     288      278264 :                 for (node_disk = state->disklist; node_disk != 0; node_disk = node_disk->next) {
     289      238512 :                         struct snapraid_disk* disk = node_disk->data;
     290      238512 :                         struct snapraid_block* block = fs_par2block_get(disk, i);
     291             : 
     292      238512 :                         if (block_has_file(block))
     293      235639 :                                 one_valid = 1;
     294      238512 :                         if (block_has_invalid_parity(block))
     295           0 :                                 one_invalid = 1;
     296             :                 }
     297             : 
     298             :                 /* if both valid and invalid, we need to update */
     299       39752 :                 if (one_invalid && one_valid) {
     300           0 :                         ++unsynced_blocks;
     301             :                 }
     302             : 
     303             :                 /* skip unused blocks */
     304       39752 :                 if (info != 0) {
     305             :                         time_t scrub_time;
     306             : 
     307       39752 :                         if (info_get_bad(info)) {
     308        1772 :                                 if (bad == 0)
     309           2 :                                         bad_first = i;
     310        1772 :                                 bad_last = i;
     311        1772 :                                 ++bad;
     312             :                         }
     313             : 
     314       39752 :                         if (info_get_rehash(info))
     315        4607 :                                 ++rehash;
     316             : 
     317       39752 :                         scrub_time = info_get_time(info);
     318             : 
     319       39752 :                         if (info_get_justsynced(info)) {
     320       20460 :                                 ++unscrubbed_blocks;
     321             : 
     322             :                                 /* mark the time as not scrubbed */
     323       20460 :                                 scrub_time |= TIME_NEW;
     324             :                         }
     325             : 
     326       39752 :                         timemap[count++] = scrub_time;
     327             :                 }
     328             : 
     329       39752 :                 if (state->opt.gui) {
     330           0 :                         if (info != 0)
     331           0 :                                 log_tag("block:%u:%" PRIu64 ":%s:%s:%s:%s\n", i, (uint64_t)info_get_time(info), one_valid ? "used" : "", one_invalid ? "unsynced" : "", info_get_bad(info) ? "bad" : "", info_get_rehash(info) ? "rehash" : "");
     332             :                         else
     333           0 :                                 log_tag("block_noinfo:%u:%s:%s\n", i, one_valid ? "used" : "", one_invalid ? "unsynced" : "");
     334             :                 }
     335             :         }
     336             : 
     337          14 :         log_tag("summary:has_unsynced:%u\n", unsynced_blocks);
     338          14 :         log_tag("summary:has_unscrubbed:%u\n", unscrubbed_blocks);
     339          14 :         log_tag("summary:has_rehash:%u\n", rehash);
     340          14 :         log_tag("summary:has_bad:%u:%u:%u\n", bad, bad_first, bad_last);
     341          14 :         log_flush();
     342             : 
     343          14 :         if (!count) {
     344           6 :                 log_fatal("The array is empty.\n");
     345           6 :                 free(timemap);
     346           6 :                 return 0;
     347             :         }
     348             : 
     349             :         /* sort the info to get the time info */
     350           8 :         qsort(timemap, count, sizeof(time_t), time_compare);
     351             : 
     352             :         /* output the info map */
     353           8 :         i = 0;
     354           8 :         log_tag("info_count:%u\n", count);
     355          40 :         while (i < count) {
     356          24 :                 unsigned j = i + 1;
     357       39776 :                 while (j < count && timemap[i] == timemap[j])
     358       39728 :                         ++j;
     359          24 :                 if ((timemap[i] & TIME_NEW) == 0) {
     360           9 :                         log_tag("info_time:%" PRIu64 ":%u:scrubbed\n", (uint64_t)timemap[i], j - i);
     361             :                 } else {
     362          15 :                         log_tag("info_time:%" PRIu64 ":%u:new\n", (uint64_t)(timemap[i] & ~TIME_NEW), j - i);
     363             :                 }
     364          24 :                 i = j;
     365             :         }
     366             : 
     367           8 :         oldest = timemap[0];
     368           8 :         median = timemap[count / 2];
     369           8 :         newest = timemap[count - 1];
     370           8 :         dayoldest = day_ago(oldest, now);
     371           8 :         daymedian = day_ago(median, now);
     372           8 :         daynewest = day_ago(newest, now);
     373             : 
     374             :         /* compute graph limits */
     375           8 :         barpos = 0;
     376           8 :         barmax = 0;
     377         568 :         for (i = 0; i < GRAPH_COLUMN; ++i) {
     378             :                 time_t limit;
     379             :                 unsigned step_scrubbed, step_new;
     380             : 
     381         560 :                 limit = oldest + (newest - oldest) * (i + 1) / GRAPH_COLUMN;
     382             : 
     383         560 :                 step_scrubbed = 0;
     384         560 :                 step_new = 0;
     385       40872 :                 while (barpos < count && timemap[barpos] <= limit) {
     386       39752 :                         if ((timemap[barpos] & TIME_NEW) != 0)
     387       20460 :                                 ++step_new;
     388             :                         else
     389       19292 :                                 ++step_scrubbed;
     390       39752 :                         ++barpos;
     391             :                 }
     392             : 
     393         560 :                 if (step_new + step_scrubbed > barmax)
     394          13 :                         barmax = step_new + step_scrubbed;
     395             : 
     396         560 :                 bar_scrubbed[i] = step_scrubbed;
     397         560 :                 bar_new[i] = step_new;
     398             :         }
     399             : 
     400           8 :         printf("\n\n");
     401             : 
     402             :         /* print the graph */
     403         128 :         for (y = 0; y < GRAPH_ROW; ++y) {
     404         120 :                 if (y == 0)
     405           8 :                         printf("%3u%%|", barmax * 100 / count);
     406         112 :                 else if (y == GRAPH_ROW - 1)
     407           8 :                         printf("  0%%|");
     408         104 :                 else if (y == GRAPH_ROW / 2)
     409           8 :                         printf("%3u%%|", barmax * 50 / count);
     410             :                 else
     411          96 :                         printf("    |");
     412        8520 :                 for (x = 0; x < GRAPH_COLUMN; ++x) {
     413        8400 :                         unsigned pivot_upper = barmax * (GRAPH_ROW - y) / GRAPH_ROW;
     414        8400 :                         unsigned pivot_lower = barmax * (GRAPH_ROW - 1 - y) / GRAPH_ROW;
     415        8400 :                         unsigned both = bar_scrubbed[x] + bar_new[x];
     416        8400 :                         unsigned scrubbed = bar_scrubbed[x];
     417             : 
     418        8400 :                         if (both > pivot_upper) {
     419         170 :                                 if (scrubbed > pivot_lower)
     420          74 :                                         printf("*");
     421             :                                 else
     422          96 :                                         printf("o");
     423        8230 :                         } else if (both > pivot_lower) {
     424          24 :                                 if (scrubbed == both)
     425           9 :                                         printf("*");
     426             :                                 else
     427          15 :                                         printf("o");
     428             :                         } else {
     429        8206 :                                 if (y == GRAPH_ROW - 1)
     430         536 :                                         printf("_");
     431             :                                 else
     432        7670 :                                         printf(" ");
     433             :                         }
     434             :                 }
     435         120 :                 printf("\n");
     436             :         }
     437           8 :         printf("   %3u                    days ago of the last scrub/sync               %3u\n", dayoldest, daynewest);
     438             : 
     439           8 :         printf("\n");
     440             : 
     441           8 :         printf("The oldest block was scrubbed %u days ago, the median %u, the newest %u.\n", dayoldest, daymedian, daynewest);
     442             : 
     443           8 :         printf("\n");
     444             : 
     445           8 :         if (unsynced_blocks) {
     446           0 :                 printf("WARNING! The array is NOT fully synced.\n");
     447           0 :                 printf("You have a sync in progress at %u%%.\n", (blockmax - unsynced_blocks) * 100 / blockmax);
     448             :         } else {
     449           8 :                 printf("No sync is in progress.\n");
     450             :         }
     451             : 
     452           8 :         if (unscrubbed_blocks) {
     453           6 :                 printf("The %u%% of the array is not scrubbed.\n", (unscrubbed_blocks * 100 + blockmax - 1) / blockmax);
     454             :         } else {
     455           2 :                 printf("The full array was scrubbed at least one time.\n");
     456             :         }
     457             : 
     458           8 :         if (rehash) {
     459           1 :                 printf("You have a rehash in progress at %u%%.\n", (count - rehash) * 100 / count);
     460             :         } else {
     461           7 :                 if (state->besthash != state->hash) {
     462           0 :                         printf("No rehash is in progress, but for optimal performance one is recommended.\n");
     463             :                 } else {
     464           7 :                         printf("No rehash is in progress or needed.\n");
     465             :                 }
     466             :         }
     467             : 
     468           8 :         if (bad) {
     469             :                 block_off_t bad_print;
     470             : 
     471           2 :                 printf("DANGER! In the array there are %u errors!\n\n", bad);
     472             : 
     473           2 :                 printf("They are from block %u to %u, specifically at blocks:", bad_first, bad_last);
     474             : 
     475             :                 /* print some of the errors */
     476           2 :                 bad_print = 0;
     477         723 :                 for (i = 0; i < blockmax; ++i) {
     478         723 :                         snapraid_info info = info_get(&state->infoarr, i);
     479             : 
     480             :                         /* skip unused blocks */
     481         723 :                         if (info == 0)
     482           0 :                                 continue;
     483             : 
     484         723 :                         if (info_get_bad(info)) {
     485         202 :                                 printf(" %u", i);
     486         202 :                                 ++bad_print;
     487             :                         }
     488             : 
     489         723 :                         if (bad_print > 100) {
     490           2 :                                 printf(" and %u more...", bad - bad_print);
     491           2 :                                 break;
     492             :                         }
     493             :                 }
     494             : 
     495           2 :                 printf("\n\n");
     496             : 
     497           2 :                 printf("To fix them use the command 'snapraid -e fix'.\n");
     498           2 :                 printf("The errors will disappear from the 'status' at the next 'scrub' command.\n");
     499             :         } else {
     500           6 :                 printf("No error detected.\n");
     501             :         }
     502             : 
     503             :         /* free the temp vector */
     504           8 :         free(timemap);
     505             : 
     506           8 :         return 0;
     507             : }
     508             : 

Generated by: LCOV version 1.10