求大侠指点一下。帮忙看看错误在哪,感谢。。。。

luweiability 2011-06-29 03:41:57
源代码如下:#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include <linux/time.h>
#include<linux/types.h>
#include<linux/proc_fs.h>
#include<linux/fs.h>
#include<linux/string.h>
#include<asm/uaccess.h> /* get_fs(),set_fs(),get_ds() */
#include <linux/mm.h>
#include <linux/syscalls.h>
#include <asm/unistd.h>
#define PROCFS_NAME "clock"
#define DELAYTIME 500 //1000?500
#define WRITE_FILE "/yang/test14/data"
#define NETLINKRATE 1000000//1Mbps
MODULE_LICENSE("GPL");
MODULE_AUTHOR("yangfan");

char *buff = NULL;
char *timebuff = NULL;
char cputmp1[100];
char cputmp2[100];
char memtmp1[100];
char nettmp1[600];
char nettmp2[600];
char timetmp1[100];
char timetmp2[100];
int readlength=0;
long usage1[30];
long usage2[30];
long timespec=0;
int usagetemp[3];
static struct file *filp = NULL;



static struct proc_dir_entry *clock_proc_file;

//called when a read is done on /proc/my_clock
int read_time(char *buf,char **start,off_t offset,int count,int *eof,void *data)
{
struct timespec now;
int len = 0;
if (offset > 0)
{
printk(KERN_INFO "offset %d : /proc/%s : read_time, \ wrote %d Bytes\n", (int)(offset),PROCFS_NAME, len);
*eof = 1;
return len;
}
now = current_kernel_time();
len = sprintf(buf, "%ld %ld\n", now.tv_sec, now.tv_nsec);
//printk(KERN_INFO "now time is %s\n",buf);
return len;
}
int creatproctime()
{
int ret_value = 0;
clock_proc_file=create_proc_entry(PROCFS_NAME,0644,NULL);
clock_proc_file->read_proc = read_time;
//printk(KERN_INFO "Trying to create /proc/clock:\n");
if (clock_proc_file == NULL)
{
ret_value = -ENOMEM;
printk(KERN_INFO "Error: Could not initialize /proc/%s\n",PROCFS_NAME);
}
else
{
//printk(KERN_INFO "Initialize /proc/%s\n",PROCFS_NAME);
}
return ret_value;
}

/*static int show_time(char *timebuffer)
{
struct timespec now;
now=current_kernel_time();
int len=0;
timebuffer=NULL;
len=sprintf(timebuffer,"%ld %ld\n",now.tv_sec,now.tv_nsec);
printk("#####%s\n",timebuffer);
return len;

}*/


int readmem(char* filename,int length,char tmp[])
{
mm_segment_t old_fs;
ssize_t ret;

filp = filp_open(filename, O_RDWR | O_CREAT, 0644);


if(IS_ERR(filp))
printk("open error...\n");

old_fs = get_fs();
set_fs(get_ds());

filp->f_op->llseek(filp,0,0);
ret = filp->f_op->read(filp, tmp, length, &filp->f_pos);
set_fs(old_fs);

if(ret > 0)
;
//printk("read OK\n");
//printk("%s\n",tmp);
else if(ret == 0)
printk("read nothing.............\n");
else
{
printk("read error\n");
return -1;
}
filp_close(filp,NULL);
return 0;

}
void writedata(char *filename, char* data)
{
struct file *filp;
mm_segment_t fs;
filp = filp_open(filename, O_RDWR|O_APPEND, 0644);
if(IS_ERR(filp))
{
printk("open error...\n");
return;
}

fs=get_fs();
set_fs(KERNEL_DS);
filp->f_op->write(filp, data, strlen(data),&filp->f_pos);
//filp->f_op->write(filp, '\n', 1,&filp->f_pos);
set_fs(fs);
filp_close(filp,NULL);
}

void delay(int ns)
{
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(ns*HZ/1000);
}
long time_count(long usge1[],long usge2[])
{
//printk("this is time_count speaking\n");
long time_spec=1000*(usge2[0]-usge1[0])+(usge2[1]-usge1[1])/1000000;
printk("the time_spec is %d ms\n",time_spec);
return time_spec;

}
int cpu_uage(long usge1[],long usge2[])
{
//printk("this is cpu_uage speaking\n");
int cpu_freerate=100*(usge2[3]-usge1[3])/((usge2[0]+usge2[1]+usge2[2]+usge2[3]+usge2[4]+usge2[5]+usge2[6])-(usge1[0]+usge1[1]+usge1[2]+usge1[3]+usge1[4]+usge1[5]+usge1[6]));
printk("the cup freeage is %d%% now\n",cpu_freerate);
return cpu_freerate;

}

int mem_uage(long usge1[])
{
//printk("this is mem_uage speaking\n");
//printk("usg11%d usg10 %d\n",usge1[1],usge1[0]);
//printk("this is mem_uage speaking\n");
int mem_freerate=100*usge1[1]/usge1[0];
printk("the mem freeage is %d%% now\n",mem_freerate);
return mem_freerate;
}

int net_uage(long usge1[],long usge2[],long timesp)
{
//printk("this is net_uage speaking\n");

//long net_freerate=100-((usge2[17]+usge2[25])-(usge1[17]+usge1[25]))/(netablity*timepect);//500ms?1000ms?
//printk("usge2[17] %d\nusge1[17] %d\nusge2[25] %d\nusge1[25] %d\n\n",usage2[17],usage1[17],usage2[25],usge1[25]);
int net_usage=(100*500*((usge2[17]-usge1[17])+(usge2[25]-usge1[25]))/timesp)/NETLINKRATE;//0.5s if 1s,yao chu 2
printk("the net has been taken is %d %% \n",net_usage);
return net_usage;

}

long formatnum(int count,char temp[],long usage[])
{
int i=0;
int j=1;
int k=0;

for(i=0;i<count;i++)
{
if(temp[i]>='0'&&temp[i]<='9')
{
while(temp[i+j]>='0'&&temp[i+j]<='9')
{
j++;
}

usage[k]=simple_strtoul(&temp[i],NULL,10);
i=i+j;
//if chao jie
j=1;

//printk("#%d##%d\n",k,usage[k]);
//wei shu
k++;//jia wei shu
}
}
/* if(type==1)
cpu_uage(usage);
if(type==2)
mem_uage();
if(type==3)
net_uage();
*/
}


static int __init getinfo_init(void)
{
printk("Welcome!!!\n");
creatproctime();//creat the "clock" file in /proc

int count=0;

long cpu_usgae_temp=0;
long mem_usgae_temp=0;
long net_usgae_temp=0;
char bu[20];

int sample_time=10;
int measure_time=0;
while(measure_time<10)
{
for(count=0;count<sample_time;count++)
{

readmem("/proc/stat",50,cputmp1);
//no need to read meminfo twice
readmem("/proc/net/dev",600,nettmp1);
readmem("/proc/clock",30,timetmp1);//the fist data is not accurate.
delay(DELAYTIME);
printk("the NO. %d time begin #########\n",count);

readmem("/proc/stat",50,cputmp2);
readmem("/proc/meminfo",60,memtmp1);
readmem("/proc/net/dev",600,nettmp2);



formatnum(50,cputmp1,usage1);
formatnum(50,cputmp2,usage2);
cpu_usgae_temp=cpu_uage(usage1,usage2);

formatnum(60,memtmp1,usage1);
mem_usgae_temp=mem_uage(usage1);

readmem("/proc/clock",30,timetmp2);
formatnum(30,timetmp1,usage1);
formatnum(30,timetmp2,usage2);
timespec=time_count(usage1,usage2);

formatnum(600,nettmp1,usage1);
formatnum(600,nettmp2,usage2);
net_usgae_temp=net_uage(usage1,usage2,timespec);
//printk("the NO. %d time end %%%%%%%#########\n",count);
if(cpu_usgae_temp>usagetemp[0])
usagetemp[0]=cpu_usgae_temp;
if(mem_usgae_temp>usagetemp[1])
usagetemp[1]=mem_usgae_temp;
if(net_usgae_temp>usagetemp[2])
usagetemp[2]=net_usgae_temp;
delay(DELAYTIME);



}
// printk("the cpu is %d\n",usagetemp[0]);
// printk("the mem is %d\n",usagetemp[1]);
// printk("the net is %d\n",usagetemp[2]);

if ((usagetemp[0]<10||usagetemp[1]<10||usagetemp[2]<10)&&sample_time<15)
sample_time++;
if ((usagetemp[0]>90||usagetemp[1]>90||usagetemp[2]>90)&&sample_time>5)
sample_time--;
if ((usagetemp[0]>10&&usagetemp[0]<90)&&(usagetemp[1]>10&&usagetemp[1]<90)&&(usagetemp[2]>10&&usagetemp[2]<90))
sample_time=10;
printk("the samep_time is %d\n",sample_time);
sprintf(bu,"%d %d %d,%d,%d\n",measure_time,sample_time,usagetemp[0],usagetemp[1],usagetemp[2]);

buff =bu;
//printk("%s",buff);
writedata(WRITE_FILE,buff);
measure_time++;

}
return 0;
}

static void __exit getinfo_exit(void)
{
remove_proc_entry(PROCFS_NAME,NULL);
printk("Goodbye!!!\n");


}
module_init(getinfo_init);
module_exit(getinfo_exit);


makefile如下:
obj-m := readmem.o
KERNEL_DIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
make -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
rm -f *.mod.c *.mod.o *.ko *.o *.tmp_versions

错误代码如下:
[root@local000c291f1f43 test14]# make
make -C /lib/modules/2.6.18-164.el5xen/build SUBDIRS=/yang/test14 modules
make[1]: Entering directory `/usr/src/kernels/2.6.18-164.el5-xen-i686'
CC [M] /yang/test14/readmem.o
/yang/test14/readmem.c:47:19: 警告:未知的转义序列:‘\040’
/yang/test14/readmem.c:57: 警告:函数声明不是一个原型
/yang/test14/readmem.c: In function ‘time_count’:
/yang/test14/readmem.c:148: 警告:格式 ‘%d’ 需要类型 ‘int’,但实参 2 的类型为 ‘long int’
/yang/test14/readmem.c: In function ‘formatnum’:
/yang/test14/readmem.c:215: 警告:在有返回值的函数中未发现 return 语句
/yang/test14/readmem.c: In function ‘getinfo_init’:
/yang/test14/readmem.c:223: 警告:ISO C90 不允许混合使用声明和代码
Building modules, stage 2.
MODPOST
CC /yang/test14/readmem.mod.o
LD [M] /yang/test14/readmem.ko

编译出来的readmem.ko 可以运行入内核 。但是有个问题运行后程序会读取/proc的内容写入data文件中。
但是只要写到1个循环 就会出现读取设备超时(我是从/VAR/LOG/MESSAGE中查到消息的)
估计是循环的问题?还是别的? 请大侠们指教一二 感谢
...全文
77 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
天亮后说晚安 2011-06-30
  • 打赏
  • 举报
回复
太长无心看。。。
mycrazycracy 2011-06-30
  • 打赏
  • 举报
回复
呃,打酱油路过,太长无心看。。。
luweiability 2011-06-29
  • 打赏
  • 举报
回复
没人么 ?顶顶 。麻烦大侠看看!!1
luweiability 2011-06-29
  • 打赏
  • 举报
回复
补充:/VAR/LOG/MESSAGE中查到的消息:

Jun 29 15:43:20 local000c291f1f43 tpvmlpd[5763]: device read timed out
Jun 29 15:43:56 local000c291f1f43 scim-bridge: The lockfile is destroied
Jun 29 15:43:56 local000c291f1f43 scim-bridge: Cleanup, done. Exitting...
Jun 29 15:44:07 local000c291f1f43 scim-bridge: The lockfile is destroied
Jun 29 15:44:07 local000c291f1f43 scim-bridge: Cleanup, done. Exitting...

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧