]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/Pages.cc
Do not create shared memory pools if caching is disabled.
[thirdparty/squid.git] / src / ipc / mem / Pages.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 54 Interprocess Communication
5 *
6 */
7
8 #include "config.h"
9 #include "base/TextException.h"
10 #include "base/RunnersRegistry.h"
11 #include "ipc/mem/PagePool.h"
12 #include "ipc/mem/Pages.h"
13 #include "structs.h"
14 #include "SwapDir.h"
15
16 // Uses a single PagePool instance, for now.
17 // Eventually, we may have pools dedicated to memory caching, disk I/O, etc.
18
19 // TODO: make pool id more unique so it does not conflict with other Squids?
20 static const char *PagePoolId = "squid-page-pool";
21 static Ipc::Mem::PagePool *ThePagePool = 0;
22
23 // TODO: make configurable to avoid waste when mem-cached objects are small/big
24 size_t
25 Ipc::Mem::PageSize() {
26 return 32*1024;
27 }
28
29 void
30 Ipc::Mem::Init()
31 {
32 Must(!ThePagePool);
33 const size_t capacity = Limit() / PageSize();
34 ThePagePool = new PagePool(PagePoolId, capacity, PageSize());
35 }
36
37 void
38 Ipc::Mem::Attach()
39 {
40 Must(!ThePagePool);
41 // TODO: make pool id more unique so it does not conflict with other Squid instances?
42 ThePagePool = new PagePool(PagePoolId);
43 }
44
45 bool
46 Ipc::Mem::GetPage(PageId &page)
47 {
48 return ThePagePool ? ThePagePool->get(page) : false;
49 }
50
51 void
52 Ipc::Mem::PutPage(PageId &page)
53 {
54 Must(ThePagePool);
55 ThePagePool->put(page);
56 }
57
58 void *
59 Ipc::Mem::PagePointer(const PageId &page)
60 {
61 Must(ThePagePool);
62 return ThePagePool->pagePointer(page);
63 }
64
65 size_t
66 Ipc::Mem::Limit()
67 {
68 // TODO: adjust cache_mem description to say that in SMP mode,
69 // in-transit objects are not allocated using cache_mem. Eventually,
70 // they should not use cache_mem even if shared memory is not used:
71 // in-transit objects have nothing to do with caching.
72 return Config.memMaxSize;
73 }
74
75 // TODO: Implement size_t Ipc::Mem::Level()
76
77
78 /// initializes shared memory pages
79 class SharedMemPagesRr: public RegisteredRunner
80 {
81 public:
82 /* RegisteredRunner API */
83 virtual void run(const RunnerRegistry &);
84 // TODO: cleanup in destructor
85 };
86
87 RunnerRegistrationEntry(rrAfterConfig, SharedMemPagesRr);
88
89
90 void SharedMemPagesRr::run(const RunnerRegistry &)
91 {
92 // XXX: restore if (!UsingSmp()) return;
93
94 // When cache_dirs start using shared memory pages, they would
95 // need to communicate their needs to us somehow.
96 if (!Ipc::Mem::Limit())
97 return;
98
99 if (IamMasterProcess())
100 Ipc::Mem::Init();
101 else
102 Ipc::Mem::Attach();
103 }