memtools.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2022 Free Software Foundation, Inc.
  4. * Copyright (C) 2022 IBM Corporation
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/command.h>
  23. #include <grub/i18n.h>
  24. #include <grub/memory.h>
  25. #include <grub/mm.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. static grub_err_t
  28. grub_cmd_lsmem (grub_command_t cmd __attribute__ ((unused)),
  29. int argc __attribute__ ((unused)),
  30. char **args __attribute__ ((unused)))
  31. {
  32. #ifndef GRUB_MACHINE_EMU
  33. grub_mm_dump (0);
  34. #endif
  35. return 0;
  36. }
  37. static grub_err_t
  38. grub_cmd_lsfreemem (grub_command_t cmd __attribute__ ((unused)),
  39. int argc __attribute__ ((unused)),
  40. char **args __attribute__ ((unused)))
  41. {
  42. #ifndef GRUB_MACHINE_EMU
  43. grub_mm_dump_free ();
  44. #endif
  45. return 0;
  46. }
  47. static grub_err_t
  48. grub_cmd_stress_big_allocs (grub_command_t cmd __attribute__ ((unused)),
  49. int argc __attribute__ ((unused)),
  50. char **args __attribute__ ((unused)))
  51. {
  52. int i, max_mb, blocks_alloced;
  53. void *mem;
  54. void **blocklist;
  55. grub_printf ("Test 1: increasingly sized allocs to 1GB block\n");
  56. for (i = 1; i < 1024; i++)
  57. {
  58. grub_printf ("%4d MB . ", i);
  59. mem = grub_malloc (i * 1024 * 1024);
  60. if (mem == NULL)
  61. {
  62. grub_printf ("failed\n");
  63. break;
  64. }
  65. else
  66. grub_free (mem);
  67. if (i % 7 == 0)
  68. grub_printf ("\n");
  69. }
  70. max_mb = i - 1;
  71. grub_printf ("\nMax sized allocation we did was %d MB\n", max_mb);
  72. grub_printf ("\nTest 2: 1MB at a time, max 4GB\n");
  73. blocklist = grub_calloc (4096, sizeof (void *));
  74. for (i = 0; i < 4096; i++)
  75. {
  76. blocklist[i] = grub_malloc (1024 * 1024);
  77. if (blocklist[i] == NULL)
  78. {
  79. grub_printf ("Ran out of memory at iteration %d\n", i);
  80. break;
  81. }
  82. }
  83. blocks_alloced = i;
  84. for (i = 0; i < blocks_alloced; i++)
  85. grub_free (blocklist[i]);
  86. grub_printf ("\nTest 3: 1MB aligned 900kB + 100kB\n");
  87. /* grub_mm_debug=1;*/
  88. for (i = 0; i < 4096; i += 2)
  89. {
  90. blocklist[i] = grub_memalign (1024 * 1024, 900 * 1024);
  91. if (blocklist[i] == NULL)
  92. {
  93. grub_printf ("Failed big allocation, iteration %d\n", i);
  94. blocks_alloced = i;
  95. break;
  96. }
  97. blocklist[i + 1] = grub_malloc (100 * 1024);
  98. if (blocklist[i + 1] == NULL)
  99. {
  100. grub_printf ("Failed small allocation, iteration %d\n", i);
  101. blocks_alloced = i + 1;
  102. break;
  103. }
  104. grub_printf (".");
  105. }
  106. for (i = 0; i < blocks_alloced; i++)
  107. grub_free (blocklist[i]);
  108. grub_free (blocklist);
  109. #if defined(__powerpc__)
  110. grub_printf ("\nA reboot may now be required.\n");
  111. #endif
  112. grub_errno = GRUB_ERR_NONE;
  113. return GRUB_ERR_NONE;
  114. }
  115. static grub_command_t cmd_lsmem, cmd_lsfreemem, cmd_sba;
  116. GRUB_MOD_INIT (memtools)
  117. {
  118. cmd_lsmem = grub_register_command ("lsmem", grub_cmd_lsmem,
  119. 0, N_("List free and allocated memory blocks."));
  120. cmd_lsfreemem = grub_register_command ("lsfreemem", grub_cmd_lsfreemem,
  121. 0, N_("List free memory blocks."));
  122. cmd_sba = grub_register_command ("stress_big_allocs", grub_cmd_stress_big_allocs,
  123. 0, N_("Stress test large allocations."));
  124. }
  125. GRUB_MOD_FINI (memtools)
  126. {
  127. grub_unregister_command (cmd_lsmem);
  128. grub_unregister_command (cmd_lsfreemem);
  129. grub_unregister_command (cmd_sba);
  130. }