Btrfs: populate heuristic with code
v5 cover Btrfs: populate heuristic with code
Based on kdave for-next
Patches short:
1. Move heuristic to use compression workspaces
Bit tricky, but works.
2. Add heuristic counters and buffer to workspaces
3. Implement simple input data sampling
It's get 16 byte samples with 256 bytes shifts over input data. Collect info about how many different bytes (symbols) has been found in sample data
4. Implement check sample to zeroes
Just check all bytes in sample to 0
5. Add code for calculate
how many unique bytes has been found in sample data That can fast detect easy compressible data
6. Add code for calculate byte core set size
i.e. how many unique bytes use 90% of sample data
That code require that numbers in bucket must be sorted That can detect easy compressible data with many repeated bytes That can detect not compressible data with evenly distributed bytes
Changes v1 -> v2:
- Change input data iterator shift 512 -> 256
- Replace magic macro numbers with direct values
- Drop useless symbol population in bucket as no one care about where and what symbol stored in bucket at now
Changes v2 -> v3 (only update #3 patch):
- Fix u64 division problem by use u32 for input_size
- Fix input size calculation start - end -> end - start
- Add missing sort.h header
Changes v3 -> v4 (only update #1 patch):
- Change counter type in bucket item u16 -> u32
- Drop other fields from bucket item for now,
no one use it
Change v4 -> v5
- Move heuristic code to external file
- Make heuristic use compression workspaces
- Add check sample to zeroes
Timofey Titovets (6):
Btrfs: heuristic make use compression workspaces
Btrfs: heuristic workspace add bucket and sample items
Btrfs: Implement heuristic sampling logic
Btrfs: heuristic add detection of zeroed sample
Btrfs: heuristic add byte set calculation
Btrfs: heuristic add byte core set calculation
fs/btrfs/Makefile | 2 +- fs/btrfs/compression.c | 18 ++-- fs/btrfs/compression.h | 7 +- fs/btrfs/heuristic.c | 220 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 234 insertions(+), 13 deletions(-) create mode 100644 fs/btrfs/heuristic.c
--
2.14.1
PATCH v5 1/6 Btrfs: heuristic make use compression workspaces
Move heuristic to external file
Implement compression workspaces support for
heuristic resources
--- fs/btrfs/Makefile | 2 +- fs/btrfs/compression.c | 18 +++++-------- fs/btrfs/compression.h | 7 ++++- fs/btrfs/heuristic.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 13 deletions(-) create mode 100644 fs/btrfs/heuristic.c
fs/btrfs/Makefile +1 -1
@@ -9,7 +9,7 @@ btrfs-y += super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \ export.o tree-log.o free-space-cache.o zlib.o lzo.o \ compression.o delayed-ref.o relocation.o delayed-inode.o scrub.o \ reada.o backref.o ulist.o qgroup.o send.o dev-replace.o raid56.o \ - uuid-tree.o props.o hash.o free-space-tree.o + uuid-tree.o props.o hash.o free-space-tree.o heuristic.o btrfs-$(CONFIG_BTRFS_FS_POSIX_ACL) += acl.o btrfs-$(CONFIG_BTRFS_FS_CHECK_INTEGRITY) += check-integrity.o
fs/btrfs/compression.c +7 -11
@@ -704,6 +704,7 @@ static struct {static const struct btrfs_compress_op *const btrfs_compress_op[] = { &btrfs_zlib_compress, &btrfs_lzo_compress, + &btrfs_heuristic, };void __init btrfs_init_compress (void )
@@ -1065,18 +1066,13 @@ int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start,*/ int btrfs_compress_heuristic (struct inode *inode,u64 start,u64 end) { -u64 index = start >>PAGE_SHIFT ; -u64 end_index = end >>PAGE_SHIFT ; -struct page *page; -int ret =1 ; +int ret; +enum btrfs_compression_type type =BTRFS_HEURISTIC ; +struct list_head *workspace =find_workspace (type); -while (index <= end_index) { - page =find_get_page (inode->i_mapping, index); -kmap (page); -kunmap (page); -put_page (page); - index++; - } + ret = btrfs_compress_op[type-1 ]->heuristic (workspace, inode, + start, end); +free_workspace (type, workspace);return ret; }
fs/btrfs/compression.h +6 -1
@@ -99,7 +99,8 @@ enum btrfs_compression_type {BTRFS_COMPRESS_NONE =0 ,BTRFS_COMPRESS_ZLIB =1 ,BTRFS_COMPRESS_LZO =2 , - BTRFS_COMPRESS_TYPES = 2, +BTRFS_HEURISTIC =3 , + BTRFS_COMPRESS_TYPES = 3, }; struct btrfs_compress_op {
@@ -123,10 +124,14 @@ struct btrfs_compress_op {struct page *dest_page, unsigned long start_byte, size_t srclen, size_t destlen); + +int (*heuristic)(struct list_head *workspace, +struct inode *inode,u64 start,u64 end); };extern const struct btrfs_compress_op btrfs_zlib_compress;extern const struct btrfs_compress_op btrfs_lzo_compress; +extern const struct btrfs_compress_op btrfs_heuristic;int btrfs_compress_heuristic (struct inode *inode,u64 start,u64 end);
fs/btrfs/heuristic.c new file +70 -0
70 lines not on this page · raw message
--
2.14.1
PATCH v5 2/6 Btrfs: heuristic workspace add bucket and sample items
In reply to Timofey TitovetsHeuristic workspace:
- Add bucket for storing byte type counters
- Add sample array for storing partial copy of input data range
- Add counter for store current sample size to workspace
--- fs/btrfs/heuristic.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
fs/btrfs/heuristic.c +30 -0
@@ -20,13 +20,36 @@#include <linux/bio.h> #include "compression.h" +#define READ_SIZE 16 +#define ITER_SHIFT 256 +#define BUCKET_SIZE 256 + +/* +* While mapping 128KiB range into pages (with 4k PAGE_SIZE as ex), +* and iterate that with index <= index_end +* code get 0-32 items, that a 33 pages +*/ +#define MAX_INPUT_PAGES ((BTRFS_MAX_UNCOMPRESSED >> PAGE_SHIFT)+1) +#define MAX_SAMPLE_SIZE (MAX_INPUT_PAGES*PAGE_SIZE*READ_SIZE/ITER_SHIFT) + +struct bucket_item { +u32 count; +}; +struct workspace { +u8 *sample; +/* Partial copy of input data */ +u32 sample_size; +/* Bucket store counter for each byte type */ +struct bucket_item bucket[BUCKET_SIZE ];struct list_head list; };static void heuristic_free_workspace (struct list_head *ws) { struct workspace *workspace = list_entry(ws, struct workspace, list); + +kvfree (workspace->sample);kfree (workspace); }
@@ -38,9 +61,16 @@ static struct list_head *heuristic_alloc_workspace(void)if (!workspace)return ERR_PTR (-ENOMEM ); + workspace->sample =kvmalloc (MAX_SAMPLE_SIZE ,GFP_KERNEL ); +if (!workspace->sample) + goto fail; +INIT_LIST_HEAD (&workspace->list);return &workspace->list; +fail: +heuristic_free_workspace (&workspace->list); +return ERR_PTR (-ENOMEM ); } static int heuristic(struct list_head *ws, struct inode *inode,
--
2.14.1
PATCH v5 3/6 Btrfs: implement heuristic sampling logic
In reply to Timofey TitovetsCopy sample data from input data range to sample buffer then calculate byte type count for that sample into bucket.
--- fs/btrfs/heuristic.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-)
fs/btrfs/heuristic.c +29 -2
@@ -76,20 +76,47 @@ static struct list_head *heuristic_alloc_workspace(void)static int heuristic (struct list_head *ws,struct inode *inode,u64 start,u64 end) { + struct workspace *workspace = list_entry(ws, struct workspace, list);struct page *page;u64 index, index_end; -u8 *input_data; +u8 *in_data; +u32 a, b; +u8 byte; + +/* +* Compression only handle first 128kb of input range +* And just shift over range in loop for compressing it. +* Let's do the same. +*/ +if (end - start >BTRFS_MAX_UNCOMPRESSED ) + end = start +BTRFS_MAX_UNCOMPRESSED ; index = start >>PAGE_SHIFT ; index_end = end >>PAGE_SHIFT ; + b =0 ;for (; index <= index_end; index++) { page =find_get_page (inode->i_mapping, index); - input_data =kmap (page); + in_data =kmap (page); + a =0 ; +while (a <PAGE_SIZE -READ_SIZE && b <MAX_SAMPLE_SIZE ) { +memcpy (&workspace->sample[b], &in_data[a],READ_SIZE ); + a +=ITER_SHIFT ; + b +=READ_SIZE ; + }kunmap (page);put_page (page); } + workspace->sample_size = b; + +memset (workspace->bucket,0 ,sizeof (*workspace->bucket)*BUCKET_SIZE ); + +for (a =0 ; a < workspace->sample_size; a++) { + byte = workspace->sample[a]; + workspace->bucket[byte].count++; + } +return 1 ; }
--
2.14.1
PATCH v5 4/6 Btrfs: heuristic add detection of zeroed sample
In reply to Timofey TitovetsUse memcmp for check sample data to zeroes.
--- fs/btrfs/heuristic.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
fs/btrfs/heuristic.c +18 -0
@@ -73,6 +73,21 @@ static struct list_head *heuristic_alloc_workspace(void)return ERR_PTR (-ENOMEM ); } +static bool sample_zeroed (struct workspace *workspace) +{ +u32 i; +u8 zero[READ_SIZE ]; + +memset (&zero,0 ,sizeof (zero)); + +for (i =0 ; i < workspace->sample_size; i +=sizeof (zero)) { +if (memcmp (&workspace->sample[i], &zero,sizeof (zero))) +return false; + } + +return true; +} +static int heuristic (struct list_head *ws,struct inode *inode,u64 start,u64 end) {
@@ -110,6 +125,9 @@ static int heuristic(struct list_head *ws, struct inode *inode, workspace->sample_size = b; +if (sample_zeroed (workspace)) +return 1 ; +memset (workspace->bucket,0 ,sizeof (*workspace->bucket)*BUCKET_SIZE );for (a =0 ; a < workspace->sample_size; a++) {
--
2.14.1
Re: [PATCH v5 4/6] Btrfs: heuristic add detection of zeroed sample
fs/btrfs/heuristic.c static struct list_head *heuristic_alloc_workspace(void) · line 85
Quoted 3 lines · El miércoles, 23 de agosto de 2017 2:26:48 (CEST) Timofey Titovets escribió:
fs/btrfs/heuristic.c +3
-0+for (i =0 ; i < workspace->sample_size; i +=sizeof (zero)) { +if (memcmp (&workspace->sample[i], &zero,sizeof (zero))) +return false;
Instead of just checking for 0, wouldn't it be a better idea to check for any kind of repetitions?
As in, iterate over the sample and memcmp() each part of sample with the previous one. The cost would be the same, and it would detect not just zeros, but any kind of repeated data. Is there any reason I'm missing for not doing this?
Re: [PATCH v5 4/6] Btrfs: heuristic add detection of zeroed sample
2017-08-23 20:55 GMT+03:00 Diego Calleja <[email protected]>:
Quoted 5 lines · El miércoles, 23 de agosto de 2017 2:26:48 (CEST) Timofey Titovets escribió:
fs/btrfs/heuristic.c static struct list_head *heuristic_alloc_workspace(void) · line 85
Quoted 3 lines
fs/btrfs/heuristic.c +3
-0+for (i =0 ; i < workspace->sample_size; i +=sizeof (zero)) { +if (memcmp (&workspace->sample[i], &zero,sizeof (zero))) +return false;
Quoted 7 lines
Instead of just checking for 0, wouldn't it be a better idea to check for any kind of repetitions?
As in, iterate over the sample and
memcmp()each part of sample with the previous one. The cost would be the same, and it would detect not just zeros, but any kind of repeated data. Is there any reason I'm missing for not doing this?
Thank you, i was not think about that,
That approach seems better, i will update the patch.
Signature · 2 lines
Have a nice day, Timofey.
PATCH v5 5/6 Btrfs: heuristic add byte set calculation
In reply to Timofey TitovetsCalculate byte set size for data sample:
Calculate how many unique bytes has been in sample By count all bytes in bucket with count > 0 If byte set low (~25%), data are easily compressible
--- fs/btrfs/heuristic.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
fs/btrfs/heuristic.c +26 -0
@@ -31,6 +31,7 @@*/ #define MAX_INPUT_PAGES ((BTRFS_MAX_UNCOMPRESSED >> PAGE_SHIFT)+1)#define MAX_SAMPLE_SIZE (MAX_INPUT_PAGES*PAGE_SIZE*READ_SIZE/ITER_SHIFT) +#define BYTE_SET_THRESHOLD 64struct bucket_item {u32 count;
@@ -73,6 +74,27 @@ static struct list_head *heuristic_alloc_workspace(void)return ERR_PTR (-ENOMEM ); } +static int byte_set_size (const struct workspace *workspace) +{ +int a =0 ; +int byte_set_size =0 ; + +for (; a <BYTE_SET_THRESHOLD ; a++) { +if (workspace->bucket[a].count >0 ) + byte_set_size++; + } + +for (; a <BUCKET_SIZE ; a++) { +if (workspace->bucket[a].count >0 ) { + byte_set_size++; +if (byte_set_size >BYTE_SET_THRESHOLD ) +return byte_set_size; + } + } + +return byte_set_size; +} +static bool sample_zeroed (struct workspace *workspace) {u32 i;
@@ -135,6 +157,10 @@ static int heuristic(struct list_head *ws, struct inode *inode, workspace->bucket[byte].count++; } + a =byte_set_size (workspace); +if (a >BYTE_SET_THRESHOLD ) +return 2 ; +return 1 ; }
--
2.14.1
PATCH v5 6/6 Btrfs: heuristic add byte core set calculation
In reply to Timofey TitovetsCalculate byte core set for data sample:
Sort bucket's numbers in decreasing order
Count how many numbers use 90% of sample
If core set are low (<=25%), data are easily compressible If core set high (>=80%), data are not compressible
--- fs/btrfs/heuristic.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-)
fs/btrfs/heuristic.c +50 -1
@@ -18,6 +18,7 @@#include <linux/pagemap.h> #include <linux/string.h> #include <linux/bio.h> +#include <linux/sort.h> #include "compression.h" #define READ_SIZE 16
@@ -32,6 +33,8 @@#define MAX_INPUT_PAGES ((BTRFS_MAX_UNCOMPRESSED >> PAGE_SHIFT)+1)#define MAX_SAMPLE_SIZE (MAX_INPUT_PAGES*PAGE_SIZE*READ_SIZE/ITER_SHIFT)#define BYTE_SET_THRESHOLD 64 +#define BYTE_CORE_SET_LOW BYTE_SET_THRESHOLD +#define BYTE_CORE_SET_HIGH 200 // ~80%struct bucket_item {u32 count;
@@ -74,6 +77,45 @@ static struct list_head *heuristic_alloc_workspace(void)return ERR_PTR (-ENOMEM ); } +/* For bucket sorting */ +static inline int bucket_compare (const void *lv,const void *rv) +{ +struct bucket_item *l = (struct bucket_item *)(lv); +struct bucket_item *r = (struct bucket_item *)(rv); + +return r->count - l->count; +} + +/* +* Byte Core set size +* How many bytes use 90% of sample +*/ +static int byte_core_set_size (struct workspace *workspace) +{ +int a =0 ; +u32 coreset_sum =0 ; +struct bucket_item *bucket = workspace->bucket; +u32 core_set_threshold = workspace->sample_size*90 /100 ; + +/* Sort in reverse order */ +sort (bucket,BUCKET_SIZE ,sizeof (*bucket), + &bucket_compare,NULL ); + +for (; a <BYTE_CORE_SET_LOW ; a++) + coreset_sum += bucket[a].count; + +if (coreset_sum > core_set_threshold) +return a; + +for (; a <BYTE_CORE_SET_HIGH && bucket[a].count >0 ; a++) { + coreset_sum += bucket[a].count; +if (coreset_sum > core_set_threshold) +break ; + } + +return a; +} +static int byte_set_size (const struct workspace *workspace) {int a =0 ;
@@ -161,7 +203,14 @@ static int heuristic(struct list_head *ws, struct inode *inode,if (a >BYTE_SET_THRESHOLD )return 2 ; -return 1 ; + a =byte_core_set_size (workspace); +if (a <=BYTE_CORE_SET_LOW ) +return 3 ; + +if (a >=BYTE_CORE_SET_HIGH ) +return 0 ; + +return 4 ; }const struct btrfs_compress_op btrfs_heuristic = {
--
2.14.1