MemMap.h
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 #ifndef SQUID_SRC_IPC_MEMMAP_H
10 #define SQUID_SRC_IPC_MEMMAP_H
11 
12 #include "debug/Stream.h"
13 #include "ipc/mem/FlexibleArray.h"
14 #include "ipc/mem/Pointer.h"
15 #include "ipc/ReadWriteLock.h"
16 #include "sbuf/SBuf.h"
17 #include "store/forward.h"
18 #include "store_key_md5.h"
19 #include "tools.h"
20 
21 #include <atomic>
22 
23 namespace Ipc
24 {
25 
26 // The MEMMAP_SLOT_KEY_SIZE and MEMMAP_SLOT_DATA_SIZE must be enough big
27 // to hold cached keys and data. Currently MemMap used only to store SSL
28 // shared session data which have keys of 32bytes and at most 10K data
29 #define MEMMAP_SLOT_KEY_SIZE 32
30 #define MEMMAP_SLOT_DATA_SIZE 10*1024
31 
34 {
35 public:
36  MemMapSlot();
37  size_t size() const {return sizeof(MemMapSlot);}
38  size_t keySize() const {return sizeof(key);}
39  bool sameKey(const cache_key *const aKey) const;
40  void set(const unsigned char *aKey, const void *block, size_t blockSize, time_t expire = 0);
41  bool empty() const;
42  bool reading() const { return lock.readers; }
43  bool writing() const { return lock.writing; }
44 
45  std::atomic<uint8_t> waitingToBeFreed;
46  mutable ReadWriteLock lock;
47  unsigned char key[MEMMAP_SLOT_KEY_SIZE];
48  unsigned char p[MEMMAP_SLOT_DATA_SIZE];
49  size_t pSize;
50  time_t expire;
51 };
52 
53 class MemMapCleaner;
54 
56 class MemMap
57 {
58 public:
59  typedef MemMapSlot Slot;
60 
62  class Shared
63  {
64  public:
65  Shared(const int aLimit, const size_t anExtrasSize);
66  size_t sharedMemorySize() const;
67  static size_t SharedMemorySize(const int limit, const size_t anExtrasSize);
68  ~Shared();
69 
70  const int limit;
71  const size_t extrasSize;
72  std::atomic<int> count;
74  };
75 
76 public:
78 
80  static Owner *Init(const char *const path, const int limit);
81 
82  MemMap(const char *const aPath);
83 
86  Slot *openForWriting(const cache_key *const key, sfileno &fileno);
87 
90  Slot *openForWritingAt(sfileno fileno, bool overwriteExisting = true);
91 
93  void closeForWriting(const sfileno fileno);
94 
96  void switchWritingToReading(const sfileno fileno);
97 
99  const Slot *peekAtReader(const sfileno fileno) const;
100 
102  void free(const sfileno fileno);
103 
105  const Slot *openForReading(const cache_key *const key, sfileno &fileno);
106 
108  const Slot *openForReadingAt(const sfileno fileno);
109 
111  void closeForReading(const sfileno fileno);
112 
113  bool full() const;
114  bool valid(const int n) const;
115  int entryCount() const;
116  int entryLimit() const;
117 
119  void updateStats(ReadWriteLockStats &stats) const;
120 
124 
125 protected:
126  static Owner *Init(const char *const path, const int limit, const size_t extrasSize);
127 
128  const SBuf path;
130 
131 private:
132  int slotIndexByKey(const cache_key *const key) const;
133  Slot &slotByKey(const cache_key *const key);
134 
135  Slot *openForReading(Slot &s);
136  void abortWriting(const sfileno fileno);
137  void freeIfNeeded(Slot &s);
138  void freeLocked(Slot &s, bool keepLocked);
139 };
140 
143 {
144 public:
145  virtual ~MemMapCleaner() {}
146 
148  virtual void noteFreeMapSlot(const sfileno slotId) = 0;
149 };
150 
151 } // namespace Ipc
152 
153 #endif /* SQUID_SRC_IPC_MEMMAP_H */
154 
ReadWriteLock lock
protects slot data below
Definition: MemMap.h:46
approximate stats of a set of ReadWriteLocks
Definition: ReadWriteLock.h:70
size_t sharedMemorySize() const
Definition: MemMap.cc:337
void freeIfNeeded(Slot &s)
std::atomic< bool > writing
there is a writing user (there can be at most 1)
Definition: ReadWriteLock.h:55
int entryCount() const
number of used slots
Definition: MemMap.cc:224
a MemMap basic element, holding basic shareable memory block info
Definition: MemMap.h:33
bool reading() const
Definition: MemMap.h:42
unsigned char cache_key
Store key.
Definition: forward.h:29
const int limit
maximum number of map slots
Definition: MemMap.h:70
A map of MemMapSlots indexed by their keys, with read/write slot locking.
Definition: MemMap.h:56
const SBuf path
cache_dir path, used for logging
Definition: MemMap.h:128
const Slot * openForReading(const cache_key *const key, sfileno &fileno)
open slot for reading, increments read level
Definition: MemMap.cc:153
time_t expire
Definition: MemMap.h:50
MemMapCleaner * cleaner
Definition: MemMap.h:123
Slot * openForWritingAt(sfileno fileno, bool overwriteExisting=true)
Definition: MemMap.cc:57
Definition: SBuf.h:93
bool empty() const
Definition: MemMap.cc:316
void closeForWriting(const sfileno fileno)
successfully finish writing the entry
Definition: MemMap.cc:91
Mem::Pointer< Shared > shared
Definition: MemMap.h:129
void updateStats(ReadWriteLockStats &stats) const
adds approximate current stats to the supplied ones
Definition: MemMap.cc:236
bool writing() const
Definition: MemMap.h:43
std::atomic< uint32_t > readers
number of reading users
Definition: ReadWriteLock.h:54
void abortWriting(const sfileno fileno)
terminate writing the entry, freeing its slot for others to use
Definition: MemMap.cc:114
std::atomic< int > count
current number of map slots
Definition: MemMap.h:72
#define MEMMAP_SLOT_KEY_SIZE
Definition: MemMap.h:29
const Slot * peekAtReader(const sfileno fileno) const
only works on locked entries; returns nil unless the slot is readable
Definition: MemMap.cc:125
MemMap(const char *const aPath)
Definition: MemMap.cc:16
bool valid(const int n) const
whether n is a valid slot coordinate
Definition: MemMap.cc:243
int entryLimit() const
maximum number of slots that can be used
Definition: MemMap.cc:218
void switchWritingToReading(const sfileno fileno)
stop writing the locked entry and start reading it
Definition: MemMap.cc:102
#define MEMMAP_SLOT_DATA_SIZE
Definition: MemMap.h:30
int slotIndexByKey(const cache_key *const key) const
Definition: MemMap.cc:262
static Owner * Init(const char *const path, const int limit)
initialize shared memory
Definition: MemMap.cc:36
API for adjusting external state when dirty map slot is being freed.
Definition: MemMap.h:142
MemMapSlot Slot
Definition: MemMap.h:59
const Slot * openForReadingAt(const sfileno fileno)
open slot for reading, increments read level
Definition: MemMap.cc:174
const size_t extrasSize
size of slot extra data
Definition: MemMap.h:71
size_t keySize() const
Definition: MemMap.h:38
size_t size() const
Definition: MemMap.h:37
signed_int32_t sfileno
Definition: forward.h:22
unsigned char key[MEMMAP_SLOT_KEY_SIZE]
The entry key.
Definition: MemMap.h:47
virtual ~MemMapCleaner()
Definition: MemMap.h:145
Ipc::Mem::FlexibleArray< Slot > slots
storage
Definition: MemMap.h:73
data shared across maps in different processes
Definition: MemMap.h:62
unsigned char p[MEMMAP_SLOT_DATA_SIZE]
The memory block;.
Definition: MemMap.h:48
void set(const unsigned char *aKey, const void *block, size_t blockSize, time_t expire=0)
Definition: MemMap.cc:300
Slot * openForWriting(const cache_key *const key, sfileno &fileno)
Definition: MemMap.cc:42
void freeLocked(Slot &s, bool keepLocked)
unconditionally frees the already exclusively locked slot and releases lock
Definition: MemMap.cc:276
Slot & slotByKey(const cache_key *const key)
Definition: MemMap.cc:269
void free(const sfileno fileno)
mark the slot as waiting to be freed and, if possible, free it
Definition: MemMap.cc:138
virtual void noteFreeMapSlot(const sfileno slotId)=0
adjust slot-linked state before a locked Readable slot is erased
Mem::Owner< Shared > Owner
Definition: MemMap.h:77
static size_t SharedMemorySize(const int limit, const size_t anExtrasSize)
Definition: MemMap.cc:343
bool sameKey(const cache_key *const aKey) const
Definition: MemMap.cc:310
Shared(const int aLimit, const size_t anExtrasSize)
Definition: MemMap.cc:327
void closeForReading(const sfileno fileno)
close slot after reading, decrements read level
Definition: MemMap.cc:207
size_t pSize
Definition: MemMap.h:49
Definition: IpcIoFile.h:23
std::atomic< uint8_t > waitingToBeFreed
may be accessed w/o a lock
Definition: MemMap.h:45
bool full() const
there are no empty slots left
Definition: MemMap.cc:230

 

Introduction

Documentation

Support

Miscellaneous