Browse Source

Removed useless argument to average calculation function

Defining a PYFCGI_STATS_SZ = 15 * 60s = 900
Yann Weber 4 years ago
parent
commit
fc0945d891
2 changed files with 24 additions and 16 deletions
  1. 12
    3
      include/stats.h
  2. 12
    13
      src/stats.c

+ 12
- 3
include/stats.h View File

@@ -28,7 +28,7 @@
28 28
 #include <signal.h>
29 29
 #include <string.h>
30 30
 
31
-#define PYFCGI_STATS_REQS_SAMPLES (900)
31
+#define PYFCGI_STATS_SZ (900)
32 32
 
33 33
 typedef struct pyfcgi_stats_s pyfcgi_stats_t;
34 34
 
@@ -38,7 +38,7 @@ typedef struct pyfcgi_stats_s pyfcgi_stats_t;
38 38
 struct pyfcgi_stats_s
39 39
 {
40 40
 	/**@brief Request per seconds on 15 minutes */
41
-	int reqs[PYFCGI_STATS_REQS_SAMPLES];
41
+	int reqs[PYFCGI_STATS_SZ];
42 42
 	/**@brief Current request index */
43 43
 	int cur_req;
44 44
 
@@ -83,7 +83,16 @@ void pyfcgi_stats_buffprintf(const char *fmt, ...);
83 83
  */
84 84
 int pyfcgi_stats_reqbuff(size_t);
85 85
 
86
-int pyfcgi_stats_avg(const int[], int*, int, int*, double[4]);
86
+/**@brief Returns statistics about a 900s ringbuffer
87
+ *
88
+ * @param const int[PYFCGI_STATS_SZ] data : a 900 ringbuffer stats samples
89
+ * @param int* idx_nxt next data index in ring buffer (allow interrupt
90
+ * detection : when *idx_nxt changes)
91
+ * @param int *last : last sample
92
+ * @param double[4] avgs : average samples on 1, 5, 10 and 15 minutes
93
+ * @return 0 if no error else -1 and set errno to EINTR
94
+ */
95
+int pyfcgi_stats_avg(const int[PYFCGI_STATS_SZ], int*, int*, double[4]);
87 96
 
88 97
 /**@brief Returns the formated buffer
89 98
  * @todo make @ref pyfcgi_stats_format() implement this functionnality

+ 12
- 13
src/stats.c View File

@@ -65,7 +65,7 @@ void pyfcgi_stats_collector(int signum)
65 65
 	pyfcgi_log(LOG_DEBUG, "s#%d %d req/s", pyfcgi_stats.cur_req,
66 66
 		pyfcgi_stats.reqs[stats->cur_req]);
67 67
 	stats->cur_req++;
68
-	stats->cur_req %= PYFCGI_STATS_REQS_SAMPLES;
68
+	stats->cur_req %= PYFCGI_STATS_SZ;
69 69
 
70 70
 	return;
71 71
 }
@@ -114,16 +114,15 @@ size_t pyfcgi_stats_format()
114 114
 	do
115 115
 	{
116 116
 		ret = pyfcgi_stats_avg(pyfcgi_stats.reqs,
117
-			&(pyfcgi_stats.cur_req), PYFCGI_STATS_REQS_SAMPLES,
118
-			&last_rs, avgs);
117
+			&(pyfcgi_stats.cur_req), &last_rs, avgs);
119 118
 	} while(ret < 0 && errno == EINTR);
120 119
 	pyfcgi_stats_buffprintf("Requests stats :\n1s:%dr/s 1m:%.2fr/s 5m:%.2fr/s 10m:%.2fr/s 15m:%.2fr/s\n",
121 120
 		last_rs, avgs[0], avgs[1], avgs[2], avgs[3]);
122 121
 	return pyfcgi_stats.buff_ptr;
123 122
 }
124 123
 
125
-int pyfcgi_stats_avg(const int data[], int *idx_first, int len,
126
-	int *last, double avgs[4])
124
+int pyfcgi_stats_avg(const int data[PYFCGI_STATS_SZ], int *idx_nxt, int *last,
125
+	double avgs[4])
127 126
 {
128 127
 	double r15, r10, r5, r1, rtmp;
129 128
 	unsigned long stmp; // stores a 60s req sum
@@ -131,23 +130,23 @@ int pyfcgi_stats_avg(const int data[], int *idx_first, int len,
131 130
 	int uptime, max;
132 131
 
133 132
 	uptime = time(NULL) - PyFCGI_conf.context.uptime;
134
-	max = (uptime > len) ? len:uptime;
133
+	max = (uptime > PYFCGI_STATS_SZ) ? PYFCGI_STATS_SZ:uptime;
135 134
 
136
-	r15 = r10 = r5 = r1 = stmp = 0;
137
-	first = *idx_first;
135
+	r15 = r10 = r5 = r1 = stmp = rtmp = 0;
136
+	first = *idx_nxt;
138 137
 
139 138
 	//Block interrupt/ALARM ??
140 139
 	i = 0;
141 140
 	do
142 141
 	{
143
-		if(first != *idx_first)
142
+		if(first != *idx_nxt)
144 143
 		{
145 144
 			errno = EINTR;
146 145
 			return -1;
147 146
 		}
148
-		idx = (*idx_first- i - 1);
149
-		idx = (idx<0)?len + idx:idx;
150
-		idx %= len;
147
+		idx = (*idx_nxt- i - 1);
148
+		idx = (idx<0)?PYFCGI_STATS_SZ + idx:idx;
149
+		idx %= PYFCGI_STATS_SZ;
151 150
 		cur = data[idx];
152 151
 		if((!i || i%60) && i < max-1)
153 152
 		{
@@ -164,7 +163,7 @@ int pyfcgi_stats_avg(const int data[], int *idx_first, int len,
164 163
 		i++;
165 164
 	}while(i<max);
166 165
 	//Restore interrupt/ALARM ??
167
-	*last = data[*idx_first-1];
166
+	*last = data[*idx_nxt-1];
168 167
 	rtmp = max?rtmp:((double)*last);
169 168
 	r1 = (uptime <= 60)?rtmp:r1;
170 169
 	r5 /= 5;

Loading…
Cancel
Save