LCOV - code coverage report
Current view: top level - cmdline - status.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 240 249 96.4 %
Date: 2015-05-12 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 time, time_t now)
      31             : {
      32          24 :         return (now - time) / (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          14 : int state_status(struct snapraid_state* state)
      47             : {
      48             :         block_off_t blockmax;
      49             :         block_off_t i;
      50             :         snapraid_info* infomap;
      51             :         time_t now;
      52             :         block_off_t bad;
      53             :         block_off_t bad_first;
      54             :         block_off_t bad_last;
      55             :         block_off_t rehash;
      56             :         block_off_t count;
      57             :         unsigned l;
      58             :         unsigned dayoldest, daymedian, daynewest;
      59             :         unsigned bar[GRAPH_COLUMN];
      60             :         unsigned barpos;
      61             :         unsigned barmax;
      62             :         time_t oldest, newest, median;
      63             :         unsigned x, y;
      64             :         tommy_node* node_disk;
      65             :         unsigned file_count;
      66             :         unsigned file_fragmented;
      67             :         unsigned extra_fragment;
      68             :         uint64_t file_size;
      69             :         uint64_t file_block_count;
      70             :         uint64_t file_block_free;
      71             :         block_off_t parity_block_free;
      72             :         unsigned unsynced_blocks;
      73             :         uint64_t all_wasted;
      74             :         int free_not_zero;
      75             : 
      76             :         /* get the present time */
      77          14 :         now = time(0);
      78             : 
      79             :         /* keep track if at least a free info is available */
      80          14 :         free_not_zero = 0;
      81             : 
      82          14 :         blockmax = parity_allocated_size(state);
      83             : 
      84          14 :         log_tag("summary:block_size:%u\n", state->block_size);
      85          14 :         log_tag("summary:parity_block_count:%u\n", blockmax);
      86             : 
      87             :         /* get the minimum parity free space */
      88          14 :         parity_block_free = state->parity[0].free_blocks;
      89          98 :         for (l = 0; l < state->level; ++l) {
      90          84 :                 log_tag("summary:parity_block_total:%s:%u\n", lev_config_name(l), state->parity[l].total_blocks);
      91          84 :                 log_tag("summary:parity_block_free:%s:%u\n", lev_config_name(l), state->parity[l].free_blocks);
      92          84 :                 if (state->parity[l].free_blocks < parity_block_free)
      93           4 :                         parity_block_free = state->parity[l].free_blocks;
      94          84 :                 if (state->parity[l].free_blocks != 0)
      95          58 :                         free_not_zero = 1;
      96             :         }
      97          14 :         log_tag("summary:parity_block_free_min:%u\n", parity_block_free);
      98             : 
      99          14 :         printf("SnapRAID status report:\n");
     100          14 :         printf("\n");
     101          14 :         printf("   Files Fragmented Excess  Wasted  Used    Free  Use Name\n");
     102          14 :         printf("            Files  Fragments  GB      GB      GB\n");
     103             : 
     104             :         /* count fragments */
     105          14 :         file_count = 0;
     106          14 :         file_size = 0;
     107          14 :         file_block_count = 0;
     108          14 :         file_block_free = 0;
     109          14 :         file_fragmented = 0;
     110          14 :         extra_fragment = 0;
     111          14 :         all_wasted = 0;
     112          98 :         for (node_disk = state->disklist; node_disk != 0; node_disk = node_disk->next) {
     113          84 :                 struct snapraid_disk* disk = node_disk->data;
     114             :                 tommy_node* node;
     115             :                 block_off_t i;
     116          84 :                 unsigned disk_file_count = 0;
     117          84 :                 unsigned disk_file_fragmented = 0;
     118          84 :                 unsigned disk_extra_fragment = 0;
     119          84 :                 block_off_t disk_block_count = 0;
     120          84 :                 uint64_t disk_file_size = 0;
     121          84 :                 block_off_t disk_block_latest_used = 0;
     122             :                 block_off_t disk_block_max_by_space;
     123             :                 block_off_t disk_block_max_by_parity;
     124             :                 block_off_t disk_block_max;
     125             :                 int64_t wasted;
     126             : 
     127             :                 /* for each file in the disk */
     128          84 :                 node = disk->filelist;
     129       95610 :                 while (node) {
     130             :                         struct snapraid_file* file;
     131             : 
     132       95442 :                         file = node->data;
     133       95442 :                         node = node->next; /* next node */
     134             : 
     135             :                         /* check fragmentation */
     136       95442 :                         if (file->blockmax != 0) {
     137             :                                 block_off_t prev_pos;
     138             :                                 int fragmented;
     139             : 
     140       95312 :                                 fragmented = 0;
     141       95312 :                                 prev_pos = file->blockvec[0].parity_pos;
     142      235007 :                                 for (i = 1; i < file->blockmax; ++i) {
     143      139695 :                                         block_off_t parity_pos = file->blockvec[i].parity_pos;
     144      139695 :                                         if (prev_pos + 1 != parity_pos) {
     145         698 :                                                 fragmented = 1;
     146         698 :                                                 ++extra_fragment;
     147         698 :                                                 ++disk_extra_fragment;
     148             :                                         }
     149      139695 :                                         prev_pos = parity_pos;
     150             :                                 }
     151             : 
     152             :                                 /* keep track of latest block used */
     153       95312 :                                 if (file->blockvec[file->blockmax - 1].parity_pos > disk_block_latest_used) {
     154       92455 :                                         disk_block_latest_used = file->blockvec[file->blockmax - 1].parity_pos;
     155             :                                 }
     156             : 
     157       95312 :                                 if (fragmented) {
     158         582 :                                         ++file_fragmented;
     159         582 :                                         ++disk_file_fragmented;
     160             :                                 }
     161             : 
     162       95312 :                                 disk_block_count += file->blockmax;
     163             :                         }
     164             : 
     165             :                         /* count files */
     166       95442 :                         ++file_count;
     167       95442 :                         ++disk_file_count;
     168       95442 :                         file_size += file->size;
     169       95442 :                         file_block_count += file->blockmax;
     170       95442 :                         disk_file_size += file->size;
     171             :                 }
     172             : 
     173          84 :                 if (disk->free_blocks != 0)
     174          48 :                         free_not_zero = 1;
     175             : 
     176             :                 /* get the free block info */
     177          84 :                 disk_block_max_by_space = disk_block_count + disk->free_blocks;
     178          84 :                 disk_block_max_by_parity = blockmax + parity_block_free;
     179             : 
     180             :                 /* the maximum usable space in a disk is limited by the smallest */
     181             :                 /* of the disk size and the parity size */
     182             :                 /* the wasted space is the space that we have to leave */
     183             :                 /* free on the data disk, when the parity is filled up */
     184          84 :                 if (disk_block_max_by_space < disk_block_max_by_parity) {
     185          50 :                         disk_block_max = disk_block_max_by_space;
     186             :                 } else {
     187          34 :                         disk_block_max = disk_block_max_by_parity;
     188             :                 }
     189             : 
     190             :                 /* wasted space is the difference of the two maximum size */
     191             :                 /* if negative, it's extra space available in parity */
     192          84 :                 wasted = (int64_t)disk_block_max_by_space - (int64_t)disk_block_max_by_parity;
     193          84 :                 wasted *= state->block_size;
     194             : 
     195          84 :                 if (wasted > 0)
     196          24 :                         all_wasted += wasted;
     197          84 :                 file_block_free += disk_block_max - disk_block_count;
     198             : 
     199          84 :                 printf("%8u", disk_file_count);
     200          84 :                 printf("%8u", disk_file_fragmented);
     201          84 :                 printf("%8u", disk_extra_fragment);
     202          84 :                 if (wasted < -100LL * GIGA) {
     203          30 :                         printf("       -");
     204             :                 } else {
     205          54 :                         printf("%8.1f", (double)wasted / GIGA);
     206             :                 }
     207          84 :                 printf("%8" PRIu64, disk_file_size / GIGA);
     208             : 
     209          84 :                 if (disk_block_max == 0 && disk_block_count == 0) {
     210             :                         /* if the disk is empty and we don't have the free space info */
     211          36 :                         printf("       -");
     212          36 :                         printf("   - ");
     213             :                 } else {
     214          48 :                         printf("%8" PRIu64, (disk_block_max - disk_block_count) * (uint64_t)state->block_size / GIGA);
     215          48 :                         printf(" %3u%%", perc(disk_block_count, disk_block_max));
     216             :                 }
     217          84 :                 printf(" %s\n", disk->name);
     218             : 
     219          84 :                 log_tag("summary:disk_file_count:%s:%u\n", disk->name, disk_file_count);
     220          84 :                 log_tag("summary:disk_block_count:%s:%u\n", disk->name, disk_block_count);
     221          84 :                 log_tag("summary:disk_fragmented_file_count:%s:%u\n", disk->name, disk_file_fragmented);
     222          84 :                 log_tag("summary:disk_excess_fragment_count:%s:%u\n", disk->name, disk_extra_fragment);
     223          84 :                 log_tag("summary:disk_file_size:%s:%" PRIu64 "\n", disk->name, disk_file_size);
     224          84 :                 log_tag("summary:disk_block_allocated:%s:%u\n", disk->name, disk_block_latest_used + 1);
     225          84 :                 log_tag("summary:disk_block_total:%s:%u\n", disk->name, disk->total_blocks);
     226          84 :                 log_tag("summary:disk_block_free:%s:%u\n", disk->name, disk->free_blocks);
     227          84 :                 log_tag("summary:disk_block_max_by_space:%s:%u\n", disk->name, disk_block_max_by_space);
     228          84 :                 log_tag("summary:disk_block_max_by_parity:%s:%u\n", disk->name, disk_block_max_by_parity);
     229          84 :                 log_tag("summary:disk_block_max:%s:%u\n", disk->name, disk_block_max);
     230          84 :                 log_tag("summary:disk_space_wasted:%s:%" PRId64 "\n", disk->name, wasted);
     231             :         }
     232             : 
     233             :         /* totals */
     234          14 :         printf(" --------------------------------------------------------------------------\n");
     235          14 :         printf("%8u", file_count);
     236          14 :         printf("%8u", file_fragmented);
     237          14 :         printf("%8u", extra_fragment);
     238          14 :         printf("%8.1f", (double)all_wasted / GIGA);
     239          14 :         printf("%8" PRIu64, file_size / GIGA);
     240          14 :         printf("%8" PRIu64, file_block_free * state->block_size / GIGA);
     241          14 :         printf(" %3u%%", perc(file_block_count, file_block_count + file_block_free));
     242          14 :         printf("\n");
     243             : 
     244             :         /* warn about invalid data free info */
     245          14 :         if (!free_not_zero)
     246           1 :                 printf("\nWARNING! Free space info will be valid after the first sync.\n");
     247             : 
     248          14 :         log_tag("summary:file_count:%u\n", file_count);
     249          14 :         log_tag("summary:file_block_count:%" PRIu64 "\n", file_block_count);
     250          14 :         log_tag("summary:fragmented_file_count:%u\n", file_fragmented);
     251          14 :         log_tag("summary:excess_fragment_count:%u\n", extra_fragment);
     252          14 :         log_tag("summary:file_size:%" PRIu64 "\n", file_size);
     253          14 :         log_tag("summary:parity_size:%" PRIu64 "\n", blockmax * (uint64_t)state->block_size);
     254          14 :         log_tag("summary:parity_size_max:%" PRIu64 "\n", (blockmax + parity_block_free) * (uint64_t)state->block_size);
     255          14 :         log_tag("summary:hash:%s\n", hash_config_name(state->hash));
     256          14 :         log_tag("summary:prev_hash:%s\n", hash_config_name(state->prevhash));
     257          14 :         log_tag("summary:best_hash:%s\n", hash_config_name(state->besthash));
     258          14 :         log_flush();
     259             : 
     260             :         /* copy the info a temp vector, and count bad/rehash/unsynced blocks */
     261          14 :         infomap = malloc_nofail(blockmax * sizeof(snapraid_info));
     262          14 :         bad = 0;
     263          14 :         bad_first = 0;
     264          14 :         bad_last = 0;
     265          14 :         count = 0;
     266          14 :         rehash = 0;
     267          14 :         unsynced_blocks = 0;
     268          14 :         log_tag("block_count:%u\n", blockmax);
     269       39620 :         for (i = 0; i < blockmax; ++i) {
     270             :                 int one_invalid;
     271             :                 int one_valid;
     272             : 
     273       39606 :                 snapraid_info info = info_get(&state->infoarr, i);
     274             : 
     275             :                 /* for each disk */
     276       39606 :                 one_invalid = 0;
     277       39606 :                 one_valid = 0;
     278      277242 :                 for (node_disk = state->disklist; node_disk != 0; node_disk = node_disk->next) {
     279      237636 :                         struct snapraid_disk* disk = node_disk->data;
     280      237636 :                         struct snapraid_block* block = disk_block_get(disk, i);
     281             : 
     282      237636 :                         if (block_has_file(block))
     283      235007 :                                 one_valid = 1;
     284      237636 :                         if (block_has_invalid_parity(block))
     285           0 :                                 one_invalid = 1;
     286             :                 }
     287             : 
     288             :                 /* if both valid and invalid, we need to update */
     289       39606 :                 if (one_invalid && one_valid) {
     290           0 :                         ++unsynced_blocks;
     291             :                 }
     292             : 
     293             :                 /* skip unused blocks */
     294       39606 :                 if (info != 0) {
     295       39606 :                         if (info_get_bad(info)) {
     296        1803 :                                 if (bad == 0)
     297           2 :                                         bad_first = i;
     298        1803 :                                 bad_last = i;
     299        1803 :                                 ++bad;
     300             :                         }
     301             : 
     302       39606 :                         if (info_get_rehash(info))
     303        4599 :                                 ++rehash;
     304             : 
     305       39606 :                         infomap[count++] = info;
     306             :                 }
     307             : 
     308       39606 :                 if (state->opt.gui) {
     309           0 :                         if (info != 0)
     310           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" : "");
     311             :                         else
     312           0 :                                 log_tag("block_noinfo:%u:%s:%s\n", i, one_valid ? "used" : "", one_invalid ? "unsynced" : "");
     313             :                 }
     314             :         }
     315             : 
     316          14 :         log_tag("summary:has_unsynced:%u\n", unsynced_blocks);
     317          14 :         log_tag("summary:has_rehash:%u\n", rehash);
     318          14 :         log_tag("summary:has_bad:%u:%u:%u\n", bad, bad_first, bad_last);
     319          14 :         log_flush();
     320             : 
     321          14 :         if (!count) {
     322           6 :                 log_fatal("The array is empty.\n");
     323           6 :                 free(infomap);
     324           6 :                 return 0;
     325             :         }
     326             : 
     327             :         /* sort the info to get the time info */
     328           8 :         qsort(infomap, count, sizeof(snapraid_info), info_time_compare);
     329             : 
     330             :         /* output the info map */
     331           8 :         i = 0;
     332           8 :         log_tag("info_count:%u\n", count);
     333          37 :         while (i < count) {
     334          21 :                 unsigned j = i + 1;
     335       39627 :                 while (j < count && info_get_time(infomap[i]) == info_get_time(infomap[j]))
     336       39585 :                         ++j;
     337          21 :                 log_tag("info_time:%" PRIu64 ":%u\n", (uint64_t)info_get_time(infomap[i]), j - i);
     338          21 :                 i = j;
     339             :         }
     340             : 
     341           8 :         oldest = info_get_time(infomap[0]);
     342           8 :         median = info_get_time(infomap[count / 2]);
     343           8 :         newest = info_get_time(infomap[count - 1]);
     344           8 :         dayoldest = day_ago(oldest, now);
     345           8 :         daymedian = day_ago(median, now);
     346           8 :         daynewest = day_ago(newest, now);
     347             : 
     348             :         /* compute graph limits */
     349           8 :         barpos = 0;
     350           8 :         barmax = 0;
     351         568 :         for (i = 0; i < GRAPH_COLUMN; ++i) {
     352             :                 time_t limit;
     353             :                 unsigned step;
     354             : 
     355         560 :                 limit = oldest + (newest - oldest) * (i + 1) / GRAPH_COLUMN;
     356             : 
     357         560 :                 step = 0;
     358       40726 :                 while (barpos < count && info_get_time(infomap[barpos]) <= limit) {
     359       39606 :                         ++barpos;
     360       39606 :                         ++step;
     361             :                 }
     362             : 
     363         560 :                 if (step > barmax)
     364          13 :                         barmax = step;
     365             : 
     366         560 :                 bar[i] = step;
     367             :         }
     368             : 
     369           8 :         printf("\n\n");
     370             : 
     371             :         /* print the graph */
     372         128 :         for (y = 0; y < GRAPH_ROW; ++y) {
     373         120 :                 if (y == 0)
     374           8 :                         printf("%3u%%|", barmax * 100 / count);
     375         112 :                 else if (y == GRAPH_ROW - 1)
     376           8 :                         printf("  0%%|");
     377         104 :                 else if (y == GRAPH_ROW / 2)
     378           8 :                         printf("%3u%%|", barmax * 50 / count);
     379             :                 else
     380          96 :                         printf("    |");
     381        8520 :                 for (x = 0; x < GRAPH_COLUMN; ++x) {
     382        8400 :                         unsigned pivot = barmax * (GRAPH_ROW - y) / GRAPH_ROW;
     383             :                         /* if it's the baseline */
     384        8400 :                         if (y == GRAPH_ROW - 1) {
     385         560 :                                 if (bar[x] >= pivot) {
     386          18 :                                         printf("*");
     387         542 :                                 } else if (bar[x] > 0) {
     388           3 :                                         printf("+");
     389             :                                 } else {
     390         539 :                                         printf("_");
     391             :                                 }
     392             :                         } else {
     393        7840 :                                 unsigned halfpivot = pivot - barmax / GRAPH_ROW / 2;
     394        7840 :                                 if (bar[x] >= pivot) {
     395         154 :                                         printf("*");
     396        7686 :                                 } else if (bar[x] >= halfpivot) {
     397           8 :                                         printf("+");
     398             :                                 } else {
     399        7678 :                                         printf(" ");
     400             :                                 }
     401             :                         }
     402             :                 }
     403         120 :                 printf("\n");
     404             :         }
     405           8 :         printf("   %3u                    days ago of the last scrub                    %3u\n", dayoldest, daynewest);
     406             : 
     407           8 :         printf("\n");
     408             : 
     409           8 :         printf("The oldest block was scrubbed %u days ago, the median %u, the newest %u.\n", dayoldest, daymedian, daynewest);
     410             : 
     411           8 :         printf("\n");
     412             : 
     413           8 :         if (unsynced_blocks) {
     414           0 :                 printf("WARNING! The array is NOT fully synced.\n");
     415           0 :                 printf("You have a sync in progress at %u%%.\n", (blockmax - unsynced_blocks) * 100 / blockmax);
     416             :         } else {
     417           8 :                 printf("No sync is in progress.\n");
     418             :         }
     419             : 
     420           8 :         if (rehash) {
     421           1 :                 printf("You have a rehash in progress at %u%%.\n", (count - rehash) * 100 / count);
     422             :         } else {
     423           7 :                 if (state->besthash != state->hash) {
     424           0 :                         printf("No rehash is in progress, but for optimal performance one is recommended.\n");
     425             :                 } else {
     426           7 :                         printf("No rehash is in progress or needed.\n");
     427             :                 }
     428             :         }
     429             : 
     430           8 :         if (bad) {
     431             :                 block_off_t bad_print;
     432             : 
     433           2 :                 printf("DANGER! In the array there are %u errors!\n\n", bad);
     434             : 
     435           2 :                 printf("They are from block %u to %u, specifically at blocks:", bad_first, bad_last);
     436             : 
     437             :                 /* print some of the errors */
     438           2 :                 bad_print = 0;
     439         610 :                 for (i = 0; i < blockmax; ++i) {
     440         610 :                         snapraid_info info = info_get(&state->infoarr, i);
     441             : 
     442             :                         /* skip unused blocks */
     443         610 :                         if (info == 0)
     444           0 :                                 continue;
     445             : 
     446         610 :                         if (info_get_bad(info)) {
     447         202 :                                 printf(" %u", i);
     448         202 :                                 ++bad_print;
     449             :                         }
     450             : 
     451         610 :                         if (bad_print > 100) {
     452           2 :                                 printf(" and %u more...", bad - bad_print);
     453           2 :                                 break;
     454             :                         }
     455             :                 }
     456             : 
     457           2 :                 printf("\n\n");
     458             : 
     459           2 :                 printf("To fix them use the command 'snapraid -e fix'.\n");
     460           2 :                 printf("The errors will disapper from the 'status' at the next 'scrub' command.\n");
     461             :         } else {
     462           6 :                 printf("No error detected.\n");
     463             :         }
     464             : 
     465             :         /* free the temp vector */
     466           8 :         free(infomap);
     467             : 
     468           8 :         return 0;
     469             : }
     470             : 

Generated by: LCOV version 1.10