public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH 3/5] jffs2: Add a "favourlzo" compression mode to jffs2
@ 2007-05-04 11:30 Richard Purdie
  0 siblings, 0 replies; 3+ messages in thread
From: Richard Purdie @ 2007-05-04 11:30 UTC (permalink / raw)
  To: LKML, David Woodhouse; +Cc: Randy Dunlap, Satyam Sharma, linux-mtd

Add a "favourlzo" compression mode to jffs2 which tries to
optimise by size but gives lzo an advantage when comparing sizes.
This means the faster lzo algorithm can be preferred when there
isn't much difference in compressed size (the exact threshold can
be changed).

Signed-off-by: Richard Purdie <rpurdie@openedhand.com>
---
 fs/Kconfig       |    7 ++++++
 fs/jffs2/compr.c |   56 ++++++++++++++++++++++++++++++++++++++++++++---------
 fs/jffs2/compr.h |    3 ++
 3 files changed, 56 insertions(+), 10 deletions(-)

diff --git a/fs/Kconfig b/fs/Kconfig
index 421c821..b8d2c58 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -1360,6 +1360,13 @@ config JFFS2_CMODE_SIZE
           Tries all compressors and chooses the one which has the smallest
           result.
 
+config JFFS2_CMODE_FAVOURLZO
+	bool "Favour LZO"
+	help
+	  Tries all compressors and chooses the one which has the smallest
+	  result but gives some preference to LZO (which has faster
+	  decompression) at the expense of size.
+
 endchoice
 
 config CRAMFS
diff --git a/fs/jffs2/compr.c b/fs/jffs2/compr.c
index 6a23408..477ec97 100644
--- a/fs/jffs2/compr.c
+++ b/fs/jffs2/compr.c
@@ -24,6 +24,34 @@ static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
 /* Statistics for blocks stored without compression */
 static uint32_t none_stat_compr_blocks=0,none_stat_decompr_blocks=0,none_stat_compr_size=0;
 
+
+/*
+ * Return 1 to use this compression
+ */
+static int jffs2_is_best_compression(struct jffs2_compressor *this,
+		struct jffs2_compressor *best, uint32_t size, uint32_t bestsize)
+{
+	switch (jffs2_compression_mode) {
+	case JFFS2_COMPR_MODE_SIZE:
+		if (bestsize > size)
+			return 1;
+		return 0;
+	case JFFS2_COMPR_MODE_FAVOURLZO:
+		if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > size))
+			return 1;
+		if ((best->compr != JFFS2_COMPR_LZO) && (bestsize > size))
+			return 1;
+		if ((this->compr == JFFS2_COMPR_LZO) && 
+				(bestsize > (size * FAVOUR_LZO_PERCENT / 100)))
+			return 1;
+		if ((bestsize * FAVOUR_LZO_PERCENT / 100) > size)
+			return 1;
+		return 0;
+	}
+	/* Shouldn't happen */
+	return 0;
+}
+
 /* jffs2_compress:
  * @data: Pointer to uncompressed data
  * @cdata: Pointer to returned pointer to buffer for compressed data
@@ -89,6 +117,7 @@ uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
                 if (ret == JFFS2_COMPR_NONE) kfree(output_buf);
                 break;
         case JFFS2_COMPR_MODE_SIZE:
+        case JFFS2_COMPR_MODE_FAVOURLZO:
                 orig_slen = *datalen;
                 orig_dlen = *cdatalen;
                 spin_lock(&jffs2_compressor_list_lock);
@@ -97,7 +126,7 @@ uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
                         if ((!this->compress)||(this->disabled))
                                 continue;
                         /* Allocating memory for output buffer if necessary */
-                        if ((this->compr_buf_size<orig_dlen)&&(this->compr_buf)) {
+                        if ((this->compr_buf_size<orig_slen)&&(this->compr_buf)) {
                                 spin_unlock(&jffs2_compressor_list_lock);
                                 kfree(this->compr_buf);
                                 spin_lock(&jffs2_compressor_list_lock);
@@ -106,15 +135,17 @@ uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
                         }
                         if (!this->compr_buf) {
                                 spin_unlock(&jffs2_compressor_list_lock);
-                                tmp_buf = kmalloc(orig_dlen,GFP_KERNEL);
+                                tmp_buf = kmalloc(orig_slen, GFP_KERNEL);
                                 spin_lock(&jffs2_compressor_list_lock);
                                 if (!tmp_buf) {
-                                        printk(KERN_WARNING "JFFS2: No memory for compressor allocation. (%d bytes)\n",orig_dlen);
+                                        printk(KERN_WARNING 
+						"JFFS2: No memory for compressor allocation."
+						" (%d bytes)\n", orig_slen);
                                         continue;
                                 }
                                 else {
                                         this->compr_buf = tmp_buf;
-                                        this->compr_buf_size = orig_dlen;
+                                        this->compr_buf_size = orig_slen;
                                 }
                         }
                         this->usecount++;
@@ -124,12 +155,12 @@ uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
                         compr_ret = this->compress(data_in, this->compr_buf, datalen, cdatalen, NULL);
                         spin_lock(&jffs2_compressor_list_lock);
                         this->usecount--;
-                        if (!compr_ret) {
-                                if ((!best_dlen)||(best_dlen>*cdatalen)) {
-                                        best_dlen = *cdatalen;
-                                        best_slen = *datalen;
-                                        best = this;
-                                }
+                        if ((!compr_ret) && (((!best_dlen) 
+					|| jffs2_is_best_compression(this, best, *cdatalen, best_dlen))
+					&& (*cdatalen < *datalen))) {
+				best_dlen = *cdatalen;
+				best_slen = *datalen;
+				best = this;
                         }
                 }
                 if (best_dlen) {
@@ -297,9 +328,14 @@ int __init jffs2_compressors_init(void)
         jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
         D1(printk(KERN_INFO "JFFS2: default compression mode: size\n");)
 #else
+#ifdef CONFIG_JFFS2_CMODE_FAVOURLZO
+        jffs2_compression_mode = JFFS2_COMPR_MODE_FAVOURLZO;
+        D1(printk(KERN_INFO "JFFS2: default compression mode: favourlzo\n");)
+#else
         D1(printk(KERN_INFO "JFFS2: default compression mode: priority\n");)
 #endif
 #endif
+#endif
         return 0;
 }
 
diff --git a/fs/jffs2/compr.h b/fs/jffs2/compr.h
index 8c6b2af..2b66941 100644
--- a/fs/jffs2/compr.h
+++ b/fs/jffs2/compr.h
@@ -38,6 +38,9 @@
 #define JFFS2_COMPR_MODE_NONE       0
 #define JFFS2_COMPR_MODE_PRIORITY   1
 #define JFFS2_COMPR_MODE_SIZE       2
+#define JFFS2_COMPR_MODE_FAVOURLZO  3
+
+#define FAVOUR_LZO_PERCENT 80
 
 struct jffs2_compressor {
         struct list_head list;

^ permalink raw reply related	[flat|nested] 3+ messages in thread
* [PATCH 3/5] jffs2: Add a "favourlzo" compression mode to jffs2
@ 2007-05-01 14:47 Richard Purdie
  2007-05-01 20:04 ` Randy Dunlap
  0 siblings, 1 reply; 3+ messages in thread
From: Richard Purdie @ 2007-05-01 14:47 UTC (permalink / raw)
  To: LKML, David Woodhouse, herbert; +Cc: linux-mtd, linux-crypto

Add a "favourlzo" compression mode to jffs2 which tries to
optimise by size but gives lzo an advantage when comparing sizes.
This means the faster lzo algorithm can be preferred when there
isn't much difference in compressed size (the exact threshold can
be changed).

Signed-off-by: Richard Purdie <rpurdie@openedhand.com>
---
 fs/Kconfig       |    7 +++++++
 fs/jffs2/compr.c |   45 ++++++++++++++++++++++++++++++++++++++++-----
 fs/jffs2/compr.h |    3 +++
 3 files changed, 50 insertions(+), 5 deletions(-)

diff --git a/fs/Kconfig b/fs/Kconfig
index 1645dfa..328a62f 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -1359,6 +1359,13 @@ config JFFS2_CMODE_SIZE
           Tries all compressors and chooses the one which has the smallest
           result.
 
+config JFFS2_CMODE_FAVOURLZO
+        bool "Favour LZO"
+        help
+          Tries all compressors and chooses the one which has the smallest
+          result but gives some preference to LZO (which has faster
+	  decompression) at the expense of size.
+
 endchoice
 
 config CRAMFS
diff --git a/fs/jffs2/compr.c b/fs/jffs2/compr.c
index 6a23408..fdb0efd 100644
--- a/fs/jffs2/compr.c
+++ b/fs/jffs2/compr.c
@@ -24,6 +24,34 @@ static int jffs2_compression_mode = JFFS2_COMPR_MODE_PRIORITY;
 /* Statistics for blocks stored without compression */
 static uint32_t none_stat_compr_blocks=0,none_stat_decompr_blocks=0,none_stat_compr_size=0;
 
+
+/*
+ * Return 1 to use this compression
+ */
+static int jffs2_is_best_compression(struct jffs2_compressor *this,
+		struct jffs2_compressor *best, uint32_t size, uint32_t bestsize)
+{
+	switch (jffs2_compression_mode) {
+		case JFFS2_COMPR_MODE_SIZE:
+			if (bestsize > size)
+				return 1;
+			return 0;
+		case JFFS2_COMPR_MODE_FAVOURLZO:
+			if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > size))
+				return 1;
+			if ((best->compr != JFFS2_COMPR_LZO) && (bestsize > size))
+				return 1;
+			if ((this->compr == JFFS2_COMPR_LZO) && (bestsize > (size * FAVOUR_LZO_PERCENT / 100)))
+				return 1;
+			if ((bestsize * FAVOUR_LZO_PERCENT / 100) > size)
+				return 1;
+
+			return 0;
+	}
+	/* Shouldn't happen */
+	return 0;
+}
+
 /* jffs2_compress:
  * @data: Pointer to uncompressed data
  * @cdata: Pointer to returned pointer to buffer for compressed data
@@ -89,6 +117,7 @@ uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
                 if (ret == JFFS2_COMPR_NONE) kfree(output_buf);
                 break;
         case JFFS2_COMPR_MODE_SIZE:
+        case JFFS2_COMPR_MODE_FAVOURLZO:
                 orig_slen = *datalen;
                 orig_dlen = *cdatalen;
                 spin_lock(&jffs2_compressor_list_lock);
@@ -97,7 +126,7 @@ uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
                         if ((!this->compress)||(this->disabled))
                                 continue;
                         /* Allocating memory for output buffer if necessary */
-                        if ((this->compr_buf_size<orig_dlen)&&(this->compr_buf)) {
+                        if ((this->compr_buf_size<orig_slen)&&(this->compr_buf)) {
                                 spin_unlock(&jffs2_compressor_list_lock);
                                 kfree(this->compr_buf);
                                 spin_lock(&jffs2_compressor_list_lock);
@@ -106,15 +135,15 @@ uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
                         }
                         if (!this->compr_buf) {
                                 spin_unlock(&jffs2_compressor_list_lock);
-                                tmp_buf = kmalloc(orig_dlen,GFP_KERNEL);
+                                tmp_buf = kmalloc(orig_slen,GFP_KERNEL);
                                 spin_lock(&jffs2_compressor_list_lock);
                                 if (!tmp_buf) {
-                                        printk(KERN_WARNING "JFFS2: No memory for compressor allocation. (%d bytes)\n",orig_dlen);
+                                        printk(KERN_WARNING "JFFS2: No memory for compressor allocation. (%d bytes)\n",orig_slen);
                                         continue;
                                 }
                                 else {
                                         this->compr_buf = tmp_buf;
-                                        this->compr_buf_size = orig_dlen;
+                                        this->compr_buf_size = orig_slen;
                                 }
                         }
                         this->usecount++;
@@ -125,7 +154,8 @@ uint16_t jffs2_compress(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
                         spin_lock(&jffs2_compressor_list_lock);
                         this->usecount--;
                         if (!compr_ret) {
-                                if ((!best_dlen)||(best_dlen>*cdatalen)) {
+				if (((!best_dlen) || jffs2_is_best_compression(this, best, *cdatalen, best_dlen))
+						&& (*cdatalen < *datalen)) {
                                         best_dlen = *cdatalen;
                                         best_slen = *datalen;
                                         best = this;
@@ -297,9 +327,14 @@ int __init jffs2_compressors_init(void)
         jffs2_compression_mode = JFFS2_COMPR_MODE_SIZE;
         D1(printk(KERN_INFO "JFFS2: default compression mode: size\n");)
 #else
+#ifdef CONFIG_JFFS2_CMODE_FAVOURLZO
+        jffs2_compression_mode = JFFS2_COMPR_MODE_FAVOURLZO;
+        D1(printk(KERN_INFO "JFFS2: default compression mode: favourlzo\n");)
+#else
         D1(printk(KERN_INFO "JFFS2: default compression mode: priority\n");)
 #endif
 #endif
+#endif
         return 0;
 }
 
diff --git a/fs/jffs2/compr.h b/fs/jffs2/compr.h
index 8c6b2af..2b66941 100644
--- a/fs/jffs2/compr.h
+++ b/fs/jffs2/compr.h
@@ -38,6 +38,9 @@
 #define JFFS2_COMPR_MODE_NONE       0
 #define JFFS2_COMPR_MODE_PRIORITY   1
 #define JFFS2_COMPR_MODE_SIZE       2
+#define JFFS2_COMPR_MODE_FAVOURLZO  3
+
+#define FAVOUR_LZO_PERCENT 80
 
 struct jffs2_compressor {
         struct list_head list;

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-05-04 11:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-04 11:30 [PATCH 3/5] jffs2: Add a "favourlzo" compression mode to jffs2 Richard Purdie
  -- strict thread matches above, loose matches on Subject: below --
2007-05-01 14:47 Richard Purdie
2007-05-01 20:04 ` Randy Dunlap

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox