How to use shared memory in 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);
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);
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);
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);
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To allow the Tomcat server to access the external network, you need to: modify the Tomcat configuration file to allow external connections. Add a firewall rule to allow access to the Tomcat server port. Create a DNS record pointing the domain name to the Tomcat server public IP. Optional: Use a reverse proxy to improve security and performance. Optional: Set up HTTPS for increased security.

The start and stop commands of Nginx are nginx and nginx -s quit respectively. The start command starts the server directly, while the stop command gracefully shuts down the server, allowing all current requests to be processed. Other available stop signals include stop and reload.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

To solve the "Welcome to nginx!" error, you need to check the virtual host configuration, enable the virtual host, reload Nginx, if the virtual host configuration file cannot be found, create a default page and reload Nginx, then the error message will disappear and the website will be normal show.

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application

To register for phpMyAdmin, you need to first create a MySQL user and grant permissions to it, then download, install and configure phpMyAdmin, and finally log in to phpMyAdmin to manage the database.

nginx appears when accessing a website. The reasons may be: server maintenance, busy server, browser cache, DNS issues, firewall blocking, website misconfiguration, network connection issues, or the website is down. Try the following solutions: wait for maintenance to end, visit during off-peak hours, clear your browser cache, flush your DNS cache, disable firewall or antivirus software, contact the site administrator, check your network connection, or use a search engine or web archive to find another copy of the site. If the problem persists, please contact the site administrator.

There are five methods for container communication in the Docker environment: shared network, Docker Compose, network proxy, shared volume, and message queue. Depending on your isolation and security needs, choose the most appropriate communication method, such as leveraging Docker Compose to simplify connections or using a network proxy to increase isolation.
