Linux(内核剖析):15---内核数据结构之队列(struct kfifo)
程序员文章站
2022-07-14 16:22:16
...
一、队列概述
- 任何操作系统内核都少不了一种编程模型:生产者和消费者。在该模式中,生产者创建数据(比如说需要读取的错误信息或者需要处理的网络包),而消费者则反过来,读取消息和处理包,或者以其他方式消费这些数据。实现该模型的最简单的方式无非是使用队列。生产者将数据推进 队列,然后消费者从队列中摘取数据。消费者获取数据的順序和推入队列的顺序一致。也就是说,第一个进队列的数据一定是第一个离开队列的。也正是这个原因,队列也称为FIFO。顾名思义,FIFO就是先进先出的缩写
- 下图是一个标准队列的例子
二、Linux内核队列概述(kfifo)
- Linux内核通用队列实现称为kfifo
- 相关声明在文件<include/kfifo.h>中,相关定义在<kernel/kfifo.c>中
struct kfifo
- 以下代码来自:Linux2.6.22/include/kfifo.h
struct kfifo { unsigned char *buffer; /* the buffer holding the data */ unsigned int size; /* the size of the allocated buffer */ unsigned int in; /* data is added at offset (in % size) */ unsigned int out; /* data is extracted from off. (out % size) */ spinlock_t *lock; /* protects concurrent modifications */ };
enqueue、dequeue
- Linux提供了两个主要操作:enqueue(入队列)、dequeue(出队列)
入口偏移、出口偏移
- kfifo对象维护了两个偏移量:入口偏移和出口偏移
- 入口偏移是指下一次入队列时的位置
- 出口偏移是指下一次出队列时的位置
- 出口偏移总是小于等于入口偏移,否则无意义,因为那样说明要出队列的元素根本还没有入队列
三、创建队列
- 备注:kfifo_init和kfifo_alloc的size必须是2的幂
创建队列并分配缓冲区(kfifo_init)
- 该函数创建并初始化一个kfifo对象,其使用buffer指向的size字节大小的内存。在kfifo_alloc中会被调用
/** * kfifo_init - allocates a new FIFO using a preallocated buffer * @buffer: the preallocated buffer to be used. * @size: the size of the internal buffer, this have to be a power of 2. * @gfp_mask: get_free_pages mask, passed to kmalloc() * @lock: the lock to be used to protect the fifo buffer * * Do NOT pass the kfifo to kfifo_free() after use! Simply free the * &struct kfifo with kfree(). */ struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size, gfp_t gfp_mask, spinlock_t *lock) { struct kfifo *fifo; /* size must be a power of 2 */ BUG_ON(size & (size - 1)); fifo = kmalloc(sizeof(struct kfifo), gfp_mask); if (!fifo) return ERR_PTR(-ENOMEM); fifo->buffer = buffer; fifo->size = size; fifo->in = fifo->out = 0; fifo->lock = lock; return fifo; } EXPORT_SYMBOL(kfifo_init);
动态创建(kfifo_alloc)
- 参数:
- size:创建的大小
- gfp_mask:使用此参数标识分配队列(在后面讨论内存分配时会介绍)
- 返回值:返回创建的队列
- kfifo_alooc()动态创建的队列需要使用kfifo_free来释放(kfifo_free见下)
/** * kfifo_alloc - allocates a new FIFO and its internal buffer * @size: the size of the internal buffer to be allocated. * @gfp_mask: get_free_pages mask, passed to kmalloc() * @lock: the lock to be used to protect the fifo buffer * * The size will be rounded-up to a power of 2. */ struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask, spinlock_t *lock) { unsigned char *buffer; struct kfifo *ret; /* * round up to the next power of 2, since our 'let the indices * wrap' tachnique works only in this case. */ if (size & (size - 1)) { BUG_ON(size > 0x80000000); size = roundup_pow_of_two(size); } buffer = kmalloc(size, gfp_mask); if (!buffer) return ERR_PTR(-ENOMEM); ret = kfifo_init(buffer, size, gfp_mask, lock); if (IS_ERR(ret)) kfree(buffer); return ret; } EXPORT_SYMBOL(kfifo_alloc);
静态声明kfifo
- 静态声明kfifo比较简单,但不太常用
- 例如下面会创建一个名称为name,大小为size的kfifo对象。和前面一样,size必须是2的幂
DECLARE_KFIFO(name,size); INIT_KFIFO(name);
四、入队列(kfifo_put)
- 将数据推入队列使用这个函数
kfifo_put
- 参数:把buffer所指的len字节数据拷贝到fifo所指的队列中
- 返回值:成功返回推入的数据长度。如果队列中的空闲字节小于len,则该函数值最多可拷贝队列可用空间那么多的数据,这样的话,返回值可能小于len,甚至会返回0,这时意味着没有任何数据被推入
/** * kfifo_put - puts some data into the FIFO * @fifo: the fifo to be used. * @buffer: the data to be added. * @len: the length of the data to be added. * * This function copies at most @len bytes from the @buffer into * the FIFO depending on the free space, and returns the number of * bytes copied. */ static inline unsigned int kfifo_put(struct kfifo *fifo, unsigned char *buffer, unsigned int len) { unsigned long flags; unsigned int ret; spin_lock_irqsave(fifo->lock, flags); ret = __kfifo_put(fifo, buffer, len); spin_unlock_irqrestore(fifo->lock, flags); return ret; }
/** * __kfifo_put - puts some data into the FIFO, no locking version * @fifo: the fifo to be used. * @buffer: the data to be added. * @len: the length of the data to be added. * * This function copies at most @len bytes from the @buffer into * the FIFO depending on the free space, and returns the number of * bytes copied. * * Note that with only one concurrent reader and one concurrent * writer, you don't need extra locking to use these functions. */ unsigned int __kfifo_put(struct kfifo *fifo, unsigned char *buffer, unsigned int len) { unsigned int l; len = min(len, fifo->size - fifo->in + fifo->out); /* * Ensure that we sample the fifo->out index -before- we * start putting bytes into the kfifo. */ smp_mb(); /* first put the data starting from fifo->in to buffer end */ l = min(len, fifo->size - (fifo->in & (fifo->size - 1))); memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l); /* then put the rest (if any) at the beginning of the buffer */ memcpy(fifo->buffer, buffer + l, len - l); /* * Ensure that we add the bytes to the kfifo -before- * we update the fifo->in index. */ smp_wmb(); fifo->in += len; return len; } EXPORT_SYMBOL(__kfifo_put);
五、出队列(kfifo_get)
- 将数据推出队列使用这个函数
kfifo_get
- 参数:从fifo所指的队列中拷贝出长度为len字节的数据到buffer所指的缓冲中
- 返回值:成功返回拷贝的数据长度。如果队列中数据大小小于len,则该函数拷贝出的数据必然小于需要的数据大小
/** * kfifo_get - gets some data from the FIFO * @fifo: the fifo to be used. * @buffer: where the data must be copied. * @len: the size of the destination buffer. * * This function copies at most @len bytes from the FIFO into the * @buffer and returns the number of copied bytes. */ static inline unsigned int kfifo_get(struct kfifo *fifo, unsigned char *buffer, unsigned int len) { unsigned long flags; unsigned int ret; spin_lock_irqsave(fifo->lock, flags); ret = __kfifo_get(fifo, buffer, len); /* * optimization: if the FIFO is empty, set the indices to 0 * so we don't wrap the next time */ if (fifo->in == fifo->out) fifo->in = fifo->out = 0; spin_unlock_irqrestore(fifo->lock, flags); return ret; }
/** * __kfifo_get - gets some data from the FIFO, no locking version * @fifo: the fifo to be used. * @buffer: where the data must be copied. * @len: the size of the destination buffer. * * This function copies at most @len bytes from the FIFO into the * @buffer and returns the number of copied bytes. * * Note that with only one concurrent reader and one concurrent * writer, you don't need extra locking to use these functions. */ unsigned int __kfifo_get(struct kfifo *fifo, unsigned char *buffer, unsigned int len) { unsigned int l; len = min(len, fifo->in - fifo->out); /* * Ensure that we sample the fifo->in index -before- we * start removing bytes from the kfifo. */ smp_rmb(); /* first get the data from fifo->out until the end of the buffer */ l = min(len, fifo->size - (fifo->out & (fifo->size - 1))); memcpy(buffer, fifo->buffer + (fifo->out & (fifo->size - 1)), l); /* then get the rest (if any) from the beginning of the buffer */ memcpy(buffer + l, fifo->buffer, len - l); /* * Ensure that we remove the bytes from the kfifo -before- * we update the fifo->out index. */ smp_mb(); fifo->out += len; return len; } EXPORT_SYMBOL(__kfifo_get);
六、重置队列(kfifo_reset)
- 重置kfifo,排期所有队列的内容
/**
* kfifo_reset - removes the entire FIFO contents
* @fifo: the fifo to be emptied.
*/
static inline void kfifo_reset(struct kfifo *fifo)
{
unsigned long flags;
spin_lock_irqsave(fifo->lock, flags);
__kfifo_reset(fifo);
spin_unlock_irqrestore(fifo->lock, flags);
}
/**
* __kfifo_reset - removes the entire FIFO contents, no locking version
* @fifo: the fifo to be emptied.
*/
static inline void __kfifo_reset(struct kfifo *fifo)
{
fifo->in = fifo->out = 0;
}
七、撤销队列(kfifo_free)
- 撤销一个使用kfifo_alloc()分配的队列,可以调研kfifo_free
/**
* kfifo_free - frees the FIFO
* @fifo: the fifo to be freed.
*/
void kfifo_free(struct kfifo *fifo)
{
kfree(fifo->buffer);
kfree(fifo);
}
EXPORT_SYMBOL(kfifo_free);
- 如果你是使用kfifo_init()方法创建的队列,那么需要负责相关的缓冲。具体方法取决于你是如何创建它的
上一篇: linux内核kfifo(二)
下一篇: 读Linux内核kfifo