|
|
@@ -0,0 +1,162 @@
|
|
|
+/* vccd/monitor.c
|
|
|
+ *
|
|
|
+ * This file is part of vccd.
|
|
|
+ *
|
|
|
+ * vccd is free software: you can redistribute it and/or modify
|
|
|
+ * it under the terms of the GNU General Public License as published by
|
|
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
|
+ * (at your option) any later version.
|
|
|
+ *
|
|
|
+ * vccd is distributed in the hope that it will be useful,
|
|
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
+ * GNU General Public License for more details.
|
|
|
+ *
|
|
|
+ * You should have received a copy of the GNU General Public License
|
|
|
+ * along with vccd. If not, see <https://www.gnu.org/licenses/>
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+#include <stdio.h>
|
|
|
+#include <stdlib.h>
|
|
|
+#include <unistd.h>
|
|
|
+#include <fcntl.h>
|
|
|
+#include <string.h>
|
|
|
+#include <sys/times.h>
|
|
|
+
|
|
|
+#include <config.h>
|
|
|
+#include <vcc/vccd.h>
|
|
|
+
|
|
|
+
|
|
|
+int allocated, current;
|
|
|
+extern int nusers;
|
|
|
+
|
|
|
+#if CONFIG_ADVANCED_AMALLOC
|
|
|
+
|
|
|
+struct klist_node amalloc_log = KLIST_NODE_INIT(&amalloc_log);
|
|
|
+
|
|
|
+#endif
|
|
|
+
|
|
|
+struct cmdtab {
|
|
|
+ char *cmd;
|
|
|
+ int (* do_cmd) (char *);
|
|
|
+} cmdtable[] = {
|
|
|
+ { "mminfo", amalloc_report },
|
|
|
+ { NULL, NULL }
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+int monitor_cmd(void) {
|
|
|
+ char buf[128];
|
|
|
+
|
|
|
+ read(0, buf, 128);
|
|
|
+
|
|
|
+#if CONFIG_ENABLE_MONITOR
|
|
|
+
|
|
|
+ struct cmdtab *c;
|
|
|
+
|
|
|
+ for (c = cmdtable; c->cmd; c++)
|
|
|
+ if (!strncmp(c->cmd, buf, strlen(c->cmd)))
|
|
|
+ return c->do_cmd(buf);
|
|
|
+
|
|
|
+ vcc_log("*** unknow command '%s'", buf);
|
|
|
+
|
|
|
+ return 1;
|
|
|
+#else
|
|
|
+ vcc_log("--- monitor on stdin disabled. \n");
|
|
|
+
|
|
|
+ return 0;
|
|
|
+#endif
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void *__amalloc(int nbytes, const char *fn) {
|
|
|
+ void *p;
|
|
|
+
|
|
|
+ current += nbytes;
|
|
|
+ allocated += nbytes;
|
|
|
+
|
|
|
+ if (unlikely(!(p = malloc(nbytes)))) {
|
|
|
+ vcc_log("*** malloc() failed\n");
|
|
|
+ amalloc_report(NULL);
|
|
|
+
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+#if CONFIG_ADVANCED_AMALLOC
|
|
|
+ struct klist_node *n;
|
|
|
+
|
|
|
+ if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
|
|
|
+ vcc_log("*** malloc() failed. \n");
|
|
|
+
|
|
|
+ return NULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ klist_init(n);
|
|
|
+
|
|
|
+ n->data = (char *) fn;
|
|
|
+ n->sending = (void *) p;
|
|
|
+ n->value = nbytes;
|
|
|
+ n->time = times(NULL);
|
|
|
+
|
|
|
+ klist_add(&amalloc_log, n);
|
|
|
+#else
|
|
|
+ (void) fn;
|
|
|
+#endif
|
|
|
+
|
|
|
+ return p;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int afree(void *p, int nbytes) {
|
|
|
+ current -= nbytes;
|
|
|
+ free(p);
|
|
|
+
|
|
|
+#if CONFIG_ADVANCED_AMALLOC
|
|
|
+ struct klist_node *n;
|
|
|
+
|
|
|
+ for (n = amalloc_log.next; n != &amalloc_log; n = n->next)
|
|
|
+ if (n->sending == p)
|
|
|
+ break;
|
|
|
+
|
|
|
+ if (unlikely(n == &amalloc_log)) {
|
|
|
+ vcc_log("*** broken amalloc_log. \n");
|
|
|
+
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ klist_del(&amalloc_log, n);
|
|
|
+ free(n);
|
|
|
+
|
|
|
+#endif
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int amalloc_report(char *s) {
|
|
|
+ (void) s;
|
|
|
+
|
|
|
+ printf("amalloc report: \n");
|
|
|
+
|
|
|
+ printf("\tallocated\tused\n");
|
|
|
+ printf("\t%d\t\t%d\n", allocated, current);
|
|
|
+ printf("\t%d KB\t\t%d KB\n", allocated >> 10, current >> 10);
|
|
|
+
|
|
|
+#if CONFIG_ADVANCED_AMALLOC
|
|
|
+ struct klist_node *n;
|
|
|
+
|
|
|
+ printf("\nadvanced amalloc report: \n");
|
|
|
+
|
|
|
+ for (n = amalloc_log.next; n != &amalloc_log; n = n->next)
|
|
|
+ printf("\t%s: %ld bytes %ld KB\n", (char *) n->data, n->value, (n->value >> 10));
|
|
|
+#endif
|
|
|
+
|
|
|
+ printf("\ntotal: %d users online. \n", nusers);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|