Measuring total traffic over a connection

Sam Varshavchik <[email protected]> Tue, 03 Dec 2019 23:12:26 -0500
Newsgroups gmane.comp.freedesktop.xcb
Message-ID <[email protected]>
I would like to be able to keep an eye out on how many bytes are  
read/written over a connection. I need this for diagnostic reasons.

I threw something together after looking over the source, see below; I think  
this is what I need. I'll appreciate if someone would look it over and let  
me know if I'm on the right track, before I sink a little bit of time making  
a custom build of libxcb and installing it. I think this might be of some  
general usefulness, so I'm happy to share it:


--- src/xcb_conn.c.orig	2018-09-27 08:02:28.000000000 -0400
+++ src/xcb_conn.c	2019-12-03 23:07:27.947188219 -0500
@@ -287,6 +287,7 @@
         return 0;
     }

+    c->out.total_written += n;
     for(; *count; --*count, ++*vector)
     {
         int cur = (*vector)->iov_len;
@@ -528,3 +529,30 @@

     return ret;
 }
+
+uint64_t xcb_total_read(xcb_connection_t *c)
+{
+    uint64_t n;
+
+    if (xcb_connection_has_error(c))
+        return 0;
+
+    pthread_mutex_lock(&c->iolock);
+    n=c->in.total_read;
+    pthread_mutex_unlock(&c->iolock);
+    return n;
+}
+
+uint64_t xcb_total_written(xcb_connection_t *c)
+{
+    uint64_t n;
+
+    if (xcb_connection_has_error(c))
+        return 0;
+
+    pthread_mutex_lock(&c->iolock);
+    n=c->out.total_written;
+    pthread_mutex_unlock(&c->iolock);
+
+    return n;
+}
--- src/xcb_in.c.orig	2018-09-27 08:02:28.000000000 -0400
+++ src/xcb_in.c	2019-12-03 23:07:47.334860323 -0500
@@ -1025,6 +1025,7 @@
             }
         }
 #endif
+        c->in.total_read += n;
         c->in.queue_len += n;
     }
     while(read_packet(c))
--- src/xcbint.h.orig	2018-09-27 08:02:28.000000000 -0400
+++ src/xcbint.h	2019-12-03 23:07:27.954188100 -0500
@@ -103,6 +103,7 @@

     uint64_t request;
     uint64_t request_written;
+    uint64_t total_written;

     pthread_mutex_t reqlenlock;
     enum lazy_reply_tag maximum_request_length_tag;
@@ -135,6 +136,7 @@
     uint64_t request_expected;
     uint64_t request_read;
     uint64_t request_completed;
+    uint64_t total_read;
     struct reply_list *current_reply;
     struct reply_list **current_reply_tail;

--- src/xproto.h.orig	2019-12-03 23:06:00.984658993 -0500
+++ src/xproto.h	2019-12-03 23:07:27.955188083 -0500
@@ -12684,6 +12684,33 @@
 xcb_void_cookie_t
 xcb_no_operation (xcb_connection_t *c);

+/**
+ *
+ * @param c The connection
+ * @return Number of bytes read from the server.
+ *
+ * Returns cumulative number of bytes received from the connection.
+ *
+ * This retrieves the total number of bytes read from this connection,
+ * to be used for diagnostic/monitoring/informative purposes.
+ */
+
+uint64_t
+xcb_total_read(xcb_connection_t *);
+
+/**
+ *
+ * @param c The connection
+ * @return Number of bytes written to the server.
+ *
+ * Returns cumulative number of bytes sent to the connection.
+ *
+ * This retrieves the total number of bytes written to this connection,
+ * to be used for diagnostic/monitoring/informative purposes.
+ */
+
+uint64_t
+xcb_total_written(xcb_connection_t *);

 #ifdef __cplusplus
 }

_______________________________________________
Xcb mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/xcb
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEMWrVnbBKLOeG9ifkazpiviedvyUFAl3nMioACgkQazpivied
vyW+ORAAj4b8K45uz3aEuJK/GNwg+L/MxtByHvH6pYGjJcInz1P5He+YXERaouzI
9CWuTAHZZQLviCWrN3lo0enSROER5I3V/ChzYuFt3kQbOcGk3AYp7Z9zJXmfKJtu
LPVg818nXJOaRO5+X5IIjHZMx8w5cCcbabsvz6+RS6/+vk7JPCJd4BDX54cmNvP7
Q8Fpg6OaensIozHW219BXpoF1k24ZPxaMcsnb77UIq+zEtoNph9873K0ugG8VIR1
yvmgzettZsLTbgEhh6YO1nwTlH/58qN5StBLiOoAyEHbbatWQqLrbeIP7iHRYikD
ff8ca0Ixp7+k6b2GC+xfoAyxfOCxmHYpfLwf3Wl209yFCZZ6RWCcdrHVLvUUM+CS
QUQHmejTeUl6DaNxGCm/9UX53Sd/H798EjYQ9CRiUwdHx5aNrU+aA2VyourcI8cs
7fJVzXTuGRrRdMp2gZRb2iM5dG7mPA6y5FOTYPexuhm6BXs2tlUgI+ecpnLq7i9M
I420aevup62PL5g8vbEDMbrQVrrOcnK6rLDoi7Wsdj5xTYvfs9xkk6Hy7FPq27yk
fJj254QTivKI8m6aMp67/U16pNFP9xL08iQOSDrBVyPJFnbTAZyR+YVPqKfuF0Zv
oR3+vAVN9riCW4J0GQM3gihXkOMa+dLQ+rdWHOT1yWwM7x0WtMk=
=8hp9
-----END PGP SIGNATURE-----