Hi PHK and varnish-dev,

Attached is a patch that will add derived values to the stat counters
reported by VSC in libvarnishapi. It also adds a derived counter called
s_avoidedbytes, which shows a rough estimate of how many bytes Varnish has
saved the backend servers from dealing with.

Any comments are appreciated.

Regards,
Martin Blix Grydeland

-- 
Martin Blix Grydeland
Varnish Software AS
diff --git a/include/vsc_fields.h b/include/vsc_fields.h
index dd68151..8536937 100644
--- a/include/vsc_fields.h
+++ b/include/vsc_fields.h
@@ -34,6 +34,11 @@
 
 /**********************************************************************/
 
+#ifndef VSC_D
+#define UNDEF_VSC_D 1
+#define VSC_D(a,b,c,d,e)
+#endif
+
 #ifdef VSC_DO_MAIN
 
 VSC_F(client_conn,		uint64_t, 0, 'a', "Client connections accepted")
@@ -106,6 +111,7 @@ VSC_F(s_pass,		uint64_t, 1, 'a', "Total pass")
 VSC_F(s_fetch,		uint64_t, 1, 'a', "Total fetch")
 VSC_F(s_hdrbytes,		uint64_t, 1, 'a', "Total header bytes")
 VSC_F(s_bodybytes,		uint64_t, 1, 'a', "Total body bytes")
+VSC_D(s_avoidedbytes,		uint64_t, 0, 'a', "Avoided backend bytes (derived)")
 
 VSC_F(sess_closed,		uint64_t, 1, 'a', "Session Closed")
 VSC_F(sess_pipeline,	uint64_t, 1, 'a', "Session Pipeline")
@@ -206,3 +212,7 @@ VSC_F(happy,		uint64_t, 0, 'b', "Happy health probes")
 
 #endif
 
+#ifdef UNDEF_VSC_D
+#undef VSC_D
+#undef UNDEF_VSC_D
+#endif
diff --git a/lib/libvarnishapi/vsc.c b/lib/libvarnishapi/vsc.c
index f912514..dc94e11 100644
--- a/lib/libvarnishapi/vsc.c
+++ b/lib/libvarnishapi/vsc.c
@@ -70,6 +70,7 @@ struct vsc {
 
 };
 
+uint64_t s_avoidedbytes = 0;
 
 /*--------------------------------------------------------------------*/
 
@@ -283,10 +284,28 @@ iter_call(const struct vsc *vsc, vsc_iter_f *func, void *priv,
 	return (func(priv, sp));
 }
 
+static int
+iter_d_s_avoidedbytes(struct VSM_data *vd, const struct vsc *vsc,
+		      vsc_iter_f *func, void *priv, struct vsc_point *const sp)
+{
+	const struct vsc_main *vsc_main;
+	uint64_t avoided = 0;
+
+	vsc_main = VSC_Main(vd);
+	if (vsc_main->s_req > 0) {
+		avoided = vsc_main->s_hdrbytes + vsc_main->s_bodybytes;
+		avoided *= vsc_main->cache_hit;
+		avoided /= vsc_main->s_req;
+	}
+	s_avoidedbytes = avoided;
+	sp->ptr = &s_avoidedbytes;
+	return (iter_call(vsc, func, priv, sp));
+}
+
 #define VSC_DO(U,l,t)							\
 	static int							\
-	iter_##l(const struct vsc *vsc, struct vsm_chunk *sha,		\
-	    vsc_iter_f *func, void *priv)				\
+	iter_##l(struct VSM_data *vd, const struct vsc *vsc,		\
+	    struct vsm_chunk *sha, vsc_iter_f *func, void *priv)	\
 	{								\
 		struct vsc_##l *st;					\
 		struct vsc_point sp;					\
@@ -308,6 +327,15 @@ iter_call(const struct vsc *vsc, vsc_iter_f *func, void *priv,
 		if (i)							\
 			return(i);
 
+#define VSC_D(nn,tt,ll,ff,dd)						\
+		sp.name = #nn;						\
+		sp.fmt = #tt;						\
+		sp.flag = ff;						\
+		sp.desc = dd;						\
+		i = iter_d_##nn(vd, vsc, func, priv, &sp);		\
+		if (i)							\
+			return (i);
+
 #define VSC_DONE(U,l,t)							\
 		return (0);						\
 	}
@@ -315,6 +343,7 @@ iter_call(const struct vsc *vsc, vsc_iter_f *func, void *priv,
 #include "vsc_all.h"
 #undef VSC_DO
 #undef VSC_F
+#undef VSC_D
 #undef VSC_DONE
 
 int
@@ -335,15 +364,17 @@ VSC_Iter(struct VSM_data *vd, vsc_iter_f *func, void *priv)
 			continue;
 
 #define VSC_F(a,b,c,d,e)
+#define VSC_D(a,b,c,d,e)
 #define VSC_DONE(a,b,c)
 #define VSC_DO(U,l,t)						\
 		if (!strcmp(sha->type, t)) {			\
-			i = iter_##l(vsc, sha, func, priv);	\
+			i = iter_##l(vd, vsc, sha, func, priv);	\
 			if (!i)					\
 				continue;			\
 		}
 #include "vsc_all.h"
 #undef VSC_F
+#undef VSC_D
 #undef VSC_DO
 #undef VSC_DONE
 		break;
_______________________________________________
varnish-dev mailing list
[email protected]
http://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev

Reply via email to