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
 
43
 
44
 #include "conf.h"
44
 #include "conf.h"
45
 #include "logger.h"
45
 #include "logger.h"
46
+#include "stats.h"
46
 
47
 
47
 union pyfcgi_monitor_addr_u
48
 union pyfcgi_monitor_addr_u
48
 {
49
 {
56
 	int sockserv;
57
 	int sockserv;
57
 	int sockargs[3];
58
 	int sockargs[3];
58
 	pyfcgi_monitor_addr_t addr;
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
 /**@brief Start the stats server monitoring server 
72
 /**@brief Start the stats server monitoring server 
62
  * @return PID of the child process and -1 on error
73
  * @return PID of the child process and -1 on error
63
  * @note When called the configuration has to be parsed */
74
  * @note When called the configuration has to be parsed */

+ 12
- 10
src/main.c View File

146
 
146
 
147
 	while(1)
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
 				fails = 1;
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
 				fails = 1;
163
 				fails = 1;
164
-				pool_handler_pid = 0;
164
+				monitor_serv_pid = 0;
165
 			}
165
 			}
166
 		}
166
 		}
167
 		if(fails)
167
 		if(fails)
229
 				child_name, WEXITSTATUS(child_ret));
229
 				child_name, WEXITSTATUS(child_ret));
230
 			if(WIFSIGNALED(child_ret))
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
 		else
238
 		else

+ 32
- 4
src/monitor.c View File

61
 	return res;
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
 void pyfcgi_monitor_loop()
82
 void pyfcgi_monitor_loop()
66
 {
83
 {
110
 		clean_exit(PYFCGI_ERR);
127
 		clean_exit(PYFCGI_ERR);
111
 	}
128
 	}
112
 	pyfcgi_log(LOG_INFO, "Listening on %s", PyFCGI_conf.mon_socket);
129
 	pyfcgi_log(LOG_INFO, "Listening on %s", PyFCGI_conf.mon_socket);
130
+
113
 	//bind successfull, sending to appropriate response loop
131
 	//bind successfull, sending to appropriate response loop
132
+	pyfcgi_monitor_IPC_init(); //check returned value !
133
+	pyfcgi_stats_init();
134
+
114
 	if(pyfcgi_mon.sockargs[1] == SOCK_DGRAM)
135
 	if(pyfcgi_mon.sockargs[1] == SOCK_DGRAM)
115
 	{
136
 	{
116
 		pyfcgi_monitor_dgram_loop();
137
 		pyfcgi_monitor_dgram_loop();
125
 {
146
 {
126
 	pyfcgi_monitor_addr_t cliaddr;
147
 	pyfcgi_monitor_addr_t cliaddr;
127
 	socklen_t addrlen;
148
 	socklen_t addrlen;
128
-	int sockcli;
149
+	int sockcli, err;
129
 	char ipstr[64];
150
 	char ipstr[64];
130
 	char name[] = "PYFCGI_"VERSION"\n";
151
 	char name[] = "PYFCGI_"VERSION"\n";
131
 
152
 
136
 			strerror(errno));
157
 			strerror(errno));
137
 		clean_exit(PYFCGI_ERR);
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
 		pyfcgi_log(LOG_DEBUG, "New client");
171
 		pyfcgi_log(LOG_DEBUG, "New client");
144
 		if(!inet_ntop(pyfcgi_mon.sockargs[0], (void*)&cliaddr, ipstr, 64))
172
 		if(!inet_ntop(pyfcgi_mon.sockargs[0], (void*)&cliaddr, ipstr, 64))
145
 		{
173
 		{

+ 2
- 0
src/pyworker.c View File

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

Loading…
Cancel
Save