appldata_mem.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Data gathering module for Linux-VM Monitor Stream, Stage 1.
  4. * Collects data related to memory management.
  5. *
  6. * Copyright IBM Corp. 2003, 2006
  7. *
  8. * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/errno.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/swap.h>
  16. #include <linux/slab.h>
  17. #include <asm/io.h>
  18. #include "appldata.h"
  19. #define P2K(x) ((x) << (PAGE_SHIFT - 10)) /* Converts #Pages to KB */
  20. /*
  21. * Memory data
  22. *
  23. * This is accessed as binary data by z/VM. If changes to it can't be avoided,
  24. * the structure version (product ID, see appldata_base.c) needs to be changed
  25. * as well and all documentation and z/VM applications using it must be
  26. * updated.
  27. */
  28. struct appldata_mem_data {
  29. u64 timestamp;
  30. u32 sync_count_1; /* after VM collected the record data, */
  31. u32 sync_count_2; /* sync_count_1 and sync_count_2 should be the
  32. same. If not, the record has been updated on
  33. the Linux side while VM was collecting the
  34. (possibly corrupt) data */
  35. u64 pgpgin; /* data read from disk */
  36. u64 pgpgout; /* data written to disk */
  37. u64 pswpin; /* pages swapped in */
  38. u64 pswpout; /* pages swapped out */
  39. u64 sharedram; /* sharedram is currently set to 0 */
  40. u64 totalram; /* total main memory size */
  41. u64 freeram; /* free main memory size */
  42. u64 totalhigh; /* total high memory size */
  43. u64 freehigh; /* free high memory size */
  44. u64 bufferram; /* memory reserved for buffers, free cache */
  45. u64 cached; /* size of (used) cache, w/o buffers */
  46. u64 totalswap; /* total swap space size */
  47. u64 freeswap; /* free swap space */
  48. // New in 2.6 -->
  49. u64 pgalloc; /* page allocations */
  50. u64 pgfault; /* page faults (major+minor) */
  51. u64 pgmajfault; /* page faults (major only) */
  52. // <-- New in 2.6
  53. } __packed;
  54. /*
  55. * appldata_get_mem_data()
  56. *
  57. * gather memory data
  58. */
  59. static void appldata_get_mem_data(void *data)
  60. {
  61. /*
  62. * don't put large structures on the stack, we are
  63. * serialized through the appldata_ops_mutex and can use static
  64. */
  65. static struct sysinfo val;
  66. unsigned long ev[NR_VM_EVENT_ITEMS];
  67. struct appldata_mem_data *mem_data;
  68. mem_data = data;
  69. mem_data->sync_count_1++;
  70. all_vm_events(ev);
  71. mem_data->pgpgin = ev[PGPGIN] >> 1;
  72. mem_data->pgpgout = ev[PGPGOUT] >> 1;
  73. mem_data->pswpin = ev[PSWPIN];
  74. mem_data->pswpout = ev[PSWPOUT];
  75. mem_data->pgalloc = ev[PGALLOC_NORMAL];
  76. mem_data->pgalloc += ev[PGALLOC_DMA];
  77. mem_data->pgfault = ev[PGFAULT];
  78. mem_data->pgmajfault = ev[PGMAJFAULT];
  79. si_meminfo(&val);
  80. mem_data->sharedram = val.sharedram;
  81. mem_data->totalram = P2K(val.totalram);
  82. mem_data->freeram = P2K(val.freeram);
  83. mem_data->totalhigh = P2K(val.totalhigh);
  84. mem_data->freehigh = P2K(val.freehigh);
  85. mem_data->bufferram = P2K(val.bufferram);
  86. mem_data->cached = P2K(global_node_page_state(NR_FILE_PAGES)
  87. - val.bufferram);
  88. si_swapinfo(&val);
  89. mem_data->totalswap = P2K(val.totalswap);
  90. mem_data->freeswap = P2K(val.freeswap);
  91. mem_data->timestamp = get_tod_clock();
  92. mem_data->sync_count_2++;
  93. }
  94. static struct appldata_ops ops = {
  95. .name = "mem",
  96. .record_nr = APPLDATA_RECORD_MEM_ID,
  97. .size = sizeof(struct appldata_mem_data),
  98. .callback = &appldata_get_mem_data,
  99. .owner = THIS_MODULE,
  100. .mod_lvl = {0xF0, 0xF0}, /* EBCDIC "00" */
  101. };
  102. /*
  103. * appldata_mem_init()
  104. *
  105. * init_data, register ops
  106. */
  107. static int __init appldata_mem_init(void)
  108. {
  109. int ret;
  110. ops.data = kzalloc(sizeof(struct appldata_mem_data), GFP_KERNEL);
  111. if (!ops.data)
  112. return -ENOMEM;
  113. ret = appldata_register_ops(&ops);
  114. if (ret)
  115. kfree(ops.data);
  116. return ret;
  117. }
  118. /*
  119. * appldata_mem_exit()
  120. *
  121. * unregister ops
  122. */
  123. static void __exit appldata_mem_exit(void)
  124. {
  125. appldata_unregister_ops(&ops);
  126. kfree(ops.data);
  127. }
  128. module_init(appldata_mem_init);
  129. module_exit(appldata_mem_exit);
  130. MODULE_LICENSE("GPL");
  131. MODULE_AUTHOR("Gerald Schaefer");
  132. MODULE_DESCRIPTION("Linux-VM Monitor Stream, MEMORY statistics");