How to use shared memory in nginx

PHPz
Release: 2023-05-14 15:55:06
forward
1329 people have browsed it

Usage of ngx_shmem

ngx_shmem.c/h file is just a simple call to mmap()/munmap() system call or shmget()/shmdt() of packaging. Implemented the ngx style basic library, which can apply for and release a continuous shared memory space. It is generally used for fixed-length shared data. The data length is fixed and will not expand or shrink during use.

typedef struct {
  u_char   *addr;
  size_t    size;
  ...
} ngx_shm_t;
ngx_int_t ngx_shm_alloc(ngx_shm_t *shm);
void ngx_shm_free(ngx_shm_t *shm);
Copy after login

The usage process of shared memory in ngxin is generally created by the master process, and the worker process obtains the memory pointer through inheritance.

Regarding the use of ngx_shmem, you can refer to some fragments in ngx_event_module_init(). This part of the code creates several variables in the shared memory for recording requests in various states (accepted/reading/writing...) Quantity, and perform addition and subtraction statistical operations on these variables at several key event entries in ngx_event_module. Implement statistics on the current request status of all worker processes.

shm.size = size;
ngx_str_set(&shm.name, "nginx_shared_zone");
shm.log = cycle->log;

if (ngx_shm_alloc(&shm) != ngx_ok) {
  return ngx_error;
}

shared = shm.addr;
...
ngx_stat_accepted = (ngx_atomic_t *) (shared + 3 * cl);
ngx_stat_handled = (ngx_atomic_t *) (shared + 4 * cl);
ngx_stat_requests = (ngx_atomic_t *) (shared + 5 * cl);
ngx_stat_active = (ngx_atomic_t *) (shared + 6 * cl);
ngx_stat_reading = (ngx_atomic_t *) (shared + 7 * cl);
ngx_stat_writing = (ngx_atomic_t *) (shared + 8 * cl);
ngx_stat_waiting = (ngx_atomic_t *) (shared + 9 * cl);
Copy after login

For more details about this function, you can view the ngx_stat_stub macro definition related code and ngx_http_stub_status_module in the code.

Usage of ngx_slab

ngx_shmem is a minimalist package that implements the basic functions of shared memory. However, most of the scene shared data in our program does not have a fixed-size structure, but is more a data structure with a variable size such as ngx_array, ngx_list, ngx_queue, and ngx_rbtree.

We hope to have a memory pool that can dynamically apply for and release space like ngx_pool_t. ngx_slab is just such a structure. In principle, it is similar to the system's malloc() in that it uses a series of algorithms to apply for and release memory segments. It’s just that the object operated by ngx_slab is shared memory based on ngx_shmem.

Let’s take a look at the interface of ngx_slab first

typedef struct {
  ngx_shmtx_t    mutex;
  ...
  void       *data; /* 一般存放从pool中申请获得的根数据地址(pool中第一个申请的数据接口) */
  void       *addr; /* 使用ngx_shmem申请获得的共享内存基地址 */
} ngx_slab_pool_t;

void ngx_slab_init(ngx_slab_pool_t *pool);
void *ngx_slab_alloc(ngx_slab_pool_t *pool, size_t size);
void *ngx_slab_alloc_locked(ngx_slab_pool_t *pool, size_t size);
void *ngx_slab_calloc(ngx_slab_pool_t *pool, size_t size);
void *ngx_slab_calloc_locked(ngx_slab_pool_t *pool, size_t size);
void ngx_slab_free(ngx_slab_pool_t *pool, void *p);
void ngx_slab_free_locked(ngx_slab_pool_t *pool, void *p);
Copy after login

You can see that the interface is not complicated. The difference between alloc and calloc is whether to clear the memory segment obtained by the application. The interface at the end of _locked indicates the operation. The pool has already acquired the lock. There is a ngx_shmtx_t mutex in the ngx_slab_pool_t structure, which is used to synchronize concurrent scenarios where multiple processes access the pool at the same time. Note that ngx_slab_alloc() will first acquire the lock, then apply for space, and finally release the lock. And ngx_slab_alloc_locked() directly applies for space, thinking that the program has obtained the lock in other logic.

Using ngx_shmem in nginx development generally requires following the following initialization process:

  • The module calls the ngx_shared_memory_add() interface during the configuration parsing process to register a shared memory. Provides callback functions for shared memory size and memory initialization.

  • The framework uses ngx_shmem in ngx_init_cycle() to apply for memory, initialize ngx_slab, and then call back the initialization function registered by the module

  • The module uses ngx_slab Apply for/Whether interface

In this process, the ngx_shared_memory_add() interface and the corresponding ngx_shm_zone_t structure are involved.

struct ngx_shm_zone_s {
  void           *data;
  ngx_shm_t         shm;
  ngx_shm_zone_init_pt   init;
  void           *tag;
  void           *sync;
  ngx_uint_t        noreuse; /* unsigned noreuse:1; */
};
ngx_shm_zone_t *ngx_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name,
  size_t size, void *tag);
Copy after login

It is worth mentioning that the noreuse attribute controls whether shared memory will be re-applied during the reload process of nginx.

Since the ngx_init_cycle() function is long, this process can be viewed by looking for the /* create shared memory */ comment or the cycle->shared_memory object to view the relevant code.

The above is the detailed content of How to use shared memory in nginx. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!