nginx_shared_memory_layout

Overview

Shared memory is memory that can be accessed by different processes, as each process’s page table maps to the same physical page, nginx tracks all used shared memory as a list in each cycle. for each shared memory, mmap() is called by master processed after parse conf if same shared memory(same name, name is the key) can’t be reused, otherwise, just copy shared memory address from old cycle to new cycle, save that shared memory address, reused shared memory is not freed during reconfigure, later on fork worker process.

Shared memory

Data structure and API

structure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struct ngx_shm_zone_s {
void *data; /* data context of the share memory
* most are conf related that will use
* the shared memory to save something
*/
ngx_shm_t shm; /* meta of the shared memory, like name, size, addr etc*/
ngx_shm_zone_init_pt init; /* init callback after memory mapped */
void *tag; /* module address used as part of key(name, tag) */
ngx_uint_t noreuse; /* unsigned noreuse:1; */
};

typedef struct {
u_char *addr; /* mmapped address, it's managed as slab pool, each worker only shares this part */
size_t size; /* size of shared memory, user should make it as pages units */
ngx_str_t name; /* key for shared memory */
} ngx_shm_t;

struct ngx_cycle_s {
/* each cycle has its shared memory list
* each element is ngx_shm_zone_t which is created during reconfigure based on nginx.conf
* the address of ngx_shm_zone_t is different for cycles
* but if old cycle and new cycle reuse same shared memory
* the shared memory address is copied from old cycle to new cycle
* saved at: ngx_shm_zone_t.shm.addr
*/
ngx_list_t shared_memory;
}

API

1
2
3
4
/* add a ngx_shm_zone_t to the cycle shared_memory list, shared memory is not mapped now */
ngx_shm_zone_t * ngx_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size, void *tag);
/* call mmap() to allocate shared memory */
ngx_int_t ngx_shm_alloc(ngx_shm_t *shm);

organize all shared memory

shared memory tracking

This picture shows how shared memory is tracked by each cycle, reused old one(same name, size, tag) or create new one if cant’ be reused.