Commit | Line | Data |
---|---|---|
f97378c7 | 1 | /*- |
88f72820 KB |
2 | * Copyright (c) 1992, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
f97378c7 | 4 | * |
ad787160 C |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
f97378c7 KB |
32 | */ |
33 | ||
34 | #ifndef lint | |
88f72820 KB |
35 | static char copyright[] = |
36 | "@(#) Copyright (c) 1992, 1993\n\ | |
37 | The Regents of the University of California. All rights reserved.\n"; | |
f97378c7 KB |
38 | #endif /* not lint */ |
39 | ||
40 | #ifndef lint | |
fd88f5c5 | 41 | static char sccsid[] = "@(#)cleanerd.c 8.5 (Berkeley) 6/10/95"; |
f97378c7 KB |
42 | #endif /* not lint */ |
43 | ||
f0f9e35d KB |
44 | #include <sys/param.h> |
45 | #include <sys/mount.h> | |
46 | #include <sys/time.h> | |
a1cd5d4a | 47 | |
f0f9e35d KB |
48 | #include <ufs/ufs/dinode.h> |
49 | #include <ufs/lfs/lfs.h> | |
a1cd5d4a KB |
50 | |
51 | #include <signal.h> | |
f0f9e35d KB |
52 | #include <stdio.h> |
53 | #include <stdlib.h> | |
54 | #include <unistd.h> | |
55 | ||
56 | #include "clean.h" | |
57 | char *special = "cleanerd"; | |
1f5c0335 MS |
58 | int do_small = 0; |
59 | int do_mmap = 0; | |
8cbab328 | 60 | int stat_report = 0; |
1f5c0335 | 61 | struct cleaner_stats { |
8cbab328 MS |
62 | double util_tot; |
63 | double util_sos; | |
1f5c0335 MS |
64 | int blocks_read; |
65 | int blocks_written; | |
66 | int segs_cleaned; | |
67 | int segs_empty; | |
68 | int segs_error; | |
69 | } cleaner_stats; | |
f0f9e35d KB |
70 | |
71 | struct seglist { | |
72 | int sl_id; /* segment number */ | |
73 | int sl_cost; /* cleaning cost */ | |
8cbab328 | 74 | char sl_bytes; /* bytes in segment */ |
f0f9e35d KB |
75 | }; |
76 | ||
77 | struct tossstruct { | |
78 | struct lfs *lfs; | |
79 | int seg; | |
80 | }; | |
81 | ||
8cbab328 MS |
82 | #define CLEAN_BYTES 0x1 |
83 | ||
f0f9e35d | 84 | /* function prototypes for system calls; not sure where they should go */ |
9bd4a030 KB |
85 | int lfs_segwait __P((fsid_t *, struct timeval *)); |
86 | int lfs_segclean __P((fsid_t *, u_long)); | |
87 | int lfs_bmapv __P((fsid_t *, BLOCK_INFO *, int)); | |
88 | int lfs_markv __P((fsid_t *, BLOCK_INFO *, int)); | |
f0f9e35d KB |
89 | |
90 | /* function prototypes */ | |
91 | int bi_tossold __P((const void *, const void *, const void *)); | |
92 | int choose_segments __P((FS_INFO *, struct seglist *, | |
93 | int (*)(FS_INFO *, SEGUSE *))); | |
8cbab328 MS |
94 | void clean_fs __P((FS_INFO *, int (*)(FS_INFO *, SEGUSE *), int, long)); |
95 | int clean_loop __P((FS_INFO *, int, long)); | |
f0f9e35d KB |
96 | int clean_segment __P((FS_INFO *, int)); |
97 | int cost_benefit __P((FS_INFO *, SEGUSE *)); | |
98 | int cost_compare __P((const void *, const void *)); | |
1f5c0335 | 99 | void sig_report __P((int)); |
f0f9e35d KB |
100 | |
101 | /* | |
102 | * Cleaning Cost Functions: | |
103 | * | |
104 | * These return the cost of cleaning a segment. The higher the cost value | |
105 | * the better it is to clean the segment, so empty segments have the highest | |
106 | * cost. (It is probably better to think of this as a priority value | |
107 | * instead). | |
108 | * | |
109 | * This is the cost-benefit policy simulated and described in Rosenblum's | |
110 | * 1991 SOSP paper. | |
111 | */ | |
112 | ||
113 | int | |
114 | cost_benefit(fsp, su) | |
115 | FS_INFO *fsp; /* file system information */ | |
116 | SEGUSE *su; | |
117 | { | |
118 | struct lfs *lfsp; | |
119 | struct timeval t; | |
120 | int age; | |
121 | int live; | |
122 | ||
123 | gettimeofday(&t, NULL); | |
124 | ||
125 | live = su->su_nbytes; | |
a1cd5d4a | 126 | age = t.tv_sec < su->su_lastmod ? 0 : t.tv_sec - su->su_lastmod; |
f0f9e35d KB |
127 | |
128 | lfsp = &fsp->fi_lfs; | |
9bd4a030 | 129 | if (live == 0) |
f0f9e35d KB |
130 | return (t.tv_sec * lblkno(lfsp, seg_size(lfsp))); |
131 | else { | |
132 | /* | |
133 | * from lfsSegUsage.c (Mendel's code). | |
134 | * priority calculation is done using INTEGER arithmetic. | |
135 | * sizes are in BLOCKS (that is why we use lblkno below). | |
136 | * age is in seconds. | |
137 | * | |
9bd4a030 | 138 | * priority = ((seg_size - live) * age) / (seg_size + live) |
f0f9e35d KB |
139 | */ |
140 | #ifdef VERBOSE | |
141 | if (live < 0 || live > seg_size(lfsp)) { | |
142 | err(0, "Bad segusage count: %d", live); | |
143 | live = 0; | |
144 | } | |
145 | #endif | |
146 | return (lblkno(lfsp, seg_size(lfsp) - live) * age) | |
147 | / lblkno(lfsp, seg_size(lfsp) + live); | |
148 | } | |
149 | } | |
150 | ||
f97378c7 | 151 | int |
f0f9e35d KB |
152 | main(argc, argv) |
153 | int argc; | |
154 | char *argv[]; | |
155 | { | |
d59d68b6 | 156 | FS_INFO *fsp; |
f0f9e35d KB |
157 | struct statfs *lstatfsp; /* file system stats */ |
158 | struct timeval timeout; /* sleep timeout */ | |
159 | fsid_t fsid; | |
8cbab328 MS |
160 | long clean_opts; /* cleaning options */ |
161 | int i, nodaemon, segs_per_clean; | |
1f5c0335 | 162 | int opt, cmd_err; |
d59d68b6 CD |
163 | char *fs_name; /* name of filesystem to clean */ |
164 | extern int optind; | |
1f5c0335 | 165 | |
3013fbb8 | 166 | cmd_err = nodaemon = 0; |
8cbab328 MS |
167 | clean_opts = 0; |
168 | segs_per_clean = 1; | |
169 | while ((opt = getopt(argc, argv, "bdmn:r:s")) != EOF) { | |
1f5c0335 | 170 | switch (opt) { |
8cbab328 MS |
171 | case 'b': /* |
172 | * Use live bytes to determine | |
173 | * how many segs to clean. | |
174 | */ | |
175 | clean_opts |= CLEAN_BYTES; | |
176 | break; | |
177 | case 'd': /* Debug mode. */ | |
178 | nodaemon = 1; | |
1f5c0335 | 179 | break; |
8cbab328 | 180 | case 'm': /* Use mmap instead of read/write */ |
1f5c0335 MS |
181 | do_mmap = 1; |
182 | break; | |
8cbab328 MS |
183 | case 'n': /* How many segs to clean at once */ |
184 | segs_per_clean = atoi(optarg); | |
185 | break; | |
186 | case 'r': /* Report every stat_report segments */ | |
187 | stat_report = atoi(optarg); | |
188 | break; | |
189 | case 's': /* small writes */ | |
190 | do_small = 1; | |
3013fbb8 | 191 | break; |
1f5c0335 MS |
192 | default: |
193 | ++cmd_err; | |
194 | } | |
195 | } | |
d59d68b6 CD |
196 | argc -= optind; |
197 | argv += optind; | |
198 | if (cmd_err || (argc != 1)) | |
3013fbb8 | 199 | err(1, "usage: lfs_cleanerd [-smd] fs_name"); |
d59d68b6 CD |
200 | |
201 | fs_name = argv[0]; | |
1f5c0335 | 202 | |
9bd4a030 KB |
203 | signal(SIGINT, sig_report); |
204 | signal(SIGUSR1, sig_report); | |
205 | signal(SIGUSR2, sig_report); | |
e66a0756 | 206 | if (fs_getmntinfo(&lstatfsp, fs_name, "lfs") == 0) { |
d59d68b6 CD |
207 | /* didn't find the filesystem */ |
208 | err(1, "lfs_cleanerd: filesystem %s isn't an LFS!", fs_name); | |
209 | } | |
f0f9e35d | 210 | |
3013fbb8 CD |
211 | if (!nodaemon) /* should we become a daemon, chdir to / & close fd's */ |
212 | if (daemon(0, 0) == -1) | |
213 | err(1, "lfs_cleanerd: couldn't become a daemon!"); | |
214 | ||
f0f9e35d KB |
215 | timeout.tv_sec = 5*60; /* five minutes */ |
216 | timeout.tv_usec = 0; | |
217 | fsid.val[0] = 0; | |
218 | fsid.val[1] = 0; | |
219 | ||
d59d68b6 CD |
220 | for (fsp = get_fs_info(lstatfsp, do_mmap); ; |
221 | reread_fs_info(fsp, do_mmap)) { | |
28cc73c5 | 222 | /* |
d59d68b6 CD |
223 | * clean the filesystem, and, if it needed cleaning |
224 | * (i.e. it returned nonzero) try it again | |
28cc73c5 KB |
225 | * to make sure that some nasty process hasn't just |
226 | * filled the disk system up. | |
227 | */ | |
8cbab328 | 228 | if (clean_loop(fsp, segs_per_clean, clean_opts)) |
28cc73c5 | 229 | continue; |
f0f9e35d KB |
230 | |
231 | #ifdef VERBOSE | |
232 | (void)printf("Cleaner going to sleep.\n"); | |
233 | #endif | |
9bd4a030 | 234 | if (lfs_segwait(&fsid, &timeout) < 0) |
f0f9e35d KB |
235 | err(0, "lfs_segwait: returned error\n"); |
236 | #ifdef VERBOSE | |
237 | (void)printf("Cleaner waking up.\n"); | |
238 | #endif | |
239 | } | |
240 | } | |
241 | ||
242 | /* return the number of segments cleaned */ | |
243 | int | |
8cbab328 | 244 | clean_loop(fsp, nsegs, options) |
f0f9e35d | 245 | FS_INFO *fsp; /* file system information */ |
8cbab328 MS |
246 | int nsegs; |
247 | long options; | |
f0f9e35d KB |
248 | { |
249 | double loadavg[MAXLOADS]; | |
250 | time_t now; | |
251 | u_long max_free_segs; | |
830c6a3d | 252 | u_long db_per_seg; |
f0f9e35d KB |
253 | |
254 | /* | |
255 | * Compute the maximum possible number of free segments, given the | |
256 | * number of free blocks. | |
257 | */ | |
830c6a3d MS |
258 | db_per_seg = fsbtodb(&fsp->fi_lfs, fsp->fi_lfs.lfs_ssize); |
259 | max_free_segs = fsp->fi_lfs.lfs_bfree / db_per_seg; | |
f0f9e35d KB |
260 | |
261 | /* | |
262 | * We will clean if there are not enough free blocks or total clean | |
263 | * space is less than BUSY_LIM % of possible clean space. | |
264 | */ | |
265 | now = time((time_t *)NULL); | |
830c6a3d MS |
266 | #ifdef VERBOSE |
267 | printf("db_er_seg = %d max_free_segs = %d, bfree = %d avail = %d ", | |
268 | db_per_seg, max_free_segs, fsp->fi_lfs.lfs_bfree, | |
269 | fsp->fi_lfs.lfs_avail); | |
270 | printf("clean = %d\n", fsp->fi_cip->clean); | |
271 | #endif | |
272 | if ((fsp->fi_lfs.lfs_bfree - fsp->fi_lfs.lfs_avail > db_per_seg && | |
273 | fsp->fi_lfs.lfs_avail < db_per_seg) || | |
274 | (fsp->fi_cip->clean < max_free_segs && | |
5c39f310 | 275 | (fsp->fi_cip->clean <= MIN_SEGS(&fsp->fi_lfs) || |
830c6a3d | 276 | fsp->fi_cip->clean < max_free_segs * BUSY_LIM))) { |
8f8caf36 KB |
277 | printf("Cleaner Running at %s (%d of %d segments available)\n", |
278 | ctime(&now), fsp->fi_cip->clean, max_free_segs); | |
8cbab328 | 279 | clean_fs(fsp, cost_benefit, nsegs, options); |
f0f9e35d KB |
280 | return (1); |
281 | } else { | |
282 | /* | |
283 | * We will also clean if the system is reasonably idle and | |
284 | * the total clean space is less then IDLE_LIM % of possible | |
285 | * clean space. | |
286 | */ | |
287 | if (getloadavg(loadavg, MAXLOADS) == -1) { | |
288 | perror("getloadavg: failed\n"); | |
289 | return (-1); | |
290 | } | |
291 | if (loadavg[ONE_MIN] == 0.0 && loadavg[FIVE_MIN] && | |
292 | fsp->fi_cip->clean < max_free_segs * IDLE_LIM) { | |
8cbab328 | 293 | clean_fs(fsp, cost_benefit, nsegs, options); |
f0f9e35d KB |
294 | printf("Cleaner Running at %s (system idle)\n", |
295 | ctime(&now)); | |
296 | return (1); | |
297 | } | |
298 | } | |
299 | printf("Cleaner Not Running at %s\n", ctime(&now)); | |
300 | return (0); | |
301 | } | |
302 | ||
303 | ||
304 | void | |
8cbab328 | 305 | clean_fs(fsp, cost_func, nsegs, options) |
f0f9e35d KB |
306 | FS_INFO *fsp; /* file system information */ |
307 | int (*cost_func) __P((FS_INFO *, SEGUSE *)); | |
8cbab328 MS |
308 | int nsegs; |
309 | long options; | |
f0f9e35d KB |
310 | { |
311 | struct seglist *segs, *sp; | |
8cbab328 | 312 | int to_clean, cleaned_bytes; |
f0f9e35d KB |
313 | int i; |
314 | ||
9bd4a030 KB |
315 | if ((segs = |
316 | malloc(fsp->fi_lfs.lfs_nseg * sizeof(struct seglist))) == NULL) { | |
f0f9e35d KB |
317 | err(0, "malloc failed"); |
318 | return; | |
319 | } | |
320 | i = choose_segments(fsp, segs, cost_func); | |
321 | #ifdef VERBOSE | |
8e5862f2 | 322 | printf("clean_fs: found %d segments to clean in file system %s\n", |
f0f9e35d KB |
323 | i, fsp->fi_statfsp->f_mntonname); |
324 | fflush(stdout); | |
325 | #endif | |
8cbab328 MS |
326 | if (i) { |
327 | /* Check which cleaning algorithm to use. */ | |
328 | if (options & CLEAN_BYTES) { | |
329 | cleaned_bytes = 0; | |
330 | to_clean = nsegs << | |
331 | (fsp->fi_lfs.lfs_segshift + fsp->fi_lfs.lfs_bshift); | |
332 | for (sp = segs; i && cleaned_bytes < to_clean; | |
333 | i--, ++sp) { | |
334 | if (clean_segment(fsp, sp->sl_id) < 0) | |
335 | perror("clean_segment failed"); | |
336 | else if (lfs_segclean(&fsp->fi_statfsp->f_fsid, | |
337 | sp->sl_id) < 0) | |
338 | perror("lfs_segclean failed"); | |
339 | printf("Cleaned segment %d (%d bytes)\n", | |
340 | sp->sl_id, sp->sl_bytes); | |
341 | cleaned_bytes += sp->sl_bytes; | |
342 | } | |
343 | } else | |
344 | for (i = MIN(i, nsegs), sp = segs; i-- ; ++sp) { | |
345 | if (clean_segment(fsp, sp->sl_id) < 0) | |
346 | perror("clean_segment failed"); | |
347 | else if (lfs_segclean(&fsp->fi_statfsp->f_fsid, | |
348 | sp->sl_id) < 0) | |
349 | perror("lfs_segclean failed"); | |
350 | printf("Completed cleaning segment %d\n", sp->sl_id); | |
351 | } | |
352 | } | |
f0f9e35d KB |
353 | free(segs); |
354 | } | |
355 | ||
356 | /* | |
357 | * Segment with the highest priority get sorted to the beginning of the | |
358 | * list. This sort assumes that empty segments always have a higher | |
359 | * cost/benefit than any utilized segment. | |
360 | */ | |
361 | int | |
362 | cost_compare(a, b) | |
363 | const void *a; | |
364 | const void *b; | |
365 | { | |
366 | return (((struct seglist *)b)->sl_cost - | |
367 | ((struct seglist *)a)->sl_cost); | |
368 | } | |
369 | ||
370 | ||
371 | /* | |
372 | * Returns the number of segments to be cleaned with the elements of seglist | |
373 | * filled in. | |
374 | */ | |
375 | int | |
376 | choose_segments(fsp, seglist, cost_func) | |
377 | FS_INFO *fsp; | |
378 | struct seglist *seglist; | |
379 | int (*cost_func) __P((FS_INFO *, SEGUSE *)); | |
380 | { | |
381 | struct lfs *lfsp; | |
382 | struct seglist *sp; | |
383 | SEGUSE *sup; | |
384 | int i, nsegs; | |
385 | ||
386 | lfsp = &fsp->fi_lfs; | |
387 | ||
388 | #ifdef VERBOSE | |
9bd4a030 | 389 | (void)printf("Entering choose_segments\n"); |
f0f9e35d KB |
390 | #endif |
391 | dump_super(lfsp); | |
392 | dump_cleaner_info(fsp->fi_cip); | |
393 | ||
394 | for (sp = seglist, i = 0; i < lfsp->lfs_nseg; ++i) { | |
395 | sup = SEGUSE_ENTRY(lfsp, fsp->fi_segusep, i); | |
396 | PRINT_SEGUSE(sup, i); | |
397 | if (!(sup->su_flags & SEGUSE_DIRTY) || | |
398 | sup->su_flags & SEGUSE_ACTIVE) | |
399 | continue; | |
400 | #ifdef VERBOSE | |
9bd4a030 | 401 | (void)printf("\tchoosing segment %d\n", i); |
f0f9e35d KB |
402 | #endif |
403 | sp->sl_cost = (*cost_func)(fsp, sup); | |
404 | sp->sl_id = i; | |
8cbab328 | 405 | sp->sl_bytes = sup->su_nbytes; |
f0f9e35d KB |
406 | ++sp; |
407 | } | |
408 | nsegs = sp - seglist; | |
409 | qsort(seglist, nsegs, sizeof(struct seglist), cost_compare); | |
410 | #ifdef VERBOSE | |
411 | (void)printf("Returning %d segments\n", nsegs); | |
412 | #endif | |
413 | return (nsegs); | |
414 | } | |
415 | ||
416 | ||
417 | int | |
418 | clean_segment(fsp, id) | |
419 | FS_INFO *fsp; /* file system information */ | |
420 | int id; /* segment number */ | |
421 | { | |
1f5c0335 | 422 | BLOCK_INFO *block_array, *bp; |
f0f9e35d KB |
423 | SEGUSE *sp; |
424 | struct lfs *lfsp; | |
425 | struct tossstruct t; | |
426 | caddr_t seg_buf; | |
8cbab328 | 427 | double util; |
1f5c0335 | 428 | int num_blocks, maxblocks, clean_blocks; |
f0f9e35d KB |
429 | |
430 | lfsp = &fsp->fi_lfs; | |
431 | sp = SEGUSE_ENTRY(lfsp, fsp->fi_segusep, id); | |
432 | ||
433 | #ifdef VERBOSE | |
9bd4a030 | 434 | (void)printf("cleaning segment %d: contains %lu bytes\n", id, |
f0f9e35d KB |
435 | sp->su_nbytes); |
436 | fflush(stdout); | |
437 | #endif | |
438 | /* XXX could add debugging to verify that segment is really empty */ | |
1f5c0335 MS |
439 | if (sp->su_nbytes == sp->su_nsums * LFS_SUMMARY_SIZE) { |
440 | ++cleaner_stats.segs_empty; | |
f0f9e35d | 441 | return (0); |
1f5c0335 | 442 | } |
f0f9e35d KB |
443 | |
444 | /* map the segment into a buffer */ | |
1f5c0335 | 445 | if (mmap_segment(fsp, id, &seg_buf, do_mmap) < 0) { |
f0f9e35d | 446 | err(0, "mmap_segment failed"); |
1f5c0335 | 447 | ++cleaner_stats.segs_error; |
f0f9e35d KB |
448 | return (-1); |
449 | } | |
450 | /* get a list of blocks that are contained by the segment */ | |
875ef07c | 451 | if (lfs_segmapv(fsp, id, seg_buf, &block_array, &num_blocks) < 0) { |
f0f9e35d | 452 | err(0, "clean_segment: lfs_segmapv failed"); |
1f5c0335 | 453 | ++cleaner_stats.segs_error; |
f0f9e35d KB |
454 | return (-1); |
455 | } | |
1f5c0335 | 456 | cleaner_stats.blocks_read += fsp->fi_lfs.lfs_ssize; |
f0f9e35d KB |
457 | |
458 | #ifdef VERBOSE | |
9bd4a030 KB |
459 | (void)printf("lfs_segmapv returned %d blocks\n", num_blocks); |
460 | fflush(stdout); | |
f0f9e35d KB |
461 | #endif |
462 | ||
463 | /* get the current disk address of blocks contained by the segment */ | |
9bd4a030 | 464 | if (lfs_bmapv(&fsp->fi_statfsp->f_fsid, block_array, num_blocks) < 0) { |
f0f9e35d | 465 | perror("clean_segment: lfs_bmapv failed\n"); |
1f5c0335 | 466 | ++cleaner_stats.segs_error; |
f0f9e35d KB |
467 | return -1; |
468 | } | |
469 | ||
470 | /* Now toss any blocks not in the current segment */ | |
471 | t.lfs = lfsp; | |
472 | t.seg = id; | |
473 | toss(block_array, &num_blocks, sizeof(BLOCK_INFO), bi_tossold, &t); | |
474 | ||
475 | /* Check if last element should be tossed */ | |
476 | if (num_blocks && bi_tossold(&t, block_array + num_blocks - 1, NULL)) | |
477 | --num_blocks; | |
478 | ||
479 | #ifdef VERBOSE | |
480 | { | |
481 | BLOCK_INFO *_bip; | |
f0f9e35d KB |
482 | u_long *lp; |
483 | int i; | |
484 | ||
9bd4a030 KB |
485 | (void)printf("after bmapv still have %d blocks\n", num_blocks); |
486 | fflush(stdout); | |
f0f9e35d KB |
487 | if (num_blocks) |
488 | printf("BLOCK INFOS\n"); | |
489 | for (_bip = block_array, i=0; i < num_blocks; ++_bip, ++i) { | |
490 | PRINT_BINFO(_bip); | |
491 | lp = (u_long *)_bip->bi_bp; | |
492 | } | |
f0f9e35d | 493 | } |
8cbab328 | 494 | |
f0f9e35d | 495 | #endif |
8cbab328 | 496 | ++cleaner_stats.segs_cleaned; |
1f5c0335 | 497 | cleaner_stats.blocks_written += num_blocks; |
8cbab328 MS |
498 | util = ((double)num_blocks / fsp->fi_lfs.lfs_ssize); |
499 | cleaner_stats.util_tot += util; | |
500 | cleaner_stats.util_sos += util * util; | |
501 | ||
1f5c0335 MS |
502 | if (do_small) |
503 | maxblocks = MAXPHYS / fsp->fi_lfs.lfs_bsize - 1; | |
504 | else | |
505 | maxblocks = num_blocks; | |
506 | ||
507 | for (bp = block_array; num_blocks > 0; bp += clean_blocks) { | |
508 | clean_blocks = maxblocks < num_blocks ? maxblocks : num_blocks; | |
9bd4a030 KB |
509 | if (lfs_markv(&fsp->fi_statfsp->f_fsid, |
510 | bp, clean_blocks) < 0) { | |
8e5862f2 | 511 | err(0, "clean_segment: lfs_markv failed"); |
1f5c0335 | 512 | ++cleaner_stats.segs_error; |
f0f9e35d KB |
513 | return (-1); |
514 | } | |
1f5c0335 MS |
515 | num_blocks -= clean_blocks; |
516 | } | |
517 | ||
f0f9e35d | 518 | free(block_array); |
9bd4a030 | 519 | munmap_segment(fsp, seg_buf, do_mmap); |
8cbab328 MS |
520 | if (stat_report && cleaner_stats.segs_cleaned % stat_report == 0) |
521 | sig_report(SIGUSR1); | |
f0f9e35d KB |
522 | return (0); |
523 | } | |
524 | ||
525 | ||
526 | int | |
527 | bi_tossold(client, a, b) | |
528 | const void *client; | |
529 | const void *a; | |
530 | const void *b; | |
531 | { | |
532 | const struct tossstruct *t; | |
533 | ||
534 | t = (struct tossstruct *)client; | |
535 | ||
536 | return (((BLOCK_INFO *)a)->bi_daddr == LFS_UNUSED_DADDR || | |
537 | datosn(t->lfs, ((BLOCK_INFO *)a)->bi_daddr) != t->seg); | |
538 | } | |
1f5c0335 MS |
539 | |
540 | void | |
541 | sig_report(sig) | |
542 | int sig; | |
543 | { | |
8cbab328 MS |
544 | double avg; |
545 | ||
9bd4a030 | 546 | printf("lfs_cleanerd:\t%s%d\n\t\t%s%d\n\t\t%s%d\n\t\t%s%d\n\t\t%s%d\n", |
1f5c0335 MS |
547 | "blocks_read ", cleaner_stats.blocks_read, |
548 | "blocks_written ", cleaner_stats.blocks_written, | |
549 | "segs_cleaned ", cleaner_stats.segs_cleaned, | |
550 | "segs_empty ", cleaner_stats.segs_empty, | |
551 | "seg_error ", cleaner_stats.segs_error); | |
8cbab328 MS |
552 | printf("\t\t%s%5.2f\n\t\t%s%5.2f\n", |
553 | "util_tot ", cleaner_stats.util_tot, | |
554 | "util_sos ", cleaner_stats.util_sos); | |
555 | printf("\t\tavg util: %4.2f std dev: %9.6f\n", | |
556 | avg = cleaner_stats.util_tot / cleaner_stats.segs_cleaned, | |
557 | cleaner_stats.util_sos / cleaner_stats.segs_cleaned - avg * avg); | |
558 | ||
559 | ||
1f5c0335 | 560 | if (sig == SIGUSR2) { |
a1cd5d4a KB |
561 | cleaner_stats.blocks_read = 0; |
562 | cleaner_stats.blocks_written = 0; | |
563 | cleaner_stats.segs_cleaned = 0; | |
564 | cleaner_stats.segs_empty = 0; | |
565 | cleaner_stats.segs_error = 0; | |
8cbab328 MS |
566 | cleaner_stats.util_tot = 0.0; |
567 | cleaner_stats.util_sos = 0.0; | |
1f5c0335 MS |
568 | } |
569 | if (sig == SIGINT) | |
570 | exit(0); | |
571 | } |