2 * Copyright (C) ARM Limited 2010-2015. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
12 #include <linux/hardirq.h>
13 #include <linux/kthread.h>
14 #include <linux/sched.h>
15 #include <linux/semaphore.h>
16 #include <linux/workqueue.h>
17 #include <trace/events/kmem.h>
19 #define USE_THREAD defined(CONFIG_PREEMPT_RT_FULL)
36 static const char * const meminfo_names[] = {
37 "Linux_meminfo_memfree",
38 "Linux_meminfo_memused",
39 "Linux_meminfo_bufferram",
42 static const char * const proc_names[] = {
43 "Linux_proc_statm_size",
44 "Linux_proc_statm_share",
45 "Linux_proc_statm_text",
46 "Linux_proc_statm_data",
49 static bool meminfo_global_enabled;
50 static ulong meminfo_enabled[MEMINFO_TOTAL];
51 static ulong meminfo_keys[MEMINFO_TOTAL];
52 static long long meminfo_buffer[2 * (MEMINFO_TOTAL + 2)];
53 static int meminfo_length;
54 static bool new_data_avail;
56 static bool proc_global_enabled;
57 static ulong proc_enabled[PROC_COUNT];
58 static ulong proc_keys[PROC_COUNT];
59 static DEFINE_PER_CPU(long long, proc_buffer[2 * (PROC_COUNT + 3)]);
61 static void do_read(void);
65 static int gator_meminfo_func(void *data);
66 static bool gator_meminfo_run;
67 /* Initialize semaphore unlocked to initialize memory values */
68 static DEFINE_SEMAPHORE(gator_meminfo_sem);
70 static void notify(void)
72 up(&gator_meminfo_sem);
77 static unsigned int mem_event;
78 static void wq_sched_handler(struct work_struct *wsptr);
79 static DECLARE_WORK(work, wq_sched_handler);
80 static struct timer_list meminfo_wake_up_timer;
81 static void meminfo_wake_up_handler(unsigned long unused_data);
83 static void notify(void)
90 GATOR_DEFINE_PROBE(mm_page_free, TP_PROTO(struct page *page, unsigned int order))
95 GATOR_DEFINE_PROBE(mm_page_free_batched, TP_PROTO(struct page *page, int cold))
100 GATOR_DEFINE_PROBE(mm_page_alloc, TP_PROTO(struct page *page, unsigned int order, gfp_t gfp_flags, int migratetype))
105 static int gator_events_meminfo_create_files(struct super_block *sb, struct dentry *root)
110 for (i = 0; i < MEMINFO_TOTAL; i++) {
111 dir = gatorfs_mkdir(sb, root, meminfo_names[i]);
114 gatorfs_create_ulong(sb, dir, "enabled", &meminfo_enabled[i]);
115 gatorfs_create_ro_ulong(sb, dir, "key", &meminfo_keys[i]);
118 for (i = 0; i < PROC_COUNT; ++i) {
119 dir = gatorfs_mkdir(sb, root, proc_names[i]);
122 gatorfs_create_ulong(sb, dir, "enabled", &proc_enabled[i]);
123 gatorfs_create_ro_ulong(sb, dir, "key", &proc_keys[i]);
129 static int gator_events_meminfo_start(void)
133 new_data_avail = false;
134 meminfo_global_enabled = 0;
135 for (i = 0; i < MEMINFO_TOTAL; i++) {
136 if (meminfo_enabled[i]) {
137 meminfo_global_enabled = 1;
142 proc_global_enabled = 0;
143 for (i = 0; i < PROC_COUNT; ++i) {
144 if (proc_enabled[i]) {
145 proc_global_enabled = 1;
149 if (meminfo_enabled[MEMINFO_MEMUSED])
150 proc_global_enabled = 1;
152 if (meminfo_global_enabled == 0)
155 if (GATOR_REGISTER_TRACE(mm_page_free))
156 goto mm_page_free_exit;
157 if (GATOR_REGISTER_TRACE(mm_page_free_batched))
158 goto mm_page_free_batched_exit;
159 if (GATOR_REGISTER_TRACE(mm_page_alloc))
160 goto mm_page_alloc_exit;
164 /* Start worker thread */
165 gator_meminfo_run = true;
166 /* Since the mutex starts unlocked, memory values will be initialized */
167 if (IS_ERR(kthread_run(gator_meminfo_func, NULL, "gator_meminfo")))
168 goto kthread_run_exit;
170 setup_deferrable_timer_on_stack(&meminfo_wake_up_timer, meminfo_wake_up_handler, 0);
177 GATOR_UNREGISTER_TRACE(mm_page_alloc);
180 GATOR_UNREGISTER_TRACE(mm_page_free_batched);
181 mm_page_free_batched_exit:
182 GATOR_UNREGISTER_TRACE(mm_page_free);
187 static void gator_events_meminfo_stop(void)
189 if (meminfo_global_enabled) {
190 GATOR_UNREGISTER_TRACE(mm_page_free);
191 GATOR_UNREGISTER_TRACE(mm_page_free_batched);
192 GATOR_UNREGISTER_TRACE(mm_page_alloc);
195 /* Stop worker thread */
196 gator_meminfo_run = false;
197 up(&gator_meminfo_sem);
199 del_timer_sync(&meminfo_wake_up_timer);
204 static void do_read(void)
208 unsigned long long value;
210 meminfo_length = len = 0;
213 for (i = 0; i < MEMINFO_TOTAL; i++) {
214 if (meminfo_enabled[i]) {
216 case MEMINFO_MEMFREE:
217 value = info.freeram * PAGE_SIZE;
219 case MEMINFO_MEMUSED:
220 /* pid -1 means system wide */
221 meminfo_buffer[len++] = 1;
222 meminfo_buffer[len++] = -1;
224 meminfo_buffer[len++] = meminfo_keys[MEMINFO_MEMUSED];
225 meminfo_buffer[len++] = (info.totalram - info.freeram) * PAGE_SIZE;
227 meminfo_buffer[len++] = 1;
228 meminfo_buffer[len++] = 0;
230 case MEMINFO_BUFFERRAM:
231 value = info.bufferram * PAGE_SIZE;
237 meminfo_buffer[len++] = meminfo_keys[i];
238 meminfo_buffer[len++] = value;
242 meminfo_length = len;
243 new_data_avail = true;
248 static int gator_meminfo_func(void *data)
251 if (down_killable(&gator_meminfo_sem))
254 /* Eat up any pending events */
255 while (!down_trylock(&gator_meminfo_sem))
258 if (!gator_meminfo_run)
269 /* Must be run in process context as the kernel function si_meminfo() can sleep */
270 static void wq_sched_handler(struct work_struct *wsptr)
275 static void meminfo_wake_up_handler(unsigned long unused_data)
277 /* had to delay scheduling work as attempting to schedule work during the context switch is illegal in kernel versions 3.5 and greater */
278 schedule_work(&work);
283 static int gator_events_meminfo_read(long long **buffer, bool sched_switch)
286 static unsigned int last_mem_event;
289 if (!on_primary_core() || !meminfo_global_enabled)
293 if (last_mem_event != mem_event) {
294 last_mem_event = mem_event;
295 mod_timer(&meminfo_wake_up_timer, jiffies + 1);
302 new_data_avail = false;
305 *buffer = meminfo_buffer;
307 return meminfo_length;
310 static int gator_events_meminfo_read_proc(long long **buffer, struct task_struct *task)
312 struct mm_struct *mm;
317 int cpu = get_physical_cpu();
318 long long *buf = per_cpu(proc_buffer, cpu);
320 if (!proc_global_enabled)
323 /* Collect the memory stats of the process instead of the thread */
324 if (task->group_leader != NULL)
325 task = task->group_leader;
327 /* get_task_mm/mmput is not needed in this context because the task and it's mm are required as part of the sched_switch */
332 /* Derived from task_statm in fs/proc/task_mmu.c */
333 if (meminfo_enabled[MEMINFO_MEMUSED] || proc_enabled[PROC_SHARE]) {
334 share = get_mm_counter(mm, MM_FILEPAGES);
337 /* key of 1 indicates a pid */
339 buf[len++] = task->pid;
341 for (i = 0; i < PROC_COUNT; ++i) {
342 if (proc_enabled[i]) {
345 value = mm->total_vm;
351 value = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> PAGE_SHIFT;
354 value = mm->total_vm - mm->shared_vm;
358 buf[len++] = proc_keys[i];
359 buf[len++] = value * PAGE_SIZE;
363 if (meminfo_enabled[MEMINFO_MEMUSED]) {
364 value = share + get_mm_counter(mm, MM_ANONPAGES);
365 /* Send resident for this pid */
366 buf[len++] = meminfo_keys[MEMINFO_MEMUSED];
367 buf[len++] = value * PAGE_SIZE;
380 static struct gator_interface gator_events_meminfo_interface = {
382 .create_files = gator_events_meminfo_create_files,
383 .start = gator_events_meminfo_start,
384 .stop = gator_events_meminfo_stop,
385 .read64 = gator_events_meminfo_read,
386 .read_proc = gator_events_meminfo_read_proc,
389 int gator_events_meminfo_init(void)
393 meminfo_global_enabled = 0;
394 for (i = 0; i < MEMINFO_TOTAL; i++) {
395 meminfo_enabled[i] = 0;
396 meminfo_keys[i] = gator_events_get_key();
399 proc_global_enabled = 0;
400 for (i = 0; i < PROC_COUNT; ++i) {
402 proc_keys[i] = gator_events_get_key();
405 return gator_events_install(&gator_events_meminfo_interface);