In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to achieve shared memory in nginx, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
The use of ngx_shmem
The ngx_shmem.c/h file is just a simple wrapper for the mmap () / munmap () system call or shmget () / shmdt (). A ngx-style basic library is implemented, which can apply for and release a continuous shared memory space. It is generally used for the use of shared data of fixed length, and the length of the data is fixed and will not be scaled.
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 process of using shared memory in ngxin is generally created by the master process, and the worker process obtains the memory pointer by inheritance.
For the use of ngx_shmem, you can refer to a section of ngx_event_module_init (), which creates several variables in shared memory to record each state (accepted/reading/writing...). And add or subtract these variables at several key event entries in ngx_event_module Implement to count 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 on this feature, you can check out the NGX_STAT_STUB macro definition related code and ngx_http_stub_status_module in the code.
The use of ngx_slab
Ngx_shmem is a minimalist package that implements the basic functions of shared memory. But most of the scenarios in our program share data not with a fixed size structure, but more with variable size data structures such as ngx_array, ngx_list, ngx_queue, and ngx_rbtree.
We expect to have a memory pool that can dynamically request free space like ngx_pool_t. Ngx_slab is such a structure. In principle, what is familiar with the malloc () of the system is to apply and release segments of memory through a series of algorithms. It's just that the object ngx_slab operates on is based on ngx_shmem 's shared memory.
Take a look at the interface of ngx_slab first.
Typedef struct {ngx_shmtx_t mutex;... Void * data; / * generally stores the root data address obtained from pool (the data interface of the first application in pool) * / void * addr; / * using the shared memory base address obtained by ngx_shmem application * /} 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 lies in whether to zero the applied memory segment. The API at the end of _ locked indicates that the pool of the operation has acquired the lock. There is a ngx_shmtx_t mutex in the structure of ngx_slab_pool_t to synchronize concurrent scenarios where multiple processes access pool at the same time. Note that ngx_slab_alloc () first acquires the lock, then requests the space, and finally releases the lock. Ngx_slab_alloc_locked (), on the other hand, applies for space directly, believing that the program has acquired the lock in other logic.
Using ngx_shmem in the development of nginx generally requires the following initialization process:
The module calls the ngx_shared_memory_add () interface during configuration parsing to register a section of shared memory. Provides callback functions for shared memory size and memory initialization.
The framework uses ngx_shmem to request memory in ngx_init_cycle (), initializes the ngx_slab, and then calls back the initialization function registered by the module
The module uses the application / interface of ngx_slab
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)
One of the things worth mentioning is the noreuse attribute, which controls whether shared memory is reclaimed during the reload process of nginx.
On how to achieve shared memory in nginx to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.