+++ /dev/null
-/*
- * This file is part of stability-monitor
- *
- * Copyright © 2019 Samsung Electronics
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <linux/module.h>
-#include <linux/init.h>
-#include <linux/kernel.h>
-
-#include <linux/fs.h>
-#include <linux/proc_fs.h>
-#include <linux/seq_file.h>
-#include <linux/sched.h>
-#include <linux/sched/signal.h>
-#include <linux/sched/cputime.h>
-#include <linux/mm.h>
-#include <linux/pid_namespace.h>
-#include <asm/uaccess.h>
-#include <linux/time.h>
-#include <linux/posix-timers.h>
-#include <linux/kernel_stat.h>
-#include <linux/fdtable.h>
-#include <linux/math64.h>
-#include <linux/task_io_accounting_ops.h>
-
-#define DEBUG 0
-#define PAGE_TO_KB(x) ((x) << (PAGE_SHIFT - 10))
-
-static struct proc_dir_entry* proc_file;
-
-
-static u64 time_now(void)
-{
- return div64_ul(ktime_get_boot_ns(), NSEC_PER_USEC);
-}
-
-static inline u64 cputime_to_usecs(const u64 cputime)
-{
- // return cputime >> 12;
- return div64_ul(cputime, NSEC_PER_USEC);
-}
-
-static unsigned int count_open_files(struct files_struct *files)
-{
- unsigned int open_files = 0;
- int i;
- struct fdtable *fdt;
-
- if (!files)
- return 0;
-
- spin_lock(&files->file_lock);
- fdt = files_fdtable(files);
-
- /* Find the last non zero cell in open fds array */
- for (i = fdt->max_fds / BITS_PER_LONG - 1; i >= 0; i--) {
- if (fdt->open_fds[i])
- break;
- }
-
- /* Count set bits */
- for (i = (i + 1) * BITS_PER_LONG - 1; i >= 0; i--) {
- if(test_bit(i, fdt->open_fds))
- open_files++;
- }
-
- spin_unlock(&files->file_lock);
- return open_files;
-}
-
-static int stability_monitor_show(struct seq_file *m, void *v)
-{
- struct sysinfo info;
- struct task_struct *task, *t;
- struct pid *pid = NULL;
- pid_t ppid = 0;
- u64 uptime, utime, stime, ut, st;
- unsigned long long vm_rss, total_ram;
- struct task_io_accounting task_ioac;
- unsigned int open_fds;
-
- rcu_read_lock();
-
- /* Uptime in us */
- uptime = time_now();
-
- /* Memory info */
- si_meminfo(&info);
- total_ram = PAGE_TO_KB(info.totalram);
-
- seq_put_decimal_ull(m, "", uptime);
- seq_put_decimal_ull(m, " ", total_ram);
- seq_printf(m, "\n");
-
- for_each_process(task) {
- ppid = task->real_parent->pid;
- pid = task_pgrp(task);
- if (!pid)
- continue;
-
- task_lock(task);
-
- if (!task->mm) {
- task_unlock(task);
- continue;
- }
-
- if (task->flags & (PF_DUMPCORE|PF_SIGNALED|PF_EXITING)) {
- task_unlock(task);
- continue;
- }
-
-
- /* Memory */
- vm_rss = get_mm_rss(task->mm);
-
- /* Open FDs */
- open_fds = count_open_files(task->files);
-
- /* CPU time + IO accounting */
- task_cputime_adjusted(task, &utime, &stime);
- task_ioac = task->ioac;
- t = task;
- while_each_thread(task, t) {
- task_cputime_adjusted(t, &ut, &st);
- utime += ut;
- stime += st;
- task_io_accounting_add(&task_ioac, &t->ioac);
- }
-
- task_unlock(task);
-
- seq_put_decimal_ll(m, "", task->pid);
- seq_put_decimal_ll(m, " ", ppid);
- seq_put_decimal_ull(m, " ", cputime_to_usecs(utime + stime));
- seq_put_decimal_ull(m, " ", PAGE_TO_KB(vm_rss));
- seq_put_decimal_ll(m, " ", open_fds);
- seq_put_decimal_ull(m, " ", task_ioac.read_bytes + task_ioac.write_bytes);
- seq_printf(m, "\n");
- }
-
- rcu_read_unlock();
-
-#if DEBUG
- printk(KERN_DEBUG "stability_monitor_show() took %llu us\n", time_now() - uptime);
-#endif
-
- return 0;
-}
-
-static int stability_monitor_open(struct inode *inode, struct file *file)
-{
- return single_open_size(file, stability_monitor_show, NULL, 8192);
-}
-
-static const struct file_operations stability_monitor_fops = {
- .owner = THIS_MODULE,
- .open = stability_monitor_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
-};
-
-static int __init stability_monitor_init(void)
-{
- proc_file = proc_create("tsm", 0, NULL, &stability_monitor_fops);
- if (!proc_file)
- return -ENOMEM;
-
- return 0;
-}
-
-static void __exit stability_monitor_exit(void)
-{
- remove_proc_entry("tsm", NULL);
-}
-
-module_init(stability_monitor_init);
-module_exit(stability_monitor_exit);
-
-MODULE_LICENSE("GPL");