Allocator_admin.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 <https://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_getIntC(args, "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 bytesAllocated(Dict* in, 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_putIntC(d, "bytes", Allocator_bytesAllocated(ctx->alloc), requestAlloc);
  39. Admin_sendMessage(d, txid, ctx->admin);
  40. }
  41. void Allocator_admin_register(struct Allocator* alloc, struct Admin* admin)
  42. {
  43. struct Allocator_admin_pvt* ctx = Allocator_clone(alloc, (&(struct Allocator_admin_pvt) {
  44. .alloc = alloc,
  45. .admin = admin
  46. }));
  47. Identity_set(ctx);
  48. Admin_registerFunction("Allocator_snapshot", snapshot, ctx, true,
  49. ((struct Admin_FunctionArg[]) {
  50. { .name = "includeAllocations", .required = 0, .type = "Int" }
  51. }), admin);
  52. Admin_registerFunction("Allocator_bytesAllocated", bytesAllocated, ctx, true, NULL, admin);
  53. }