]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/Pages.cc
Enable source-formatting tools to collapse multiple whitelines in the source to one.
[thirdparty/squid.git] / src / ipc / mem / Pages.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 54 Interprocess Communication
5 *
6 */
7
8 #include "squid.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 "protos.h"
14 #include "structs.h"
15 #include "SwapDir.h"
16
17 // Uses a single PagePool instance, for now.
18 // Eventually, we may have pools dedicated to memory caching, disk I/O, etc.
19
20 // TODO: make pool id more unique so it does not conflict with other Squids?
21 static const char *PagePoolId = "squid-page-pool";
22 static Ipc::Mem::PagePool *ThePagePool = 0;
23 static int TheLimits[Ipc::Mem::PageId::maxPurpose];
24
25 // TODO: make configurable to avoid waste when mem-cached objects are small/big
26 size_t
27 Ipc::Mem::PageSize()
28 {
29 return 32*1024;
30 }
31
32 bool
33 Ipc::Mem::GetPage(const PageId::Purpose purpose, PageId &page)
34 {
35 return ThePagePool && PagesAvailable(purpose) > 0 ?
36 ThePagePool->get(purpose, page) : false;
37 }
38
39 void
40 Ipc::Mem::PutPage(PageId &page)
41 {
42 Must(ThePagePool);
43 ThePagePool->put(page);
44 }
45
46 char *
47 Ipc::Mem::PagePointer(const PageId &page)
48 {
49 Must(ThePagePool);
50 return ThePagePool->pagePointer(page);
51 }
52
53 size_t
54 Ipc::Mem::PageLimit()
55 {
56 size_t limit = 0;
57 for (int i = 0; i < PageId::maxPurpose; ++i)
58 limit += PageLimit(i);
59 return limit;
60 }
61
62 size_t
63 Ipc::Mem::PageLimit(const int purpose)
64 {
65 Must(0 <= purpose && purpose <= PageId::maxPurpose);
66 return TheLimits[purpose];
67 }
68
69 // note: adjust this if we start recording needs during reconfigure
70 void
71 Ipc::Mem::NotePageNeed(const int purpose, const int count)
72 {
73 Must(0 <= purpose && purpose <= PageId::maxPurpose);
74 Must(count >= 0);
75 TheLimits[purpose] += count;
76 }
77
78 size_t
79 Ipc::Mem::PageLevel()
80 {
81 return ThePagePool ? ThePagePool->level() : 0;
82 }
83
84 size_t
85 Ipc::Mem::PageLevel(const int purpose)
86 {
87 return ThePagePool ? ThePagePool->level(purpose) : 0;
88 }
89
90 /// initializes shared memory pages
91 class SharedMemPagesRr: public Ipc::Mem::RegisteredRunner
92 {
93 public:
94 /* RegisteredRunner API */
95 SharedMemPagesRr(): owner(NULL) {}
96 virtual void run(const RunnerRegistry &);
97 virtual void create(const RunnerRegistry &);
98 virtual void open(const RunnerRegistry &);
99 virtual ~SharedMemPagesRr();
100
101 private:
102 Ipc::Mem::PagePool::Owner *owner;
103 };
104
105 RunnerRegistrationEntry(rrAfterConfig, SharedMemPagesRr);
106
107
108 void
109 SharedMemPagesRr::run(const RunnerRegistry &r)
110 {
111 if (Ipc::Mem::PageLimit() <= 0)
112 return;
113
114 Ipc::Mem::RegisteredRunner::run(r);
115 }
116
117 void
118 SharedMemPagesRr::create(const RunnerRegistry &)
119 {
120 Must(!owner);
121 owner = Ipc::Mem::PagePool::Init(PagePoolId, Ipc::Mem::PageLimit(),
122 Ipc::Mem::PageSize());
123 }
124
125 void
126 SharedMemPagesRr::open(const RunnerRegistry &)
127 {
128 Must(!ThePagePool);
129 ThePagePool = new Ipc::Mem::PagePool(PagePoolId);
130 }
131
132 SharedMemPagesRr::~SharedMemPagesRr()
133 {
134 if (!UsingSmp())
135 return;
136
137 delete ThePagePool;
138 ThePagePool = NULL;
139 delete owner;
140 }