]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/mem/Pages.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[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 "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 static int TheLimits[Ipc::Mem::PageId::maxPurpose];
23
24 // TODO: make configurable to avoid waste when mem-cached objects are small/big
25 size_t
26 Ipc::Mem::PageSize()
27 {
28 return 32*1024;
29 }
30
31 bool
32 Ipc::Mem::GetPage(const PageId::Purpose purpose, PageId &page)
33 {
34 return ThePagePool && PagesAvailable(purpose) > 0 ?
35 ThePagePool->get(purpose, page) : false;
36 }
37
38 void
39 Ipc::Mem::PutPage(PageId &page)
40 {
41 Must(ThePagePool);
42 ThePagePool->put(page);
43 }
44
45 char *
46 Ipc::Mem::PagePointer(const PageId &page)
47 {
48 Must(ThePagePool);
49 return ThePagePool->pagePointer(page);
50 }
51
52 size_t
53 Ipc::Mem::PageLimit()
54 {
55 size_t limit = 0;
56 for (int i = 0; i < PageId::maxPurpose; ++i)
57 limit += PageLimit(i);
58 return limit;
59 }
60
61 size_t
62 Ipc::Mem::PageLimit(const int purpose)
63 {
64 Must(0 <= purpose && purpose <= PageId::maxPurpose);
65 return TheLimits[purpose];
66 }
67
68 // note: adjust this if we start recording needs during reconfigure
69 void
70 Ipc::Mem::NotePageNeed(const int purpose, const int count)
71 {
72 Must(0 <= purpose && purpose <= PageId::maxPurpose);
73 Must(count >= 0);
74 TheLimits[purpose] += count;
75 }
76
77 size_t
78 Ipc::Mem::PageLevel()
79 {
80 return ThePagePool ? ThePagePool->level() : 0;
81 }
82
83 size_t
84 Ipc::Mem::PageLevel(const int purpose)
85 {
86 return ThePagePool ? ThePagePool->level(purpose) : 0;
87 }
88
89 /// initializes shared memory pages
90 class SharedMemPagesRr: public Ipc::Mem::RegisteredRunner
91 {
92 public:
93 /* RegisteredRunner API */
94 SharedMemPagesRr(): owner(NULL) {}
95 virtual void run(const RunnerRegistry &);
96 virtual void create(const RunnerRegistry &);
97 virtual void open(const RunnerRegistry &);
98 virtual ~SharedMemPagesRr();
99
100 private:
101 Ipc::Mem::PagePool::Owner *owner;
102 };
103
104 RunnerRegistrationEntry(rrAfterConfig, SharedMemPagesRr);
105
106
107 void
108 SharedMemPagesRr::run(const RunnerRegistry &r)
109 {
110 if (Ipc::Mem::PageLimit() <= 0)
111 return;
112
113 Ipc::Mem::RegisteredRunner::run(r);
114 }
115
116 void
117 SharedMemPagesRr::create(const RunnerRegistry &)
118 {
119 Must(!owner);
120 owner = Ipc::Mem::PagePool::Init(PagePoolId, Ipc::Mem::PageLimit(),
121 Ipc::Mem::PageSize());
122 }
123
124 void
125 SharedMemPagesRr::open(const RunnerRegistry &)
126 {
127 Must(!ThePagePool);
128 ThePagePool = new Ipc::Mem::PagePool(PagePoolId);
129 }
130
131 SharedMemPagesRr::~SharedMemPagesRr()
132 {
133 if (!UsingSmp())
134 return;
135
136 delete ThePagePool;
137 ThePagePool = NULL;
138 delete owner;
139 }