poll源码剖析

Poll系统调用,是在指定时间内轮询一定数量的文件描述符,以测试是否有就绪者。

函数原型:

int poll (struct poddfd *fds, nfds_t nfds, int timeout);

 

· fds参数是一个pollfd结构类型的数组,指定所有感兴趣的文件描述符上发生的可读,可写和异常等事件。

· nfds参数指定被监听事件集合fds的大小。

    typedef unsigned long int nfds_t;

· timeout参数指定poll的超时值,单位是毫秒。

  当超时为-1时,轮询调用将一直阻塞,直到某个事件发生;当超时为0时,轮询调用立即返回。

 

基本数据结构

一个pollfd结构类型的数组,指定所有感兴趣的文件描述符上发生的可读,可写和异常等事件。

fd是文件描述符;事件是注册的事件,即监听FD上的事件; revents中是指实际发生的事情,由内核填充。

struct pollfd {
	int fd;
	short events;
	short revents;
};

poll_wqueues:

struct poll_wqueues {
	poll_table pt;
	struct poll_table_page * table;
	int error;
};

poll_table_entry:

struct poll_table_entry {
	struct file * filp;
	wait_queue_t wait;/* 等待队列*/
	wait_queue_head_t * wait_address;
};

poll_list:

struct poll_list {
	struct poll_list *next;
	int len;
	struct pollfd entries[0];
};

链表结点由一个指向struct poll_list的指针掌控,而众多strut pollfd就通过struct list的entries成员访问。

每个链表的节点是一个page大小(通常是4K)

 

基本结构关系:

 

 

 

 

Poll系统调用原理:

    先注册回调函数__poll_wait,再初始化table variable(类型为struct poll_wqueues);拷贝用户传入的struct pollfd;

    轮流调用所有FD对应的轮询(把current挂到每个FD对应的设备等待队列上)。在设备收到一条消息或填写完文件数据(磁盘设备)后,会唤醒设备等待队列上的进程,这时current便被唤醒了。

    当前唤醒后离开sys_poll。

 

(主要基于内核源代码2.6.9版本,在FD \ select.c中)

sys_poll

asmlinkage long sys_poll(struct pollfd __user * ufds, unsigned int nfds, long timeout)
{
	struct poll_wqueues table;
 	int fdcount, err;
 	unsigned int i;
	struct poll_list *head;
 	struct poll_list *walk;

	/* Do a sanity check on nfds ... OPEN_MAX=256*/
	if (nfds > current->files->max_fdset && nfds > OPEN_MAX)
		return -EINVAL;

	if (timeout) {
		/* Careful about overflow in the intermediate values */
		if ((unsigned long) timeout < MAX_SCHEDULE_TIMEOUT / HZ)
			timeout = (unsigned long)(timeout*HZ+999)/1000+1;
		else /* Negative or overflow */
			timeout = MAX_SCHEDULE_TIMEOUT;
	}

	poll_initwait(&table);

	head = NULL;
	walk = NULL;
	i = nfds;
	err = -ENOMEM;
	while(i!=0) {
		struct poll_list *pp;
		pp = kmalloc(sizeof(struct poll_list)+
				sizeof(struct pollfd)*
				(i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i),
					GFP_KERNEL);
		if(pp==NULL)
			goto out_fds;
		pp->next=NULL;
		pp->len = (i>POLLFD_PER_PAGE?POLLFD_PER_PAGE:i);
		if (head == NULL)
			head = pp;
		else
			walk->next = pp;

		walk = pp;
		if (copy_from_user(pp->entries, ufds + nfds-i, 
				sizeof(struct pollfd)*pp->len)) {
			err = -EFAULT;
			goto out_fds;
		}
		i -= pp->len;
	}
	fdcount = do_poll(nfds, head, &table, timeout);

	/* OK, now copy the revents fields back to user space. */
	walk = head;
	err = -EFAULT;
	while(walk != NULL) {
		struct pollfd *fds = walk->entries;
		int j;

		for (j=0; j < walk->len; j++, ufds++) {
			if(__put_user(fds[j].revents, &ufds->revents))
				goto out_fds;
		}
		walk = walk->next;
  	}
	err = fdcount;
	if (!fdcount && signal_pending(current))
		err = -EINTR;
out_fds:
	walk = head;
	while(walk!=NULL) {
		struct poll_list *pp = walk->next;
		kfree(walk);
		walk = pp;
	}
	poll_freewait(&table);
	return err;
}

(1)poll_initwait()

void poll_initwait(struct poll_wqueues *pwq)
{
	init_poll_funcptr(&pwq->pt, __pollwait);
	pwq->error = 0;
	pwq->table = NULL;
}
static inline void init_poll_funcptr(poll_table *pt, poll_queue_proc qproc)
{
	pt->qproc = qproc;
}

注册回调函数__pollwait,

void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *_p)
{
	struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
	struct poll_table_page *table = p->table;

	if (!table || POLL_TABLE_FULL(table)) {
		struct poll_table_page *new_table;

		new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
		if (!new_table) {
			p->error = -ENOMEM;
			__set_current_state(TASK_RUNNING);
			return;
		}
		new_table->entry = new_table->entries;
		new_table->next = table;
		p->table = new_table;
		table = new_table;
	}

	/* Add a new entry */

		struct poll_table_entry * entry = table->entry;
		table->entry = entry+1;
		get_file(filp);
		entry->filp = filp;
		entry->wait_address = wait_address;
		init_waitqueue_entry(&entry->wait, current);
		add_wait_queue(wait_address, &entry->wait);
}

(2)调用copy_from_user()

    这里的循环中,建立链表,然后调用copy_from_user将文件描述符从用户空间拷贝到内核空间。也就是把用户态的struct pollfd拷进进这些条目中。通常用户程序的poll调用就监控几个fd,所以上面这个链表通常也就只需要一个节点,即操作系统的一页。

但是,当用户传入的fd很多时,由于poll系统调用每次要把所有的struct pollfd拷进进内核,所以参数传递和页分配此时就成了poll系统调用的性能瓶颈。

/**
 * 从用户空间复制任意大小的块。
 */
unsigned long
copy_from_user(void *to, const void __user *from, unsigned long n)
{
	might_sleep();
	BUG_ON((long) n < 0);
	if (access_ok(VERIFY_READ, from, n))
		n = __copy_from_user(to, from, n);
	else
		memset(to, 0, n);
	return n;
}

(3)do_poll()

static int do_poll(unsigned int nfds,  struct poll_list *list,
			struct poll_wqueues *wait, long timeout)
{
	int count = 0;
	poll_table* pt = &wait->pt;

	if (!timeout)
		pt = NULL;
 
	for (;;) {
		struct poll_list *walk;
		set_current_state(TASK_INTERRUPTIBLE);
		walk = list;
		while(walk != NULL) {
			do_pollfd( walk->len, walk->entries, &pt, &count);
			walk = walk->next;
		}
		pt = NULL;
		if (count || !timeout || signal_pending(current))
			break;
		count = wait->error;
		if (count)
			break;
		timeout = schedule_timeout(timeout);
	}
	__set_current_state(TASK_RUNNING);
	return count;
}

在对循环中,直到计数大于0才跳出循环。而count 主要是依靠do_pollfd函数处理。

当用户传入的FD很多时(比如1000个),对do_pollfd就会调用很多次,调查效率出现瓶颈。

 

set_current _STATE和signal_pending函数

    保障了当用户程序在调用调查后挂起时,发信号可以让程序迅速退出民调调用,而通常的系统调用是不会被信号打断的。

 

do_pollfd()

static void do_pollfd(unsigned int num, struct pollfd * fdpage,
	poll_table ** pwait, int *count)
{
	int i;

	for (i = 0; i < num; i++) {
		int fd;
		unsigned int mask;
		struct pollfd *fdp;

		mask = 0;
		fdp = fdpage+i;
		fd = fdp->fd;
		if (fd >= 0) {
			struct file * file = fget(fd);
			mask = POLLNVAL;
			if (file != NULL) {
				mask = DEFAULT_POLLMASK;
				if (file->f_op && file->f_op->poll)
					mask = file->f_op->poll(file, *pwait);
				mask &= fdp->events | POLLERR | POLLHUP;
				fput(file);
			}
			if (mask) {
				*pwait = NULL;
				(*count)++;
			}
		}
		fdp->revents = mask;
	}
}

 

    如果FD对应的是某个 socket,do_pollfd调用的就是网络设备驱动实现的轮询;

 

    如果FD对应的是某个EXT3文件系统的一个打开文件,那么do_pollfd调用的就是EXT3文件系统驱动实现的Poll。

这个file- > f_op->poll的是设备驱动程序实现的。

而设备驱动程序的标准实现是:

(1)调用poll_wait,即以设备自己的等待队列为参数(通常设备都有自己的等待队列)

(2)调用struct poll_talbe的回调函数。

 

__pollwait

    回调函数__pollwait的作用是创建一个poll_table_entry数据结构(一次__poll_wait即一次设备poll调用只创建一个poll_table_entry),并通过struct poll_table_entry的wait成员,把currrent挂在设备的等待队列上,此处的等待队列是wait_address,对应tcp_poll中的SK-Ⅱ> sk_sleep。

void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *_p)
{
	struct poll_wqueues *p = container_of(_p, struct poll_wqueues, pt);
	struct poll_table_page *table = p->table;

	if (!table || POLL_TABLE_FULL(table)) {
		struct poll_table_page *new_table;

		new_table = (struct poll_table_page *) __get_free_page(GFP_KERNEL);
		if (!new_table) {
			p->error = -ENOMEM;
			__set_current_state(TASK_RUNNING);
			return;
		}
		new_table->entry = new_table->entries;
		new_table->next = table;
		p->table = new_table;
		table = new_table;
	}

	/* Add a new entry */

		struct poll_table_entry * entry = table->entry;
		table->entry = entry+1;
		get_file(filp);
		entry->filp = filp;
		entry->wait_address = wait_address;
		init_waitqueue_entry(&entry->wait, current);
		add_wait_queue(wait_address, &entry->wait);
}

接着调用tcp_poll(),

unsigned int tcp_poll(struct file *file, struct socket *sock, poll_talbe *wait)

{

    unsigned int mask;

    struct sock *sk = sock->sk;

    struct tcp_sock *tp = tcp_sk(sk);

    poll_wait(file, sk->sk_sleep, wait);

    ......//省略判断状态,返回状态值

}

 

struct sock是socket连接的内核表示,sk->sk_sleep是struct wait_queue_head_t结构类型,这表示的是socket的等待队列,每一个socket都有自己的一个等待队列,由内核结构struct sock来维护。

其实大多数驱动实现的时候,此时都调用这个函数poll_wait.

poll_wait(file, sk->sk_slepp, wait);

间接调用p->qproc(file, sk->sk_slepp, wait);

 

总结:

1. 统一处理所有事件类型,只需一个事件集参数。用户通过pollfd.events传入感兴趣事件,内核通过修改pollfd.revents成员反馈其中就绪的事件,而events成员保持不变,下此调用poll时无须重置pollfd类型的事件集参数。

2. poll和select类似,每次调用都返回整个用户注册的事件集合(包括就绪的和未就绪的),应用程序索引就绪文件描述符的时间复杂度为O(n)。而epoll是在内核中维护一个事件表,epoll_wait的events参数返回就绪的事件,时间复杂度为O(1).

3. poll和epoll_wait分别用nfds和maxevents参数指定最多监听多少个文件描述符和事件个数,即65535(cat/proc/sys/fs/file-max)。而select允许监听的最大文件描述符个数为1024.

4. poll只能工作在相对低效的LT模式(电平触发),而epoll可工作在ET高效模式(边沿触发)。

5. poll采用轮询方式,即每次调用都要扫描整个注册文件描述符集合,并将其中就绪的文件描述符返回个用户,因此检测就绪事件的时间复杂度是O(n)。epoll则采用回调方式。内核检测到就绪的文件描述符,将触发回调函数,回调函数将该文件描述符上对应的事件插入内核就绪事件队列。内核最后将该就绪事件队列的内容拷贝到用户空间。时间复杂度为O(1).

 

缺点

 

1. 在sys_poll中的循环中,建立链表,然后调用copy_from_user将文件描述符从用户空间拷贝到内核空间。也就是把用户态的struct pollfd拷进进这些条目中。通常用户程序的poll调用就监控几个fd,所以上面这个链表通常也就只需要一个节点,即操作系统的一页。

但是,当用户传入的fd很多时,由于poll系统调用每次要把所有的struct pollfd拷进进内核,所以参数传递和页分配此时就成了poll系统调用的性能瓶颈。

2. 在函数do_poll()中的循环语句中,直到计数大于0才跳出循环。而count 主要是依靠do_pollfd函数处理。

当用户传入的FD很多时(比如1000个),对do_pollfd就会调用很多次,调查效率出现瓶颈。

 

应用场景

1. 适用于I/O少,即连接数量少,但活动连接多的情况。socket少于1000个。

2. 没有其他线程干扰。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ModbusPoll是一种常用的Modbus通信协议工具,在工业控制系统中广泛应用。ModbusPoll源码指的是该软件的程序代码。 ModbusPoll源码通常由C或C++编写,采用面向对象的编程风格。源码主要包含通信协议的实现、界面设计、数据解析和处理等功能。 在通信协议实现方面,源码通过使用串行通信和Modbus协议规范,实现了与Modbus从设备之间的数据交换。它支持Modbus协议的主要功能,如读取/写入寄存器、读取/写入线圈等。源码会对Modbus报文进行解析和封装,实现对Modbus通信指令的处理。 在界面设计方面,源码采用图形用户界面(GUI),通过可视化操作界面,方便用户进行参数设置和数据监控。源码提供了各种界面元素,如窗口、按钮、文本框等,以实现用户与软件的交互。 在数据解析和处理方面,源码会对收到的Modbus数据进行解析,提取出有用的信息。它将数据进行处理,并根据用户设置的规则进行分析。然后,源码将解析后的数据显示在界面上,以便用户进行监控和分析。 ModbusPoll源码可以为用户提供一个基于Modbus通信协议的开发平台,用户可以根据自己的需求进行二次开发和定制。通过查看源码,用户可以理解Modbus通信协议的实现细节,从而更好地掌握和应用该通信协议。 总的来说,ModbusPoll源码是ModbusPoll软件的程序代码,它实现了Modbus通信协议的相关功能,包括通信协议实现、界面设计、数据解析和处理。用户可以通过查看源码,进行二次开发和定制,以满足自己的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值