monitor.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* vccd/monitor.c
  2. *
  3. * This file is part of vccd.
  4. *
  5. * vccd is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vccd is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vccd. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <string.h>
  23. #include <sys/times.h>
  24. #include <config.h>
  25. #include <vcc/vccd.h>
  26. int allocated, current;
  27. extern int nusers;
  28. #if CONFIG_ADVANCED_AMALLOC
  29. struct klist_node amalloc_log = KLIST_NODE_INIT(&amalloc_log);
  30. #endif
  31. struct cmdtab {
  32. char *cmd;
  33. int (* do_cmd) (char *);
  34. } cmdtable[] = {
  35. { "mminfo", amalloc_report },
  36. { NULL, NULL }
  37. };
  38. int monitor_cmd(void) {
  39. char buf[128];
  40. read(0, buf, 128);
  41. #if CONFIG_ENABLE_MONITOR
  42. struct cmdtab *c;
  43. for (c = cmdtable; c->cmd; c++)
  44. if (!strncmp(c->cmd, buf, strlen(c->cmd)))
  45. return c->do_cmd(buf);
  46. vcc_log("*** unknow command '%s'", buf);
  47. return 1;
  48. #else
  49. vcc_log("--- monitor on stdin disabled. \n");
  50. return 0;
  51. #endif
  52. }
  53. void *__amalloc(int nbytes, const char *fn) {
  54. void *p;
  55. current += nbytes;
  56. allocated += nbytes;
  57. if (unlikely(!(p = malloc(nbytes)))) {
  58. vcc_log("*** malloc() failed\n");
  59. amalloc_report(NULL);
  60. return NULL;
  61. }
  62. #if CONFIG_ADVANCED_AMALLOC
  63. struct klist_node *n;
  64. if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
  65. vcc_log("*** malloc() failed. \n");
  66. return NULL;
  67. }
  68. klist_init(n);
  69. n->data = (char *) fn;
  70. n->sending = (void *) p;
  71. n->value = nbytes;
  72. n->time = times(NULL);
  73. klist_add(&amalloc_log, n);
  74. #else
  75. (void) fn;
  76. #endif
  77. return p;
  78. }
  79. int afree(void *p, int nbytes) {
  80. current -= nbytes;
  81. free(p);
  82. #if CONFIG_ADVANCED_AMALLOC
  83. struct klist_node *n;
  84. for (n = amalloc_log.next; n != &amalloc_log; n = n->next)
  85. if (n->sending == p)
  86. break;
  87. if (unlikely(n == &amalloc_log)) {
  88. vcc_log("*** broken amalloc_log. \n");
  89. return 1;
  90. }
  91. klist_del(&amalloc_log, n);
  92. free(n);
  93. #endif
  94. return 0;
  95. }
  96. int amalloc_report(char *s) {
  97. (void) s;
  98. printf("amalloc report: \n");
  99. printf("\tallocated\tused\n");
  100. printf("\t%d\t\t%d\n", allocated, current);
  101. printf("\t%d KB\t\t%d KB\n", allocated >> 10, current >> 10);
  102. #if CONFIG_ADVANCED_AMALLOC
  103. struct klist_node *n;
  104. printf("\nadvanced amalloc report: \n");
  105. for (n = amalloc_log.next; n != &amalloc_log; n = n->next)
  106. printf("\t%s: %ld bytes %ld KB\n", (char *) n->data, n->value, (n->value >> 10));
  107. #endif
  108. printf("\ntotal: %d users online. \n", nusers);
  109. return 0;
  110. }