3 * $Id: store_dir.cc,v 1.134 2001/08/16 00:16:18 hno Exp $
5 * DEBUG: section 47 Store Directory Routines
6 * AUTHOR: Duane Wessels
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
39 #if HAVE_SYS_STATVFS_H
40 #include <sys/statvfs.h>
43 /* Windows uses sys/vfs.h */
48 static int storeDirValidSwapDirSize(int, ssize_t
);
49 static STDIRSELECT storeDirSelectSwapDirRoundRobin
;
50 static STDIRSELECT storeDirSelectSwapDirLeastLoad
;
53 * This function pointer is set according to 'store_dir_select_algorithm'
56 STDIRSELECT
*storeDirSelectSwapDir
= storeDirSelectSwapDirLeastLoad
;
63 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++) {
64 sd
= &Config
.cacheSwap
.swapDirs
[i
];
67 if (0 == strcasecmp(Config
.store_dir_select_algorithm
, "round-robin")) {
68 storeDirSelectSwapDir
= storeDirSelectSwapDirRoundRobin
;
69 debug(47, 1) ("Using Round Robin store dir selection\n");
71 storeDirSelectSwapDir
= storeDirSelectSwapDirLeastLoad
;
72 debug(47, 1) ("Using Least Load store dir selection\n");
77 storeCreateSwapDirectories(void)
83 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++) {
86 sd
= &Config
.cacheSwap
.swapDirs
[i
];
92 pid
= wait3(&status
, WNOHANG
, NULL
);
94 pid
= waitpid(-1, &status
, 0);
96 } while (pid
> 0 || (pid
< 0 && errno
== EINTR
));
100 * Determine whether the given directory can handle this object
103 * Note: if the object size is -1, then the only swapdirs that
104 * will return true here are ones that have max_obj_size = -1,
105 * ie any-sized-object swapdirs. This is a good thing.
108 storeDirValidSwapDirSize(int swapdir
, ssize_t objsize
)
111 * If the swapdir's max_obj_size is -1, then it definitely can
113 if (Config
.cacheSwap
.swapDirs
[swapdir
].max_objsize
== -1)
117 * If the object size is -1, then if the storedir isn't -1 we
120 if ((objsize
== -1) &&
121 (Config
.cacheSwap
.swapDirs
[swapdir
].max_objsize
!= -1))
125 * Else, make sure that the max object size is larger than objsize
127 if (Config
.cacheSwap
.swapDirs
[swapdir
].max_objsize
> objsize
)
135 * This new selection scheme simply does round-robin on all SwapDirs.
136 * A SwapDir is skipped if it is over the max_size (100%) limit, or
140 storeDirSelectSwapDirRoundRobin(const StoreEntry
* e
)
146 ssize_t objsize
= (ssize_t
) objectLen(e
);
147 for (i
= 0; i
<= Config
.cacheSwap
.n_configured
; i
++) {
148 if (++dirn
>= Config
.cacheSwap
.n_configured
)
150 sd
= &Config
.cacheSwap
.swapDirs
[dirn
];
151 if (sd
->flags
.read_only
)
153 if (sd
->cur_size
> sd
->max_size
)
155 if (!storeDirValidSwapDirSize(i
, objsize
))
157 /* check for error or overload condition */
158 load
= sd
->checkobj(sd
, e
);
159 if (load
< 0 || load
> 1000) {
168 * Spread load across all of the store directories
170 * Note: We should modify this later on to prefer sticking objects
171 * in the *tightest fit* swapdir to conserve space, along with the
172 * actual swapdir usage. But for now, this hack will do while
173 * testing, so you should order your swapdirs in the config file
174 * from smallest maxobjsize to unlimited (-1) maxobjsize.
176 * We also have to choose nleast == nconf since we need to consider
177 * ALL swapdirs, regardless of state. Again, this is a hack while
178 * we sort out the real usefulness of this algorithm.
181 storeDirSelectSwapDirLeastLoad(const StoreEntry
* e
)
184 ssize_t most_free
= 0, cur_free
;
185 ssize_t least_objsize
= -1;
186 int least_load
= INT_MAX
;
192 /* Calculate the object size */
193 objsize
= (ssize_t
) objectLen(e
);
195 objsize
+= e
->mem_obj
->swap_hdr_sz
;
196 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++) {
197 SD
= &Config
.cacheSwap
.swapDirs
[i
];
198 SD
->flags
.selected
= 0;
199 load
= SD
->checkobj(SD
, e
);
200 if (load
< 0 || load
> 1000) {
203 if (!storeDirValidSwapDirSize(i
, objsize
))
205 if (SD
->flags
.read_only
)
207 if (SD
->cur_size
> SD
->max_size
)
209 if (load
> least_load
)
211 cur_free
= SD
->max_size
- SD
->cur_size
;
212 /* If the load is equal, then look in more details */
213 if (load
== least_load
) {
214 /* closest max_objsize fit */
215 if (least_objsize
!= -1)
216 if (SD
->max_objsize
> least_objsize
|| SD
->max_objsize
== -1)
219 if (cur_free
< most_free
)
223 least_objsize
= SD
->max_objsize
;
224 most_free
= cur_free
;
228 Config
.cacheSwap
.swapDirs
[dirn
].flags
.selected
= 1;
235 storeSwapDir(int dirn
)
237 assert(0 <= dirn
&& dirn
< Config
.cacheSwap
.n_configured
);
238 return Config
.cacheSwap
.swapDirs
[dirn
].path
;
242 * An entry written to the swap log MUST have the following
244 * 1. It MUST be a public key. It does no good to log
245 * a public ADD, change the key, then log a private
246 * DEL. So we need to log a DEL before we change a
247 * key from public to private.
248 * 2. It MUST have a valid (> -1) swap_filen.
251 storeDirSwapLog(const StoreEntry
* e
, int op
)
254 assert(!EBIT_TEST(e
->flags
, KEY_PRIVATE
));
255 assert(e
->swap_filen
>= 0);
257 * icons and such; don't write them to the swap log
259 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
))
261 assert(op
> SWAP_LOG_NOP
&& op
< SWAP_LOG_MAX
);
262 debug(20, 3) ("storeDirSwapLog: %s %s %d %08X\n",
264 storeKeyText(e
->hash
.key
),
267 sd
= &Config
.cacheSwap
.swapDirs
[e
->swap_dirn
];
268 sd
->log
.write(sd
, e
, op
);
272 storeDirUpdateSwapSize(SwapDir
* SD
, size_t size
, int sign
)
274 int blks
= (size
+ SD
->fs
.blksize
- 1) / SD
->fs
.blksize
;
275 int k
= (blks
* SD
->fs
.blksize
>> 10) * sign
;
277 store_swap_size
+= k
;
285 storeDirStats(StoreEntry
* sentry
)
290 storeAppendPrintf(sentry
, "Store Directory Statistics:\n");
291 storeAppendPrintf(sentry
, "Store Entries : %d\n",
292 memInUse(MEM_STOREENTRY
));
293 storeAppendPrintf(sentry
, "Maximum Swap Size : %8d KB\n",
294 Config
.Swap
.maxSize
);
295 storeAppendPrintf(sentry
, "Current Store Swap Size: %8d KB\n",
297 storeAppendPrintf(sentry
, "Current Capacity : %d%% used, %d%% free\n",
298 percent((int) store_swap_size
, (int) Config
.Swap
.maxSize
),
299 percent((int) (Config
.Swap
.maxSize
- store_swap_size
), (int) Config
.Swap
.maxSize
));
300 /* FIXME Here we should output memory statistics */
302 /* Now go through each swapdir, calling its statfs routine */
303 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++) {
304 storeAppendPrintf(sentry
, "\n");
305 SD
= &(Config
.cacheSwap
.swapDirs
[i
]);
306 storeAppendPrintf(sentry
, "Store Directory #%d (%s): %s\n", i
, SD
->type
,
308 storeAppendPrintf(sentry
, "FS Block Size %d Bytes\n",
310 SD
->statfs(SD
, sentry
);
312 storeAppendPrintf(sentry
, "Removal policy: %s\n", SD
->repl
->_type
);
314 SD
->repl
->Stats(SD
->repl
, sentry
);
320 storeDirConfigure(void)
324 Config
.Swap
.maxSize
= 0;
325 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++) {
326 SD
= &Config
.cacheSwap
.swapDirs
[i
];
327 Config
.Swap
.maxSize
+= SD
->max_size
;
328 SD
->low_size
= (int) (((float) SD
->max_size
*
329 (float) Config
.Swap
.lowWaterMark
) / 100.0);
334 storeDirDiskFull(sdirno dirn
)
336 SwapDir
*SD
= &Config
.cacheSwap
.swapDirs
[dirn
];
337 assert(0 <= dirn
&& dirn
< Config
.cacheSwap
.n_configured
);
338 if (SD
->cur_size
>= SD
->max_size
)
340 SD
->max_size
= SD
->cur_size
;
341 debug(20, 1) ("WARNING: Shrinking cache_dir #%d to %d KB\n",
346 storeDirOpenSwapLogs(void)
350 for (dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; dirn
++) {
351 sd
= &Config
.cacheSwap
.swapDirs
[dirn
];
358 storeDirCloseSwapLogs(void)
362 for (dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; dirn
++) {
363 sd
= &Config
.cacheSwap
.swapDirs
[dirn
];
370 * storeDirWriteCleanLogs
372 * Writes a "clean" swap log file from in-memory metadata.
373 * This is a rewrite of the original function to troll each
374 * StoreDir and write the logs, and flush at the end of
375 * the run. Thanks goes to Eric Stern, since this solution
376 * came out of his COSS code.
378 #define CLEAN_BUF_SZ 16384
380 storeDirWriteCleanLogs(int reopen
)
382 const StoreEntry
*e
= NULL
;
384 struct timeval start
;
389 if (store_dirs_rebuilding
) {
390 debug(20, 1) ("Not currently OK to rewrite swap log.\n");
391 debug(20, 1) ("storeDirWriteCleanLogs: Operation aborted.\n");
394 debug(20, 1) ("storeDirWriteCleanLogs: Starting...\n");
396 start
= current_time
;
397 for (dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; dirn
++) {
398 sd
= &Config
.cacheSwap
.swapDirs
[dirn
];
399 if (sd
->log
.clean
.start(sd
) < 0) {
400 debug(20, 1) ("log.clean.start() failed for dir #%d\n", sd
->index
);
406 for (dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; dirn
++) {
407 sd
= &Config
.cacheSwap
.swapDirs
[dirn
];
408 if (NULL
== sd
->log
.clean
.write
)
410 e
= sd
->log
.clean
.nextentry(sd
);
414 if (e
->swap_filen
< 0)
416 if (e
->swap_status
!= SWAPOUT_DONE
)
418 if (e
->swap_file_sz
<= 0)
420 if (EBIT_TEST(e
->flags
, RELEASE_REQUEST
))
422 if (EBIT_TEST(e
->flags
, KEY_PRIVATE
))
424 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
))
426 sd
->log
.clean
.write(sd
, e
);
427 if ((++n
& 0xFFFF) == 0) {
429 debug(20, 1) (" %7d entries written so far.\n", n
);
434 for (dirn
= 0; dirn
< Config
.cacheSwap
.n_configured
; dirn
++) {
435 sd
= &Config
.cacheSwap
.swapDirs
[dirn
];
436 sd
->log
.clean
.done(sd
);
439 storeDirOpenSwapLogs();
441 dt
= tvSubDsec(start
, current_time
);
442 debug(20, 1) (" Finished. Wrote %d entries.\n", n
);
443 debug(20, 1) (" Took %3.1f seconds (%6.1f entries/sec).\n",
444 dt
, (double) n
/ (dt
> 0.0 ? dt
: 1.0));
450 * sync all avaliable fs'es ..
458 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++) {
459 SD
= &Config
.cacheSwap
.swapDirs
[i
];
460 if (SD
->sync
!= NULL
)
466 * handle callbacks all avaliable fs'es ..
469 storeDirCallback(void)
476 for (i
= 0; i
< Config
.cacheSwap
.n_configured
; i
++) {
477 if (ndir
>= Config
.cacheSwap
.n_configured
)
478 ndir
= ndir
% Config
.cacheSwap
.n_configured
;
479 SD
= &Config
.cacheSwap
.swapDirs
[ndir
++];
480 if (NULL
== SD
->callback
)
482 j
+= SD
->callback(SD
);
489 storeDirGetBlkSize(const char *path
, int *blksize
)
493 if (statvfs(path
, &sfs
)) {
494 debug(50, 1) ("%s: %s\n", path
, xstrerror());
498 *blksize
= (int) sfs
.f_frsize
;
501 if (statfs(path
, &sfs
)) {
502 debug(50, 1) ("%s: %s\n", path
, xstrerror());
506 *blksize
= (int) sfs
.f_bsize
;
509 * Sanity check; make sure we have a meaningful value.
516 #define fsbtoblk(num, fsbs, bs) \
517 (((fsbs) != 0 && (fsbs) < (bs)) ? \
518 (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
520 storeDirGetUFSStats(const char *path
, int *totl_kb
, int *free_kb
, int *totl_in
, int *free_in
)
524 if (statvfs(path
, &sfs
)) {
525 debug(50, 1) ("%s: %s\n", path
, xstrerror());
528 *totl_kb
= (int) fsbtoblk(sfs
.f_blocks
, sfs
.f_frsize
, 1024);
529 *free_kb
= (int) fsbtoblk(sfs
.f_bfree
, sfs
.f_frsize
, 1024);
530 *totl_in
= (int) sfs
.f_files
;
531 *free_in
= (int) sfs
.f_ffree
;
534 if (statfs(path
, &sfs
)) {
535 debug(50, 1) ("%s: %s\n", path
, xstrerror());
538 *totl_kb
= (int) fsbtoblk(sfs
.f_blocks
, sfs
.f_bsize
, 1024);
539 *free_kb
= (int) fsbtoblk(sfs
.f_bfree
, sfs
.f_bsize
, 1024);
540 *totl_in
= (int) sfs
.f_files
;
541 *free_in
= (int) sfs
.f_ffree
;