Browse Source

Rollback average function

Yann Weber 4 years ago
parent
commit
15e396778b
2 changed files with 53 additions and 1 deletions
  1. 5
    0
      include/stats.h
  2. 48
    1
      src/stats.c

+ 5
- 0
include/stats.h View File

@@ -93,6 +93,11 @@ int pyfcgi_stats_reqbuff(size_t);
93 93
  * @return 0 if no error else -1 and set errno to EINTR
94 94
  */
95 95
 int pyfcgi_stats_avg(const int[PYFCGI_STATS_SZ], int*, int*, double[4]);
96
+/**@brief Same than @ref pyfcgi_stats_avg but normalize average when
97
+ * uptime is bellow 900
98
+ * @see pyfcgi_stats_avg
99
+ */
100
+int pyfcgi_stats_avg_const(const int[PYFCGI_STATS_SZ], int*, int*, double[4]);
96 101
 
97 102
 /**@brief Returns the formated buffer
98 103
  * @todo make @ref pyfcgi_stats_format() implement this functionnality

+ 48
- 1
src/stats.c View File

@@ -123,10 +123,57 @@ size_t pyfcgi_stats_format()
123 123
 
124 124
 int pyfcgi_stats_avg(const int data[PYFCGI_STATS_SZ], int *idx_nxt, int *last,
125 125
 	double avgs[4])
126
+{
127
+	double r15, r10, r5, r1, rtmp;
128
+	unsigned long stmp; // stores a 60s req sum
129
+	int i, cur, idx, first;
130
+
131
+	r15 = r10 = r5 = r1 = stmp = 0;
132
+	first = *idx_nxt;
133
+
134
+	//Block interrupt/ALARM ??
135
+	for(i=0; i<PYFCGI_STATS_SZ; i++)
136
+	{
137
+		if(first != *idx_nxt)
138
+		{
139
+			errno = EINTR;
140
+			return -1;
141
+		}
142
+		idx = (*idx_nxt- i - 1);
143
+		idx = (idx<0)?PYFCGI_STATS_SZ + idx:idx;
144
+		idx %= PYFCGI_STATS_SZ;
145
+		cur = data[idx];
146
+		if(!i || i%60)
147
+		{
148
+			stmp += cur;
149
+			continue;
150
+		}
151
+		rtmp = (long double)stmp / 60;
152
+		if(i==60) { r1 = rtmp; }
153
+		if(i<5*60) { r5 += rtmp; }
154
+		if(i<10*60) { r10 += rtmp; }
155
+		if(i<15*60) { r15 += rtmp; }
156
+		stmp = cur;
157
+	}
158
+	//Restore interrupt/ALARM ??
159
+	r5 /= 5;
160
+	r10 /= 10;
161
+	r15 /= 15;
162
+	*last = data[*idx_nxt-1];
163
+	avgs[0] = r1;
164
+	avgs[1] = r5;
165
+	avgs[2] = r10;
166
+	avgs[3] = r15;
167
+
168
+	return 0;
169
+}
170
+
171
+int pyfcgi_stats_avg_const(const int data[PYFCGI_STATS_SZ], int *idx_nxt, int *last,
172
+	double avgs[4])
126 173
 {
127 174
 	double r15, r10, r5, r1, rtmp, dmax;
128 175
 	unsigned long stmp; // stores a 60s req sum
129
-	int i, cur, idx, first, idiv;
176
+	int i, cur, idx, first;
130 177
 	int uptime, max;
131 178
 
132 179
 	uptime = time(NULL) - PyFCGI_conf.context.uptime;

Loading…
Cancel
Save