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 27 : unsigned day_ago(time_t ref, time_t now)
31 : {
32 : /* in case some dates is in the future */
33 27 : if (now < ref)
34 0 : return 0;
35 :
36 27 : return (now - ref) / (24 * 3600);
37 : }
38 :
39 : #define GRAPH_COLUMN 70
40 : #define GRAPH_ROW 15
41 :
42 : /**
43 : * Bit used to mark unscrubbed time info.
44 : */
45 : #define TIME_NEW 1
46 :
47 15 : int state_status(struct snapraid_state* state)
48 : {
49 : block_off_t blockmax;
50 : block_off_t i;
51 : time_t now;
52 : block_off_t count;
53 : unsigned l;
54 : unsigned dayoldest, daymedian, daynewest;
55 : unsigned bar_scrubbed[GRAPH_COLUMN];
56 : unsigned bar_new[GRAPH_COLUMN];
57 : unsigned barmax;
58 : time_t oldest, newest, median;
59 : unsigned x, y;
60 : tommy_node* node_disk;
61 : unsigned file_count;
62 : unsigned file_fragmented;
63 : unsigned extra_fragment;
64 : unsigned file_zerosubsecond;
65 : uint64_t file_size;
66 : uint64_t file_block_count;
67 : uint64_t file_block_free;
68 : block_off_t parity_block_free;
69 : uint64_t all_wasted;
70 : int free_not_zero;
71 :
72 : /* get the present time */
73 15 : now = time(0);
74 :
75 : /* keep track if at least a free info is available */
76 15 : free_not_zero = 0;
77 :
78 15 : blockmax = parity_allocated_size(state);
79 :
80 15 : log_tag("summary:block_size:%u\n", state->block_size);
81 15 : log_tag("summary:parity_block_count:%u\n", blockmax);
82 :
83 : /* get the minimum parity free space */
84 15 : parity_block_free = state->parity[0].free_blocks;
85 105 : for (l = 0; l < state->level; ++l) {
86 90 : log_tag("summary:parity_block_total:%s:%u\n", lev_config_name(l), state->parity[l].total_blocks);
87 90 : log_tag("summary:parity_block_free:%s:%u\n", lev_config_name(l), state->parity[l].free_blocks);
88 90 : if (state->parity[l].free_blocks < parity_block_free)
89 1 : parity_block_free = state->parity[l].free_blocks;
90 90 : if (state->parity[l].free_blocks != 0)
91 79 : free_not_zero = 1;
92 : }
93 15 : log_tag("summary:parity_block_free_min:%u\n", parity_block_free);
94 :
95 15 : printf("SnapRAID status report:\n");
96 15 : printf("\n");
97 15 : printf(" Files Fragmented Excess Wasted Used Free Use Name\n");
98 15 : printf(" Files Fragments GB GB GB\n");
99 :
100 : /* count fragments */
101 15 : file_count = 0;
102 15 : file_size = 0;
103 15 : file_block_count = 0;
104 15 : file_block_free = 0;
105 15 : file_fragmented = 0;
106 15 : extra_fragment = 0;
107 15 : file_zerosubsecond = 0;
108 15 : all_wasted = 0;
109 105 : for (node_disk = state->disklist; node_disk != 0; node_disk = node_disk->next) {
110 90 : struct snapraid_disk* disk = node_disk->data;
111 : tommy_node* node;
112 : block_off_t j;
113 90 : unsigned disk_file_count = 0;
114 90 : unsigned disk_file_fragmented = 0;
115 90 : unsigned disk_extra_fragment = 0;
116 90 : unsigned disk_file_zerosubsecond = 0;
117 90 : block_off_t disk_block_count = 0;
118 90 : uint64_t disk_file_size = 0;
119 90 : block_off_t disk_block_latest_used = 0;
120 : block_off_t disk_block_max_by_space;
121 : block_off_t disk_block_max_by_parity;
122 : block_off_t disk_block_max;
123 : uint64_t disk_used_bytes;
124 : uint64_t disk_free_bytes;
125 : int64_t wasted;
126 :
127 : /* for each file in the disk */
128 90 : node = disk->filelist;
129 95576 : while (node) {
130 : struct snapraid_file* file;
131 :
132 95486 : file = node->data;
133 95486 : node = node->next; /* next node */
134 :
135 95486 : if (file->mtime_nsec == STAT_NSEC_INVALID
136 95486 : || file->mtime_nsec == 0
137 : ) {
138 0 : ++file_zerosubsecond;
139 0 : ++disk_file_zerosubsecond;
140 0 : if (disk_file_zerosubsecond < 50)
141 0 : log_tag("zerosubsecond:%s:%s: \n", disk->name, file->sub);
142 0 : if (disk_file_zerosubsecond == 50)
143 0 : log_tag("zerosubsecond:%s:%s: (more follow)\n", disk->name, file->sub);
144 : }
145 :
146 : /* check fragmentation */
147 95486 : if (file->blockmax != 0) {
148 : block_off_t prev_pos;
149 : block_off_t last_pos;
150 : int fragmented;
151 :
152 95348 : fragmented = 0;
153 95348 : prev_pos = fs_file2par_get(disk, file, 0);
154 235648 : for (j = 1; j < file->blockmax; ++j) {
155 140300 : block_off_t parity_pos = fs_file2par_get(disk, file, j);
156 140300 : if (prev_pos + 1 != parity_pos) {
157 713 : fragmented = 1;
158 713 : ++extra_fragment;
159 713 : ++disk_extra_fragment;
160 : }
161 140300 : prev_pos = parity_pos;
162 : }
163 :
164 : /* keep track of latest block used */
165 95348 : last_pos = fs_file2par_get(disk, file, file->blockmax - 1);
166 95348 : if (last_pos > disk_block_latest_used) {
167 92458 : disk_block_latest_used = last_pos;
168 : }
169 :
170 95348 : if (fragmented) {
171 593 : ++file_fragmented;
172 593 : ++disk_file_fragmented;
173 : }
174 :
175 95348 : disk_block_count += file->blockmax;
176 : }
177 :
178 : /* count files */
179 95486 : ++file_count;
180 95486 : ++disk_file_count;
181 95486 : file_size += file->size;
182 95486 : file_block_count += file->blockmax;
183 95486 : disk_file_size += file->size;
184 : }
185 :
186 90 : if (disk->free_blocks != 0)
187 53 : free_not_zero = 1;
188 :
189 : /* get the free block info */
190 90 : disk_block_max_by_space = disk_block_count + disk->free_blocks;
191 90 : disk_block_max_by_parity = blockmax + parity_block_free;
192 :
193 : /* the maximum usable space in a disk is limited by the smallest */
194 : /* of the disk size and the parity size */
195 : /* the wasted space is the space that we have to leave */
196 : /* free on the data disk, when the parity is filled up */
197 90 : if (disk_block_max_by_space < disk_block_max_by_parity) {
198 78 : disk_block_max = disk_block_max_by_space;
199 : } else {
200 12 : disk_block_max = disk_block_max_by_parity;
201 : }
202 :
203 : /* wasted space is the difference of the two maximum size */
204 : /* if negative, it's extra space available in parity */
205 90 : wasted = (int64_t)disk_block_max_by_space - (int64_t)disk_block_max_by_parity;
206 90 : wasted *= state->block_size;
207 :
208 90 : if (wasted > 0)
209 6 : all_wasted += wasted;
210 90 : file_block_free += disk_block_max - disk_block_count;
211 :
212 90 : printf("%8u", disk_file_count);
213 90 : printf("%8u", disk_file_fragmented);
214 90 : printf("%8u", disk_extra_fragment);
215 90 : if (wasted < -100LL * GIGA) {
216 78 : printf(" -");
217 : } else {
218 12 : printf("%8.1f", (double)wasted / GIGA);
219 : }
220 90 : printf("%8" PRIu64, disk_file_size / GIGA);
221 :
222 90 : if (disk_block_max == 0 && disk_block_count == 0) {
223 : /* if the disk is empty and we don't have the free space info */
224 37 : printf(" -");
225 37 : printf(" - ");
226 : } else {
227 53 : printf("%8" PRIu64, (disk_block_max - disk_block_count) * (uint64_t)state->block_size / GIGA);
228 53 : printf(" %3u%%", muldiv(disk_block_count, 100, disk_block_max));
229 : }
230 90 : printf(" %s\n", disk->name);
231 :
232 90 : log_tag("summary:disk_file_count:%s:%u\n", disk->name, disk_file_count);
233 90 : log_tag("summary:disk_block_count:%s:%u\n", disk->name, disk_block_count);
234 90 : log_tag("summary:disk_fragmented_file_count:%s:%u\n", disk->name, disk_file_fragmented);
235 90 : log_tag("summary:disk_excess_fragment_count:%s:%u\n", disk->name, disk_extra_fragment);
236 90 : log_tag("summary:disk_zerosubsecond_file_count:%s:%u\n", disk->name, disk_file_zerosubsecond);
237 90 : log_tag("summary:disk_file_size:%s:%" PRIu64 "\n", disk->name, disk_file_size);
238 90 : log_tag("summary:disk_block_allocated:%s:%u\n", disk->name, disk_block_latest_used + 1);
239 90 : log_tag("summary:disk_block_total:%s:%u\n", disk->name, disk->total_blocks);
240 90 : log_tag("summary:disk_block_free:%s:%u\n", disk->name, disk->free_blocks);
241 90 : log_tag("summary:disk_block_max_by_space:%s:%u\n", disk->name, disk_block_max_by_space);
242 90 : log_tag("summary:disk_block_max_by_parity:%s:%u\n", disk->name, disk_block_max_by_parity);
243 90 : log_tag("summary:disk_block_max:%s:%u\n", disk->name, disk_block_max);
244 90 : log_tag("summary:disk_space_wasted:%s:%" PRId64 "\n", disk->name, wasted);
245 :
246 90 : disk_used_bytes = disk_block_count * (uint64_t)state->block_size;
247 90 : disk_free_bytes = (disk_block_max - disk_block_count) * (uint64_t)state->block_size;
248 90 : log_tag("summary:disk_used:%s:%" PRIu64 "\n", disk->name, disk_used_bytes);
249 90 : log_tag("summary:disk_free:%s:%" PRIu64 "\n", disk->name, disk_free_bytes);
250 90 : log_tag("summary:disk_use_percent:%s:%u\n", disk->name, muldiv(disk_block_count, 100, disk_block_max));
251 : }
252 :
253 : /* totals */
254 15 : printf(" --------------------------------------------------------------------------\n");
255 15 : printf("%8u", file_count);
256 15 : printf("%8u", file_fragmented);
257 15 : printf("%8u", extra_fragment);
258 15 : printf("%8.1f", (double)all_wasted / GIGA);
259 15 : printf("%8" PRIu64, file_size / GIGA);
260 15 : printf("%8" PRIu64, file_block_free * state->block_size / GIGA);
261 15 : printf(" %3u%%", muldiv(file_block_count, 100, file_block_count + file_block_free));
262 15 : printf("\n");
263 :
264 : /* warn about invalid data free info */
265 15 : if (!free_not_zero)
266 1 : printf("\nWARNING! Free space info will be valid after the first sync.\n");
267 :
268 15 : log_tag("summary:file_count:%u\n", file_count);
269 15 : log_tag("summary:file_block_count:%" PRIu64 "\n", file_block_count);
270 15 : log_tag("summary:fragmented_file_count:%u\n", file_fragmented);
271 15 : log_tag("summary:excess_fragment_count:%u\n", extra_fragment);
272 15 : log_tag("summary:zerosubsecond_file_count:%u\n", file_zerosubsecond);
273 15 : log_tag("summary:file_size:%" PRIu64 "\n", file_size);
274 15 : log_tag("summary:parity_size:%" PRIu64 "\n", blockmax * (uint64_t)state->block_size);
275 15 : log_tag("summary:parity_size_max:%" PRIu64 "\n", (blockmax + parity_block_free) * (uint64_t)state->block_size);
276 15 : log_tag("summary:hash:%s\n", hash_config_name(state->hash));
277 15 : log_tag("summary:prev_hash:%s\n", hash_config_name(state->prevhash));
278 15 : log_tag("summary:best_hash:%s\n", hash_config_name(state->besthash));
279 15 : log_tag("summary:total_wasted:%" PRIu64 "\n", all_wasted);
280 15 : log_tag("summary:total_used:%" PRIu64 "\n", file_size);
281 15 : log_tag("summary:total_free:%" PRIu64 "\n", file_block_free * state->block_size);
282 15 : log_tag("summary:total_use_percent:%u\n", muldiv(file_block_count, 100, file_block_count + file_block_free));
283 15 : log_flush();
284 :
285 15 : oldest = 0;
286 15 : median = 0;
287 15 : newest = 0;
288 15 : count = 0;
289 34 : for (tommy_node* j = tommy_list_head(&state->bucketlist); j != 0; j = j->next) {
290 19 : struct snapraid_bucket* bucket = j->data;
291 19 : block_off_t bucket_count = bucket->count_scrubbed + bucket->count_justsynced;
292 :
293 19 : if (count == 0)
294 9 : oldest = bucket->time_at;
295 19 : if (count < state->bucketcount / 2)
296 14 : median = bucket->time_at;
297 19 : newest = bucket->time_at;
298 :
299 19 : count += bucket_count;
300 : }
301 :
302 15 : if (!count) {
303 6 : log_fatal(EUSER, "The array is empty.\n");
304 6 : return 0;
305 : }
306 :
307 9 : dayoldest = day_ago(oldest, now);
308 9 : daymedian = day_ago(median, now);
309 9 : daynewest = day_ago(newest, now);
310 :
311 9 : log_tag("summary:scrub_oldest_days:%u\n", dayoldest);
312 9 : log_tag("summary:scrub_median_days:%u\n", daymedian);
313 9 : log_tag("summary:scrub_newest_days:%u\n", daynewest);
314 :
315 : /* compute graph limits */
316 9 : barmax = 0;
317 9 : memset(bar_scrubbed, 0, sizeof(bar_scrubbed));
318 9 : memset(bar_new, 0, sizeof(bar_new));
319 28 : for (tommy_node* j = tommy_list_head(&state->bucketlist); j != 0; j = j->next) {
320 19 : struct snapraid_bucket* bucket = j->data;
321 :
322 19 : unsigned column = muldiv(bucket->time_at - oldest, GRAPH_COLUMN, newest - oldest + 1);
323 :
324 19 : bar_scrubbed[column] += bucket->count_scrubbed;
325 19 : bar_new[column] += bucket->count_justsynced;
326 :
327 19 : if (bar_scrubbed[column] + bar_new[column] > barmax)
328 12 : barmax = bar_scrubbed[column] + bar_new[column];
329 : }
330 :
331 : /* output scrub history as structured data */
332 9 : log_tag("scrub_graph_range:%u:%u\n", GRAPH_COLUMN, barmax);
333 639 : for (i = 0; i < GRAPH_COLUMN; ++i) {
334 630 : unsigned days_ago = dayoldest - (dayoldest - daynewest) * i / (GRAPH_COLUMN - 1);
335 630 : log_tag("scrub_graph_bar:%u:%u:%u:%u\n", i, days_ago, bar_scrubbed[i], bar_new[i]);
336 : }
337 :
338 9 : printf("\n\n");
339 :
340 : /* print the graph */
341 144 : for (y = 0; y < GRAPH_ROW; ++y) {
342 135 : if (y == 0)
343 9 : printf("%3u%%|", barmax * 100 / count);
344 126 : else if (y == GRAPH_ROW - 1)
345 9 : printf(" 0%%|");
346 117 : else if (y == GRAPH_ROW / 2)
347 9 : printf("%3u%%|", barmax * 50 / count);
348 : else
349 108 : printf(" |");
350 9585 : for (x = 0; x < GRAPH_COLUMN; ++x) {
351 9450 : unsigned pivot_upper = barmax * (GRAPH_ROW - y) / GRAPH_ROW;
352 9450 : unsigned pivot_lower = barmax * (GRAPH_ROW - 1 - y) / GRAPH_ROW;
353 9450 : unsigned both = bar_scrubbed[x] + bar_new[x];
354 9450 : unsigned scrubbed = bar_scrubbed[x];
355 :
356 9450 : if (both > pivot_upper) {
357 156 : if (scrubbed > pivot_lower)
358 60 : printf("*");
359 : else
360 96 : printf("o");
361 9294 : } else if (both > pivot_lower) {
362 19 : if (scrubbed == both)
363 6 : printf("*");
364 : else
365 13 : printf("o");
366 : } else {
367 9275 : if (y == GRAPH_ROW - 1)
368 611 : printf("_");
369 : else
370 8664 : printf(" ");
371 : }
372 : }
373 135 : printf("\n");
374 : }
375 9 : printf(" %3u days ago of the last scrub/sync %3u\n", dayoldest, daynewest);
376 :
377 9 : printf("\n");
378 :
379 9 : printf("The oldest block was scrubbed %u days ago, the median %u, the newest %u.\n", dayoldest, daymedian, daynewest);
380 :
381 9 : printf("\n");
382 :
383 9 : if (newest > now) {
384 0 : printf("WARNING! You have scrub dates in the future! The next sync/scrub will truncate them!\n");
385 : }
386 :
387 9 : if (state->unsynced_blocks) {
388 1 : printf("WARNING! The array is NOT fully synced.\n");
389 1 : printf("You have a sync in progress at %u%%.\n", muldiv(blockmax - state->unsynced_blocks, 100, blockmax));
390 : } else {
391 8 : printf("No sync is in progress.\n");
392 : }
393 :
394 9 : if (state->unscrubbed_blocks) {
395 7 : printf("%u%% of the array is not scrubbed.\n", muldiv_upper(state->unscrubbed_blocks, 100, blockmax));
396 : } else {
397 2 : printf("The full array was scrubbed at least one time.\n");
398 : }
399 :
400 9 : if (file_zerosubsecond) {
401 0 : printf("You have %u files with a zero sub-second timestamp.\n", file_zerosubsecond);
402 0 : printf("Run 'snapraid touch' to set their sub-second timestamps to a non-zero value.\n");
403 : } else {
404 9 : printf("No file has a zero sub-second timestamp.\n");
405 : }
406 :
407 9 : if (state->rehash_blocks) {
408 1 : printf("You have a rehash in progress at %u%%.\n", muldiv(count - state->rehash_blocks, 100, count));
409 : } else {
410 8 : if (state->besthash != state->hash) {
411 7 : printf("No rehash is in progress, but for optimal performance one is recommended.\n");
412 : } else {
413 1 : printf("No rehash is in progress or needed.\n");
414 : }
415 : }
416 :
417 9 : if (state->bad_blocks) {
418 2 : printf("DANGER! In the array there are %u errors!\n\n", state->bad_blocks);
419 :
420 : block_off_t bad_range;
421 : block_off_t bad_count;
422 : block_off_t range_start;
423 : block_off_t range_count;
424 :
425 2 : printf("They are at blocks:");
426 :
427 : /* print some of the errors */
428 2 : bad_range = 0;
429 2 : bad_count = 0;
430 2 : range_start = 0;
431 2 : range_count = 0;
432 873 : for (i = 0; i <= blockmax; ++i) { /* one extra iteration to print the final range */
433 873 : snapraid_info info = 0;
434 873 : int is_bad = 0;
435 :
436 873 : if (i < blockmax) {
437 873 : info = info_get(&state->infoarr, i);
438 873 : if (info != 0) /* unused blocks are never bad */
439 873 : is_bad = info_get_bad(info);
440 : }
441 873 : if (is_bad) {
442 : /* create or extend the range */
443 254 : if (!range_count)
444 202 : range_start = i;
445 254 : ++range_count;
446 : } else {
447 : /* break the range */
448 619 : if (range_count) {
449 202 : if (range_count == 1) {
450 160 : printf(" %u", range_start);
451 : } else {
452 42 : printf(" %u-%u", range_start, range_start + range_count - 1);
453 : }
454 202 : bad_count += range_count;
455 202 : ++bad_range;
456 202 : range_count = 0;
457 : }
458 : }
459 :
460 873 : if (bad_range > 100) {
461 2 : printf(" and %u more...", state->bad_blocks - bad_count);
462 2 : break;
463 : }
464 : }
465 :
466 2 : printf("\n");
467 :
468 2 : printf("To fix them use the command 'snapraid -e fix'.\n");
469 2 : printf("The errors will disappear from the 'status' at the next 'scrub' command.\n");
470 : } else {
471 7 : printf("No error detected.\n");
472 : }
473 :
474 9 : if (state->bad_blocks)
475 2 : log_tag("summary:exit:bad\n");
476 7 : else if (state->unsynced_blocks != 0)
477 1 : log_tag("summary:exit:unsynced\n");
478 : else
479 6 : log_tag("summary:exit:ok\n");
480 :
481 9 : return 0;
482 : }
483 :
|