summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-10 20:04:01 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-10 20:04:01 +0200
commit11e6254179cb78412f40d2a263bf4fb40dd7f2ff (patch)
treed641877095f16a1c6edb575c41c40b95e43b8004
parent320f600cd980230b2b2e34c7256c9564af6afae9 (diff)
Improve memdebug, add basic proxy stats
-rw-r--r--main.c3
-rw-r--r--proxy.c33
-rw-r--r--proxy.h4
-rw-r--r--server.c2
-rw-r--r--uring.c7
-rw-r--r--utils.c68
-rw-r--r--utils.h14
7 files changed, 93 insertions, 38 deletions
diff --git a/main.c b/main.c
index 4471064..c8e8a8d 100644
--- a/main.c
+++ b/main.c
@@ -236,6 +236,9 @@ hack_handler(int signum)
break;
}
+ if (count > 5)
+ exit(EXIT_FAILURE);
+
write(hack_efd, &val, sizeof(val));
count++;
}
diff --git a/proxy.c b/proxy.c
index 6699c39..c61ad9c 100644
--- a/proxy.c
+++ b/proxy.c
@@ -35,6 +35,19 @@ proxy_server_free(struct uring_task *task)
*/
}
+void
+proxy_delete(struct cfg *cfg, struct server_proxy *proxy)
+{
+ char cts[] = "13kB";
+ char stc[] = "28kB";
+ char duration[] = "00:12:23";
+
+ fprintf(stderr, "%s: would delete proxy 0x%p\n", __func__, proxy);
+
+ fprintf(stderr, "%s: proxy connection %s -> %s closed (CtS: %s, StC: %s), duration %s\n",
+ proxy->scfg->name, proxy->clientstr, proxy->serverstr, cts, stc, duration);
+}
+
static void proxy_client_data_in(struct cfg *cfg, struct uring_task *task, int res);
static void
@@ -43,9 +56,12 @@ proxy_client_data_out(struct cfg *cfg, struct uring_task *task, int res)
struct server_proxy *proxy = container_of(task, struct server_proxy, clienttask);
fprintf(stderr, "%s: result was %i\n", __func__, res);
- if (res <= 0)
+ if (res <= 0) {
+ proxy_delete(cfg, proxy);
return;
+ }
+ proxy->client_bytes += res;
uring_task_set_fd(&proxy->clienttask, proxy->cfd);
uring_tbuf_read(cfg, task, proxy_client_data_in);
}
@@ -56,8 +72,10 @@ proxy_client_data_in(struct cfg *cfg, struct uring_task *task, int res)
struct server_proxy *proxy = container_of(task, struct server_proxy, clienttask);
fprintf(stderr, "%s: result was %i\n", __func__, res);
- if (res <= 0)
+ if (res <= 0) {
+ proxy_delete(cfg, proxy);
return;
+ }
uring_task_set_fd(&proxy->clienttask, proxy->sfd);
uring_tbuf_write(cfg, task, proxy_client_data_out);
@@ -71,9 +89,12 @@ proxy_server_data_out(struct cfg *cfg, struct uring_task *task, int res)
struct server_proxy *proxy = container_of(task, struct server_proxy, servertask);
fprintf(stderr, "%s: result was %i\n", __func__, res);
- if (res <= 0)
+ if (res <= 0) {
+ proxy_delete(cfg, proxy);
return;
+ }
+ proxy->server_bytes += res;
uring_task_set_fd(&proxy->servertask, proxy->sfd);
uring_tbuf_read(cfg, &proxy->servertask, proxy_server_data_in);
}
@@ -84,8 +105,10 @@ proxy_server_data_in(struct cfg *cfg, struct uring_task *task, int res)
struct server_proxy *proxy = container_of(task, struct server_proxy, servertask);
fprintf(stderr, "%s: result was %i\n", __func__, res);
- if (res <= 0)
+ if (res <= 0) {
+ proxy_delete(cfg, proxy);
return;
+ }
uring_task_set_fd(&proxy->servertask, proxy->cfd);
uring_tbuf_write(cfg, task, proxy_server_data_out);
@@ -106,6 +129,8 @@ proxy_server_connected(struct cfg *cfg, struct uring_task *task, int res)
return;
}
+ fprintf(stderr, "%s: proxy connection %s -> %s opened\n",
+ proxy->scfg->name, proxy->clientstr, proxy->serverstr);
uring_tbuf_read(cfg, &proxy->clienttask, proxy_client_data_in);
uring_tbuf_read(cfg, &proxy->servertask, proxy_server_data_in);
}
diff --git a/proxy.h b/proxy.h
index cbd2990..62ac5b7 100644
--- a/proxy.h
+++ b/proxy.h
@@ -6,12 +6,14 @@ struct server_proxy {
char clientstr[ADDRSTRLEN];
struct uring_task_buf clientbuf;
struct uring_task clienttask;
+ uint64_t client_bytes;
int cfd;
struct sockaddr_in46 server;
char serverstr[ADDRSTRLEN];
struct uring_task_buf serverbuf;
struct uring_task servertask;
+ uint64_t server_bytes;
int sfd;
unsigned next_remote;
@@ -21,6 +23,8 @@ struct server_proxy {
void proxy_refdump(struct server_proxy *proxy);
+void proxy_delete(struct cfg *cfg, struct server_proxy *proxy);
+
struct server_proxy *proxy_new(struct cfg *cfg, struct server *scfg, struct sockaddr_in46 *client, int fd);
#endif
diff --git a/server.c b/server.c
index 1750e58..9ce133a 100644
--- a/server.c
+++ b/server.c
@@ -496,7 +496,7 @@ server_commit(struct cfg *cfg, struct server *scfg)
return false;
}
- scfg->pretty_name = strndup(scfg->name, suffix - scfg->name);
+ scfg->pretty_name = xstrndup(scfg->name, suffix - scfg->name);
if (!scfg->pretty_name) {
fprintf(stderr, "%s(%s): failed to create display name\n", __func__, scfg->name);
return false;
diff --git a/uring.c b/uring.c
index 4859ac0..c8e1dbb 100644
--- a/uring.c
+++ b/uring.c
@@ -191,7 +191,7 @@ uring_tbuf_write_cb(struct cfg *cfg, struct uring_task *task, int res)
/* We wrote some more data */
task->tbuf->done += res;
- if (task->tbuf->done >= task->tbuf->len) {
+ if (task->tbuf->done >= task->tbuf->len || res == 0) {
r = task->tbuf->len;
goto finished;
}
@@ -315,7 +315,10 @@ uring_tbuf_read_until_eof(struct cfg *cfg, struct uring_task *task, callback_t c
static int
uring_tbuf_have_data(struct cfg *cfg, struct uring_task *task, int res)
{
- return res;
+ if (res < 0)
+ return res;
+ else
+ return 1;
}
void
diff --git a/utils.c b/utils.c
index b9a4bed..08a5464 100644
--- a/utils.c
+++ b/utils.c
@@ -14,57 +14,72 @@ static int malloc_count = 0;
LIST_HEAD(malloc_list);
struct allocation {
- void *caller;
+ const char *allocfn;
+ const char *callerfn;
+ int line;
void *ptr;
size_t size;
struct list_head list;
};
static void
-__add_allocation(void *caller, void *ptr, size_t size)
+add_allocation(const char *allocfn, const char *callerfn, int line, void *ptr, size_t size)
{
struct allocation *a = malloc(sizeof(*a));
- a->caller = caller;
+ fprintf(stderr, "Allocation: %s:%i %s(%zu) = %p\n",
+ callerfn, line, allocfn, size, ptr);
+ a->allocfn = allocfn;
+ a->callerfn = callerfn;
+ a->line = line;
a->ptr = ptr;
a->size = size;
list_add(&a->list, &malloc_list);
+ total_malloc_count++;
+ malloc_count++;
}
-#define add_allocation(p, s) __add_allocation(__builtin_extract_return_addr(__builtin_return_address(0)), p, s)
-
void *
-__zmalloc(size_t size)
+__zmalloc(const char *fn, int line, size_t size)
{
void *ptr;
- fprintf(stderr, "zmalloc: Allocation size %zi\n", size);
ptr = calloc(1, size);
- if (ptr) {
- add_allocation(ptr, size);
- total_malloc_count++;
- malloc_count++;
- }
+ if (ptr)
+ add_allocation("zmalloc", fn, line, ptr, size);
+ else
+ perrordie("zmalloc");
return ptr;
}
char *
-__xstrdup(const char *s)
+__xstrdup(const char *fn, int line, const char *s)
{
- char *r;
-
- fprintf(stderr, "xstrdup: Allocation size %zi\n", strlen(s) + 1);
- r = strdup(s);
- if (r) {
- add_allocation(r, strlen(s) + 1);
- total_malloc_count++;
- malloc_count++;
- }
- return r;
+ char *ptr;
+
+ ptr = strdup(s);
+ if (ptr)
+ add_allocation("xstrdup", fn, line, ptr, strlen(s) + 1);
+ else
+ perrordie("strdup");
+ return ptr;
+}
+
+char *
+__xstrndup(const char *fn, int line, const char *s, size_t n)
+{
+ char *ptr;
+
+ ptr = strndup(s, n);
+ if (ptr)
+ add_allocation("xstrndup", fn, line, ptr, n);
+ else
+ perrordie("strndup");
+ return ptr;
}
void
-xfree(void *ptr)
+__xfree(const char *fn, int line, void *ptr)
{
struct allocation *a, *tmp;
unsigned delete_count = 0;
@@ -74,6 +89,7 @@ xfree(void *ptr)
free(ptr);
malloc_count--;
+ fprintf(stderr, "Deallocation: %s:%i %p\n", fn, line, ptr);
list_for_each_entry_safe(a, tmp, &malloc_list, list) {
if (a->ptr == ptr) {
list_del(&a->list);
@@ -97,8 +113,8 @@ debug_resource_usage()
malloc_count, total_malloc_count);
list_for_each_entry(a, &malloc_list, list) {
- fprintf(stderr, "* Allocation - caller: 0x%p, ptr: 0x%p, size %zu\n",
- a->caller, a->ptr, a->size);
+ fprintf(stderr, "* Lost allocation - %s:%i - ptr: %p, size: %zu\n",
+ a->callerfn, a->line, a->ptr, a->size);
}
}
diff --git a/utils.h b/utils.h
index 2eeb5d4..52a1078 100644
--- a/utils.h
+++ b/utils.h
@@ -79,13 +79,17 @@ static inline bool list_empty(struct list_head *list)
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
-#define zmalloc(s) ({ fprintf(stderr, "Alloc: zmalloc called from %s: %zu\n", __func__, s); __zmalloc(s); })
-void *__zmalloc(size_t s);
+#define zmalloc(s) __zmalloc(__func__, __LINE__, s)
+void *__zmalloc(const char *fn, int line, size_t s);
-#define xstrdup(s) ({ fprintf(stderr, "Alloc: xstrdup called from %s: %zu\n", __func__, strlen(s) + 1); __xstrdup(s); })
-char *__xstrdup(const char *s);
+#define xstrdup(s) __xstrdup(__func__, __LINE__, s)
+char *__xstrdup(const char *fn, int line, const char *s);
-void xfree(void *ptr);
+#define xstrndup(s, n) __xstrndup(__func__, __LINE__, s, n)
+char *__xstrndup(const char *fn, int line, const char *s, size_t n);
+
+#define xfree(s) __xfree(__func__, __LINE__, s)
+void __xfree(const char *fn, int line, void *ptr);
void debug_resource_usage();