8000 Merge branch 'master' of github.com:home-coder/ring-buffer · home-coder/ring-buffer@1a6a157 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a6a157

Browse files
committed
Merge branch 'master' of github.com:home-coder/ring-buffer
2 parents fa8740e + 6f3ba44 commit 1a6a157

File tree

5 files changed

+122
-31
lines changed

5 files changed

+122
-31
lines changed

Makefile

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1-
#ARCH=arm
1+
#ARCH=armh
2+
#ARCH=armc
23
#与硬件相关,需要联调
3-
ARCH=x86
4+
#ARCH=x86org
5+
#ARCH=x86mmap
6+
ARCH=amlogic
7+
48
#$(info, $(ARCH))//内核 androidmk使用
5-
ifeq ($(ARCH), arm)
9+
ifeq ($(ARCH), armh)
610
CC = ~/basic/cross_compile/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-fsl-linux-gnueabi-gcc
711
CFLAGS = -O2 -Wall --static
8-
else
12+
endif
13+
14+
ifeq ($(ARCH), armc)
15+
CC = /opt/toolchain/mstar/linaro-aarch64_linux-2014.09_843419-patched/bin/aarch64-linux-gnu-gcc
16+
CFLAGS = -O2 -Wall --static
17+
endif
18+
19+
ifeq ($(ARCH), amlogic)
20+
CC = /opt/toolchain/amlogic/gcc-linaro-aarch64-linux-gnu-4.9-2014.09_linux/bin/aarch64-linux-gnu-gcc
21+
CFLAGS = -O2 -Wall --static
22+
endif
23+
24+
ifeq ($(ARCH), x86mmap)
25+
CC = gcc
26+
CFLAGS = -O2 -Wall --static
27+
endif
28+
29+
ifeq ($(ARCH), x86org)
930
CC = gcc
1031
CFLAGS = -DORG_TEST -O2 -Wall --static
1132
endif

helper/kernel/Makefile

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
1-
export ARCH=arm
2-
export CROSS_COMPILE=/home/java/basic/cross_compile/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-fsl-linux-gnueabi-
3-
LINUX_PATH :=/home/java/opt/hhzn/kernel_imx
1+
#ARCH=armh
2+
#ARCH=armc
3+
#ARCH=x86c
4+
ARCH=amlogic
45

6+
ifeq ($(ARCH), armh)
7+
export ARCH=arm
8+
export CROSS_COMPILE=/home/java/basic/cross_compile/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-fsl-linux-gnueabi-
9+
LINUX_PATH :=/home/java/opt/hhzn/kernel_imx
10+
endif
11+
12+
ifeq ($(ARCH), armc)
13+
export ARCH=arm64
14+
export CROSS_COMPILE=/opt/toolchain/mstar/linaro-aarch64_linux-2014.09_843419-patched/bin/aarch64-linux-gnu-
15+
LINUX_PATH :=/home/jiangxiujie/mstar-828-tv/Mstar-828/vendor/mstar/kernel/3.10.40
16+
endif
17+
18+
ifeq ($(ARCH), amlogic)
19+
export ARCH=arm64
20+
export CROSS_COMPILE=/opt/toolchain/amlogic/gcc-linaro-aarch64-linux-gnu-4.9-2014.09_linux/bin/aarch64-linux-gnu-
21+
LINUX_PATH :=/home/jiangxiujie/z1111/Amlogic-905/common
22+
endif
23+
24+
ifeq ($(ARCH), x86c)
25+
LINUX_PATH :=/usr/src/linux-headers-4.4.0-75-generic
26+
endif
527
obj-m += pages.o
628

729
all:

helper/kernel/pages.c

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
1-
//#include <linux/config.h>
21
#include <linux/module.h>
32
#include <linux/kernel.h>
43
#include <linux/mm.h>
54
#include <linux/delay.h>
5+
#include <linux/sched.h>//wake_up_process()
6+
#include <linux/kthread.h>//kthread_ceate(), kthread_run()
67

78
static unsigned long p = 0;
89
static unsigned long pp = 0;
910
int count = 0;
1011
char buf[32];
12+
static struct task_struct *mmap_task = NULL;
1113

12-
static int put_pages(void *p)
14+
/*循环的写数据*/
15+
static int put_pages(void *param)
1316
{
14-
while (1) {
15-
msleep(1);
16-
memset(buf, 0, 32);
17-
sprintf(buf, "hello %d", count++ % 1000);
18-
strcpy((char *)p, buf);
17+
while (!kthread_should_stop()) { //不要用while 1, 因为那样rmmod会出错
18+
msleep(1);
19+
memset(buf, 0, 32);
20+
sprintf(buf, "hello %d", count++ % 1000);
21+
if ((void *)p == NULL) {
22+
printk("kerr, ---->p is null\n");
23+
return 0;
24+
}
25+
26+
//printk("put mmap -->%d\n", count);
27+
strcpy((char *)p, buf);
1928
}
29+
2030
return 0;
2131
}
2232

23-
static int __init shao_init(void)
33+
static int creat_work(void)
2434
{
25-
//分配共享内存(一个页面)
35+
int err = -1;
36+
37+
mmap_task = kthread_run(put_pages, NULL, "mmap_task");
38+
if (IS_ERR(mmap_task)) {
39+
printk("create new kernel thread failed\n");
40+
err = PTR_ERR(mmap_task);
41+
mmap_task = NULL;
42+
return err;
43+
}
44+
45+
return 0;
46+
}
2647

48+
static int __init kfifo_mmap_init(void)
49+
{
50+
//分配共享内存(一个页面)
2751
p = __get_free_pages(GFP_KERNEL, 0);
2852

2953
SetPageReserved(virt_to_page(p));
@@ -32,21 +56,27 @@ static int __init shao_init(void)
3256
printk("<1> pp = 0x%lx\n", pp);
3357

3458
//在共享内存中写上一个字符串
35-
kernel_thread(put_pages, (void *)p, 0);
59+
// kernel_thread(put_pages, (void *)p, 0);
60+
61+
creat_work();
62+
3663
return 0;
3764
}
3865

39-
static void __exit shao_exit(void)
66+
static void __exit kfifo_mmap_exit(void)
4067
{
68+
printk("----module exit----\n");
69+
if (mmap_task) {
70+
kthread_stop(mmap_task);
71+
mmap_task = NULL;
72+
}
4173

4274
ClearPageReserved(virt_to_page(p));
43-
4475
free_pages(p, 0);
45-
4676
}
4777

4878
MODULE_LICENSE("GPL");
49-
MODULE_AUTHOR("Kuanish");
79+
MODULE_AUTHOR("home-coder");
5080
MODULE_DESCRIPTION("mmap test");
51-
module_init(shao_init);
52-
module_exit(shao_exit);
81+
module_init(kfifo_mmap_init);
82+
module_exit(kfifo_mmap_exit);

kfifo.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ unsigned int __kfifo_put(struct kfifo *fifo, unsigned char *buffer,
5757
memcpy(fifo->buffer, buffer + l, len - l);
5858

5959
fifo->in += len;
60-
static int i = 0;
61-
printf("put put put %d\n", i++);
6260
return len;
6361
}
6462

test.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ void thread_reader(void *param)
4747
} else {
4848
counter++;
4949
if (counter > 2) {
50+
printf("这里会走到吗\n");
5051
break;
5152
}
5253
}
@@ -84,16 +85,18 @@ void thread_writer(void *param)
8485
// usleep(100);
8586
}
8687
}
88+
8789
#else
8890
void thread_writer(void *param)
8991
{
9092
#define PAGE_SIZE (4*1024)
91-
#define PAGE_OFFSET 0xc0000000 //32位的偏移3G, 但是我在内核求得用户空间地址了,这个变量不需要了
92-
#define KERNEL_VIRT_ADDR 0x22049000 //此处地址即为内核模块打印的地址p,动态的不固定,需要自行修改
93+
//#define PAGE_OFFSET 0xc0000000 //32位的偏移3G, 但是我在内核求得用户空间地址了,这个变量不需要了
94+
#define KERNEL_VIRT_ADDR 0x8346000 //此处地址即为内核模块打印的地址p,动态的不固定,需要自行修改
9395
unsigned char *buffer;
9496
int fd;
9597
unsigned long phy_addr;
9698
struct ll_param *p = (struct ll_param *)param;
99+
unsigned int klen = 0;
97100

98101
fd = open("/dev/mem", O_RDWR);
99102
if (fd == -1)
@@ -104,9 +107,26 @@ void thread_writer(void *param)
104107
if (buffer == MAP_FAILED)
105108
perror("mmap");
106109
while (1) {
107-
kfifo_put(p->fifo, buffer, 32); //strlen((char *)buffer)
108-
bzero(buffer, 32);
109-
usleep(10);
110+
pthread_mutex_lock(&qlock);
111+
112+
if (buffer[0] != '\0') {
113+
if (klen >= FIFO_LENGTH) {
114+
pthread_cond_wait(&q_not_full, &qlock);
115+
}
116+
kfifo_put(p->fifo, buffer, 32); //strlen((char *)buffer)
117+
bzero(buffer, 32);
118+
} else {
119+
//等待内核填充mmap内存缓冲, kfifo机制的作用就是让快者线程腾出CPU一段时间,假设500us, 情景适用于生产者速度大于消费者
120+
//如果底层吐数据为低速总线,而上层取数据为从内存取高速的,这个延时就是错误的方式, 需要给消费者一个延时让出CPU,并空闲等待数据到来
121+
//但是如果从内存去玩数据,还需要计算等等很多操作,那么消费者速度就会低于生产者,可以使用下面的sleep..注delay忙等不可以.
122+
usleep(500);
123+
pthread_mutex_unlock(&qlock);
124+
continue;
125+
}
126+
127+
pthread_mutex_unlock(&qlock);
128+
pthread_cond_signal(&q_not_empty);
129+
usleep(500);//给读者一段时间处理数据
110130
}
111131

112132
munmap(buffer, PAGE_SIZE);
@@ -132,6 +152,6 @@ int main(void)
132152
pthread_join(pidw, NULL);
133153

134154
kfifo_free(fifo.fifo);
135-
printf("nGoodbye!n\n");
155+
printf("====== <kfifo & mmap test over> =====\n");
136156
return 0;
137157
}

0 commit comments

Comments
 (0)
0