Allocator_admin.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 "memory/Allocator_admin.h"
  20. #include "util/Identity.h"
  21. struct Allocator_admin_pvt
  22. {
  23. struct Allocator* alloc;
  24. struct Admin* admin;
  25. Identity
  26. };
  27. static void snapshot(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  28. {
  29. struct Allocator_admin_pvt* ctx = Identity_check((struct Allocator_admin_pvt*)vcontext);
  30. uint64_t* includeAllocations = Dict_getIntC(args, "includeAllocations");
  31. Allocator_snapshot(ctx->alloc, (includeAllocations && *includeAllocations != 0));
  32. Dict d = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST("none")), NULL);
  33. Admin_sendMessage(&d, txid, ctx->admin);
  34. }
  35. static void bytesAllocated(Dict* in, void* vcontext, String* txid, struct Allocator* requestAlloc)
  36. {
  37. struct Allocator_admin_pvt* ctx = Identity_check((struct Allocator_admin_pvt*)vcontext);
  38. Dict* d = Dict_new(requestAlloc);
  39. Dict_putIntC(d, "bytes", Allocator_bytesAllocated(ctx->alloc), requestAlloc);
  40. Admin_sendMessage(d, txid, ctx->admin);
  41. }
  42. void Allocator_admin_register(struct Allocator* alloc, struct Admin* admin)
  43. {
  44. struct Allocator_admin_pvt* ctx = Allocator_clone(alloc, (&(struct Allocator_admin_pvt) {
  45. .alloc = alloc,
  46. .admin = admin
  47. }));
  48. Identity_set(ctx);
  49. Admin_registerFunction("Allocator_snapshot", snapshot, ctx, true,
  50. ((struct Admin_FunctionArg[]) {
  51. { .name = "includeAllocations", .required = 0, .type = "Int" }
  52. }), admin);
  53. Admin_registerFunction("Allocator_bytesAllocated", bytesAllocated, ctx, true, NULL, admin);
  54. }