summaryrefslogtreecommitdiffstats
blob: f1595bdac2021a8e61e046e19105dd17a56413cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
 * Copyright (C) ARM Limited 2010-2011. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

#include "gator.h"
#include <linux/workqueue.h>
#include <trace/events/kmem.h>

#define MEMINFO_MEMFREE		0
#define MEMINFO_MEMUSED		1
#define MEMINFO_BUFFERRAM	2
#define MEMINFO_TOTAL		3

static ulong meminfo_global_enabled;
static ulong meminfo_enabled[MEMINFO_TOTAL];
static ulong meminfo_key[MEMINFO_TOTAL];
static int meminfo_buffer[MEMINFO_TOTAL * 2];
static int meminfo_length = 0;
static unsigned int mem_event = 0;
static bool new_data_avail;

static void wq_sched_handler(struct work_struct *wsptr);

DECLARE_WORK(work, wq_sched_handler);

GATOR_DEFINE_PROBE(mm_page_free_direct, TP_PROTO(struct page *page, unsigned int order)) {
	mem_event++;
}

GATOR_DEFINE_PROBE(mm_pagevec_free, TP_PROTO(struct page *page, int cold)) {
	mem_event++;
}

GATOR_DEFINE_PROBE(mm_page_alloc, TP_PROTO(struct page *page, unsigned int order, gfp_t gfp_flags, int migratetype)) {
	mem_event++;
}

static int gator_events_meminfo_create_files(struct super_block *sb, struct dentry *root)
{
	struct dentry *dir;
	int i;

	for (i = 0; i < MEMINFO_TOTAL; i++) {
		switch (i) {
		case MEMINFO_MEMFREE:
			dir = gatorfs_mkdir(sb, root, "Linux_meminfo_memfree");
			break;
		case MEMINFO_MEMUSED:
			dir = gatorfs_mkdir(sb, root, "Linux_meminfo_memused");
			break;
		case MEMINFO_BUFFERRAM:
			dir = gatorfs_mkdir(sb, root, "Linux_meminfo_bufferram");
			break;
		default:
			return -1;
		}
		if (!dir) {
			return -1;
		}
		gatorfs_create_ulong(sb, dir, "enabled", &meminfo_enabled[i]);
		gatorfs_create_ro_ulong(sb, dir, "key", &meminfo_key[i]);
	}

	return 0;
}

static int gator_events_meminfo_start(void)
{
	int i;

	new_data_avail = true;
	for (i = 0; i < MEMINFO_TOTAL; i++) {
		if (meminfo_enabled[i]) {
			meminfo_global_enabled = 1;
		}
	}

	if (meminfo_global_enabled == 0)
		return 0;

	if (GATOR_REGISTER_TRACE(mm_page_free_direct))
		goto mm_page_free_direct_exit;
	if (GATOR_REGISTER_TRACE(mm_pagevec_free))
		goto mm_pagevec_free_exit;
	if (GATOR_REGISTER_TRACE(mm_page_alloc))
		goto mm_page_alloc_exit;

	return 0;

mm_page_alloc_exit:
	GATOR_UNREGISTER_TRACE(mm_pagevec_free);
mm_pagevec_free_exit:
	GATOR_UNREGISTER_TRACE(mm_page_free_direct);
mm_page_free_direct_exit:
	return -1;
}

static void gator_events_meminfo_stop(void)
{
	int i;

	if (meminfo_global_enabled) {
		GATOR_UNREGISTER_TRACE(mm_page_free_direct);
		GATOR_UNREGISTER_TRACE(mm_pagevec_free);
		GATOR_UNREGISTER_TRACE(mm_page_alloc);
	}

	meminfo_global_enabled = 0;
	for (i = 0; i < MEMINFO_TOTAL; i++) {
		meminfo_enabled[i] = 0;
	}
}

// Must be run in a work queue as the kernel function si_meminfo() can sleep
static void wq_sched_handler(struct work_struct *wsptr)
{
	struct sysinfo info;
	int i, len, value;

	meminfo_length = len = 0;

	si_meminfo(&info);
	for (i = 0; i < MEMINFO_TOTAL; i++) {
		if (meminfo_enabled[i]) {
			switch (i) {
			case MEMINFO_MEMFREE:
				value = info.freeram * PAGE_SIZE;
				break;
			case MEMINFO_MEMUSED:
				value = (info.totalram - info.freeram) * PAGE_SIZE;
				break;
			case MEMINFO_BUFFERRAM:
				value = info.bufferram * PAGE_SIZE;
				break;
			default:
				value = 0;
				break;
			}
			meminfo_buffer[len++] = meminfo_key[i];
			meminfo_buffer[len++] = value;
		}
	}

	meminfo_length = len;
	new_data_avail = true;
}

static int gator_events_meminfo_read(int **buffer)
{
	static unsigned int last_mem_event = 0;

	if (smp_processor_id() || !meminfo_global_enabled)
		return 0;

	if (last_mem_event != mem_event) {
		last_mem_event = mem_event;
		schedule_work(&work);
	}

	if (!new_data_avail)
		return 0;

	new_data_avail = false;

	if (buffer)
		*buffer = meminfo_buffer;

	return meminfo_length;
}

static struct gator_interface gator_events_meminfo_interface = {
	.create_files = gator_events_meminfo_create_files,
	.start = gator_events_meminfo_start,
	.stop = gator_events_meminfo_stop,
	.read = gator_events_meminfo_read,
};

int gator_events_meminfo_init(void)
{
	int i;

	meminfo_global_enabled = 0;
	for (i = 0; i < MEMINFO_TOTAL; i++) {
		meminfo_enabled[i] = 0;
		meminfo_key[i] = gator_events_get_key();
	}

	return gator_events_install(&gator_events_meminfo_interface);
}
gator_events_init(gator_events_meminfo_init);