Pages.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 /* DEBUG: section 54 Interprocess Communication */
10 
11 #include "squid.h"
12 #include "base/RunnersRegistry.h"
13 #include "base/TextException.h"
14 #include "ipc/mem/PagePool.h"
15 #include "ipc/mem/Pages.h"
16 #include "tools.h"
17 
18 // Uses a single PagePool instance, for now.
19 // Eventually, we may have pools dedicated to memory caching, disk I/O, etc.
20 
21 // TODO: make pool id more unique so it does not conflict with other Squids?
22 static const char *PagePoolId = "squid-page-pool";
23 static Ipc::Mem::PagePool *ThePagePool = nullptr;
25 
26 // TODO: make configurable to avoid waste when mem-cached objects are small/big
27 size_t
29 {
30  return 32*1024;
31 }
32 
33 bool
35 {
36  return ThePagePool && PagesAvailable(purpose) > 0 ?
37  ThePagePool->get(purpose, page) : false;
38 }
39 
40 void
42 {
44  ThePagePool->put(page);
45 }
46 
47 char *
49 {
51  return ThePagePool->pagePointer(page);
52 }
53 
54 size_t
56 {
57  size_t limit = 0;
58  for (int i = 0; i <= PageId::maxPurpose; ++i)
59  limit += PageLimit(i);
60  return limit;
61 }
62 
63 size_t
64 Ipc::Mem::PageLimit(const int purpose)
65 {
66  Must(0 <= purpose && purpose <= PageId::maxPurpose);
67  return TheLimits[purpose];
68 }
69 
70 // note: adjust this if we start recording needs during reconfigure
71 void
72 Ipc::Mem::NotePageNeed(const int purpose, const int count)
73 {
74  Must(0 <= purpose && purpose <= PageId::maxPurpose);
75  Must(count >= 0);
76  TheLimits[purpose] += count;
77 }
78 
79 size_t
81 {
82  return ThePagePool ? ThePagePool->level() : 0;
83 }
84 
85 size_t
86 Ipc::Mem::PageLevel(const int purpose)
87 {
88  return ThePagePool ? ThePagePool->level(purpose) : 0;
89 }
90 
93 {
94 public:
95  /* RegisteredRunner API */
96  SharedMemPagesRr(): owner(nullptr) {}
97  void useConfig() override;
98  void create() override;
99  void open() override;
100  ~SharedMemPagesRr() override;
101 
102 private:
104 };
105 
107 
108 void
110 {
111  if (Ipc::Mem::PageLimit() <= 0)
112  return;
113 
115 }
116 
117 void
119 {
120  Must(!owner);
125 }
126 
127 void
129 {
130  Must(!ThePagePool);
132 }
133 
135 {
136  delete ThePagePool;
137  ThePagePool = nullptr;
138  delete owner;
139 }
140 
static const char * PagePoolId
Definition: Pages.cc:22
static Ipc::Mem::PagePool * ThePagePool
Definition: Pages.cc:23
void useConfig() override
Definition: Pages.cc:109
void open() override
Definition: Pages.cc:128
Shared memory page identifier, address, or handler.
Definition: Page.h:23
static Owner * Init(const char *const shmId, const Ipc::Mem::PoolId stackId, const unsigned int capacity, const size_t pageSize)
Definition: PagePool.cc:19
DefineRunnerRegistrator(SharedMemPagesRr)
void create() override
called when the runner should create a new memory segment
Definition: Pages.cc:118
static PoolId IdForMultipurposePool()
multipurpose PagePool of shared memory pages
Definition: PageStack.h:169
void NotePageNeed(const int purpose, const int count)
claim the need for a number of pages for a given purpose
Definition: Pages.cc:72
initializes shared memory pages
Definition: Pages.cc:92
SharedMemPagesRr()
Definition: Pages.cc:96
size_t PageLevel()
approximate total number of shared memory pages used now
Definition: Pages.cc:80
~SharedMemPagesRr() override
Definition: Pages.cc:134
void PutPage(PageId &page)
makes identified page available as a free page to future GetPage() callers
Definition: Pages.cc:41
void useConfig() override
Definition: Segment.cc:377
static int TheLimits[Ipc::Mem::PageId::maxPurpose+1]
Definition: Pages.cc:24
char * PagePointer(const PageId &page)
converts page handler into a temporary writeable shared memory pointer
Definition: Pages.cc:48
void put(PageId &page)
makes identified page available as a free page to future get() callers
Definition: PagePool.cc:58
size_t PageLimit()
the total number of shared memory pages that can be in use at any time
Definition: Pages.cc:55
bool GetPage(const PageId::Purpose purpose, PageId &page)
sets page ID and returns true unless no free pages are found
Definition: Pages.cc:34
Ipc::Mem::PagePool::Owner * owner
Definition: Pages.cc:103
#define Must(condition)
Definition: TextException.h:75
char * pagePointer(const PageId &page)
converts page handler into a temporary writeable shared memory pointer
Definition: PagePool.cc:70
bool get(const PageId::Purpose purpose, PageId &page)
sets page ID and returns true unless no free pages are found
Definition: PagePool.cc:46
size_t PagesAvailable()
approximate total number of shared memory pages we can allocate now
Definition: Pages.h:47
size_t PageSize()
returns page size in bytes; all pages are assumed to be the same size
Definition: Pages.cc:28
size_t level() const
approximate number of shared memory pages used now
Definition: PagePool.h:39

 

Introduction

Documentation

Support

Miscellaneous