Skip to content

Commit

Permalink
zeromq dumpstat implementation
Browse files Browse the repository at this point in the history
This tries to do as little as possible on top of zeromq,
which might not work. It sends an "id" field with each
message to keep some state. Probably this should be
inherited through the environment.

Signed-off-by: Jeff King <[email protected]>
  • Loading branch information
peff committed Jan 3, 2025
1 parent 76e6577 commit 08c4ba3
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,7 @@ LIB_OBJS += dumpstat/dumpstat.o
LIB_OBJS += dumpstat/fd.o
LIB_OBJS += dumpstat/file.o
LIB_OBJS += dumpstat/json.o
LIB_OBJS += dumpstat/zeromq.o
LIB_OBJS += editor.o
LIB_OBJS += entry.o
LIB_OBJS += environment.o
Expand Down Expand Up @@ -2164,6 +2165,11 @@ ifdef HAVE_BSD_KERN_PROC_SYSCTL
BASIC_CFLAGS += -DHAVE_BSD_KERN_PROC_SYSCTL
endif

ifdef USE_DUMPSTAT_ZEROMQ
EXTLIBS += -lzmq
COMPAT_CFLAGS += -DDUMPSTAT_ZEROMQ
endif

ifdef HAVE_GETDELIM
BASIC_CFLAGS += -DHAVE_GETDELIM
endif
Expand Down
2 changes: 2 additions & 0 deletions dumpstat/dumpstat.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ static struct dumpstat_writer *parse_writer(const char *s)
return dumpstat_to_fd(s + 3);
else if (isdigit(s[0]))
return dumpstat_to_fd(s);
else if (starts_with(s, "zeromq:"))
return dumpstat_to_zeromq(s + 7);

warning("unknown dumpstat type: %s", s);
return NULL;
Expand Down
1 change: 1 addition & 0 deletions dumpstat/dumpstat.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct dumpstat_writer {
};
struct dumpstat_writer *dumpstat_to_file(const char *path);
struct dumpstat_writer *dumpstat_to_fd(const char *desc);
struct dumpstat_writer *dumpstat_to_zeromq(const char *endpoint);

struct dumpstat_formatter {
void (*start)(void);
Expand Down
55 changes: 55 additions & 0 deletions dumpstat/zeromq.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "cache.h"
#include "dumpstat/dumpstat.h"

#ifndef DUMPSTAT_ZEROMQ
struct dumpstat_writer *dumpstat_to_zeromq(const char *endpoint)
{
warning("zeromq dumpstat support not built");
return NULL;
}

#else

#include <zmq.h>

static char id[256];
static void *context;
static void *mq;

static int dumpstat_zeromq_write(const char *buf, size_t len)
{
zmq_msg_t msg;

zmq_msg_init_size(&msg, len);
memcpy(zmq_msg_data(&msg), buf, len);
if (zmq_send(mq, &msg, 0) < 0) {
warning("unable to write to zeromq: %s", strerror(errno));
return -1;
}
return 0;
}

struct dumpstat_writer *dumpstat_to_zeromq(const char *endpoint)
{
static struct dumpstat_writer writer = {
dumpstat_zeromq_write
};
int len;

len = snprintf(id, sizeof(id), "%d@", (int)getpid());
if (gethostname(id + len, sizeof(id) - len) < 0) {
warning("unable to gethostname: %s", strerror(errno));
return NULL;
}

context = zmq_init(1);
mq = zmq_socket(context, ZMQ_PUB);
if (!mq || zmq_connect(mq, endpoint) < 0) {
warning("unable to open zeromq socket: %s", strerror(errno));
return NULL;
}

return &writer;
}

#endif /* DUMPSTAT_ZEROMQ */

0 comments on commit 08c4ba3

Please sign in to comment.