blob: e68136c3c23aa467a9e10b362888023abe9559d4 [file] [log] [blame]
Greg Kroah-Hartman0caa8cd2017-11-24 15:00:37 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Data gathering module for Linux-VM Monitor Stream, Stage 1.
4 * Collects data related to memory management.
5 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02006 * Copyright IBM Corp. 2003, 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Gerald Schaefer5b5dd212006-06-29 15:08:35 +02008 * Author: Gerald Schaefer <gerald.schaefer@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/errno.h>
14#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/pagemap.h>
16#include <linux/swap.h>
Jeff Mahoneyf1a85822014-04-27 17:35:43 -040017#include <linux/slab.h>
Gerald Schaeferd3ae9422008-07-14 09:59:34 +020018#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include "appldata.h"
21
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define P2K(x) ((x) << (PAGE_SHIFT - 10)) /* Converts #Pages to KB */
24
25/*
26 * Memory data
27 *
28 * This is accessed as binary data by z/VM. If changes to it can't be avoided,
29 * the structure version (product ID, see appldata_base.c) needs to be changed
30 * as well and all documentation and z/VM applications using it must be
31 * updated.
32 *
33 * The record layout is documented in the Linux for zSeries Device Drivers
34 * book:
35 * http://oss.software.ibm.com/developerworks/opensource/linux390/index.shtml
36 */
Sebastian Otta94f0fb2013-06-25 15:36:12 +020037struct appldata_mem_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 u64 timestamp;
39 u32 sync_count_1; /* after VM collected the record data, */
40 u32 sync_count_2; /* sync_count_1 and sync_count_2 should be the
41 same. If not, the record has been updated on
42 the Linux side while VM was collecting the
43 (possibly corrupt) data */
44
45 u64 pgpgin; /* data read from disk */
46 u64 pgpgout; /* data written to disk */
47 u64 pswpin; /* pages swapped in */
48 u64 pswpout; /* pages swapped out */
49
50 u64 sharedram; /* sharedram is currently set to 0 */
51
52 u64 totalram; /* total main memory size */
53 u64 freeram; /* free main memory size */
54 u64 totalhigh; /* total high memory size */
55 u64 freehigh; /* free high memory size */
56
57 u64 bufferram; /* memory reserved for buffers, free cache */
58 u64 cached; /* size of (used) cache, w/o buffers */
59 u64 totalswap; /* total swap space size */
60 u64 freeswap; /* free swap space */
61
62// New in 2.6 -->
63 u64 pgalloc; /* page allocations */
64 u64 pgfault; /* page faults (major+minor) */
65 u64 pgmajfault; /* page faults (major only) */
66// <-- New in 2.6
67
Sebastian Otta94f0fb2013-06-25 15:36:12 +020068} __packed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
72 * appldata_get_mem_data()
73 *
74 * gather memory data
75 */
76static void appldata_get_mem_data(void *data)
77{
78 /*
79 * don't put large structures on the stack, we are
Gerald Schaeferb1ad1712009-04-23 13:58:07 +020080 * serialized through the appldata_ops_mutex and can use static
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82 static struct sysinfo val;
Christoph Lameterf8891e52006-06-30 01:55:45 -070083 unsigned long ev[NR_VM_EVENT_ITEMS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct appldata_mem_data *mem_data;
85
86 mem_data = data;
87 mem_data->sync_count_1++;
88
Christoph Lameterf8891e52006-06-30 01:55:45 -070089 all_vm_events(ev);
90 mem_data->pgpgin = ev[PGPGIN] >> 1;
91 mem_data->pgpgout = ev[PGPGOUT] >> 1;
92 mem_data->pswpin = ev[PSWPIN];
93 mem_data->pswpout = ev[PSWPOUT];
Al Virobb81e602007-03-14 09:04:41 +000094 mem_data->pgalloc = ev[PGALLOC_NORMAL];
Al Virobb81e602007-03-14 09:04:41 +000095 mem_data->pgalloc += ev[PGALLOC_DMA];
Christoph Lameterf8891e52006-06-30 01:55:45 -070096 mem_data->pgfault = ev[PGFAULT];
97 mem_data->pgmajfault = ev[PGMAJFAULT];
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 si_meminfo(&val);
100 mem_data->sharedram = val.sharedram;
101 mem_data->totalram = P2K(val.totalram);
102 mem_data->freeram = P2K(val.freeram);
103 mem_data->totalhigh = P2K(val.totalhigh);
104 mem_data->freehigh = P2K(val.freehigh);
105 mem_data->bufferram = P2K(val.bufferram);
Mel Gorman11fb9982016-07-28 15:46:20 -0700106 mem_data->cached = P2K(global_node_page_state(NR_FILE_PAGES)
Christoph Lameter347ce432006-06-30 01:55:35 -0700107 - val.bufferram);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 si_swapinfo(&val);
110 mem_data->totalswap = P2K(val.totalswap);
111 mem_data->freeswap = P2K(val.freeswap);
112
Heiko Carstens1aae0562013-01-30 09:49:40 +0100113 mem_data->timestamp = get_tod_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 mem_data->sync_count_2++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117
118static struct appldata_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .name = "mem",
120 .record_nr = APPLDATA_RECORD_MEM_ID,
121 .size = sizeof(struct appldata_mem_data),
122 .callback = &appldata_get_mem_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .owner = THIS_MODULE,
Gerald Schaefer5b5dd212006-06-29 15:08:35 +0200124 .mod_lvl = {0xF0, 0xF0}, /* EBCDIC "00" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127
128/*
129 * appldata_mem_init()
130 *
131 * init_data, register ops
132 */
133static int __init appldata_mem_init(void)
134{
Sebastian Otta94f0fb2013-06-25 15:36:12 +0200135 int ret;
136
137 ops.data = kzalloc(sizeof(struct appldata_mem_data), GFP_KERNEL);
138 if (!ops.data)
139 return -ENOMEM;
140
141 ret = appldata_register_ops(&ops);
142 if (ret)
143 kfree(ops.data);
144
145 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148/*
149 * appldata_mem_exit()
150 *
151 * unregister ops
152 */
153static void __exit appldata_mem_exit(void)
154{
155 appldata_unregister_ops(&ops);
Sebastian Otta94f0fb2013-06-25 15:36:12 +0200156 kfree(ops.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159
160module_init(appldata_mem_init);
161module_exit(appldata_mem_exit);
162
163MODULE_LICENSE("GPL");
164MODULE_AUTHOR("Gerald Schaefer");
165MODULE_DESCRIPTION("Linux-VM Monitor Stream, MEMORY statistics");