Browse Source

Decrease poll timer & move piper control pipe

Yann Weber 4 years ago
parent
commit
4aa0614383
1 changed files with 46 additions and 14 deletions
  1. 46
    14
      src/pyworker.c

+ 46
- 14
src/pyworker.c View File

@@ -28,6 +28,7 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
28 28
 	int count, pipe_out[2], pipe_err[2], pipe_ctl[2], err, piper_status;
29 29
 	struct sembuf sop;
30 30
 	struct sigaction act;
31
+	struct timeval start, stop;
31 32
 	sigset_t emptyset;
32 33
 	char buf[PIPE_BUF];
33 34
 
@@ -85,9 +86,19 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
85 86
 		       wrk_id, strerror(err));
86 87
 	}
87 88
 
89
+	if(pipe(pipe_ctl) == -1)
90
+	{
91
+		err = errno;
92
+		syslog(LOG_ALERT,
93
+		       "Worker[%d] Pipe fails for piper ctl : %s",
94
+		       wrk_id, strerror(err));
95
+		exit(err);
96
+	}
97
+
88 98
 	count = 0;
89 99
 	while ((!count || count != max_reqs) && FCGI_Accept() >= 0)
90 100
 	{
101
+		gettimeofday(&start, NULL);
91 102
 		sop.sem_op = -1; // decrementing sem to show worker busy
92 103
 		if(semop(semid, &sop, 1) < 0)
93 104
 		{
@@ -103,7 +114,6 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
103 114
 			"Worker[%d] request %d", wrk_id, count);
104 115
 		*/
105 116
 		worker_piper_sigrcv = 0;
106
-		pipe(pipe_ctl); //TODO : check for pipe error
107 117
 		pid_t pid = fork();
108 118
 		if(pid < 0)
109 119
 		{
@@ -122,8 +132,7 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
122 132
 					strerror(err));
123 133
 				exit(err);
124 134
 			}
125
-			close(pipe_ctl[0]);
126
-			close(pipe_ctl[1]);
135
+			write(pipe_ctl[1], " ", 1);
127 136
 	
128 137
 			worker_piper(wrk_id, count, pipe_out[0], pipe_err[0],
129 138
 			             pipe_ctl[1]);
@@ -131,8 +140,7 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
131 140
 			exit(1);
132 141
 		}
133 142
 		update_pyenv(py_osmod);
134
-		//TODO : check if pipe_ctl lock is really needed anymore
135
-		close(pipe_ctl[1]);
143
+		//close(pipe_ctl[1]);
136 144
 		PyObject_CallObject(entry_fun, NULL);
137 145
 		if(PyErr_Occurred())
138 146
 		{
@@ -148,14 +156,31 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
148 156
 		PyObject_CallObject(pystdout_flush, NULL);
149 157
 		PyObject_CallObject(pystderr_flush, NULL);
150 158
 		read(pipe_ctl[0], &buf, 1); // unblock when child ready
159
+		//close(pipe_ctl[0]);
151 160
 
152 161
 		kill(pid, WPIPER_SIG); //indicate child python call ended
162
+/*
163
+gettimeofday(&stop, NULL);
164
+syslog(LOG_DEBUG, "W%dR%dtimer KIL %ld.%06ld us", wrk_id, count, stop.tv_sec - start.tv_sec, stop.tv_usec - start.tv_usec);
165
+*/
153 166
 		waitpid(pid, &piper_status, 0);
154
-		if(piper_status)
167
+/*
168
+gettimeofday(&stop, NULL);
169
+syslog(LOG_DEBUG, "W%dR%dtimer W8  %ld.%06ld us", wrk_id, count, stop.tv_sec - start.tv_sec, stop.tv_usec - start.tv_usec);
170
+*/
171
+		if(WIFSIGNALED(piper_status))
172
+		{
173
+			syslog(LOG_ERR,
174
+			       "Woker[%d] req #%d piper terminated by sig %d",
175
+			       wrk_id, count, WTERMSIG(piper_status));
176
+			exit(40);
177
+		}
178
+		else if(WEXITSTATUS(piper_status))
155 179
 		{
156
-			syslog(LOG_WARNING,
180
+			syslog(LOG_ERR,
157 181
 			       "Woker[%d] req #%d piper exited with error status %d",
158
-			       wrk_id, count, piper_status);
182
+			       wrk_id, count, WEXITSTATUS(piper_status));
183
+			exit(41);
159 184
 		}
160 185
 
161 186
 		FCGI_Finish();
@@ -168,9 +193,16 @@ int work(char* py_entrypoint, int wrk_id, int semid, int max_reqs)
168 193
 			       "Worker[%d] error incrementing the semaphore : %s",
169 194
 			       wrk_id, strerror(err));
170 195
 		}
171
-		
172
-		syslog(LOG_DEBUG, "Worker[%d] request %d END [OK]",
173
-			wrk_id, count);
196
+		gettimeofday(&stop, NULL);
197
+		stop.tv_sec = stop.tv_sec - start.tv_sec;
198
+		stop.tv_usec = stop.tv_usec - start.tv_usec;
199
+		if(stop.tv_usec < 0)
200
+		{
201
+			stop.tv_usec += 1000000;
202
+			stop.tv_sec -= 1;
203
+		}
204
+		syslog(LOG_DEBUG, "Worker[%d] request %d END [OK] in %ld.%06lds",
205
+			wrk_id, count, stop.tv_sec, stop.tv_usec);
174 206
 	}
175 207
 	Py_Exit(count == max_reqs ?0:EXIT_PYERR);
176 208
 }
@@ -179,7 +211,7 @@ void worker_piper(int wrk_id, int req_id, int pystdout, int pystderr,
179 211
 	int ctl_pipe)
180 212
 {
181 213
 	struct pollfd fds[2];
182
-	int err, poll_ret, ret, cont;
214
+	int err, ret, cont, poll_ret;
183 215
 	short revents;
184 216
 	char buf[PIPE_BUF];
185 217
 
@@ -191,10 +223,10 @@ void worker_piper(int wrk_id, int req_id, int pystdout, int pystderr,
191 223
 	fds[0].revents = fds[1].revents = 0;
192 224
 
193 225
 
194
-	cont = 3;
226
+	cont = 2;
195 227
 	while(cont)
196 228
 	{
197
-		poll_ret = poll(fds, 2, 10);
229
+		poll_ret = poll(fds, 2, 0);
198 230
 //syslog(LOG_DEBUG, "Worker[%d] req #%d poll_ret = %d", wrk_id, req_id, poll_ret);
199 231
 		if(poll_ret < 0)
200 232
 		{

Loading…
Cancel
Save