ngx_cycle 的初始化
整个初始化过程中,最重要的就是全局变量 nginx_cycle 的初始化,很多变量都是在这个过程中初始化的
nginx_cycle 又是通过两个局部变量 init_cycle 和 cycle 实现初始化的
事实上,日志初始化也可以算是对 nginx_cyle 的初始化,因为在代码中接下来马上要发生的就是一个赋值
ngx_memzero(&init_cycle, sizeof(ngx_cycle_t)); init_cycle.log = log; ngx_cycle = &init_cycle; // 创建内存池 1kb init_cycle.pool = ngx_create_pool(1024, log); if (init_cycle.pool == null) { return 1; } // 保存调用参数到全局变量,init_cycle 只用于提供 log 参数 if (ngx_save_argv(&init_cycle, argc, argv) != ngx_ok) { return 1; } // 保存配置文件路径、程序运行路径、调用参数到 init_cycle if (ngx_process_options(&init_cycle) != ngx_ok) { return 1; } // 获取操作系统信息、cpu信息、最大连接数、是否支持非阻塞连接等 if (ngx_os_init(log) != ngx_ok) { return 1; } /* * ngx_crc32_table_init() requires ngx_cacheline_size set in ngx_os_init() */ // 对齐校验表 if (ngx_crc32_table_init() != ngx_ok) { return 1; } // 获取所有继承连接fd的相关信息 if (ngx_add_inherited_sockets(&init_cycle) != ngx_ok) { return 1; }
内存池
nginx 是通过资源集中管理的方式管理资源的,即打开所有即将要用的资源,以备随时取用,无论是文件还是内存
这样做的好处是避免了每次创建、打开资源造成的性能消耗
因此,便有了内存池模块,用来集中申请内存资源并进行内存资源的管理和分配
内存池结构:
// struct ngx_pool_data_t // 内存池数据块结构 {{{ typedef struct { u_char *last; // 当前内存分配的结束位置 u_char *end; // 内存池的结束位置 ngx_pool_t *next; // 下一内存池 ngx_uint_t failed; // 内存分配失败计数 } ngx_pool_data_t; // }}} // struct ngx_pool_s // 内存池结构 {{{ struct ngx_pool_s { ngx_pool_data_t d; // 内存池数据块 size_t max; // 待分配内存大小 ngx_pool_t *current; // 指向当前内存池起始位置 ngx_chain_t *chain; ngx_pool_large_t *large; // 指向大块内存分配 ngx_pool_cleanup_t *cleanup; // 析构函数 ngx_log_t *log; // 内存分配相关的log }; // }}}
在这个函数中,使用了一个封装好的函数 ngx_memalign,这个函数是对系统中按照数据对齐方式分配内存的函数的封装,在不同的系统中实现方式不同,通过宏定义,实现了操作系统的适配,这是一个很漂亮的技巧
#if (ngx_have_posix_memalign) // void * ngx_memalign(size_t alignment, size_t size, ngx_log_t *log) // 用数据对齐的方式进行内存分配 {{{ void * ngx_memalign(size_t alignment, size_t size, ngx_log_t *log) { void *p; int err; // size 单位是 byte 而不是 bit err = posix_memalign(&p, alignment, size); if (err) { ngx_log_error(ngx_log_emerg, log, err, "posix_memalign(%uz, %uz) failed", alignment, size); p = null; } ngx_log_debug3(ngx_log_debug_alloc, log, 0, "posix_memalign: %p:%uz @%uz", p, size, alignment); return p; } // }}} #elif (ngx_have_memalign) // void * ngx_memalign(size_t alignment, size_t size, ngx_log_t *log) // 用数据对齐的方式进行内存分配 {{{ void * ngx_memalign(size_t alignment, size_t size, ngx_log_t *log)
所有有关内存分配的系统调用函数的封装都定义在 ngx_alloc.c 文件中
这里用到了 posix_memalign 系统调用,使用这个系统调用分配出来的内存是默认按照第二个参数的大小对齐的,这样在进行数据读写的时候,cpu可以周期地对整块数据进行读写,很大程度的节省了cpu时间
这个系统调用所分配的内存也是存在于堆内存中的,可以使用 free 函数进行释放,不过 malloc 分配的内存默认也是对齐的,它相对于 malloc 的优势仅仅在于可以指定默认对齐大小。
函数完成了内存池的初步分配,执行后 pool 取值:
$23 = (ngx_pool_t *) 0x80fe9f0 (gdb) p *init_cycle.pool $24 = { d = { last = 0x80fea18, end = 0x80fedf0, next = 0x0, failed = 0 }, max = 984, current = 0x80fe9f0, chain = 0x0, large = 0x0, cleanup = 0x0, log = 0x80e3020 <ngx_log> }
如下图所示:
The above is the detailed content of How to configure basic memory pool initialization in Nginx. For more information, please follow other related articles on the PHP Chinese website!

NGINXUnit can be used to deploy and manage applications in multiple languages. 1) Install NGINXUnit. 2) Configure it to run different types of applications such as Python and PHP. 3) Use its dynamic configuration function for application management. Through these steps, you can efficiently deploy and manage applications and improve project efficiency.

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINXUnit improves application flexibility and performance with its dynamic configuration and high-performance architecture. 1. Dynamic configuration allows the application configuration to be adjusted without restarting the server. 2. High performance is reflected in event-driven and non-blocking architectures and multi-process models, and can efficiently handle concurrent connections and utilize multi-core CPUs.

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

NGINX is suitable for handling high concurrent requests, while Apache is suitable for scenarios where complex configurations and functional extensions are required. 1.NGINX adopts an event-driven, non-blocking architecture, and is suitable for high concurrency environments. 2. Apache adopts process or thread model to provide a rich module ecosystem that is suitable for complex configuration needs.

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

NGINXUnit supports multiple programming languages and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.


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

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

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools