1 ngx_int_t
2 ngx_os_init(ngx_log_t *log)
3 {
4 ngx_time_t *tp;
5 ngx_uint_t n;
6 #if (NGX_HAVE_LEVEL1_DCACHE_LINESIZE)
7 long size;
8 #endif
9
10 #if (NGX_HAVE_OS_SPECIFIC_INIT)
11 // 初始化linux内核版本号
12 if (ngx_os_specific_init(log) != NGX_OK) {
13 return NGX_ERROR;
14 }
15 #endif
16 // 初始化proctitle内存空间
17 if (ngx_init_setproctitle(log) != NGX_OK) {
18 return NGX_ERROR;
19 }
20
21 // 获取page size
22 ngx_pagesize = getpagesize();
23 // 获取cacheline size
24 ngx_cacheline_size = NGX_CPU_CACHE_LINE;
25
26 // 2 ^ ngx_pagesize_shift == ngx_page_size
27 for (n = ngx_pagesize; n >>= 1; ngx_pagesize_shift++) { /* void */ }
28
29 #if (NGX_HAVE_SC_NPROCESSORS_ONLN)
30 if (ngx_ncpu == 0) {
31 // 获取CPU核数
32 ngx_ncpu = sysconf(_SC_NPROCESSORS_ONLN);
33 }
34 #endif
35
36 if (ngx_ncpu < 1) {
37 ngx_ncpu = 1;
38 }
39
40 #if (NGX_HAVE_LEVEL1_DCACHE_LINESIZE)
41 //获取1-cache line size
42 size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
43 if (size > 0) {
44 ngx_cacheline_size = size;
45 }
46 #endif
47
48 // 根据CPU info获取cacheline size
49 ngx_cpuinfo();
50
51 //获取打开最多文件数
52 if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
53 ngx_log_error(NGX_LOG_ALERT, log, errno,
54 "getrlimit(RLIMIT_NOFILE) failed");
55 return NGX_ERROR;
56 }
57
58 ngx_max_sockets = (ngx_int_t) rlmt.rlim_cur;
59
60 #if (NGX_HAVE_INHERITED_NONBLOCK || NGX_HAVE_ACCEPT4)
61 ngx_inherited_nonblocking = 1;
62 #else
63 ngx_inherited_nonblocking = 0;
64 #endif
65
66 tp = ngx_timeofday();
67 //初始化rand种子
68 srandom(((unsigned) ngx_pid << 16) ^ tp->sec ^ tp->msec);
69
70 return NGX_OK;
71 }