vmstat.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*++
  2. Copyright (c) 2015 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. vmstat.c
  9. Abstract:
  10. This module implements the vmstat application.
  11. Author:
  12. Evan Green 5-Mar-2015
  13. Environment:
  14. User
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include <minoca/lib/minocaos.h>
  20. #include <minoca/lib/mlibc.h>
  21. #include <assert.h>
  22. #include <errno.h>
  23. #include <getopt.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. //
  29. // ---------------------------------------------------------------- Definitions
  30. //
  31. #define VMSTAT_VERSION_MAJOR 1
  32. #define VMSTAT_VERSION_MINOR 0
  33. #define VMSTAT_USAGE \
  34. "usage: vmstat\n\n" \
  35. "The vmstat utility prints information about current system memory \n" \
  36. "usage. Options are:\n" \
  37. " --help -- Display this help text.\n" \
  38. " --version -- Display the application version and exit.\n\n"
  39. #define VMSTAT_OPTIONS_STRING "hV"
  40. //
  41. // ------------------------------------------------------ Data Type Definitions
  42. //
  43. //
  44. // ----------------------------------------------- Internal Function Prototypes
  45. //
  46. INT
  47. VmstatPrintInformation (
  48. VOID
  49. );
  50. //
  51. // -------------------------------------------------------------------- Globals
  52. //
  53. struct option VmstatLongOptions[] = {
  54. {"help", no_argument, 0, 'h'},
  55. {"version", no_argument, 0, 'V'},
  56. {NULL, 0, 0, 0}
  57. };
  58. //
  59. // ------------------------------------------------------------------ Functions
  60. //
  61. INT
  62. main (
  63. INT ArgumentCount,
  64. CHAR **Arguments
  65. )
  66. /*++
  67. Routine Description:
  68. This routine implements the vmstat user mode program.
  69. Arguments:
  70. ArgumentCount - Supplies the number of elements in the arguments array.
  71. Arguments - Supplies an array of strings. The array count is bounded by the
  72. previous parameter, and the strings are null-terminated.
  73. Return Value:
  74. 0 on success.
  75. Non-zero on failure.
  76. --*/
  77. {
  78. ULONG ArgumentIndex;
  79. INT Option;
  80. INT ReturnValue;
  81. ReturnValue = 0;
  82. //
  83. // If there are no arguments, just print the memory information.
  84. //
  85. if (ArgumentCount == 1) {
  86. ReturnValue = VmstatPrintInformation();
  87. goto mainEnd;
  88. }
  89. //
  90. // Process the control arguments.
  91. //
  92. while (TRUE) {
  93. Option = getopt_long(ArgumentCount,
  94. Arguments,
  95. VMSTAT_OPTIONS_STRING,
  96. VmstatLongOptions,
  97. NULL);
  98. if (Option == -1) {
  99. break;
  100. }
  101. if ((Option == '?') || (Option == ':')) {
  102. ReturnValue = 1;
  103. goto mainEnd;
  104. }
  105. switch (Option) {
  106. case 'V':
  107. printf("vmstat version %d.%02d\n",
  108. VMSTAT_VERSION_MAJOR,
  109. VMSTAT_VERSION_MINOR);
  110. ReturnValue = 1;
  111. goto mainEnd;
  112. case 'h':
  113. printf(VMSTAT_USAGE);
  114. return 1;
  115. default:
  116. assert(FALSE);
  117. ReturnValue = 1;
  118. goto mainEnd;
  119. }
  120. }
  121. ArgumentIndex = optind;
  122. if (ArgumentIndex > ArgumentCount) {
  123. ArgumentIndex = ArgumentCount;
  124. }
  125. if (ArgumentIndex < ArgumentCount) {
  126. fprintf(stderr,
  127. "vmstat: Unexpected argument %s\n",
  128. Arguments[ArgumentIndex]);
  129. }
  130. ReturnValue = 0;
  131. mainEnd:
  132. return ReturnValue;
  133. }
  134. //
  135. // --------------------------------------------------------- Internal Functions
  136. //
  137. INT
  138. VmstatPrintInformation (
  139. VOID
  140. )
  141. /*++
  142. Routine Description:
  143. This routine prints system memory information.
  144. Arguments:
  145. None.
  146. Return Value:
  147. 0 on success.
  148. Non-zero on failure.
  149. --*/
  150. {
  151. IO_CACHE_STATISTICS IoCache;
  152. ULONGLONG Megabytes;
  153. MM_STATISTICS MmStatistics;
  154. INT ReturnValue;
  155. UINTN Size;
  156. KSTATUS Status;
  157. UINTN Value;
  158. ReturnValue = 0;
  159. Size = sizeof(MM_STATISTICS);
  160. MmStatistics.Version = MM_STATISTICS_VERSION;
  161. Status = OsGetSetSystemInformation(SystemInformationMm,
  162. MmInformationSystemMemory,
  163. &MmStatistics,
  164. &Size,
  165. FALSE);
  166. if (!KSUCCESS(Status)) {
  167. ReturnValue = ClConvertKstatusToErrorNumber(Status);
  168. fprintf(stderr,
  169. "Error: failed to get memory information: status 0x%08x: %s.\n",
  170. Status,
  171. strerror(ReturnValue));
  172. return ReturnValue;
  173. }
  174. Megabytes = (MmStatistics.PhysicalPages * MmStatistics.PageSize) / _1MB;
  175. printf("Total Physical Memory: %I64dMB\n", Megabytes);
  176. Megabytes = (MmStatistics.AllocatedPhysicalPages *
  177. MmStatistics.PageSize) / _1MB;
  178. printf("Allocated Physical Memory: %I64dMB\n", Megabytes);
  179. Megabytes = (MmStatistics.NonPagedPhysicalPages *
  180. MmStatistics.PageSize) / _1MB;
  181. printf("Non-Paged Physical Memory: %I64dMB\n", Megabytes);
  182. printf("Non Paged Pool:\n");
  183. printf(" Size: %ld\n", MmStatistics.NonPagedPool.TotalHeapSize);
  184. printf(" Maximum Size: %ld\n", MmStatistics.NonPagedPool.MaxHeapSize);
  185. Value = MmStatistics.NonPagedPool.TotalHeapSize -
  186. MmStatistics.NonPagedPool.FreeListSize;
  187. printf(" Allocated: %ld\n", Value);
  188. printf(" Allocation Count: %ld (lifetime %ld)\n",
  189. MmStatistics.NonPagedPool.Allocations,
  190. MmStatistics.NonPagedPool.TotalAllocationCalls);
  191. printf(" Failed Allocations: %ld\n",
  192. MmStatistics.NonPagedPool.FailedAllocations);
  193. printf("Paged Pool:\n");
  194. printf(" Size: %ld\n", MmStatistics.PagedPool.TotalHeapSize);
  195. printf(" Maximum Size: %ld\n", MmStatistics.PagedPool.MaxHeapSize);
  196. Value = MmStatistics.PagedPool.TotalHeapSize -
  197. MmStatistics.PagedPool.FreeListSize;
  198. printf(" Allocated: %ld\n", Value);
  199. printf(" Allocation Count: %ld (lifetime %ld)\n",
  200. MmStatistics.PagedPool.Allocations,
  201. MmStatistics.PagedPool.TotalAllocationCalls);
  202. printf(" Failed Allocations: %ld\n",
  203. MmStatistics.PagedPool.FailedAllocations);
  204. Size = sizeof(IO_CACHE_STATISTICS);
  205. IoCache.Version = IO_CACHE_STATISTICS_VERSION;
  206. Status = OsGetSetSystemInformation(SystemInformationIo,
  207. IoInformationCacheStatistics,
  208. &IoCache,
  209. &Size,
  210. FALSE);
  211. if (!KSUCCESS(Status)) {
  212. ReturnValue = ClConvertKstatusToErrorNumber(Status);
  213. fprintf(stderr,
  214. "Error: failed to get I/O cache information: status %d: "
  215. "%s.\n",
  216. Status,
  217. strerror(ReturnValue));
  218. return ReturnValue;
  219. }
  220. Megabytes = (IoCache.PhysicalPageCount * MmStatistics.PageSize) / _1MB;
  221. printf("Page Cache Size: %lldMB\n", Megabytes);
  222. Megabytes = (IoCache.DirtyPageCount * MmStatistics.PageSize) / _1MB;
  223. printf("Dirty Page Cache Size: %lldMB\n", Megabytes);
  224. return ReturnValue;
  225. }