测试一个进程的运行时间

功能:测试一个进程开始结束的运行时间

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查看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值