Klaus Demo nginx / fbd32d4
worker_cpu_affinity: cleaned up Linux implementation, added FreeBSD support. Ruslan Ermilov 10 years ago
9 changed file(s) with 120 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
133133 exit 1
134134 fi
135135 fi
136
137
138 # cpuset_setaffinity()
139
140 if [ $version -ge 701000 ]; then
141 echo " + cpuset_setaffinity() found"
142 have=NGX_HAVE_CPUSET_SETAFFINITY . auto/have
143 fi
127127 ngx_feature_incs="#include <sched.h>"
128128 ngx_feature_path=
129129 ngx_feature_libs=
130 ngx_feature_test="long mask = 0;
131 sched_setaffinity(0, 32, (cpu_set_t *) &mask)"
130 ngx_feature_test="cpu_set_t mask;
131 CPU_ZERO(&mask);
132 sched_setaffinity(0, sizeof(cpu_set_t), &mask)"
132133 . auto/feature
133134
134135
144144 src/os/unix/ngx_channel.h \
145145 src/os/unix/ngx_shmem.h \
146146 src/os/unix/ngx_process.h \
147 src/os/unix/ngx_setaffinity.h \
147148 src/os/unix/ngx_setproctitle.h \
148149 src/os/unix/ngx_atomic.h \
149150 src/os/unix/ngx_gcc_atomic_x86.h \
178179 src/os/unix/ngx_shmem.c \
179180 src/os/unix/ngx_process.c \
180181 src/os/unix/ngx_daemon.c \
182 src/os/unix/ngx_setaffinity.c \
181183 src/os/unix/ngx_setproctitle.c \
182184 src/os/unix/ngx_posix_init.c \
183185 src/os/unix/ngx_user.c \
982982 ngx_conf_init_value(ccf->worker_processes, 1);
983983 ngx_conf_init_value(ccf->debug_points, 0);
984984
985 #if (NGX_HAVE_SCHED_SETAFFINITY)
985 #if (NGX_HAVE_CPU_AFFINITY)
986986
987987 if (ccf->cpu_affinity_n
988988 && ccf->cpu_affinity_n != 1
989989 && ccf->cpu_affinity_n != (ngx_uint_t) ccf->worker_processes)
990990 {
991991 ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
992 "number of the \"worker_processes\" is not equal to "
993 "the number of the \"worker_cpu_affinity\" mask, "
992 "the number of \"worker_processes\" is not equal to "
993 "the number of \"worker_cpu_affinity\" masks, "
994994 "using last mask for remaining worker processes");
995995 }
996996
12411241 static char *
12421242 ngx_set_cpu_affinity(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
12431243 {
1244 #if (NGX_HAVE_SCHED_SETAFFINITY)
1244 #if (NGX_HAVE_CPU_AFFINITY)
12451245 ngx_core_conf_t *ccf = conf;
12461246
12471247 u_char ch;
1248 u_long *mask;
1248 uint64_t *mask;
12491249 ngx_str_t *value;
12501250 ngx_uint_t i, n;
12511251
12531253 return "is duplicate";
12541254 }
12551255
1256 mask = ngx_palloc(cf->pool, (cf->args->nelts - 1) * sizeof(long));
1256 mask = ngx_palloc(cf->pool, (cf->args->nelts - 1) * sizeof(uint64_t));
12571257 if (mask == NULL) {
12581258 return NGX_CONF_ERROR;
12591259 }
12651265
12661266 for (n = 1; n < cf->args->nelts; n++) {
12671267
1268 if (value[n].len > 32) {
1268 if (value[n].len > 64) {
12691269 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1270 "\"worker_cpu_affinity\" supports up to 32 CPU only");
1270 "\"worker_cpu_affinity\" supports up to 64 CPUs only");
12711271 return NGX_CONF_ERROR;
12721272 }
12731273
13101310 }
13111311
13121312
1313 u_long
1313 uint64_t
13141314 ngx_get_cpu_affinity(ngx_uint_t n)
13151315 {
13161316 ngx_core_conf_t *ccf;
8585 int priority;
8686
8787 ngx_uint_t cpu_affinity_n;
88 u_long *cpu_affinity;
88 uint64_t *cpu_affinity;
8989
9090 char *username;
9191 ngx_uid_t user;
123123 void ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user);
124124 char **ngx_set_environment(ngx_cycle_t *cycle, ngx_uint_t *last);
125125 ngx_pid_t ngx_exec_new_binary(ngx_cycle_t *cycle, char *const *argv);
126 u_long ngx_get_cpu_affinity(ngx_uint_t n);
126 uint64_t ngx_get_cpu_affinity(ngx_uint_t n);
127127 ngx_shm_zone_t *ngx_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name,
128128 size_t size, void *tag);
129129
88 #define _NGX_PROCESS_H_INCLUDED_
99
1010
11 #include <ngx_setaffinity.h>
1112 #include <ngx_setproctitle.h>
1213
1314
6161 #endif
6262
6363
64 u_long cpu_affinity;
64 uint64_t cpu_affinity;
6565 static u_char master_process[] = "master process";
6666
6767
912912 }
913913 }
914914
915 #if (NGX_HAVE_SCHED_SETAFFINITY)
916
917915 if (cpu_affinity) {
918 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
919 "sched_setaffinity(0x%08Xl)", cpu_affinity);
920
921 if (sched_setaffinity(0, sizeof(cpu_affinity),
922 (cpu_set_t *) &cpu_affinity)
923 == -1)
924 {
925 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
926 "sched_setaffinity(0x%08Xl) failed", cpu_affinity);
927 }
928 }
929
930 #endif
916 ngx_setaffinity(cpu_affinity, cycle->log);
917 }
931918
932919 #if (NGX_HAVE_PR_SET_DUMPABLE)
933920
0
1 /*
2 * Copyright (C) Nginx, Inc.
3 */
4
5
6 #include <ngx_config.h>
7 #include <ngx_core.h>
8
9
10 #if (NGX_HAVE_CPUSET_SETAFFINITY)
11
12 #include <sys/cpuset.h>
13
14 void
15 ngx_setaffinity(uint64_t cpu_affinity, ngx_log_t *log)
16 {
17 cpuset_t mask;
18 ngx_uint_t i;
19
20 ngx_log_error(NGX_LOG_NOTICE, log, 0,
21 "cpuset_setaffinity(0x%08Xl)", cpu_affinity);
22
23 CPU_ZERO(&mask);
24 i = 0;
25 do {
26 if (cpu_affinity & 1) {
27 CPU_SET(i, &mask);
28 }
29 i++;
30 cpu_affinity >>= 1;
31 } while (cpu_affinity);
32
33 if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
34 sizeof(cpuset_t), &mask) == -1)
35 {
36 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
37 "cpuset_setaffinity() failed");
38 }
39 }
40
41 #elif (NGX_HAVE_SCHED_SETAFFINITY)
42
43 void
44 ngx_setaffinity(uint64_t cpu_affinity, ngx_log_t *log)
45 {
46 cpu_set_t mask;
47 ngx_uint_t i;
48
49 ngx_log_error(NGX_LOG_NOTICE, log, 0,
50 "sched_setaffinity(0x%08Xl)", cpu_affinity);
51
52 CPU_ZERO(&mask);
53 i = 0;
54 do {
55 if (cpu_affinity & 1) {
56 CPU_SET(i, &mask);
57 }
58 i++;
59 cpu_affinity >>= 1;
60 } while (cpu_affinity);
61
62 if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
63 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
64 "sched_setaffinity() failed");
65 }
66 }
67
68 #endif
0
1 /*
2 * Copyright (C) Nginx, Inc.
3 */
4
5 #ifndef _NGX_SETAFFINITY_H_INCLUDED_
6 #define _NGX_SETAFFINITY_H_INCLUDED_
7
8
9 #if (NGX_HAVE_SCHED_SETAFFINITY || NGX_HAVE_CPUSET_SETAFFINITY)
10
11 #define NGX_HAVE_CPU_AFFINITY 1
12
13 void ngx_setaffinity(uint64_t cpu_affinity, ngx_log_t *log);
14
15 #else
16
17 #define ngx_setaffinity(cpu_affinity, log)
18
19 #endif
20
21
22 #endif /* _NGX_SETAFFINITY_H_INCLUDED_ */