缓冲区 :
缓冲区是一个临时存储区域,通常是内存中的一个块,在等待从输入设备或输出设备传输时,项目被放置在其中。它主要用于输入/输出过程。举个例子,如果你要打印一份长文件,你不会希望你的CPU在等待时问你的打印机 "你准备好再来一段了吗?" 相反,CPU会用文档的数据填充一个内存缓冲区,指示打印机打印缓冲区的内容,然后再去做其他事情。
缓存:
缓存是计算机中一个较小且快速的内存组件,它被插入到CPU和主内存之间。为了使这种安排有效。缓存需要比主存储器快得多。这种方法比使用快速存储器设备来实现整个主存储器更经济。
缓冲区最终会循环播放并保存从一个存储位置传输到另一个存储位置的每一条数据(比如在音频处理中使用循环缓冲区)。缓冲区就是这样:在数据流中你的当前位置之前和之后的数据的 "缓冲区"。的确,缓冲区和高速缓存有一些共同点。然而,传统意义上的缓冲区在从一个地方移动到另一个地方时,通常不存储所有的数据(即CPU缓冲区)。
缓存的目的是以一种透明的方式来存储数据,这样一来,只有足够的数据被缓存起来,这样剩余的数据就可以在没有任何性能损失的情况下被传输。在这种情况下,缓冲区只 "预取 "少量的数据(取决于传输速率和缓冲区大小等)。
主要的区别是,缓冲区最终会容纳所有的数据。相反,缓冲区可能保存了所有的、部分的、或者没有的数据(取决于设计)。然而,对缓冲区的访问,就像你一开始就直接访问数据一样--缓冲区的具体内容对缓冲区的 "用户 "是透明的。
区别在于界面。
当你使用缓存来访问一个数据源时,你使用它就好像缓存就是数据源一样--你可以通过缓存来访问数据源的每一部分,而缓存将决定数据来自哪里(缓存本身,或者数据源)。缓存本身决定了要预加载数据的哪些部分(通常只是开头,但有时是全部),而使用中的缓存替换算法决定了哪些/何时从缓存中删除东西。在一个系统中,除了CPU缓存本身之外,最好的例子就是预置器/读取器。两者都是将它们认为你最会使用的数据部分加载到内存中,如果没有缓存的东西,就会恢复到硬盘上。
相反,缓冲区不能用来即时移动你在数据流中的位置,除非新的部分已经被移到缓冲区中。这样做需要缓冲区重新定位(鉴于新的位置超过了缓冲区的长度),实际上需要你
从新的位置 "重新启动 "缓冲区。这方面最好的例子是在Youtube视频中移动滑块。缓冲区的另一个好例子是在Winamp中播放音频。由于音频文件需要由CPU解码,从歌曲被读入,到音频被处理,再到被发送到声卡,需要一些时间。Winamp会缓冲一些音频数据,这样就有足够的音频数据已经被处理,以避免任何 "锁定"(即CPU一直在准备你将在几百毫秒内听到的音频,它从来不是实时的;你听到的是来自缓冲区,这是CPU在过去准备的)。
附录:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CB_WENXUE.c // 菜鸟麦文学的环形缓冲 // A SIMPLE CIRCULAR BUFFER EXAMPLE // // LICENSE : WTFPL // #include <stdio.h> #include <pthread.h> #include <unistd.h> //sleep() is from here #include <malloc.h> #include <sched.h> #include <string.h> #include <stdbool.h> #include <stdint.h> #define DATACAP 16 typedef struct cbuff{ float rt_arr[DATACAP]; int dhead_p; //实际上是 GLOBAL 全局计数器 int dtail_p; int dqlen; }*RQB; struct cbuff *init_cb(void){ struct cbuff *cbStru_ptr = NULL; cbStru_ptr = (struct cbuff *)malloc(sizeof(struct cbuff)); if(cbStru_ptr == NULL) { puts("init_cb: Mem alloc error!
"); return NULL;} cbStru_ptr->dhead_p = 0; cbStru_ptr->dtail_p = 0; cbStru_ptr->dqlen = 0; return cbStru_ptr; } // KEEP IT SIMPLE int isfull_cb(struct cbuff *cbStru_ptr){ return (cbStru_ptr->dqlen == DATACAP) & (cbStru_ptr->dhead_p == cbStru_ptr->dtail_p ); } int isempt_cb(struct cbuff *cbStru_ptr){ return (cbStru_ptr->dqlen != DATACAP) & (cbStru_ptr->dhead_p == cbStru_ptr->dtail_p) ; } int isXrun_cb(struct cbuff *cbStru_ptr){ return (cbStru_ptr->dqlen !=0) & (cbStru_ptr->dhead_p == cbStru_ptr->dtail_p ); } // Delete one element from Data Queue int poll_cb(struct cbuff *cbStru_ptr) { if( isempt_cb(cbStru_ptr) ) { puts("poll_cb: buffer queue is empty"); return -1;} else { cbStru_ptr->rt_arr[ (cbStru_ptr->dhead_p + 1) % DATACAP ] ; cbStru_ptr->dqlen = cbStru_ptr->dqlen -1 ; } return 0 ; } int put_cb(struct cbuff *cbStru_ptr, float rt_data) { if( isfull_cb(cbStru_ptr) ) { puts("put_cb: buffer queue is full. Overwriting data.
"); cbStru_ptr->rt_arr[ cbStru_ptr->dtail_p % DATACAP ] = rt_data; printf (" put_cb:data %f , stored to pos--> %d
", rt_data, (cbStru_ptr->dtail_p) ); cbStru_ptr->dtail_p = (cbStru_ptr->dhead_p +1) % DATACAP ; cbStru_ptr->dhead_p = (cbStru_ptr->dhead_p +1) % DATACAP ; cbStru_ptr->dqlen = DATACAP ; printf ("put_cb:data queue Head ----------------->>> %d
", cbStru_ptr->dhead_p ); printf ("put_cb:data queue Tail ------------------->>> %d
", cbStru_ptr->dtail_p ); printf ("put_cb:data queue Length-------->>> %d
", cbStru_ptr->dqlen ); return -10089; } else { cbStru_ptr->rt_arr[ (cbStru_ptr->dtail_p) % DATACAP ] = rt_data; printf ("put_cb:data %f , stored to pos--> %d
", rt_data, (cbStru_ptr->dtail_p) ); cbStru_ptr->dtail_p = (cbStru_ptr->dtail_p +1) % DATACAP ; //cbStru_ptr->dqlen = cbStru_ptr->dqlen +1 ; cbStru_ptr->dqlen = cbStru_ptr->dqlen +1 ; printf ("put_cb:data queue Head -------------------> %d
", cbStru_ptr->dhead_p ); printf ("put_cb:data queue Tail ---------------------> %d
", cbStru_ptr->dtail_p ); printf ("put_cb:data queue Length----------> %d
", cbStru_ptr->dqlen ); return 0 ; } } float get_cb(struct cbuff *cbStru_ptr, float rt_data ) { if(isempt_cb(cbStru_ptr) ) { puts("
get_cb:buffer empty :p
"); return -10086; } else { rt_data = cbStru_ptr->rt_arr[ cbStru_ptr->dhead_p % DATACAP ]; cbStru_ptr->dhead_p = (cbStru_ptr->dhead_p +1) % DATACAP ; cbStru_ptr->dqlen = (cbStru_ptr->dqlen -1) % DATACAP; return rt_data; } } // Clear the Data Queue int reset_cb(struct cbuff *cbStru_ptr) { cbStru_ptr->dhead_p = 0; cbStru_ptr->dtail_p = 0; cbStru_ptr->dqlen =0; printf( "
reset_cb: Address of Array:%p, Array pointer Size:%d
", cbStru_ptr, sizeof(cbStru_ptr->rt_arr)); return 0; } int gc_cb(struct cbuff *cbStru_ptr) { if(cbStru_ptr == NULL) { puts("gc_cb: pointer null
"); return -1; } free(cbStru_ptr); return 0; } /////////////////////////////////////////////////////////////////////////////////// int main() { float test_input = 0.111111, ret_float; int ret_err; RQB my_cbuff_ptr; my_cbuff_ptr = init_cb(); reset_cb(my_cbuff_ptr) ; printf ("
Size of CBStruct:%d
", sizeof(struct cbuff)); //testing for(int i = 0;i< 50;i++) { ret_err = put_cb(my_cbuff_ptr, i * test_input) ; if(ret_err==0 ) { printf("
Attempt %d: %10.5f <<<<<<<<<<ERROR: %d <<<<<<<<<<<<<<<<<<< Data to inject
", (i+1), i * test_input , ret_err); printf("Data queue Length: %d:
", my_cbuff_ptr->dqlen ); printf("Data injected: %f:
", my_cbuff_ptr->dhead_p ); } else { printf("
Attempt %d: %10.5f <<<<<<<<<<ERROR: %d <<<<<<<<<<<<<<<<<<< Data to inject
", (i+1), i * test_input, ret_err); printf("Data queue Length: %d:
", my_cbuff_ptr->dqlen ); printf("Data injected: %f:
", my_cbuff_ptr->dhead_p ); printf (" Error: %d
", ret_err); } } sleep(1); for(int i = 0;i<32;i++) { ret_float = get_cb(my_cbuff_ptr, ret_float); printf("
get attempt %d ---> %10.5f
", (1+i), ret_float); } //testing 2 for(int i = 0;i< 16;i++) { ret_err = put_cb(my_cbuff_ptr, i * test_input) ; if(ret_err==0 ) { printf("
Test2 try %d: %10.5f <<<<<<<<<<ERROR: %d <<<<<<<<<<<<<<<<<<< Data to inject
", (i+1), i * test_input , ret_err); printf("Data queue Length: %d:
", my_cbuff_ptr->dqlen ); printf("Data injected: %f:
", my_cbuff_ptr->dhead_p ); } else { printf("
Test2 try %d: %10.5f <<<<<<<<<<ERROR: %d <<<<<<<<<<<<<<<<<<< Data to inject
", (i+1), i * test_input, ret_err); printf("Data queue Length: %d:
", my_cbuff_ptr->dqlen ); printf("Data injected: %f:
", my_cbuff_ptr->dhead_p ); printf (" Error: %d
", ret_err); } } sleep(1); for(int i = 0;i<16;i++) { ret_float = get_cb(my_cbuff_ptr, ret_float); printf("
get attempt %d ---> %10.5f
", (1+i), ret_float); } gc_cb(my_cbuff_ptr); return 0; }
效果:
reset_cb: Address of Array:0x40eb90, Array pointer Size:64 Size of CBStruct:76 put_cb:data 0.000000 , stored to pos--> 0 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 1 put_cb:data queue Length----------> 1 Attempt 1: 0.00000 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 1: Data injected: 0.000000: put_cb:data 0.111111 , stored to pos--> 1 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 2 put_cb:data queue Length----------> 2 Attempt 2: 0.11111 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 2: Data injected: 0.111111: put_cb:data 0.222222 , stored to pos--> 2 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 3 put_cb:data queue Length----------> 3 Attempt 3: 0.22222 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 3: Data injected: 0.222222: put_cb:data 0.333333 , stored to pos--> 3 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 4 put_cb:data queue Length----------> 4 Attempt 4: 0.33333 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 4: Data injected: 0.333333: put_cb:data 0.444444 , stored to pos--> 4 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 5 put_cb:data queue Length----------> 5 Attempt 5: 0.44444 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 5: Data injected: 0.444444: put_cb:data 0.555555 , stored to pos--> 5 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 6 put_cb:data queue Length----------> 6 Attempt 6: 0.55555 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 6: Data injected: 0.555555: put_cb:data 0.666666 , stored to pos--> 6 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 7 put_cb:data queue Length----------> 7 Attempt 7: 0.66667 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 7: Data injected: 0.666666: put_cb:data 0.777777 , stored to pos--> 7 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 8 put_cb:data queue Length----------> 8 Attempt 8: 0.77778 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 8: Data injected: 0.777777: put_cb:data 0.888888 , stored to pos--> 8 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 9 put_cb:data queue Length----------> 9 Attempt 9: 0.88889 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 9: Data injected: 0.888888: put_cb:data 0.999999 , stored to pos--> 9 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 10 put_cb:data queue Length----------> 10 Attempt 10: 1.00000 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 10: Data injected: 0.999999: put_cb:data 1.111110 , stored to pos--> 10 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 11 put_cb:data queue Length----------> 11 Attempt 11: 1.11111 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 11: Data injected: 1.111110: put_cb:data 1.222221 , stored to pos--> 11 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 12 put_cb:data queue Length----------> 12 Attempt 12: 1.22222 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 12: Data injected: 1.222221: put_cb:data 1.333332 , stored to pos--> 12 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 13 put_cb:data queue Length----------> 13 Attempt 13: 1.33333 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 13: Data injected: 1.333332: put_cb:data 1.444443 , stored to pos--> 13 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 14 put_cb:data queue Length----------> 14 Attempt 14: 1.44444 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 14: Data injected: 1.444443: put_cb:data 1.555554 , stored to pos--> 14 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 15 put_cb:data queue Length----------> 15 Attempt 15: 1.55555 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 15: Data injected: 1.555554: put_cb:data 1.666665 , stored to pos--> 15 put_cb:data queue Head -------------------> 0 put_cb:data queue Tail ---------------------> 0 put_cb:data queue Length----------> 16 Attempt 16: 1.66666 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 1.666665: put_cb: buffer queue is full. Overwriting data. put_cb:data 1.777776 , stored to pos--> 0 put_cb:data queue Head ----------------->>> 1 put_cb:data queue Tail ------------------->>> 1 put_cb:data queue Length-------->>> 16 Attempt 17: 1.77778 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 1.777776: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 1.888887 , stored to pos--> 1 put_cb:data queue Head ----------------->>> 2 put_cb:data queue Tail ------------------->>> 2 put_cb:data queue Length-------->>> 16 Attempt 18: 1.88889 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 1.888887: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 1.999998 , stored to pos--> 2 put_cb:data queue Head ----------------->>> 3 put_cb:data queue Tail ------------------->>> 3 put_cb:data queue Length-------->>> 16 Attempt 19: 2.00000 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 1.999998: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 2.111109 , stored to pos--> 3 put_cb:data queue Head ----------------->>> 4 put_cb:data queue Tail ------------------->>> 4 put_cb:data queue Length-------->>> 16 Attempt 20: 2.11111 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 2.111109: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 2.222220 , stored to pos--> 4 put_cb:data queue Head ----------------->>> 5 put_cb:data queue Tail ------------------->>> 5 put_cb:data queue Length-------->>> 16 Attempt 21: 2.22222 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 2.222220: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 2.333331 , stored to pos--> 5 put_cb:data queue Head ----------------->>> 6 put_cb:data queue Tail ------------------->>> 6 put_cb:data queue Length-------->>> 16 Attempt 22: 2.33333 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 2.333331: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 2.444442 , stored to pos--> 6 put_cb:data queue Head ----------------->>> 7 put_cb:data queue Tail ------------------->>> 7 put_cb:data queue Length-------->>> 16 Attempt 23: 2.44444 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 2.444442: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 2.555553 , stored to pos--> 7 put_cb:data queue Head ----------------->>> 8 put_cb:data queue Tail ------------------->>> 8 put_cb:data queue Length-------->>> 16 Attempt 24: 2.55555 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 2.555553: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 2.666664 , stored to pos--> 8 put_cb:data queue Head ----------------->>> 9 put_cb:data queue Tail ------------------->>> 9 put_cb:data queue Length-------->>> 16 Attempt 25: 2.66666 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 2.666664: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 2.777775 , stored to pos--> 9 put_cb:data queue Head ----------------->>> 10 put_cb:data queue Tail ------------------->>> 10 put_cb:data queue Length-------->>> 16 Attempt 26: 2.77778 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 2.777775: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 2.888886 , stored to pos--> 10 put_cb:data queue Head ----------------->>> 11 put_cb:data queue Tail ------------------->>> 11 put_cb:data queue Length-------->>> 16 Attempt 27: 2.88889 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 2.888886: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 2.999997 , stored to pos--> 11 put_cb:data queue Head ----------------->>> 12 put_cb:data queue Tail ------------------->>> 12 put_cb:data queue Length-------->>> 16 Attempt 28: 3.00000 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 2.999997: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 3.111108 , stored to pos--> 12 put_cb:data queue Head ----------------->>> 13 put_cb:data queue Tail ------------------->>> 13 put_cb:data queue Length-------->>> 16 Attempt 29: 3.11111 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 3.111108: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 3.222219 , stored to pos--> 13 put_cb:data queue Head ----------------->>> 14 put_cb:data queue Tail ------------------->>> 14 put_cb:data queue Length-------->>> 16 Attempt 30: 3.22222 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 3.222219: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 3.333330 , stored to pos--> 14 put_cb:data queue Head ----------------->>> 15 put_cb:data queue Tail ------------------->>> 15 put_cb:data queue Length-------->>> 16 Attempt 31: 3.33333 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 3.333330: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 3.444441 , stored to pos--> 15 put_cb:data queue Head ----------------->>> 0 put_cb:data queue Tail ------------------->>> 0 put_cb:data queue Length-------->>> 16 Attempt 32: 3.44444 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 3.444441: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 3.555552 , stored to pos--> 0 put_cb:data queue Head ----------------->>> 1 put_cb:data queue Tail ------------------->>> 1 put_cb:data queue Length-------->>> 16 Attempt 33: 3.55555 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 3.555552: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 3.666663 , stored to pos--> 1 put_cb:data queue Head ----------------->>> 2 put_cb:data queue Tail ------------------->>> 2 put_cb:data queue Length-------->>> 16 Attempt 34: 3.66666 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 3.666663: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 3.777774 , stored to pos--> 2 put_cb:data queue Head ----------------->>> 3 put_cb:data queue Tail ------------------->>> 3 put_cb:data queue Length-------->>> 16 Attempt 35: 3.77777 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 3.777774: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 3.888885 , stored to pos--> 3 put_cb:data queue Head ----------------->>> 4 put_cb:data queue Tail ------------------->>> 4 put_cb:data queue Length-------->>> 16 Attempt 36: 3.88889 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 3.888885: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 3.999996 , stored to pos--> 4 put_cb:data queue Head ----------------->>> 5 put_cb:data queue Tail ------------------->>> 5 put_cb:data queue Length-------->>> 16 Attempt 37: 4.00000 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 3.999996: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 4.111107 , stored to pos--> 5 put_cb:data queue Head ----------------->>> 6 put_cb:data queue Tail ------------------->>> 6 put_cb:data queue Length-------->>> 16 Attempt 38: 4.11111 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 4.111107: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 4.222218 , stored to pos--> 6 put_cb:data queue Head ----------------->>> 7 put_cb:data queue Tail ------------------->>> 7 put_cb:data queue Length-------->>> 16 Attempt 39: 4.22222 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 4.222218: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 4.333329 , stored to pos--> 7 put_cb:data queue Head ----------------->>> 8 put_cb:data queue Tail ------------------->>> 8 put_cb:data queue Length-------->>> 16 Attempt 40: 4.33333 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 4.333329: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 4.444440 , stored to pos--> 8 put_cb:data queue Head ----------------->>> 9 put_cb:data queue Tail ------------------->>> 9 put_cb:data queue Length-------->>> 16 Attempt 41: 4.44444 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 4.444440: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 4.555551 , stored to pos--> 9 put_cb:data queue Head ----------------->>> 10 put_cb:data queue Tail ------------------->>> 10 put_cb:data queue Length-------->>> 16 Attempt 42: 4.55555 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 4.555551: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 4.666662 , stored to pos--> 10 put_cb:data queue Head ----------------->>> 11 put_cb:data queue Tail ------------------->>> 11 put_cb:data queue Length-------->>> 16 Attempt 43: 4.66666 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 4.666662: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 4.777773 , stored to pos--> 11 put_cb:data queue Head ----------------->>> 12 put_cb:data queue Tail ------------------->>> 12 put_cb:data queue Length-------->>> 16 Attempt 44: 4.77777 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 4.777773: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 4.888884 , stored to pos--> 12 put_cb:data queue Head ----------------->>> 13 put_cb:data queue Tail ------------------->>> 13 put_cb:data queue Length-------->>> 16 Attempt 45: 4.88888 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 4.888884: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 4.999995 , stored to pos--> 13 put_cb:data queue Head ----------------->>> 14 put_cb:data queue Tail ------------------->>> 14 put_cb:data queue Length-------->>> 16 Attempt 46: 5.00000 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 4.999995: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 5.111106 , stored to pos--> 14 put_cb:data queue Head ----------------->>> 15 put_cb:data queue Tail ------------------->>> 15 put_cb:data queue Length-------->>> 16 Attempt 47: 5.11111 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 5.111106: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 5.222217 , stored to pos--> 15 put_cb:data queue Head ----------------->>> 0 put_cb:data queue Tail ------------------->>> 0 put_cb:data queue Length-------->>> 16 Attempt 48: 5.22222 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 5.222217: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 5.333328 , stored to pos--> 0 put_cb:data queue Head ----------------->>> 1 put_cb:data queue Tail ------------------->>> 1 put_cb:data queue Length-------->>> 16 Attempt 49: 5.33333 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 5.333328: Error: -10089 put_cb: buffer queue is full. Overwriting data. put_cb:data 5.444439 , stored to pos--> 1 put_cb:data queue Head ----------------->>> 2 put_cb:data queue Tail ------------------->>> 2 put_cb:data queue Length-------->>> 16 Attempt 50: 5.44444 <<<<<<<<<<ERROR: -10089 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 5.444439: Error: -10089 get attempt 1 ---> 3.77777 get attempt 2 ---> 3.88889 get attempt 3 ---> 4.00000 get attempt 4 ---> 4.11111 get attempt 5 ---> 4.22222 get attempt 6 ---> 4.33333 get attempt 7 ---> 4.44444 get attempt 8 ---> 4.55555 get attempt 9 ---> 4.66666 get attempt 10 ---> 4.77777 get attempt 11 ---> 4.88888 get attempt 12 ---> 5.00000 get attempt 13 ---> 5.11111 get attempt 14 ---> 5.22222 get attempt 15 ---> 5.33333 get attempt 16 ---> 5.44444 get_cb:buffer empty :p get attempt 17 ---> -10086.00000 get_cb:buffer empty :p get attempt 18 ---> -10086.00000 get_cb:buffer empty :p get attempt 19 ---> -10086.00000 get_cb:buffer empty :p get attempt 20 ---> -10086.00000 get_cb:buffer empty :p get attempt 21 ---> -10086.00000 get_cb:buffer empty :p get attempt 22 ---> -10086.00000 get_cb:buffer empty :p get attempt 23 ---> -10086.00000 get_cb:buffer empty :p get attempt 24 ---> -10086.00000 get_cb:buffer empty :p get attempt 25 ---> -10086.00000 get_cb:buffer empty :p get attempt 26 ---> -10086.00000 get_cb:buffer empty :p get attempt 27 ---> -10086.00000 get_cb:buffer empty :p get attempt 28 ---> -10086.00000 get_cb:buffer empty :p get attempt 29 ---> -10086.00000 get_cb:buffer empty :p get attempt 30 ---> -10086.00000 get_cb:buffer empty :p get attempt 31 ---> -10086.00000 get_cb:buffer empty :p get attempt 32 ---> -10086.00000 put_cb:data 0.000000 , stored to pos--> 2 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 3 put_cb:data queue Length----------> 1 Test2 try 1: 0.00000 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 1: Data injected: 0.000000: put_cb:data 0.111111 , stored to pos--> 3 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 4 put_cb:data queue Length----------> 2 Test2 try 2: 0.11111 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 2: Data injected: 0.111111: put_cb:data 0.222222 , stored to pos--> 4 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 5 put_cb:data queue Length----------> 3 Test2 try 3: 0.22222 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 3: Data injected: 0.222222: put_cb:data 0.333333 , stored to pos--> 5 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 6 put_cb:data queue Length----------> 4 Test2 try 4: 0.33333 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 4: Data injected: 0.333333: put_cb:data 0.444444 , stored to pos--> 6 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 7 put_cb:data queue Length----------> 5 Test2 try 5: 0.44444 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 5: Data injected: 0.444444: put_cb:data 0.555555 , stored to pos--> 7 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 8 put_cb:data queue Length----------> 6 Test2 try 6: 0.55555 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 6: Data injected: 0.555555: put_cb:data 0.666666 , stored to pos--> 8 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 9 put_cb:data queue Length----------> 7 Test2 try 7: 0.66667 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 7: Data injected: 0.666666: put_cb:data 0.777777 , stored to pos--> 9 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 10 put_cb:data queue Length----------> 8 Test2 try 8: 0.77778 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 8: Data injected: 0.777777: put_cb:data 0.888888 , stored to pos--> 10 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 11 put_cb:data queue Length----------> 9 Test2 try 9: 0.88889 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 9: Data injected: 0.888888: put_cb:data 0.999999 , stored to pos--> 11 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 12 put_cb:data queue Length----------> 10 Test2 try 10: 1.00000 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 10: Data injected: 0.999999: put_cb:data 1.111110 , stored to pos--> 12 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 13 put_cb:data queue Length----------> 11 Test2 try 11: 1.11111 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 11: Data injected: 1.111110: put_cb:data 1.222221 , stored to pos--> 13 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 14 put_cb:data queue Length----------> 12 Test2 try 12: 1.22222 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 12: Data injected: 1.222221: put_cb:data 1.333332 , stored to pos--> 14 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 15 put_cb:data queue Length----------> 13 Test2 try 13: 1.33333 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 13: Data injected: 1.333332: put_cb:data 1.444443 , stored to pos--> 15 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 0 put_cb:data queue Length----------> 14 Test2 try 14: 1.44444 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 14: Data injected: 1.444443: put_cb:data 1.555554 , stored to pos--> 0 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 1 put_cb:data queue Length----------> 15 Test2 try 15: 1.55555 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 15: Data injected: 1.555554: put_cb:data 1.666665 , stored to pos--> 1 put_cb:data queue Head -------------------> 2 put_cb:data queue Tail ---------------------> 2 put_cb:data queue Length----------> 16 Test2 try 16: 1.66666 <<<<<<<<<<ERROR: 0 <<<<<<<<<<<<<<<<<<< Data to inject Data queue Length: 16: Data injected: 1.666665: get attempt 1 ---> 0.00000 get attempt 2 ---> 0.11111 get attempt 3 ---> 0.22222 get attempt 4 ---> 0.33333 get attempt 5 ---> 0.44444 get attempt 6 ---> 0.55555 get attempt 7 ---> 0.66667 get attempt 8 ---> 0.77778 get attempt 9 ---> 0.88889 get attempt 10 ---> 1.00000 get attempt 11 ---> 1.11111 get attempt 12 ---> 1.22222 get attempt 13 ---> 1.33333 get attempt 14 ---> 1.44444 get attempt 15 ---> 1.55555 get attempt 16 ---> 1.66666 [wenxue@hpi7 alsaaa]$
俩英文单词,不先看看本义吗?虽然都是抽象单词,但在各个地方都有具象的应用。
Buffer常见的是这个:
(来源:train stop buffer bumper)
对,就是铁道端头那个巨大的弹簧一类的东西。作用是万一车没停住(是没停住啊,刹车了但是差一点没刹住那种,不是不拉刹直接撞上来),撞弹簧上减速慢,危险小一些。叫缓冲。
Cache常见的是这个:
(来源:https://upload.wikimedia.org/wikipedia/commons/6/68/Bear_caches.jpg)
没错,就是一种保管箱。看到右边那个被锈掉的Food Cache没?这是部署在森林里的存应急物资的保管箱。功能是把你需要用的东西放在更容易拿到的地方。虽然常用准确翻译叫缓存,但个人以为意思表达的不对,丢了一半的功能。台湾的翻译更好,叫快取。
相信看完这些应该不用我说区别了?
哎呀还是卖弄一下吧。
简单说,Buffer的核心作用是用来缓冲,缓和冲击。比如你每秒要写100次硬盘,对系统冲击很大,浪费了大量时间在忙着处理开始写和结束写这两件事嘛。用个buffer暂存起来,变成每10秒写一次硬盘,对系统的冲击就很小,写入效率高了,日子过得爽了。极大缓和了冲击。
Cache的核心作用是加快取用的速度。比如你一个很复杂的计算做完了,下次还要用结果,就把结果放手边一个好拿的地方存着,下次不用再算了。加快了数据取用的速度。
所以,如果你注意关心过存储系统的话,你会发现硬盘的读写缓冲/缓存名称是不一样的,叫write-buffer和read-cache。很明显地说出了两者的区别。
当然很多时候宏观上说两者可能是混用的。比如实际上memcached很多人就是拿来读写都用的。不少时候Non-SQL数据库也是。严格来说,CPU里的L2和L3 Cache也都是读写兼用——因为你没法简单地定义CPU用它们的方法是读还是写。硬盘里也是个典型例子,buffer和cache都在一块空间上,到底是buffer还是cache?
不过仔细想一下,你说拿cache做buffer用行不行?当然行,只要能控制cache淘汰逻辑就没有任何问题。那么拿buffer做cache用呢?貌似在很特殊的情况下,能确定访问顺序的时候,也是可以的。简单想一下就明白——buffer根据定义,需要随机存储吗?一般是不需要的。但cache一定要。所以大多数时候用cache代替buffer可以,反之就比较局限。这也是技术上说cache和buffer的关键区别。
——————
补充1:不要误解Buffer就是用来写的,Cache就是用来读的。读可以用Buffer吗?当然可以,比如你想一批一批地处理读取而非有啥处理啥的时候,就可以用读buffer。写当然也可以用cache,比如你的写入有很高的随机性的时候。具体什么场景用Buffer什么场景用Cache要根据场景的具体需要决定。
补充2:不要误解Cache或Buffer就一定是内存或者存在什么高速媒介上的东西。只要相对高速即可。我完全可以在硬盘上存Cache,比如有些游戏会在运行时建立预编译的shader(暴露年龄),这本质上就是一种cache,它存在速度缓慢的硬盘上,因为读硬盘依旧比重新编译要快。Buffer也同理,例如NTFS文件系统自己就有Logging Buffer,这个甚至明确拒绝放在任何易失缓存里。
我是一名基层派出所民警。
可以说当今中国警察普遍羡慕美国警察可以采取暴力手段绝对的镇压不法分子。
但是,不得不说,这次这位美国警察,太过分了,不仅是过分,而且我的理解是那已经构成了犯罪行为。那黑人已经制服了就可以正常上拷带走了,没必要一直压着脖子压那么长时间。没能置身其中不知现场那美国警察的所思所想,反正我个人挺不理解他为啥那样干的。
只能说无论什么地方,无论什么行业,只要是人的社会,都有像样的也有操蛋的吧。
_________此处为分割线 _________
以下为统一答复评论中有些人质疑的我所讲的羡慕二字。
能够出现这种质疑在我料想之中,因为中国警察也有过过分的时代,据我所知就是在七十八十九十年代,就如同地痞流氓,看谁不顺眼就能打谁对老百姓而言没王法可讲,那时候的警察说好听点可以说是威风凛凛说难听点儿是横行霸道。
但我想表明的是,时过境迁,现在的中国警察无论是受舆论约束还是因为法治社会建设制度规范都已经变得逐步文明与规范起来,起码我认为从我们现在开始从公安司法院校毕业参加公务员考试考进来的新一代警察已经具备新的面目,当然不可否认的是在这个行业内目前仍然存有历史的顽疾,仍然存在着臭虫,但我已经讲过无论什么行业都有操蛋的吧,这是个人问题,不是群体问题。相比之下,拍拍良心看,现在的整个警察队伍比照曾经确实过分的年代是不是已经是天地之别,问问曾经真正挨过曾经年代老警察欺负的中老年人就知道了。
为何会说起羡慕,因为警察每天面对的人群,大多是三教九流之辈,没有武力加身,很多事情在处理上警察显得软弱无能,说白了,好人谁没事儿上派出所转悠啊都忙着自己的生活呢,警察打人这句话,我们常常听到,但是但凡有点脑袋的人都能想明白,警察会闲着没事儿干把那在家里消停待着的遵纪守法的人抓起来暴揍一顿吗?
以上言辞不免更会有人质疑,请允许我解释,武力,当然不可滥用,我所说的羡慕不是羡慕美国警察的随意滥用武力,而是在合法范围内准许在对方不听从警察指令时动用武力,现在确实有人民警察法赋予了相关权力,但实践中现在的中国警察并不能或者说不敢执行人民警察法里的所有权力。拿防疫工作举例,卡口的工作人员在让出入的人员扫码登记时,就会有不愿意配合的人,然而这些不愿意配合的人可会知道工作人员的所做所为是为了整个社区的稳定安全,因为这整个社区包括了这名不愿意配合的人啊,在这个时候是否应当对其进行武力控制来保障其他居民的安全呢。同理,警察盘查也好,调查也好,总会有那些不愿意配合的人,自我感觉良好认为自己没问题所以警察不必要对其进行盘查所以就不配合,而警察当看到对方不配合时会以什么视角审视,难道要说谢谢您的不配合吗,万一这不愿配合的人真背着案子呢,那便是对更多的人民群众的不负责任。因此,我要说,民众的素质如果真正达到了人人互相敬重路不拾遗夜不闭户的文明程度,要求警察绝对文明不要有暴力举动,一点问题没有,一味强调了警察不该暴力执法而分毫不过问被执法对象自身是否存在问题,是不是看问题的角度些微的片面了些。
请注意,我说羡慕里的那句话尾巴实际已经表明了,羡慕的是暴力手段对不法分子的镇压,可不是对遵纪守法的百姓也要肆意妄为。例如像给群众办个身份证居住证之类的业务,警察当然应该热心服务。但当面对泼皮无赖时,还要笑脸相迎,得来的只有蹬鼻子上脸,警察都不怕了,您们认为这些无赖还有谁管得了。
列位存有异议的同志们,谢谢您们的教诲。言辞中犀利的同志们,谢谢您们的敦促。
让我知道当警察,需要吾日三省吾身。
还想要质疑甚或是骂的您们,若是能让您舒服,骂两句无妨。我不算您辱骂警察。不过是,道不同不相为谋罢了吧。
_____分割线
2020年6月5日22:53 出警在路上
共产主义政党长期治理的喀拉拉邦在印度处于人类发展指数的前茅,这就是共产主义对印度的影响。
另外,南亚人是非常非常喜欢取经名的。这也是一个地域特色了。