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_IPC_STORE_MAP_H
10#define SQUID_IPC_STORE_MAP_H
11
12#include "debug/Stream.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
23namespace 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{
35public:
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;
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
53class MemMapCleaner;
54
56class MemMap
57{
58public:
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
76public:
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
120
124
125protected:
126 static Owner *Init(const char *const path, const int limit, const size_t extrasSize);
127
128 const SBuf path;
130
131private:
132 int slotIndexByKey(const cache_key *const key) const;
133 Slot &slotByKey(const cache_key *const key);
134
136 void abortWriting(const sfileno fileno);
138 void freeLocked(Slot &s, bool keepLocked);
139};
140
143{
144public:
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
#define MEMMAP_SLOT_KEY_SIZE
Definition: MemMap.h:29
#define MEMMAP_SLOT_DATA_SIZE
Definition: MemMap.h:30
API for adjusting external state when dirty map slot is being freed.
Definition: MemMap.h:143
virtual ~MemMapCleaner()
Definition: MemMap.h:145
virtual void noteFreeMapSlot(const sfileno slotId)=0
adjust slot-linked state before a locked Readable slot is erased
a MemMap basic element, holding basic shareable memory block info
Definition: MemMap.h:34
bool sameKey(const cache_key *const aKey) const
Definition: MemMap.cc:310
void set(const unsigned char *aKey, const void *block, size_t blockSize, time_t expire=0)
Definition: MemMap.cc:300
size_t pSize
Definition: MemMap.h:49
size_t size() const
Definition: MemMap.h:37
std::atomic< uint8_t > waitingToBeFreed
may be accessed w/o a lock
Definition: MemMap.h:45
bool reading() const
Definition: MemMap.h:42
bool writing() const
Definition: MemMap.h:43
bool empty() const
Definition: MemMap.cc:316
ReadWriteLock lock
protects slot data below
Definition: MemMap.h:46
unsigned char key[MEMMAP_SLOT_KEY_SIZE]
The entry key.
Definition: MemMap.h:47
unsigned char p[MEMMAP_SLOT_DATA_SIZE]
The memory block;.
Definition: MemMap.h:48
size_t keySize() const
Definition: MemMap.h:38
time_t expire
Definition: MemMap.h:50
data shared across maps in different processes
Definition: MemMap.h:63
std::atomic< int > count
current number of map slots
Definition: MemMap.h:72
Ipc::Mem::FlexibleArray< Slot > slots
storage
Definition: MemMap.h:73
static size_t SharedMemorySize(const int limit, const size_t anExtrasSize)
Definition: MemMap.cc:343
size_t sharedMemorySize() const
Definition: MemMap.cc:337
const size_t extrasSize
size of slot extra data
Definition: MemMap.h:71
const int limit
maximum number of map slots
Definition: MemMap.h:70
Shared(const int aLimit, const size_t anExtrasSize)
Definition: MemMap.cc:327
A map of MemMapSlots indexed by their keys, with read/write slot locking.
Definition: MemMap.h:57
void free(const sfileno fileno)
mark the slot as waiting to be freed and, if possible, free it
Definition: MemMap.cc:138
const SBuf path
cache_dir path, used for logging
Definition: MemMap.h:128
void updateStats(ReadWriteLockStats &stats) const
adds approximate current stats to the supplied ones
Definition: MemMap.cc:236
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
Mem::Pointer< Shared > shared
Definition: MemMap.h:129
void abortWriting(const sfileno fileno)
terminate writing the entry, freeing its slot for others to use
Definition: MemMap.cc:114
bool full() const
there are no empty slots left
Definition: MemMap.cc:230
int slotIndexByKey(const cache_key *const key) const
Definition: MemMap.cc:262
int entryLimit() const
maximum number of slots that can be used
Definition: MemMap.cc:218
void freeLocked(Slot &s, bool keepLocked)
unconditionally frees the already exclusively locked slot and releases lock
Definition: MemMap.cc:276
Slot * openForReading(Slot &s)
void closeForReading(const sfileno fileno)
close slot after reading, decrements read level
Definition: MemMap.cc:207
void freeIfNeeded(Slot &s)
const Slot * openForReading(const cache_key *const key, sfileno &fileno)
open slot for reading, increments read level
Definition: MemMap.cc:153
void switchWritingToReading(const sfileno fileno)
stop writing the locked entry and start reading it
Definition: MemMap.cc:102
const Slot * openForReadingAt(const sfileno fileno)
open slot for reading, increments read level
Definition: MemMap.cc:174
Slot * openForWriting(const cache_key *const key, sfileno &fileno)
Definition: MemMap.cc:42
Slot & slotByKey(const cache_key *const key)
Definition: MemMap.cc:269
static Owner * Init(const char *const path, const int limit)
initialize shared memory
Definition: MemMap.cc:36
int entryCount() const
number of used slots
Definition: MemMap.cc:224
MemMapCleaner * cleaner
Definition: MemMap.h:123
void closeForWriting(const sfileno fileno)
successfully finish writing the entry
Definition: MemMap.cc:91
const Slot * peekAtReader(const sfileno fileno) const
only works on locked entries; returns nil unless the slot is readable
Definition: MemMap.cc:125
MemMapSlot Slot
Definition: MemMap.h:59
Slot * openForWritingAt(sfileno fileno, bool overwriteExisting=true)
Definition: MemMap.cc:57
Mem::Owner< Shared > Owner
Definition: MemMap.h:77
approximate stats of a set of ReadWriteLocks
Definition: ReadWriteLock.h:71
std::atomic< uint32_t > readers
number of reading users
Definition: ReadWriteLock.h:54
std::atomic< bool > writing
there is a writing user (there can be at most 1)
Definition: ReadWriteLock.h:55
Definition: SBuf.h:94
Definition: IpcIoFile.h:24
class Ping::pingStats_ stats
unsigned char cache_key
Store key.
Definition: forward.h:29
signed_int32_t sfileno
Definition: forward.h:22

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors