Skip to content

Instantly share code, notes, and snippets.

@nkottary

nkottary/bindtest.c

Created Feb 23, 2016
Embed
What would you like to do?
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include "zmq.h"
#include <string.h>
/* Look for leaks when repeatedly binding.
Compile like this: gcc -I/usr/local/include bindtest.c -L/usr/local/lib -lzmq -g -o bt
*/
int get_vmem_usage ()
{
int pid = getpid(), vmem = 0;
char command[100], path[100];
sprintf(command, "ps -p %d -o vsz | sed -n 2p", pid);
FILE *fp = popen(command, "r");
if (fp == NULL) {
printf("ERROR: Failed to run shell command.\n");
exit(1);
}
while (fgets(path, sizeof(path)-1, fp) != NULL) {
sscanf(path, "%d", &vmem);
}
pclose(fp);
return vmem;
}
void show_version()
{
int major, minor, patch;
major = minor = patch = 0;
zmq_version(&major, &minor, &patch);
printf("Version is %d-%d-%d\n", major, minor, patch);
}
int main ()
{
void *ctx = NULL;
void *socket = NULL;
int rc = 0, i = 0;
char strdata[] = "abcdefghijklmnopqrstuvwxyz";
const char socket_name[] = "ipc:///tmp/abcd";
show_version();
ctx = zmq_ctx_new();
if (ctx == NULL) {
printf("ERROR: Failed to create context.\n");
exit(1);
}
socket = zmq_socket(ctx, ZMQ_PUB);
if (socket == NULL) {
printf("ERROR: Failed to create socket.\n");
exit(1);
}
while (i < 10000000) {
rc = zmq_bind(socket, socket_name);
if (rc != 0) {
printf("ERROR: Failed to bind to socket.\n");
exit(1);
}
// rc = zmq_unbind(socket, socket_name);
// if (rc != 0) {
// printf("ERROR: Failed to unbind socket.\n");
// exit(1);
// }
if (i % 100000 == 0) {
printf("VMEM usage is: %d\n", get_vmem_usage());
}
}
rc = zmq_close(socket);
if (rc != 0) {
printf("ERROR: Failed to close socket.\n");
exit(1);
}
rc = zmq_ctx_destroy(ctx);
if (rc != 0) {
printf("ERROR: Failed to destroy context.\n");
exit(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.