Home Operation and Maintenance Nginx How to use shared memory in nginx

How to use shared memory in nginx

May 14, 2023 pm 03:55 PM
nginx

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

What to do if nginx server is hung What to do if nginx server is hung Apr 14, 2025 am 11:42 AM

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.

See all articles