]> git.ipfire.org Git - thirdparty/squid.git/blob - src/store_dir.cc
swap space accounting used to assume 1K block size. This patch uses
[thirdparty/squid.git] / src / store_dir.cc
1
2 /*
3 * $Id: store_dir.cc,v 1.118 2001/01/02 01:41:31 wessels Exp $
4 *
5 * DEBUG: section 47 Store Directory Routines
6 * AUTHOR: Duane Wessels
7 *
8 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by the
14 * National Science Foundation. Squid is Copyrighted (C) 1998 by
15 * the Regents of the University of California. Please see the
16 * COPYRIGHT file for full details. Squid incorporates software
17 * developed and/or copyrighted by other sources. Please see the
18 * CREDITS file for full details.
19 *
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.
24 *
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.
29 *
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.
33 *
34 */
35
36 #include "squid.h"
37
38 static int storeDirValidSwapDirSize(int, ssize_t);
39 static STDIRSELECT storeDirSelectSwapDirRoundRobin;
40 static STDIRSELECT storeDirSelectSwapDirLeastLoad;
41
42 /*
43 * This function pointer is set according to 'store_dir_select_algorithm'
44 * in squid.conf.
45 */
46 STDIRSELECT *storeDirSelectSwapDir = storeDirSelectSwapDirLeastLoad;
47
48 void
49 storeDirInit(void)
50 {
51 int i;
52 SwapDir *sd;
53 for (i = 0; i < Config.cacheSwap.n_configured; i++) {
54 sd = &Config.cacheSwap.swapDirs[i];
55 sd->init(sd);
56 }
57 if (0 == strcasecmp(Config.store_dir_select_algorithm, "round-robin")) {
58 storeDirSelectSwapDir = storeDirSelectSwapDirRoundRobin;
59 debug(47, 1) ("Using Round Robin store dir selection\n");
60 } else {
61 storeDirSelectSwapDir = storeDirSelectSwapDirLeastLoad;
62 debug(47, 1) ("Using Least Load store dir selection\n");
63 }
64 }
65
66 void
67 storeCreateSwapDirectories(void)
68 {
69 int i;
70 SwapDir *sd;
71 pid_t pid;
72 int status;
73 for (i = 0; i < Config.cacheSwap.n_configured; i++) {
74 if (fork())
75 continue;
76 sd = &Config.cacheSwap.swapDirs[i];
77 sd->newfs(sd);
78 exit(0);
79 }
80 do {
81 #ifdef _SQUID_NEXT_
82 pid = wait3(&status, WNOHANG, NULL);
83 #else
84 pid = waitpid(-1, &status, 0);
85 #endif
86 } while (pid > 0 || (pid < 0 && errno == EINTR));
87 }
88
89 /*
90 * Determine whether the given directory can handle this object
91 * size
92 *
93 * Note: if the object size is -1, then the only swapdirs that
94 * will return true here are ones that have max_obj_size = -1,
95 * ie any-sized-object swapdirs. This is a good thing.
96 */
97 static int
98 storeDirValidSwapDirSize(int swapdir, ssize_t objsize)
99 {
100 /*
101 * If the swapdir's max_obj_size is -1, then it definitely can
102 */
103 if (Config.cacheSwap.swapDirs[swapdir].max_objsize == -1)
104 return 1;
105 /*
106 * Else, make sure that the max object size is larger than objsize
107 */
108 if (Config.cacheSwap.swapDirs[swapdir].max_objsize > objsize)
109 return 1;
110 else
111 return 0;
112 }
113
114
115 /*
116 * This new selection scheme simply does round-robin on all SwapDirs.
117 * A SwapDir is skipped if it is over the max_size (100%) limit. If
118 * all SwapDir's are above the limit, then the first dirn that we
119 * checked is returned. Note that 'dirn' is guaranteed to advance even
120 * if all SwapDirs are full.
121 *
122 * XXX This function does NOT account for the read_only flag!
123 */
124 static int
125 storeDirSelectSwapDirRoundRobin(const StoreEntry * unused)
126 {
127 static int dirn = 0;
128 int i;
129 SwapDir *sd;
130 /*
131 * yes, the '<=' is intentional. If all dirs are full we want to
132 * make sure 'dirn' advances every time this gets called, otherwise
133 * we get stuck on one dir.
134 */
135 for (i = 0; i <= Config.cacheSwap.n_configured; i++) {
136 if (++dirn >= Config.cacheSwap.n_configured)
137 dirn = 0;
138 sd = &Config.cacheSwap.swapDirs[dirn];
139 if (sd->cur_size > sd->max_size)
140 continue;
141 return dirn;
142 }
143 return dirn;
144 }
145
146 /*
147 * Spread load across all of the store directories
148 *
149 * Note: We should modify this later on to prefer sticking objects
150 * in the *tightest fit* swapdir to conserve space, along with the
151 * actual swapdir usage. But for now, this hack will do while
152 * testing, so you should order your swapdirs in the config file
153 * from smallest maxobjsize to unlimited (-1) maxobjsize.
154 *
155 * We also have to choose nleast == nconf since we need to consider
156 * ALL swapdirs, regardless of state. Again, this is a hack while
157 * we sort out the real usefulness of this algorithm.
158 */
159 static int
160 storeDirSelectSwapDirLeastLoad(const StoreEntry * e)
161 {
162 ssize_t objsize;
163 ssize_t least_size;
164 ssize_t least_objsize;
165 int least_load = 1000;
166 int load;
167 int dirn = -1;
168 int i;
169 SwapDir *SD;
170
171 /* Calculate the object size */
172 objsize = (ssize_t) objectLen(e);
173 if (objsize != -1)
174 objsize += e->mem_obj->swap_hdr_sz;
175 /* Initial defaults */
176 least_size = Config.cacheSwap.swapDirs[0].cur_size;
177 least_objsize = Config.cacheSwap.swapDirs[0].max_objsize;
178 for (i = 0; i < Config.cacheSwap.n_configured; i++) {
179 SD = &Config.cacheSwap.swapDirs[i];
180 SD->flags.selected = 0;
181 if (SD->flags.read_only)
182 continue;
183 /* Valid for object size check */
184 if (!storeDirValidSwapDirSize(i, objsize))
185 continue;
186 load = SD->checkobj(SD, e);
187 if (load < 0)
188 continue;
189 if (SD->cur_size > SD->max_size)
190 continue;
191 if (load > least_load)
192 continue;
193 if ((least_objsize > 0) && (objsize > least_objsize))
194 continue;
195 /* Only use leastsize if the load is equal */
196 if ((load == least_load) && (SD->cur_size > least_size))
197 continue;
198 least_load = load;
199 least_size = SD->cur_size;
200 dirn = i;
201 }
202
203 if (dirn >= 0)
204 Config.cacheSwap.swapDirs[dirn].flags.selected = 1;
205
206 return dirn;
207 }
208
209
210
211 char *
212 storeSwapDir(int dirn)
213 {
214 assert(0 <= dirn && dirn < Config.cacheSwap.n_configured);
215 return Config.cacheSwap.swapDirs[dirn].path;
216 }
217
218 /*
219 * An entry written to the swap log MUST have the following
220 * properties.
221 * 1. It MUST be a public key. It does no good to log
222 * a public ADD, change the key, then log a private
223 * DEL. So we need to log a DEL before we change a
224 * key from public to private.
225 * 2. It MUST have a valid (> -1) swap_filen.
226 */
227 void
228 storeDirSwapLog(const StoreEntry * e, int op)
229 {
230 SwapDir *sd;
231 assert(!EBIT_TEST(e->flags, KEY_PRIVATE));
232 assert(e->swap_filen >= 0);
233 /*
234 * icons and such; don't write them to the swap log
235 */
236 if (EBIT_TEST(e->flags, ENTRY_SPECIAL))
237 return;
238 assert(op > SWAP_LOG_NOP && op < SWAP_LOG_MAX);
239 debug(20, 3) ("storeDirSwapLog: %s %s %d %08X\n",
240 swap_log_op_str[op],
241 storeKeyText(e->hash.key),
242 e->swap_dirn,
243 e->swap_filen);
244 sd = &Config.cacheSwap.swapDirs[e->swap_dirn];
245 sd->log.write(sd, e, op);
246 }
247
248 void
249 storeDirUpdateSwapSize(SwapDir * SD, size_t size, int sign)
250 {
251 int blks = (size + SD->fs.blksize - 1) / SD->fs.blksize;
252 int k = blks * SD->fs.kperblk * sign;
253 SD->cur_size += k;
254 store_swap_size += k;
255 if (sign > 0)
256 n_disk_objects++;
257 else if (sign < 0)
258 n_disk_objects--;
259 }
260
261 void
262 storeDirStats(StoreEntry * sentry)
263 {
264 int i;
265 SwapDir *SD;
266
267 storeAppendPrintf(sentry, "Store Directory Statistics:\n");
268 storeAppendPrintf(sentry, "Store Entries : %d\n",
269 memInUse(MEM_STOREENTRY));
270 storeAppendPrintf(sentry, "Maximum Swap Size : %8d KB\n",
271 Config.Swap.maxSize);
272 storeAppendPrintf(sentry, "Current Store Swap Size: %8d KB\n",
273 store_swap_size);
274 storeAppendPrintf(sentry, "Current Capacity : %d%% used, %d%% free\n",
275 percent((int) store_swap_size, (int) Config.Swap.maxSize),
276 percent((int) (Config.Swap.maxSize - store_swap_size), (int) Config.Swap.maxSize));
277
278 /* Now go through each swapdir, calling its statfs routine */
279 for (i = 0; i < Config.cacheSwap.n_configured; i++) {
280 storeAppendPrintf(sentry, "\n");
281 SD = &(Config.cacheSwap.swapDirs[i]);
282 storeAppendPrintf(sentry, "Store Directory #%d (%s): %s\n", i, SD->type,
283 storeSwapDir(i));
284 storeAppendPrintf(sentry, "FS Block Size %d KB\n",
285 SD->fs.kperblk);
286 SD->statfs(SD, sentry);
287 }
288 }
289
290 void
291 storeDirConfigure(void)
292 {
293 SwapDir *SD;
294 int i;
295 Config.Swap.maxSize = 0;
296 for (i = 0; i < Config.cacheSwap.n_configured; i++) {
297 SD = &Config.cacheSwap.swapDirs[i];
298 Config.Swap.maxSize += SD->max_size;
299 SD->low_size = (int) (((float) SD->max_size *
300 (float) Config.Swap.lowWaterMark) / 100.0);
301 }
302 }
303
304 void
305 storeDirDiskFull(sdirno dirn)
306 {
307 SwapDir *SD = &Config.cacheSwap.swapDirs[dirn];
308 assert(0 <= dirn && dirn < Config.cacheSwap.n_configured);
309 if (SD->cur_size >= SD->max_size)
310 return;
311 SD->max_size = SD->cur_size;
312 debug(20, 1) ("WARNING: Shrinking cache_dir #%d to %d KB\n",
313 dirn, SD->cur_size);
314 }
315
316 void
317 storeDirOpenSwapLogs(void)
318 {
319 int dirn;
320 SwapDir *sd;
321 for (dirn = 0; dirn < Config.cacheSwap.n_configured; dirn++) {
322 sd = &Config.cacheSwap.swapDirs[dirn];
323 sd->log.open(sd);
324 }
325 }
326
327 void
328 storeDirCloseSwapLogs(void)
329 {
330 int dirn;
331 SwapDir *sd;
332 for (dirn = 0; dirn < Config.cacheSwap.n_configured; dirn++) {
333 sd = &Config.cacheSwap.swapDirs[dirn];
334 sd->log.close(sd);
335 }
336 }
337
338 /*
339 * storeDirWriteCleanLogs
340 *
341 * Writes a "clean" swap log file from in-memory metadata.
342 * This is a rewrite of the original function to troll each
343 * StoreDir and write the logs, and flush at the end of
344 * the run. Thanks goes to Eric Stern, since this solution
345 * came out of his COSS code.
346 */
347 #define CLEAN_BUF_SZ 16384
348 int
349 storeDirWriteCleanLogs(int reopen)
350 {
351 const StoreEntry *e = NULL;
352 int n = 0;
353 struct timeval start;
354 double dt;
355 SwapDir *sd;
356 RemovalPolicyWalker **walkers;
357 int dirn;
358 int notdone = 1;
359 if (store_dirs_rebuilding) {
360 debug(20, 1) ("Not currently OK to rewrite swap log.\n");
361 debug(20, 1) ("storeDirWriteCleanLogs: Operation aborted.\n");
362 return 0;
363 }
364 debug(20, 1) ("storeDirWriteCleanLogs: Starting...\n");
365 getCurrentTime();
366 start = current_time;
367 walkers = xcalloc(Config.cacheSwap.n_configured, sizeof *walkers);
368 for (dirn = 0; dirn < Config.cacheSwap.n_configured; dirn++) {
369 sd = &Config.cacheSwap.swapDirs[dirn];
370 if (sd->log.clean.start(sd) < 0) {
371 debug(20, 1) ("log.clean.start() failed for dir #%d\n", sd->index);
372 continue;
373 }
374 }
375 while (notdone) {
376 notdone = 0;
377 for (dirn = 0; dirn < Config.cacheSwap.n_configured; dirn++) {
378 sd = &Config.cacheSwap.swapDirs[dirn];
379 if (NULL == sd->log.clean.write)
380 continue;
381 e = sd->log.clean.nextentry(sd);
382 if (!e)
383 continue;
384 notdone = 1;
385 if (e->swap_filen < 0)
386 continue;
387 if (e->swap_status != SWAPOUT_DONE)
388 continue;
389 if (e->swap_file_sz <= 0)
390 continue;
391 if (EBIT_TEST(e->flags, RELEASE_REQUEST))
392 continue;
393 if (EBIT_TEST(e->flags, KEY_PRIVATE))
394 continue;
395 if (EBIT_TEST(e->flags, ENTRY_SPECIAL))
396 continue;
397 sd->log.clean.write(sd, e);
398 if ((++n & 0xFFFF) == 0) {
399 getCurrentTime();
400 debug(20, 1) (" %7d entries written so far.\n", n);
401 }
402 }
403 }
404 /* Flush */
405 for (dirn = 0; dirn < Config.cacheSwap.n_configured; dirn++) {
406 sd = &Config.cacheSwap.swapDirs[dirn];
407 sd->log.clean.done(sd);
408 }
409 if (reopen)
410 storeDirOpenSwapLogs();
411 getCurrentTime();
412 dt = tvSubDsec(start, current_time);
413 debug(20, 1) (" Finished. Wrote %d entries.\n", n);
414 debug(20, 1) (" Took %3.1f seconds (%6.1f entries/sec).\n",
415 dt, (double) n / (dt > 0.0 ? dt : 1.0));
416 return n;
417 }
418 #undef CLEAN_BUF_SZ
419
420 /*
421 * sync all avaliable fs'es ..
422 */
423 void
424 storeDirSync(void)
425 {
426 int i;
427 SwapDir *SD;
428
429 for (i = 0; i < Config.cacheSwap.n_configured; i++) {
430 SD = &Config.cacheSwap.swapDirs[i];
431 if (SD->sync != NULL)
432 SD->sync(SD);
433 }
434 }
435
436 /*
437 * handle callbacks all avaliable fs'es ..
438 */
439 void
440 storeDirCallback(void)
441 {
442 int i, j;
443 SwapDir *SD;
444 static int ndir = 0;
445 do {
446 j = 0;
447 for (i = 0; i < Config.cacheSwap.n_configured; i++) {
448 if (ndir >= Config.cacheSwap.n_configured)
449 ndir = ndir % Config.cacheSwap.n_configured;
450 SD = &Config.cacheSwap.swapDirs[ndir++];
451 if (NULL == SD->callback)
452 continue;
453 j += SD->callback(SD);
454 }
455 } while (j > 0);
456 ndir++;
457 }
458
459 int
460 storeDirGetBlkSize(const char *path, int *blksize)
461 {
462 #if HAVE_STATVFS
463 struct statvfs sfs;
464 if (statvfs(path, &sfs)) {
465 debug(0, 0) ("%s: %s\n", path, xstrerror());
466 return 1;
467 }
468 #else
469 struct statfs sfs;
470 if (statfs(path, &sfs)) {
471 debug(0, 0) ("%s: %s\n", path, xstrerror());
472 return 1;
473 }
474 #endif
475 *blksize = (int) sfs.f_bsize;
476 return 0;
477 }