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