]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/ipc/mem/Pages.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / ipc / mem / Pages.cc
index dad5f8ca03a99350777db8dd48ed273cf1473d9c..57df37d8e5f8a26d2b9ae5e4fee24c7215a93d27 100644 (file)
@@ -1,17 +1,19 @@
 /*
- * $Id$
- *
- * DEBUG: section 54    Interprocess Communication
+ * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
  *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
  */
 
-#include "config.h"
-#include "base/TextException.h"
+/* DEBUG: section 54    Interprocess Communication */
+
+#include "squid.h"
 #include "base/RunnersRegistry.h"
+#include "base/TextException.h"
 #include "ipc/mem/PagePool.h"
 #include "ipc/mem/Pages.h"
-#include "structs.h"
-#include "SwapDir.h"
+#include "tools.h"
 
 // Uses a single PagePool instance, for now.
 // Eventually, we may have pools dedicated to memory caching, disk I/O, etc.
 // TODO: make pool id more unique so it does not conflict with other Squids?
 static const char *PagePoolId = "squid-page-pool";
 static Ipc::Mem::PagePool *ThePagePool = 0;
+static int TheLimits[Ipc::Mem::PageId::maxPurpose];
 
 // TODO: make configurable to avoid waste when mem-cached objects are small/big
 size_t
-Ipc::Mem::PageSize() {
-    return 32*1024;
-}
-
-void
-Ipc::Mem::Init()
+Ipc::Mem::PageSize()
 {
-    Must(!ThePagePool);
-    const size_t capacity = Limit() / PageSize();
-    ThePagePool = new PagePool(PagePoolId, capacity, PageSize());
-}
-
-void
-Ipc::Mem::Attach()
-{
-    Must(!ThePagePool);
-    // TODO: make pool id more unique so it does not conflict with other Squid instances?
-    ThePagePool = new PagePool(PagePoolId);
+    return 32*1024;
 }
 
 bool
-Ipc::Mem::GetPage(PageId &page)
+Ipc::Mem::GetPage(const PageId::Purpose purpose, PageId &page)
 {
-    return ThePagePool ? ThePagePool->get(page) : false;
+    return ThePagePool && PagesAvailable(purpose) > 0 ?
+           ThePagePool->get(purpose, page) : false;
 }
 
 void
@@ -55,7 +44,7 @@ Ipc::Mem::PutPage(PageId &page)
     ThePagePool->put(page);
 }
 
-void *
+char *
 Ipc::Mem::PagePointer(const PageId &page)
 {
     Must(ThePagePool);
@@ -63,36 +52,87 @@ Ipc::Mem::PagePointer(const PageId &page)
 }
 
 size_t
-Ipc::Mem::Limit()
+Ipc::Mem::PageLimit()
+{
+    size_t limit = 0;
+    for (int i = 0; i < PageId::maxPurpose; ++i)
+        limit += PageLimit(i);
+    return limit;
+}
+
+size_t
+Ipc::Mem::PageLimit(const int purpose)
+{
+    Must(0 <= purpose && purpose <= PageId::maxPurpose);
+    return TheLimits[purpose];
+}
+
+// note: adjust this if we start recording needs during reconfigure
+void
+Ipc::Mem::NotePageNeed(const int purpose, const int count)
 {
-    // TODO: adjust cache_mem description to say that in SMP mode,
-    // in-transit objects are not allocated using cache_mem. Eventually,
-    // they should not use cache_mem even if shared memory is not used:
-    // in-transit objects have nothing to do with caching.
-    return Config.memMaxSize;
+    Must(0 <= purpose && purpose <= PageId::maxPurpose);
+    Must(count >= 0);
+    TheLimits[purpose] += count;
 }
 
-// TODO: Implement size_t Ipc::Mem::Level()
+size_t
+Ipc::Mem::PageLevel()
+{
+    return ThePagePool ? ThePagePool->level() : 0;
+}
 
+size_t
+Ipc::Mem::PageLevel(const int purpose)
+{
+    return ThePagePool ? ThePagePool->level(purpose) : 0;
+}
 
 /// initializes shared memory pages
-class SharedMemPagesRr: public RegisteredRunner
+class SharedMemPagesRr: public Ipc::Mem::RegisteredRunner
 {
 public:
     /* RegisteredRunner API */
-    virtual void run(const RunnerRegistry &);
-    // TODO: cleanup in destructor
+    SharedMemPagesRr(): owner(NULL) {}
+    virtual void useConfig();
+    virtual void create();
+    virtual void open();
+    virtual ~SharedMemPagesRr();
+
+private:
+    Ipc::Mem::PagePool::Owner *owner;
 };
 
-RunnerRegistrationEntry(rrAfterConfig, SharedMemPagesRr);
+RunnerRegistrationEntry(SharedMemPagesRr);
 
+void
+SharedMemPagesRr::useConfig()
+{
+    if (Ipc::Mem::PageLimit() <= 0)
+        return;
 
-void SharedMemPagesRr::run(const RunnerRegistry &)
+    Ipc::Mem::RegisteredRunner::useConfig();
+}
+
+void
+SharedMemPagesRr::create()
+{
+    Must(!owner);
+    owner = Ipc::Mem::PagePool::Init(PagePoolId, Ipc::Mem::PageLimit(),
+                                     Ipc::Mem::PageSize());
+}
+
+void
+SharedMemPagesRr::open()
 {
-    // XXX: restore if (!UsingSmp()) return;
+    Must(!ThePagePool);
+    ThePagePool = new Ipc::Mem::PagePool(PagePoolId);
+}
 
-    if (IamMasterProcess())
-        Ipc::Mem::Init();
-    else
-        Ipc::Mem::Attach();
+SharedMemPagesRr::~SharedMemPagesRr()
+{
+    delete ThePagePool;
+    ThePagePool = NULL;
+    delete owner;
 }
+