nginx 源代码分析 - 启动(一)

auto/cc/gcc,CFLAGS不仅要加上-g,还要加上-ggdb -O0

sudo gdb ./objs/nginx
r -p /home/liuwb/Desktop/code/nginx-code/ -c conf/nginx.conf


ngx_strerror_init
NGX_SYS_NERR = 135
初始化ngx_sys_errlist数组,索引代表errcode,内容代表错误信息。

ngx_get_options
全局变量赋值,ngx_prefix,ngx_conf_file

ngx_time_init
ngx_cached_err_log_time/ngx_cached_http_time/ngx_cached_http_log_time/ngx_cached_http_log_iso8601/ngx_cached_syslog_time
给这些字符串的长度赋值
ngx_time_t        cached_time[NGX_TIME_SLOTS]; 64
ngx_cached_time = &cached_time[0];指向第一个元素

获取当前时间,计算当前的毫秒时间戳,更新全局变量ngx_current_msec
当前的slot是0,如果当前时间和当前slot对应的秒数相等,更新当前slot的毫秒数,解锁,返回1
否则,slot循环自加,更新当前slot的秒数和毫秒数

调用ngx_gmtime(sec, &gmt);根据当前的秒数计算出年月日,小时分钟秒

static u_char            cached_http_time[NGX_TIME_SLOTS]
                                    [sizeof("Mon, 28 Sep 1970 06:00:00 GMT")];
更新cached_http_time[slot]的内容

调用ngx_localtime(sec, &tm);
cached_gmtoff = (ngx_int_t) (tm.ngx_tm_gmtoff / 60);
tm.ngx_tm_gmtoff = -28800
全局变量cached_gmtoff = -480
将cached_gmtoff赋给cached_time[slot]的gmtoff字段
gmtoff字段指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数

用tm更新cached_err_log_time[slot] "2016/11/08 18:39:46"
cached_http_log_time[slot] "08/Nov/2016:18:39:46 -0800"
cached_http_log_iso8601[slot] "2016-11-08T18:39:46-08:00"                                    
cached_syslog_time[slot] "Nov  8 18:39:46"

全局变量赋值
ngx_cached_time = &cached_time[slot]
ngx_cached_http_time/ngx_cached_err_log_time/ngx_cached_http_log_time/ngx_cached_http_log_iso8601/ngx_cached_syslog_time
用&cached_XXX[slot]
全局变量的字符串并没有实际分配内存

ngx_regex_init
pcre_malloc和pcre_free是libpcre库中的全局变量,类型是函数指针
pcre_malloc = ngx_regex_malloc;
pcre_free = ngx_regex_free;

ngx_pid = ngx_getpid();得到本进程的pid

ngx_log_t *log = ngx_log_init(ngx_prefix); 返回值实际上是指向静态变量ngx_log
ngx_log.file = &ngx_log_file; ngx_log_file也是一个静态变量
ngx_log.log_level = NGX_LOG_NOTICE;

NGX_ERROR_LOG_PATH = "logs/error.log"
根据字符串拼接成/home/liuwb/Desktop/code/nginx-code/logs/error.log。打开

全局变量ngx_cycle指向局部init_cycle,log字段赋值为全局的log

init_cycle.pool = ngx_create_pool(1024, log);
p = ngx_memalign(NGX_POOL_ALIGNMENT, size, log);
NGX_POOL_ALIGNMENT = 16, size=1024
16字节对齐,分配1024字节,前80个字节放ngx_pool_t
p->d.last = (u_char *) p + sizeof(ngx_pool_t); 这个pool的最后一个有效数据
p->d.end = (u_char *) p + size; pool的结束位置
p->d.next = NULL; 下一个pool
p->d.failed = 0;

ngx_pagesize = 0

p->current = p;
p->chain = NULL;
p->large = NULL;
p->cleanup = NULL;
p->log = log;


ngx_save_argv(&init_cycle, argc, argv)
把命令行参数拷贝到全局变量ngx_argv

ngx_process_options(&init_cycle)

ngx_conf_full_name给cycle->conf_file赋值

更新cycle->conf_prefix,截止到最后一个/

ngx_pnalloc(ngx_pool_t *pool, size_t size)
当size<p->max,调用ngx_palloc_small
不够时调用ngx_palloc_large

ngx_palloc_small(ngx_pool_t *pool, size_t size, ngx_uint_t align)
从pool链表的pool->current开始,找到一个pool,p->d.end - p->d.last > size
返回分配的地址

如果没有找到,调用ngx_palloc_block(ngx_pool_t *pool, size_t size)
创建一个新的ngx_pool_t,连入pool链表,返回新pool的起始空闲地址
调用ngx_palloc_block,说明现有的pool链表中没有一个元素可以满足一次性分配size大小的
pool的每个数据块,p->d.failed++
如果p->d.failed++ > 4,pool->current = p->d.next;指向下一个pool,init_cycle的pool的地址并没有改变

ngx_palloc_large(ngx_pool_t *pool, size_t size)
直接分配size大小,遍历pool->large链表,这里是pool,而不是pool->current,找到一个large结构体,并且它的数据指针是NULL,
如果连续3个large的结构体数据指针都不为NULL,跳出查找循环,在pool的小块中分配ngx_pool_large_t结构体,large放到pool链表的表头


ngx_os_init
ngx_os_specific_init
69 = {sysname = "Linux", '\000' <repeats 59 times>, nodename = "ubuntu", '\000' <repeats 58 times>,
  release = "3.19.0-25-generic", '\000' <repeats 47 times>,
  version = "#26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015", '\000' <repeats 13 times>,
  machine = "x86_64", '\000' <repeats 58 times>, domainname = "(none)", '\000' <repeats 58 times>}
调用uname取得操作系统信息
拷贝到ngx_linux_kern_ostype和ngx_linux_kern_osrelease全局变量
ngx_os_io = ngx_linux_io; linux操作集合

ngx_init_setproctitle ngx_setproctitle.c
unistd.h extern char **environ; 是系统环境变量

ngx_os_argv = argv,是系统传给程序的一个二维字符数字,其实是连续的一维数组组成的
以0分割每个参数
environ也是,而且environ是跟在argv后面

将系统环境变量指针指向新分配的内存,并拷贝内容到新分配的内存,这里分配的内存是直接malloc

ngx_pagesize = getpagesize(); 4096
ngx_cacheline_size = NGX_CPU_CACHE_LINE 64

定义了NGX_HAVE_SC_NPROCESSORS_ONLN
ngx_ncpu = sysconf(_SC_NPROCESSORS_ONLN); 2

ngx_cpuinfo();
ngx_cpuid(0, vbuf);
执行cpuid指令,vendor = (u_char *) &vbuf[1] vendor指向一个地址
ngx_cpuid(1, cpu);
vendor = GenuineIntel 改变地址的值

ngx_cacheline_size = 64;


getrlimit(RLIMIT_NOFILE, &rlmt)
ngx_max_sockets = 1024
ngx_inherited_nonblocking = 1

得到ngx_cached_time,
srandom(((unsigned) ngx_pid << 16) ^ tp->sec ^ tp->msec);


ngx_crc32_table_init
看ngx_crc32_table_short这个指针是否和ngx_cacheline_size对齐,对齐就返回OK,没对齐就重新分配,拷贝

ngx_add_inherited_sockets(&init_cycle)
查找NGINX环境变量,如果没有,返回
如果有,cycle->listening = 10个ngx_listening_t组成的数组,先初始化10个,不够ngx_array_push可以自动增加空间
inherited是NGINX环境变量的值,用字符串表示,以冒号或者分号分割,内容是一个文件描述符
从cycle->listening数组中取出一个ngx_listening_t ls,清空ls,ls->fd = (ngx_socket_t) s;

置全局变量ngx_inherited = 1;
ngx_set_inherited_sockets(cycle);
遍历cycle->listening数组,得到s的各种属性,存在ls结构中

ngx_preinit_modules
在objs的ngx_modules.c文件中定义了ngx_module_names和ngx_modules数组,具体的module在各个模块中定义,然后加到configure中,
以生成数组
主要是用ngx_module_names初始化,ngx_modules
ngx_modules_n = 48
ngx_max_module = 48 + 128 = 176

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值