Allocator_admin.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "admin/Admin.h"
  16. #include "benc/String.h"
  17. #include "benc/Dict.h"
  18. #include "memory/Allocator.h"
  19. #include "util/Identity.h"
  20. struct Allocator_admin_pvt
  21. {
  22. struct Allocator* alloc;
  23. struct Admin* admin;
  24. Identity
  25. };
  26. static void snapshot(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  27. {
  28. struct Allocator_admin_pvt* ctx = Identity_check((struct Allocator_admin_pvt*)vcontext);
  29. uint64_t* includeAllocations = Dict_getInt(args, String_CONST("includeAllocations"));
  30. Allocator_snapshot(ctx->alloc, (includeAllocations && *includeAllocations != 0));
  31. Dict d = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST("none")), NULL);
  32. Admin_sendMessage(&d, txid, ctx->admin);
  33. }
  34. static void adminMemory(Dict* input, void* vcontext, String* txid, struct Allocator* requestAlloc)
  35. {
  36. struct Allocator_admin_pvt* ctx = Identity_check((struct Allocator_admin_pvt*)vcontext);
  37. Dict* d = Dict_new(requestAlloc);
  38. Dict_putString(d, String_CONST("deprecationWarning"),
  39. String_CONST("use Allocator_bytesAllocated() instead"), requestAlloc);
  40. Dict_putInt(d, String_CONST("bytes"), Allocator_bytesAllocated(ctx->alloc), requestAlloc);
  41. Admin_sendMessage(d, txid, ctx->admin);
  42. }
  43. static void bytesAllocated(Dict* in, void* vcontext, String* txid, struct Allocator* requestAlloc)
  44. {
  45. struct Allocator_admin_pvt* ctx = Identity_check((struct Allocator_admin_pvt*)vcontext);
  46. Dict* d = Dict_new(requestAlloc);
  47. Dict_putInt(d, String_CONST("bytes"), Allocator_bytesAllocated(ctx->alloc), requestAlloc);
  48. Admin_sendMessage(d, txid, ctx->admin);
  49. }
  50. void Allocator_admin_register(struct Allocator* alloc, struct Admin* admin)
  51. {
  52. struct Allocator_admin_pvt* ctx = Allocator_clone(alloc, (&(struct Allocator_admin_pvt) {
  53. .alloc = alloc,
  54. .admin = admin
  55. }));
  56. Identity_set(ctx);
  57. Admin_registerFunction("Allocator_snapshot", snapshot, ctx, true,
  58. ((struct Admin_FunctionArg[]) {
  59. { .name = "includeAllocations", .required = 0, .type = "Int" }
  60. }), admin);
  61. Admin_registerFunction("Allocator_bytesAllocated", bytesAllocated, ctx, true, NULL, admin);
  62. Admin_registerFunction("memory", adminMemory, ctx, true, NULL, admin);
  63. }