歡迎您光臨本站 註冊首頁

求大俠指點一下。幫忙看看錯誤在哪,感謝。。。。

←手機掃碼閱讀     火星人 @ 2014-03-24 , reply:0

源代碼如下:#include
#include
#include
#include
#include
#include
#include
#include
#include /* get_fs(),set_fs(),get_ds() */
#include
#include
#include
#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 {
if(temp>='0'&&temp<='9')
{
while(temp[i+j]>='0'&&temp[i+j]<='9')
{
j++;
}

usage[k]=simple_strtoul(&temp,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 {

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中查到消息的)
估計是循環的問題?還是別的? 請大俠們指教一二 感謝


補充:/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...

[火星人 ] 求大俠指點一下。幫忙看看錯誤在哪,感謝。。。。已經有706次圍觀

http://coctec.com/docs/linux/show-post-141878.html