]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ipc/mem/Pages.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / ipc / mem / Pages.cc
CommitLineData
3e0ddf16 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3e0ddf16 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
3e0ddf16
AR
7 */
8
bbc27441
AJ
9/* DEBUG: section 54 Interprocess Communication */
10
f7f3304a 11#include "squid.h"
a4555399 12#include "base/RunnersRegistry.h"
602d9612 13#include "base/TextException.h"
56f8aa50 14#include "ipc/mem/PagePool.h"
3e0ddf16 15#include "ipc/mem/Pages.h"
56f8aa50 16#include "SwapDir.h"
5bed43d6 17#include "tools.h"
3e0ddf16 18
56f8aa50 19// Uses a single PagePool instance, for now.
3e0ddf16 20// Eventually, we may have pools dedicated to memory caching, disk I/O, etc.
56f8aa50
DK
21
22// TODO: make pool id more unique so it does not conflict with other Squids?
a4555399 23static const char *PagePoolId = "squid-page-pool";
56f8aa50 24static Ipc::Mem::PagePool *ThePagePool = 0;
ea2cdeb6 25static int TheLimits[Ipc::Mem::PageId::maxPurpose];
56f8aa50 26
72261456 27// TODO: make configurable to avoid waste when mem-cached objects are small/big
d1ad4017 28size_t
9199139f
AR
29Ipc::Mem::PageSize()
30{
72261456 31 return 32*1024;
56f8aa50
DK
32}
33
56f8aa50 34bool
551f8a18 35Ipc::Mem::GetPage(const PageId::Purpose purpose, PageId &page)
56f8aa50 36{
551f8a18 37 return ThePagePool && PagesAvailable(purpose) > 0 ?
9199139f 38 ThePagePool->get(purpose, page) : false;
56f8aa50
DK
39}
40
41void
42Ipc::Mem::PutPage(PageId &page)
43{
44 Must(ThePagePool);
45 ThePagePool->put(page);
46}
47
8ed94021 48char *
56f8aa50
DK
49Ipc::Mem::PagePointer(const PageId &page)
50{
51 Must(ThePagePool);
52 return ThePagePool->pagePointer(page);
53}
72261456
AR
54
55size_t
e0bdae60
DK
56Ipc::Mem::PageLimit()
57{
551f8a18
DK
58 size_t limit = 0;
59 for (int i = 0; i < PageId::maxPurpose; ++i)
60 limit += PageLimit(i);
61 return limit;
e0bdae60
DK
62}
63
64size_t
551f8a18 65Ipc::Mem::PageLimit(const int purpose)
72261456 66{
ea2cdeb6
DK
67 Must(0 <= purpose && purpose <= PageId::maxPurpose);
68 return TheLimits[purpose];
69}
70
71// note: adjust this if we start recording needs during reconfigure
72void
73Ipc::Mem::NotePageNeed(const int purpose, const int count)
74{
75 Must(0 <= purpose && purpose <= PageId::maxPurpose);
76 Must(count >= 0);
77 TheLimits[purpose] += count;
e0bdae60
DK
78}
79
80size_t
81Ipc::Mem::PageLevel()
82{
551f8a18 83 return ThePagePool ? ThePagePool->level() : 0;
72261456
AR
84}
85
b940ff87 86size_t
551f8a18 87Ipc::Mem::PageLevel(const int purpose)
b940ff87 88{
551f8a18 89 return ThePagePool ? ThePagePool->level(purpose) : 0;
b940ff87 90}
a4555399
AR
91
92/// initializes shared memory pages
4404f1c5 93class SharedMemPagesRr: public Ipc::Mem::RegisteredRunner
a4555399
AR
94{
95public:
96 /* RegisteredRunner API */
68353d5a 97 SharedMemPagesRr(): owner(NULL) {}
21b7990f
AR
98 virtual void useConfig();
99 virtual void create();
100 virtual void open();
c011f9bc 101 virtual ~SharedMemPagesRr();
68353d5a
DK
102
103private:
104 Ipc::Mem::PagePool::Owner *owner;
a4555399
AR
105};
106
21b7990f 107RunnerRegistrationEntry(SharedMemPagesRr);
a4555399 108
4404f1c5 109void
21b7990f 110SharedMemPagesRr::useConfig()
a4555399 111{
ea2cdeb6 112 if (Ipc::Mem::PageLimit() <= 0)
3f2ac0f7 113 return;
3f2ac0f7 114
21b7990f 115 Ipc::Mem::RegisteredRunner::useConfig();
4404f1c5 116}
68353d5a 117
4404f1c5 118void
21b7990f 119SharedMemPagesRr::create()
4404f1c5
DK
120{
121 Must(!owner);
122 owner = Ipc::Mem::PagePool::Init(PagePoolId, Ipc::Mem::PageLimit(),
123 Ipc::Mem::PageSize());
124}
125
126void
21b7990f 127SharedMemPagesRr::open()
4404f1c5 128{
68353d5a
DK
129 Must(!ThePagePool);
130 ThePagePool = new Ipc::Mem::PagePool(PagePoolId);
a4555399 131}
c011f9bc
DK
132
133SharedMemPagesRr::~SharedMemPagesRr()
134{
60be8b2d
AR
135 if (!UsingSmp())
136 return;
137
c011f9bc 138 delete ThePagePool;
282ee641 139 ThePagePool = NULL;
68353d5a 140 delete owner;
c011f9bc 141}
f53969cc 142