MemMap.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 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_IPC_STORE_MAP_H
10 #define SQUID_IPC_STORE_MAP_H
11 
12 #include "Debug.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_IPC_STORE_MAP_H */
154 
bool writing() const
Definition: MemMap.h:43
API for adjusting external state when dirty map slot is being freed.
Definition: MemMap.h:142
std::atomic< int > count
current number of map slots
Definition: MemMap.h:72
MemMapSlot Slot
Definition: MemMap.h:59
unsigned char p[MEMMAP_SLOT_DATA_SIZE]
The memory block;.
Definition: MemMap.h:48
unsigned char key[MEMMAP_SLOT_KEY_SIZE]
The entry key.
Definition: MemMap.h:47
Definition: SBuf.h:86
unsigned char cache_key
Store key.
Definition: forward.h:29
Definition: IpcIoFile.h:23
MemMapCleaner * cleaner
Definition: MemMap.h:123
class Ping::pingStats_ stats
approximate stats of a set of ReadWriteLocks
Definition: ReadWriteLock.h:56
size_t keySize() const
Definition: MemMap.h:38
const size_t extrasSize
size of slot extra data
Definition: MemMap.h:71
Ipc::Mem::FlexibleArray< Slot > slots
storage
Definition: MemMap.h:73
virtual ~MemMapCleaner()
Definition: MemMap.h:145
const int limit
maximum number of map slots
Definition: MemMap.h:70
bool reading() const
Definition: MemMap.h:42
ReadWriteLock lock
protects slot data below
Definition: MemMap.h:46
bool empty() const
Definition: MemMap.cc:316
size_t pSize
Definition: MemMap.h:49
size_t size() const
Definition: MemMap.h:37
#define MEMMAP_SLOT_KEY_SIZE
Definition: MemMap.h:29
time_t expire
Definition: MemMap.h:50
void Init(void)
prepares to parse ACLs configuration
Definition: AclRegs.cc:114
#define MEMMAP_SLOT_DATA_SIZE
Definition: MemMap.h:30
a MemMap basic element, holding basic shareable memory block info
Definition: MemMap.h:33
signed_int32_t sfileno
Definition: forward.h:22
std::atomic< uint8_t > waitingToBeFreed
may be accessed w/o a lock
Definition: MemMap.h:45
Mem::Pointer< Shared > shared
Definition: MemMap.h:129
bool sameKey(const cache_key *const aKey) const
Definition: MemMap.cc:310
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
std::atomic< uint32_t > readers
number of reading users
Definition: ReadWriteLock.h:45
std::atomic< bool > writing
there is a writing user (there can be at most 1)
Definition: ReadWriteLock.h:46
Mem::Owner< Shared > Owner
Definition: MemMap.h:77
data shared across maps in different processes
Definition: MemMap.h:62
#define free(a)
Definition: hash.c:31

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors