测试一个进程的运行时间
2017-06-14 10:41
417 查看
功能:测试一个进程开始结束的运行时间
test_thread.c
Makefile
编译模块
make
插入模块
sudo insmod test_thread.ko pid=***
dmesg查看
test_thread.c
#include "linux/init.h" #include "linux/kernel.h" #include "linux/module.h" //3个头文件包含了内核提供的所有内核模块的头文件 #include "linux/sched.h" #include <linux/moduleparam.h> #include "linux/timer.h" #include "linux/rtc.h" #include <linux/kthread.h> #include <linux/uaccess.h> #include <linux/string.h> #include "linux/fs.h" #include "linux/delay.h" static int tid = 1; module_param(tid,int,S_IRUSR); struct timeval tv; struct rtc_time tm; static struct task_struct *myThread=NULL; static struct task_struct *p=NULL; static struct task_struct *monitor=NULL; static int flag_start=0; static int flag_end=0; static int start; static int end; static int atoi(char a[], int n) { int ret = a[n-1] - '0'; int i = n - 2; int carry = 1; for(;i >= 0; i--) { carry *= 10; ret += (a[i] - '0')*carry; } return ret; } static int get_start_time(void) { struct timeval tv2; struct file *fp; mm_segment_t fs; loff_t pos; char str[10] = {'\0'}; char str2[10] = {0}; int i=0; int mytime; fp = filp_open("/proc/uptime", O_RDONLY, 0); if (IS_ERR(fp)) { printk("open file error\n"); } else { fs = get_fs(); set_fs(KERNEL_DS); pos = 0; vfs_read(fp, str, sizeof(str), &pos); while(str[i] != '.') { str2[i]=str[i]; i++; } mytime=atoi(str2,strlen(str2)); filp_close(fp, NULL); set_fs(fs); do_gettimeofday(&tv2); rtc_time_to_tm(tv2.tv_sec-mytime+monitor->start_time.tv_sec+8*3600,&tm); printk("%d-%d-%d %d:%d:%d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec); return (int)(tv2.tv_sec-mytime+monitor->start_time.tv_sec); } } static int get_end_time(void) { do_gettimeofday(&tv); rtc_time_to_tm(tv.tv_sec+8*3600,&tm); printk("%d-%d-%d %d:%d:%d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec); return (int)tv.tv_sec; } static void test_thread(void *data) { while(!kthread_should_stop()) { monitor=NULL; for_each_process(p){ if(p->pid == tid) { monitor=p; if(flag_start == 0) { printk("start time:"); start=get_start_time(); flag_start=1; } break; } } if(monitor == NULL) { if(flag_end == 0) { printk("stop time:"); end=get_end_time(); flag_end=1; printk("thread %d is last for %d seconds\n",tid,end-start); } } msleep(10); } } static int __init test_thread_init(void) { myThread=kthread_run(test_thread,NULL,"mythread"); return 0; } static void __exit test_thread_exit(void) { if(myThread) { printk("stop thread\n"); kthread_stop(myThread); } } module_init(test_thread_init); //告诉内核当内核模块第一次运行时哪个函数将被执行 module_exit(test_thread_exit); //内核模块被卸载时被执行的函数 MODULE_LICENSE("GPL"); //开源license
Makefile
obj-m := test_thread.o KERNELBUILD := /lib/modules/$(shell uname -r)/build default: make -C $(KERNELBUILD) M=$(shell pwd) modules clean: rm -rf *.o *.ko *.mod.c .*.cmd *.markers *.order *.symvers .tmp_versions
编译模块
make
插入模块
sudo insmod test_thread.ko pid=***
dmesg查看
相关文章推荐
- 测试一个函数的运行时间(C++)
- 什么样的平台定义一个多长的压力测试时间让其稳定运行才是科学的?
- linux下查看一个进程的启动时间和运行时间
- 用回调函数写一个测试方法运行时间
- 如何测试一个函数的运行时间
- linux下查看一个进程的启动时间和运行时间
- 测试一个方法的运行时间
- linux下查看一个进程的启动时间和运行时间
- C语言测试一个函数运行时间的模板
- C++::如何测试一个函数的运行时间
- 一个有关程序运行时间的小测试
- 一个测试运行时间的函数
- 测试进程运行时间的的各种-time
- linux下查看一个进程的启动时间和运行时间
- 测试运行时间
- 这段时间正在搞一个小项目的测试计划和设计工作
- 计算一个Simulink模型运行时间的函数