Browse Source

Use the new stats features in PyFCGI

The worker indicate each new request using semaphore
Yann Weber 4 years ago
parent
commit
7b99f5f48f
4 changed files with 57 additions and 14 deletions
  1. 11
    0
      include/monitor.h
  2. 12
    10
      src/main.c
  3. 32
    4
      src/monitor.c
  4. 2
    0
      src/pyworker.c

+ 11
- 0
include/monitor.h View File

@@ -43,6 +43,7 @@ typedef union pyfcgi_monitor_addr_u pyfcgi_monitor_addr_t;
43 43
 
44 44
 #include "conf.h"
45 45
 #include "logger.h"
46
+#include "stats.h"
46 47
 
47 48
 union pyfcgi_monitor_addr_u
48 49
 {
@@ -56,8 +57,18 @@ struct pyfcgi_monitor_s
56 57
 	int sockserv;
57 58
 	int sockargs[3];
58 59
 	pyfcgi_monitor_addr_t addr;
60
+
59 61
 };
60 62
 
63
+/**@brief Attempt to init unitialized IPC component
64
+ *
65
+ * If an error occured it will be logged, and the init should be retry latter.
66
+ *
67
+ * @note the monitor process uses IPC_WREQS to count requests IPC_SEMST for
68
+ * SHM syn and IPC_SHMST for SHM initalization
69
+ */
70
+int pyfcgi_monitor_IPC_init();
71
+
61 72
 /**@brief Start the stats server monitoring server 
62 73
  * @return PID of the child process and -1 on error
63 74
  * @note When called the configuration has to be parsed */

+ 12
- 10
src/main.c View File

@@ -146,22 +146,22 @@ int main(int argc, char **argv)
146 146
 
147 147
 	while(1)
148 148
 	{
149
-		if(PyFCGI_conf.mon_socket && !monitor_serv_pid)
149
+		if(!pool_handler_pid)
150 150
 		{
151
-			monitor_serv_pid = pyfcgi_spawn_monitor();
152
-			if(monitor_serv_pid < 0)
151
+			pool_handler_pid = spawn_pool_handler();
152
+			if(pool_handler_pid < 0)
153 153
 			{
154 154
 				fails = 1;
155
-				monitor_serv_pid = 0;
155
+				pool_handler_pid = 0;
156 156
 			}
157 157
 		}
158
-		if(!pool_handler_pid)
158
+		if(PyFCGI_conf.mon_socket && !monitor_serv_pid)
159 159
 		{
160
-			pool_handler_pid = spawn_pool_handler();
161
-			if(pool_handler_pid < 0)
160
+			monitor_serv_pid = pyfcgi_spawn_monitor();
161
+			if(monitor_serv_pid < 0)
162 162
 			{
163 163
 				fails = 1;
164
-				pool_handler_pid = 0;
164
+				monitor_serv_pid = 0;
165 165
 			}
166 166
 		}
167 167
 		if(fails)
@@ -229,8 +229,10 @@ int main(int argc, char **argv)
229 229
 				child_name, WEXITSTATUS(child_ret));
230 230
 			if(WIFSIGNALED(child_ret))
231 231
 			{
232
-				pyfcgi_log(LOG_ERR, "%s terminated by sig '%d'",
233
-					child_name, WTERMSIG(child_ret));
232
+				pyfcgi_log(LOG_ERR, "%s terminated by sig %s(%d)",
233
+					child_name,
234
+					strsignal(WTERMSIG(child_ret)),
235
+					WTERMSIG(child_ret));
234 236
 			}
235 237
 		}
236 238
 		else

+ 32
- 4
src/monitor.c View File

@@ -61,6 +61,23 @@ pid_t pyfcgi_spawn_monitor()
61 61
 	return res;
62 62
 }
63 63
 
64
+int pyfcgi_monitor_IPC_init()
65
+{
66
+	int ret;
67
+	ret = 0;
68
+	if(!PyFCGI_SEM_OPEN(SEM_WREQS))
69
+	{
70
+		pyfcgi_IPC_init(IPC_WREQS);
71
+		ret = -1;
72
+	}
73
+	if(!PyFCGI_SEM_OPEN(SEM_STATS))
74
+	{
75
+		pyfcgi_IPC_init(IPC_SEMST);
76
+		ret = -1;
77
+	}
78
+	//pyfcgi_IPC_init(IPC_SHMST);
79
+	return ret;
80
+}
64 81
 
65 82
 void pyfcgi_monitor_loop()
66 83
 {
@@ -110,7 +127,11 @@ void pyfcgi_monitor_loop()
110 127
 		clean_exit(PYFCGI_ERR);
111 128
 	}
112 129
 	pyfcgi_log(LOG_INFO, "Listening on %s", PyFCGI_conf.mon_socket);
130
+
113 131
 	//bind successfull, sending to appropriate response loop
132
+	pyfcgi_monitor_IPC_init(); //check returned value !
133
+	pyfcgi_stats_init();
134
+
114 135
 	if(pyfcgi_mon.sockargs[1] == SOCK_DGRAM)
115 136
 	{
116 137
 		pyfcgi_monitor_dgram_loop();
@@ -125,7 +146,7 @@ void pyfcgi_monitor_stream_loop(pyfcgi_monitor_addr_t addr_serv)
125 146
 {
126 147
 	pyfcgi_monitor_addr_t cliaddr;
127 148
 	socklen_t addrlen;
128
-	int sockcli;
149
+	int sockcli, err;
129 150
 	char ipstr[64];
130 151
 	char name[] = "PYFCGI_"VERSION"\n";
131 152
 
@@ -136,10 +157,17 @@ void pyfcgi_monitor_stream_loop(pyfcgi_monitor_addr_t addr_serv)
136 157
 			strerror(errno));
137 158
 		clean_exit(PYFCGI_ERR);
138 159
 	}
139
-
140
-	while( (sockcli = accept(pyfcgi_mon.sockserv,
141
-		(struct sockaddr*)&cliaddr, &addrlen)) >= 0)
160
+	
161
+	while(1)
142 162
 	{
163
+		sockcli = accept(pyfcgi_mon.sockserv,
164
+			(struct sockaddr*)&cliaddr, &addrlen);
165
+		if(sockcli < 0)
166
+		{
167
+			err = errno;
168
+			if(err == EINTR) { continue; }
169
+			break;
170
+		}
143 171
 		pyfcgi_log(LOG_DEBUG, "New client");
144 172
 		if(!inet_ntop(pyfcgi_mon.sockargs[0], (void*)&cliaddr, ipstr, 64))
145 173
 		{

+ 2
- 0
src/pyworker.c View File

@@ -80,6 +80,7 @@ int work333(int wrk_id)
80 80
 		FCGX_Accept(&in_stream, &out_stream, &err_stream, &envp) >= 0)
81 81
 	{
82 82
 		pyfcgi_wd_arm();
83
+		sem_post(PyFCGI_SEM(SEM_WREQS).sem); // increment request counter
83 84
 		gettimeofday(&start, NULL);
84 85
 		worker_set_busy();
85 86
 		count++;
@@ -254,6 +255,7 @@ int work(int wrk_id)
254 255
 	while ((!count || count != max_reqs) && FCGX_Accept(&in_stream, &out_stream, &err_stream, &envp) >= 0)
255 256
 	{
256 257
 		pyfcgi_wd_arm();
258
+		sem_post(PyFCGI_SEM(SEM_WREQS).sem); // increment request counter
257 259
 		gettimeofday(&start, NULL);
258 260
 		worker_set_busy();
259 261
 

Loading…
Cancel
Save