From bf3df84cdfbdb0da776d8fb2af9fa2cf53eec985 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Tue, 16 Jun 2020 22:13:05 +0200 Subject: Convert utils to use debugging --- main.c | 3 ++- utils.c | 49 ++++++++++++++++++++++++------------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/main.c b/main.c index d7d31a5..5296171 100644 --- a/main.c +++ b/main.c @@ -409,7 +409,8 @@ main(int argc, char **argv) xfree(cfg); - debug_resource_usage(); + if (debug_enabled(DBG_MALLOC)) + debug_resource_usage(); fflush(stdout); fflush(stderr); diff --git a/utils.c b/utils.c index 5830dfc..c36d18a 100644 --- a/utils.c +++ b/utils.c @@ -125,21 +125,21 @@ debug_resource_usage() ssize_t r; unsigned file_count = 0; - fprintf(stderr, "Still malloced %i (total %u)\n", - malloc_count, total_malloc_count); + debug(DBG_MALLOC, "Still malloced %i (total %u)\n", + malloc_count, total_malloc_count); list_for_each_entry(a, &malloc_list, list) { - fprintf(stderr, "* Lost allocation - %s:%i - ptr: %p, size: %zu\n", - a->callerfn, a->line, a->ptr, a->size); + debug(DBG_MALLOC, "* Lost allocation - %s:%i - ptr: %p, size: %zu\n", + a->callerfn, a->line, a->ptr, a->size); } dir = opendir("/proc/self/fd"); if (!dir) { - fprintf(stderr, "Failed to open fd dir\n"); + error("failed to open fd dir\n"); return; } - fprintf(stderr, "Open files:\n"); + debug(DBG_MALLOC, "Open files:\n"); while ((dent = readdir(dir)) != NULL) { if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) @@ -147,22 +147,20 @@ debug_resource_usage() r = readlinkat(dirfd(dir), dent->d_name, buf, sizeof(buf)); if (r < 0) { - fprintf(stderr, "Failed to readlink %s\n", dent->d_name); + debug(DBG_MALLOC, "Failed to readlink %s\n", dent->d_name); continue; } buf[r] = '\0'; - fprintf(stderr, " * %s -> %s\n", dent->d_name, buf); + debug(DBG_MALLOC, " * %s -> %s\n", dent->d_name, buf); file_count++; } closedir(dir); - if (file_count > 4) { - fprintf(stderr, "Lost file descriptor(s)\n"); - exit(EXIT_FAILURE); - } + if (file_count > 4) + debug(DBG_MALLOC, "Lost file descriptor(s)\n"); - fprintf(stderr, "CQEs used: %" PRIu64 ", SQEs used: %" PRIu64 "\n", - cqe_count, sqe_count); + debug(DBG_MALLOC, "CQEs used: %" PRIu64 ", SQEs used: %" PRIu64 "\n", + cqe_count, sqe_count); } void @@ -176,17 +174,17 @@ socket_set_low_latency(struct cfg *cfg, int sfd) /* FIXME: could make this configurable */ option = true; if (setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option)) < 0) - perror("setsockopt"); + error("setsockopt: %m"); /* Doubtful if it has much effect, but can't hurt */ option = IPTOS_LOWDELAY; if (setsockopt(sfd, IPPROTO_IP, IP_TOS, &option, sizeof(option)) < 0) - perror("setsockopt"); + error("setsockopt: %m"); /* Nagle's algorithm is a poor fit for gaming */ option = true; if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &option, sizeof(option)) < 0) - perror("setsockopt"); + error("setsockopt: %m"); } void @@ -215,8 +213,8 @@ connect_cb(struct cfg *cfg, struct uring_task *task, int res) struct connection *conn = task->priv; if (res < 0) { - fprintf(stderr, "%s: connection to %s failed\n", - __func__, conn->remotestr); + debug(DBG_UR, "%s: connection to %s failed\n", + task->name, conn->remotestr); uring_task_close_fd(cfg, task); connect_next(cfg, task, conn); return; @@ -224,8 +222,8 @@ connect_cb(struct cfg *cfg, struct uring_task *task, int res) connection_set_local(cfg, conn, task->fd); - fprintf(stderr, "%s: (%s) connection established %s -> %s\n", - __func__, task->name, conn->localstr, conn->remotestr); + debug(DBG_UR, "%s: connection established %s -> %s\n", + task->name, conn->localstr, conn->remotestr); conn->callback(cfg, conn, true); } @@ -249,15 +247,16 @@ again: } if (!remote) { - fprintf(stderr, "%s: no more remote addresses to attempt\n", __func__); + debug(DBG_UR, "%s: no more remote addresses to attempt\n", + task->name); conn->callback(cfg, conn, false); return; } conn->next_addr++; connection_set_remote(cfg, conn, remote); - fprintf(stderr, "%s: attempting to connect to %s\n", - task->name, conn->remotestr); + debug(DBG_MALLOC, "%s: attempting to connect to %s\n", + task->name, conn->remotestr); sfd = socket(conn->remote.storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0); if (sfd < 0) { @@ -278,7 +277,7 @@ connect_any(struct cfg *cfg, struct uring_task *task, void (*callback)(struct cfg *, struct connection *, bool res)) { if (!cfg || !task || !addrs || !conn || !callback) { - fprintf(stderr, "%s: invalid arguments\n", __func__); + error("invalid arguments\n"); return; } -- cgit v1.2.3