1 ======================================== 1 ======================================== 2 zram: Compressed RAM-based block devices 2 zram: Compressed RAM-based block devices 3 ======================================== 3 ======================================== 4 4 5 Introduction 5 Introduction 6 ============ 6 ============ 7 7 8 The zram module creates RAM-based block device 8 The zram module creates RAM-based block devices named /dev/zram<id> 9 (<id> = 0, 1, ...). Pages written to these dis 9 (<id> = 0, 1, ...). Pages written to these disks are compressed and stored 10 in memory itself. These disks allow very fast 10 in memory itself. These disks allow very fast I/O and compression provides 11 good amounts of memory savings. Some of the us 11 good amounts of memory savings. Some of the use cases include /tmp storage, 12 use as swap disks, various caches under /var a 12 use as swap disks, various caches under /var and maybe many more. :) 13 13 14 Statistics for individual zram devices are exp 14 Statistics for individual zram devices are exported through sysfs nodes at 15 /sys/block/zram<id>/ 15 /sys/block/zram<id>/ 16 16 17 Usage 17 Usage 18 ===== 18 ===== 19 19 20 There are several ways to configure and manage 20 There are several ways to configure and manage zram device(-s): 21 21 22 a) using zram and zram_control sysfs attribute 22 a) using zram and zram_control sysfs attributes 23 b) using zramctl utility, provided by util-lin 23 b) using zramctl utility, provided by util-linux (util-linux@vger.kernel.org). 24 24 25 In this document we will describe only 'manual 25 In this document we will describe only 'manual' zram configuration steps, 26 IOW, zram and zram_control sysfs attributes. 26 IOW, zram and zram_control sysfs attributes. 27 27 28 In order to get a better idea about zramctl pl 28 In order to get a better idea about zramctl please consult util-linux 29 documentation, zramctl man-page or `zramctl -- 29 documentation, zramctl man-page or `zramctl --help`. Please be informed 30 that zram maintainers do not develop/maintain 30 that zram maintainers do not develop/maintain util-linux or zramctl, should 31 you have any questions please contact util-lin 31 you have any questions please contact util-linux@vger.kernel.org 32 32 33 Following shows a typical sequence of steps fo 33 Following shows a typical sequence of steps for using zram. 34 34 35 WARNING 35 WARNING 36 ======= 36 ======= 37 37 38 For the sake of simplicity we skip error check 38 For the sake of simplicity we skip error checking parts in most of the 39 examples below. However, it is your sole respo 39 examples below. However, it is your sole responsibility to handle errors. 40 40 41 zram sysfs attributes always return negative v 41 zram sysfs attributes always return negative values in case of errors. 42 The list of possible return codes: 42 The list of possible return codes: 43 43 44 ======== ==================================== 44 ======== ============================================================= 45 -EBUSY an attempt to modify an attribute th 45 -EBUSY an attempt to modify an attribute that cannot be changed once 46 the device has been initialised. Ple 46 the device has been initialised. Please reset device first. 47 -ENOMEM zram was not able to allocate enough 47 -ENOMEM zram was not able to allocate enough memory to fulfil your 48 needs. 48 needs. 49 -EINVAL invalid input has been provided. 49 -EINVAL invalid input has been provided. 50 ======== ==================================== 50 ======== ============================================================= 51 51 52 If you use 'echo', the returned value is set b 52 If you use 'echo', the returned value is set by the 'echo' utility, 53 and, in general case, something like:: 53 and, in general case, something like:: 54 54 55 echo 3 > /sys/block/zram0/max_comp_str 55 echo 3 > /sys/block/zram0/max_comp_streams 56 if [ $? -ne 0 ]; then 56 if [ $? -ne 0 ]; then 57 handle_error 57 handle_error 58 fi 58 fi 59 59 60 should suffice. 60 should suffice. 61 61 62 1) Load Module 62 1) Load Module 63 ============== 63 ============== 64 64 65 :: 65 :: 66 66 67 modprobe zram num_devices=4 67 modprobe zram num_devices=4 68 68 69 This creates 4 devices: /dev/zram{0,1,2,3} 69 This creates 4 devices: /dev/zram{0,1,2,3} 70 70 71 num_devices parameter is optional and tells zr 71 num_devices parameter is optional and tells zram how many devices should be 72 pre-created. Default: 1. 72 pre-created. Default: 1. 73 73 74 2) Set max number of compression streams 74 2) Set max number of compression streams 75 ======================================== 75 ======================================== 76 76 77 Regardless of the value passed to this attribu 77 Regardless of the value passed to this attribute, ZRAM will always 78 allocate multiple compression streams - one pe 78 allocate multiple compression streams - one per online CPU - thus 79 allowing several concurrent compression operat 79 allowing several concurrent compression operations. The number of 80 allocated compression streams goes down when s 80 allocated compression streams goes down when some of the CPUs 81 become offline. There is no single-compression 81 become offline. There is no single-compression-stream mode anymore, 82 unless you are running a UP system or have onl 82 unless you are running a UP system or have only 1 CPU online. 83 83 84 To find out how many streams are currently ava 84 To find out how many streams are currently available:: 85 85 86 cat /sys/block/zram0/max_comp_streams 86 cat /sys/block/zram0/max_comp_streams 87 87 88 3) Select compression algorithm 88 3) Select compression algorithm 89 =============================== 89 =============================== 90 90 91 Using comp_algorithm device attribute one can 91 Using comp_algorithm device attribute one can see available and 92 currently selected (shown in square brackets) 92 currently selected (shown in square brackets) compression algorithms, 93 or change the selected compression algorithm ( 93 or change the selected compression algorithm (once the device is initialised 94 there is no way to change compression algorith 94 there is no way to change compression algorithm). 95 95 96 Examples:: 96 Examples:: 97 97 98 #show supported compression algorithms 98 #show supported compression algorithms 99 cat /sys/block/zram0/comp_algorithm 99 cat /sys/block/zram0/comp_algorithm 100 lzo [lz4] 100 lzo [lz4] 101 101 102 #select lzo compression algorithm 102 #select lzo compression algorithm 103 echo lzo > /sys/block/zram0/comp_algor 103 echo lzo > /sys/block/zram0/comp_algorithm 104 104 105 For the time being, the `comp_algorithm` conte !! 105 For the time being, the `comp_algorithm` content does not necessarily 106 algorithms that are supported by zram. !! 106 show every compression algorithm supported by the kernel. We keep this >> 107 list primarily to simplify device configuration and one can configure >> 108 a new device with a compression algorithm that is not listed in >> 109 `comp_algorithm`. The thing is that, internally, ZRAM uses Crypto API >> 110 and, if some of the algorithms were built as modules, it's impossible >> 111 to list all of them using, for instance, /proc/crypto or any other >> 112 method. This, however, has an advantage of permitting the usage of >> 113 custom crypto compression modules (implementing S/W or H/W compression). 107 114 108 4) Set compression algorithm parameters: Optio !! 115 4) Set Disksize 109 ============================================== << 110 << 111 Compression algorithms may support specific pa << 112 tweaked for particular dataset. ZRAM has an `a << 113 attribute which provides a per-algorithm param << 114 << 115 For example, several compression algorithms su << 116 In addition, certain compression algorithms su << 117 which significantly change algorithms' charact << 118 compression algorithm to use external pre-trai << 119 path to the `dict` along with other parameters << 120 << 121 #pass path to pre-trained zstd diction << 122 echo "algo=zstd dict=/etc/dictioary" > << 123 << 124 #same, but using algorithm priority << 125 echo "priority=1 dict=/etc/dictioary" << 126 /sys/block/zram0/algorithm_par << 127 << 128 #pass path to pre-trained zstd diction << 129 echo "algo=zstd level=8 dict=/etc/dict << 130 /sys/block/zram0/algorithm_par << 131 << 132 Parameters are algorithm specific: not all alg << 133 dictionaries, not all algorithms support `leve << 134 algorithms `level` controls the compression le << 135 better the compression ratio, it even can take << 136 algorithms), for other algorithms `level` is a << 137 the value the lower the compression ratio). << 138 << 139 5) Set Disksize << 140 =============== 116 =============== 141 117 142 Set disk size by writing the value to sysfs no 118 Set disk size by writing the value to sysfs node 'disksize'. 143 The value can be either in bytes or you can us 119 The value can be either in bytes or you can use mem suffixes. 144 Examples:: 120 Examples:: 145 121 146 # Initialize /dev/zram0 with 50MB disk 122 # Initialize /dev/zram0 with 50MB disksize 147 echo $((50*1024*1024)) > /sys/block/zr 123 echo $((50*1024*1024)) > /sys/block/zram0/disksize 148 124 149 # Using mem suffixes 125 # Using mem suffixes 150 echo 256K > /sys/block/zram0/disksize 126 echo 256K > /sys/block/zram0/disksize 151 echo 512M > /sys/block/zram0/disksize 127 echo 512M > /sys/block/zram0/disksize 152 echo 1G > /sys/block/zram0/disksize 128 echo 1G > /sys/block/zram0/disksize 153 129 154 Note: 130 Note: 155 There is little point creating a zram of great 131 There is little point creating a zram of greater than twice the size of memory 156 since we expect a 2:1 compression ratio. Note 132 since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the 157 size of the disk when not in use so a huge zra 133 size of the disk when not in use so a huge zram is wasteful. 158 134 159 6) Set memory limit: Optional !! 135 5) Set memory limit: Optional 160 ============================= 136 ============================= 161 137 162 Set memory limit by writing the value to sysfs 138 Set memory limit by writing the value to sysfs node 'mem_limit'. 163 The value can be either in bytes or you can us 139 The value can be either in bytes or you can use mem suffixes. 164 In addition, you could change the value in run 140 In addition, you could change the value in runtime. 165 Examples:: 141 Examples:: 166 142 167 # limit /dev/zram0 with 50MB memory 143 # limit /dev/zram0 with 50MB memory 168 echo $((50*1024*1024)) > /sys/block/zr 144 echo $((50*1024*1024)) > /sys/block/zram0/mem_limit 169 145 170 # Using mem suffixes 146 # Using mem suffixes 171 echo 256K > /sys/block/zram0/mem_limit 147 echo 256K > /sys/block/zram0/mem_limit 172 echo 512M > /sys/block/zram0/mem_limit 148 echo 512M > /sys/block/zram0/mem_limit 173 echo 1G > /sys/block/zram0/mem_limit 149 echo 1G > /sys/block/zram0/mem_limit 174 150 175 # To disable memory limit 151 # To disable memory limit 176 echo 0 > /sys/block/zram0/mem_limit 152 echo 0 > /sys/block/zram0/mem_limit 177 153 178 7) Activate !! 154 6) Activate 179 =========== 155 =========== 180 156 181 :: 157 :: 182 158 183 mkswap /dev/zram0 159 mkswap /dev/zram0 184 swapon /dev/zram0 160 swapon /dev/zram0 185 161 186 mkfs.ext4 /dev/zram1 162 mkfs.ext4 /dev/zram1 187 mount /dev/zram1 /tmp 163 mount /dev/zram1 /tmp 188 164 189 8) Add/remove zram devices !! 165 7) Add/remove zram devices 190 ========================== 166 ========================== 191 167 192 zram provides a control interface, which enabl 168 zram provides a control interface, which enables dynamic (on-demand) device 193 addition and removal. 169 addition and removal. 194 170 195 In order to add a new /dev/zramX device, perfo 171 In order to add a new /dev/zramX device, perform a read operation on the hot_add 196 attribute. This will return either the new dev 172 attribute. This will return either the new device's device id (meaning that you 197 can use /dev/zram<id>) or an error code. 173 can use /dev/zram<id>) or an error code. 198 174 199 Example:: 175 Example:: 200 176 201 cat /sys/class/zram-control/hot_add 177 cat /sys/class/zram-control/hot_add 202 1 178 1 203 179 204 To remove the existing /dev/zramX device (wher 180 To remove the existing /dev/zramX device (where X is a device id) 205 execute:: 181 execute:: 206 182 207 echo X > /sys/class/zram-control/hot_r 183 echo X > /sys/class/zram-control/hot_remove 208 184 209 9) Stats !! 185 8) Stats 210 ======== 186 ======== 211 187 212 Per-device statistics are exported as various 188 Per-device statistics are exported as various nodes under /sys/block/zram<id>/ 213 189 214 A brief description of exported device attribu 190 A brief description of exported device attributes follows. For more details 215 please read Documentation/ABI/testing/sysfs-bl 191 please read Documentation/ABI/testing/sysfs-block-zram. 216 192 217 ====================== ====== ============== 193 ====================== ====== =============================================== 218 Name access desc 194 Name access description 219 ====================== ====== ============== 195 ====================== ====== =============================================== 220 disksize RW show and set t 196 disksize RW show and set the device's disk size 221 initstate RO shows the init 197 initstate RO shows the initialization state of the device 222 reset WO trigger device 198 reset WO trigger device reset 223 mem_used_max WO reset the `mem 199 mem_used_max WO reset the `mem_used_max` counter (see later) 224 mem_limit WO specifies the 200 mem_limit WO specifies the maximum amount of memory ZRAM can 225 use to store t 201 use to store the compressed data 226 writeback_limit WO specifies the 202 writeback_limit WO specifies the maximum amount of write IO zram 227 can write out 203 can write out to backing device as 4KB unit 228 writeback_limit_enable RW show and set w 204 writeback_limit_enable RW show and set writeback_limit feature 229 max_comp_streams RW the number of 205 max_comp_streams RW the number of possible concurrent compress 230 operations 206 operations 231 comp_algorithm RW show and chang 207 comp_algorithm RW show and change the compression algorithm 232 algorithm_params WO setup compress << 233 compact WO trigger memory 208 compact WO trigger memory compaction 234 debug_stat RO this file is u 209 debug_stat RO this file is used for zram debugging purposes 235 backing_dev RW set up backend 210 backing_dev RW set up backend storage for zram to write out 236 idle WO mark allocated 211 idle WO mark allocated slot as idle 237 ====================== ====== ============== 212 ====================== ====== =============================================== 238 213 239 214 240 User space is advised to use the following fil 215 User space is advised to use the following files to read the device statistics. 241 216 242 File /sys/block/zram<id>/stat 217 File /sys/block/zram<id>/stat 243 218 244 Represents block layer statistics. Read Docume 219 Represents block layer statistics. Read Documentation/block/stat.rst for 245 details. 220 details. 246 221 247 File /sys/block/zram<id>/io_stat 222 File /sys/block/zram<id>/io_stat 248 223 249 The stat file represents device's I/O statisti 224 The stat file represents device's I/O statistics not accounted by block 250 layer and, thus, not available in zram<id>/sta 225 layer and, thus, not available in zram<id>/stat file. It consists of a 251 single line of text and contains the following 226 single line of text and contains the following stats separated by 252 whitespace: 227 whitespace: 253 228 254 ============= ============================ 229 ============= ============================================================= 255 failed_reads The number of failed reads 230 failed_reads The number of failed reads 256 failed_writes The number of failed writes 231 failed_writes The number of failed writes 257 invalid_io The number of non-page-size- 232 invalid_io The number of non-page-size-aligned I/O requests 258 notify_free Depending on device usage sc 233 notify_free Depending on device usage scenario it may account 259 234 260 a) the number of pages freed 235 a) the number of pages freed because of swap slot free 261 notifications 236 notifications 262 b) the number of pages freed 237 b) the number of pages freed because of 263 REQ_OP_DISCARD requests s 238 REQ_OP_DISCARD requests sent by bio. The former ones are 264 sent to a swap block devi 239 sent to a swap block device when a swap slot is freed, 265 which implies that this d 240 which implies that this disk is being used as a swap disk. 266 241 267 The latter ones are sent by 242 The latter ones are sent by filesystem mounted with 268 discard option, whenever som 243 discard option, whenever some data blocks are getting 269 discarded. 244 discarded. 270 ============= ============================ 245 ============= ============================================================= 271 246 272 File /sys/block/zram<id>/mm_stat 247 File /sys/block/zram<id>/mm_stat 273 248 274 The mm_stat file represents the device's mm st 249 The mm_stat file represents the device's mm statistics. It consists of a single 275 line of text and contains the following stats 250 line of text and contains the following stats separated by whitespace: 276 251 277 ================ ============================ 252 ================ ============================================================= 278 orig_data_size uncompressed size of data st 253 orig_data_size uncompressed size of data stored in this disk. 279 Unit: bytes 254 Unit: bytes 280 compr_data_size compressed size of data stor 255 compr_data_size compressed size of data stored in this disk 281 mem_used_total the amount of memory allocat 256 mem_used_total the amount of memory allocated for this disk. This 282 includes allocator fragmenta 257 includes allocator fragmentation and metadata overhead, 283 allocated for this disk. So, 258 allocated for this disk. So, allocator space efficiency 284 can be calculated using comp 259 can be calculated using compr_data_size and this statistic. 285 Unit: bytes 260 Unit: bytes 286 mem_limit the maximum amount of memory 261 mem_limit the maximum amount of memory ZRAM can use to store 287 the compressed data 262 the compressed data 288 mem_used_max the maximum amount of memory 263 mem_used_max the maximum amount of memory zram has consumed to 289 store the data 264 store the data 290 same_pages the number of same element f 265 same_pages the number of same element filled pages written to this disk. 291 No memory is allocated for s 266 No memory is allocated for such pages. 292 pages_compacted the number of pages freed du 267 pages_compacted the number of pages freed during compaction 293 huge_pages the number of incompressible 268 huge_pages the number of incompressible pages 294 huge_pages_since the number of incompressible 269 huge_pages_since the number of incompressible pages since zram set up 295 ================ ============================ 270 ================ ============================================================= 296 271 297 File /sys/block/zram<id>/bd_stat 272 File /sys/block/zram<id>/bd_stat 298 273 299 The bd_stat file represents a device's backing 274 The bd_stat file represents a device's backing device statistics. It consists of 300 a single line of text and contains the followi 275 a single line of text and contains the following stats separated by whitespace: 301 276 302 ============== ============================== 277 ============== ============================================================= 303 bd_count size of data written in backin 278 bd_count size of data written in backing device. 304 Unit: 4K bytes 279 Unit: 4K bytes 305 bd_reads the number of reads from backi 280 bd_reads the number of reads from backing device 306 Unit: 4K bytes 281 Unit: 4K bytes 307 bd_writes the number of writes to backin 282 bd_writes the number of writes to backing device 308 Unit: 4K bytes 283 Unit: 4K bytes 309 ============== ============================== 284 ============== ============================================================= 310 285 311 10) Deactivate !! 286 9) Deactivate 312 ============== !! 287 ============= 313 288 314 :: 289 :: 315 290 316 swapoff /dev/zram0 291 swapoff /dev/zram0 317 umount /dev/zram1 292 umount /dev/zram1 318 293 319 11) Reset !! 294 10) Reset 320 ========= 295 ========= 321 296 322 Write any positive value to 'reset' sy 297 Write any positive value to 'reset' sysfs node:: 323 298 324 echo 1 > /sys/block/zram0/rese 299 echo 1 > /sys/block/zram0/reset 325 echo 1 > /sys/block/zram1/rese 300 echo 1 > /sys/block/zram1/reset 326 301 327 This frees all the memory allocated fo 302 This frees all the memory allocated for the given device and 328 resets the disksize to zero. You must 303 resets the disksize to zero. You must set the disksize again 329 before reusing the device. 304 before reusing the device. 330 305 331 Optional Feature 306 Optional Feature 332 ================ 307 ================ 333 308 334 writeback 309 writeback 335 --------- 310 --------- 336 311 337 With CONFIG_ZRAM_WRITEBACK, zram can write idl 312 With CONFIG_ZRAM_WRITEBACK, zram can write idle/incompressible page 338 to backing storage rather than keeping it in m 313 to backing storage rather than keeping it in memory. 339 To use the feature, admin should set up backin 314 To use the feature, admin should set up backing device via:: 340 315 341 echo /dev/sda5 > /sys/block/zramX/back 316 echo /dev/sda5 > /sys/block/zramX/backing_dev 342 317 343 before disksize setting. It supports only part 318 before disksize setting. It supports only partitions at this moment. 344 If admin wants to use incompressible page writ 319 If admin wants to use incompressible page writeback, they could do it via:: 345 320 346 echo huge > /sys/block/zramX/writeback 321 echo huge > /sys/block/zramX/writeback 347 322 348 To use idle page writeback, first, user need t 323 To use idle page writeback, first, user need to declare zram pages 349 as idle:: 324 as idle:: 350 325 351 echo all > /sys/block/zramX/idle 326 echo all > /sys/block/zramX/idle 352 327 353 From now on, any pages on zram are idle pages. 328 From now on, any pages on zram are idle pages. The idle mark 354 will be removed until someone requests access 329 will be removed until someone requests access of the block. 355 IOW, unless there is access request, those pag 330 IOW, unless there is access request, those pages are still idle pages. 356 Additionally, when CONFIG_ZRAM_TRACK_ENTRY_ACT 331 Additionally, when CONFIG_ZRAM_TRACK_ENTRY_ACTIME is enabled pages can be 357 marked as idle based on how long (in seconds) 332 marked as idle based on how long (in seconds) it's been since they were 358 last accessed:: 333 last accessed:: 359 334 360 echo 86400 > /sys/block/zramX/idle 335 echo 86400 > /sys/block/zramX/idle 361 336 362 In this example all pages which haven't been a 337 In this example all pages which haven't been accessed in more than 86400 363 seconds (one day) will be marked idle. 338 seconds (one day) will be marked idle. 364 339 365 Admin can request writeback of those idle page 340 Admin can request writeback of those idle pages at right timing via:: 366 341 367 echo idle > /sys/block/zramX/writeback 342 echo idle > /sys/block/zramX/writeback 368 343 369 With the command, zram will writeback idle pag 344 With the command, zram will writeback idle pages from memory to the storage. 370 345 371 Additionally, if a user choose to writeback on 346 Additionally, if a user choose to writeback only huge and idle pages 372 this can be accomplished with:: 347 this can be accomplished with:: 373 348 374 echo huge_idle > /sys/block/zramX/writ 349 echo huge_idle > /sys/block/zramX/writeback 375 350 376 If a user chooses to writeback only incompress 351 If a user chooses to writeback only incompressible pages (pages that none of 377 algorithms can compress) this can be accomplis 352 algorithms can compress) this can be accomplished with:: 378 353 379 echo incompressible > /sys/block/zramX 354 echo incompressible > /sys/block/zramX/writeback 380 355 381 If an admin wants to write a specific page in 356 If an admin wants to write a specific page in zram device to the backing device, 382 they could write a page index into the interfa 357 they could write a page index into the interface:: 383 358 384 echo "page_index=1251" > /sys/block/zr 359 echo "page_index=1251" > /sys/block/zramX/writeback 385 360 386 If there are lots of write IO with flash devic 361 If there are lots of write IO with flash device, potentially, it has 387 flash wearout problem so that admin needs to d 362 flash wearout problem so that admin needs to design write limitation 388 to guarantee storage health for entire product 363 to guarantee storage health for entire product life. 389 364 390 To overcome the concern, zram supports "writeb 365 To overcome the concern, zram supports "writeback_limit" feature. 391 The "writeback_limit_enable"'s default value i 366 The "writeback_limit_enable"'s default value is 0 so that it doesn't limit 392 any writeback. IOW, if admin wants to apply wr 367 any writeback. IOW, if admin wants to apply writeback budget, they should 393 enable writeback_limit_enable via:: 368 enable writeback_limit_enable via:: 394 369 395 $ echo 1 > /sys/block/zramX/writeback_ 370 $ echo 1 > /sys/block/zramX/writeback_limit_enable 396 371 397 Once writeback_limit_enable is set, zram doesn 372 Once writeback_limit_enable is set, zram doesn't allow any writeback 398 until admin sets the budget via /sys/block/zra 373 until admin sets the budget via /sys/block/zramX/writeback_limit. 399 374 400 (If admin doesn't enable writeback_limit_enabl 375 (If admin doesn't enable writeback_limit_enable, writeback_limit's value 401 assigned via /sys/block/zramX/writeback_limit 376 assigned via /sys/block/zramX/writeback_limit is meaningless.) 402 377 403 If admin wants to limit writeback as per-day 4 378 If admin wants to limit writeback as per-day 400M, they could do it 404 like below:: 379 like below:: 405 380 406 $ MB_SHIFT=20 381 $ MB_SHIFT=20 407 $ 4K_SHIFT=12 382 $ 4K_SHIFT=12 408 $ echo $((400<<MB_SHIFT>>4K_SHIFT)) > 383 $ echo $((400<<MB_SHIFT>>4K_SHIFT)) > \ 409 /sys/block/zram0/writeback_lim 384 /sys/block/zram0/writeback_limit. 410 $ echo 1 > /sys/block/zram0/writeback_ 385 $ echo 1 > /sys/block/zram0/writeback_limit_enable 411 386 412 If admins want to allow further write again on 387 If admins want to allow further write again once the budget is exhausted, 413 they could do it like below:: 388 they could do it like below:: 414 389 415 $ echo $((400<<MB_SHIFT>>4K_SHIFT)) > 390 $ echo $((400<<MB_SHIFT>>4K_SHIFT)) > \ 416 /sys/block/zram0/writeback_lim 391 /sys/block/zram0/writeback_limit 417 392 418 If an admin wants to see the remaining writeba 393 If an admin wants to see the remaining writeback budget since last set:: 419 394 420 $ cat /sys/block/zramX/writeback_limit 395 $ cat /sys/block/zramX/writeback_limit 421 396 422 If an admin wants to disable writeback limit, 397 If an admin wants to disable writeback limit, they could do:: 423 398 424 $ echo 0 > /sys/block/zramX/writeback_ 399 $ echo 0 > /sys/block/zramX/writeback_limit_enable 425 400 426 The writeback_limit count will reset whenever 401 The writeback_limit count will reset whenever you reset zram (e.g., 427 system reboot, echo 1 > /sys/block/zramX/reset 402 system reboot, echo 1 > /sys/block/zramX/reset) so keeping how many of 428 writeback happened until you reset the zram to 403 writeback happened until you reset the zram to allocate extra writeback 429 budget in next setting is user's job. 404 budget in next setting is user's job. 430 405 431 If admin wants to measure writeback count in a 406 If admin wants to measure writeback count in a certain period, they could 432 know it via /sys/block/zram0/bd_stat's 3rd col 407 know it via /sys/block/zram0/bd_stat's 3rd column. 433 408 434 recompression 409 recompression 435 ------------- 410 ------------- 436 411 437 With CONFIG_ZRAM_MULTI_COMP, zram can recompre 412 With CONFIG_ZRAM_MULTI_COMP, zram can recompress pages using alternative 438 (secondary) compression algorithms. The basic 413 (secondary) compression algorithms. The basic idea is that alternative 439 compression algorithm can provide better compr 414 compression algorithm can provide better compression ratio at a price of 440 (potentially) slower compression/decompression 415 (potentially) slower compression/decompression speeds. Alternative compression 441 algorithm can, for example, be more successful 416 algorithm can, for example, be more successful compressing huge pages (those 442 that default algorithm failed to compress). An 417 that default algorithm failed to compress). Another application is idle pages 443 recompression - pages that are cold and sit in 418 recompression - pages that are cold and sit in the memory can be recompressed 444 using more effective algorithm and, hence, red 419 using more effective algorithm and, hence, reduce zsmalloc memory usage. 445 420 446 With CONFIG_ZRAM_MULTI_COMP, zram supports up 421 With CONFIG_ZRAM_MULTI_COMP, zram supports up to 4 compression algorithms: 447 one primary and up to 3 secondary ones. Primar 422 one primary and up to 3 secondary ones. Primary zram compressor is explained 448 in "3) Select compression algorithm", secondar 423 in "3) Select compression algorithm", secondary algorithms are configured 449 using recomp_algorithm device attribute. 424 using recomp_algorithm device attribute. 450 425 451 Example::: 426 Example::: 452 427 453 #show supported recompression algorith 428 #show supported recompression algorithms 454 cat /sys/block/zramX/recomp_algorithm 429 cat /sys/block/zramX/recomp_algorithm 455 #1: lzo lzo-rle lz4 lz4hc [zstd] 430 #1: lzo lzo-rle lz4 lz4hc [zstd] 456 #2: lzo lzo-rle lz4 [lz4hc] zstd 431 #2: lzo lzo-rle lz4 [lz4hc] zstd 457 432 458 Alternative compression algorithms are sorted 433 Alternative compression algorithms are sorted by priority. In the example 459 above, zstd is used as the first alternative a 434 above, zstd is used as the first alternative algorithm, which has priority 460 of 1, while lz4hc is configured as a compressi 435 of 1, while lz4hc is configured as a compression algorithm with priority 2. 461 Alternative compression algorithm's priority i 436 Alternative compression algorithm's priority is provided during algorithms 462 configuration::: 437 configuration::: 463 438 464 #select zstd recompression algorithm, 439 #select zstd recompression algorithm, priority 1 465 echo "algo=zstd priority=1" > /sys/blo 440 echo "algo=zstd priority=1" > /sys/block/zramX/recomp_algorithm 466 441 467 #select deflate recompression algorith 442 #select deflate recompression algorithm, priority 2 468 echo "algo=deflate priority=2" > /sys/ 443 echo "algo=deflate priority=2" > /sys/block/zramX/recomp_algorithm 469 444 470 Another device attribute that CONFIG_ZRAM_MULT 445 Another device attribute that CONFIG_ZRAM_MULTI_COMP enables is recompress, 471 which controls recompression. 446 which controls recompression. 472 447 473 Examples::: 448 Examples::: 474 449 475 #IDLE pages recompression is activated 450 #IDLE pages recompression is activated by `idle` mode 476 echo "type=idle" > /sys/block/zramX/re 451 echo "type=idle" > /sys/block/zramX/recompress 477 452 478 #HUGE pages recompression is activated 453 #HUGE pages recompression is activated by `huge` mode 479 echo "type=huge" > /sys/block/zram0/re 454 echo "type=huge" > /sys/block/zram0/recompress 480 455 481 #HUGE_IDLE pages recompression is acti 456 #HUGE_IDLE pages recompression is activated by `huge_idle` mode 482 echo "type=huge_idle" > /sys/block/zra 457 echo "type=huge_idle" > /sys/block/zramX/recompress 483 458 484 The number of idle pages can be significant, s 459 The number of idle pages can be significant, so user-space can pass a size 485 threshold (in bytes) to the recompress knob: z 460 threshold (in bytes) to the recompress knob: zram will recompress only pages 486 of equal or greater size::: 461 of equal or greater size::: 487 462 488 #recompress all pages larger than 3000 463 #recompress all pages larger than 3000 bytes 489 echo "threshold=3000" > /sys/block/zra 464 echo "threshold=3000" > /sys/block/zramX/recompress 490 465 491 #recompress idle pages larger than 200 466 #recompress idle pages larger than 2000 bytes 492 echo "type=idle threshold=2000" > /sys 467 echo "type=idle threshold=2000" > /sys/block/zramX/recompress 493 468 494 It is also possible to limit the number of pag 469 It is also possible to limit the number of pages zram re-compression will 495 attempt to recompress::: 470 attempt to recompress::: 496 471 497 echo "type=huge_idle max_pages=42" > / 472 echo "type=huge_idle max_pages=42" > /sys/block/zramX/recompress 498 473 499 Recompression of idle pages requires memory tr 474 Recompression of idle pages requires memory tracking. 500 475 501 During re-compression for every page, that mat 476 During re-compression for every page, that matches re-compression criteria, 502 ZRAM iterates the list of registered alternati 477 ZRAM iterates the list of registered alternative compression algorithms in 503 order of their priorities. ZRAM stops either w 478 order of their priorities. ZRAM stops either when re-compression was 504 successful (re-compressed object is smaller in 479 successful (re-compressed object is smaller in size than the original one) 505 and matches re-compression criteria (e.g. size 480 and matches re-compression criteria (e.g. size threshold) or when there are 506 no secondary algorithms left to try. If none o 481 no secondary algorithms left to try. If none of the secondary algorithms can 507 successfully re-compressed the page such a pag 482 successfully re-compressed the page such a page is marked as incompressible, 508 so ZRAM will not attempt to re-compress it in 483 so ZRAM will not attempt to re-compress it in the future. 509 484 510 This re-compression behaviour, when it iterate 485 This re-compression behaviour, when it iterates through the list of 511 registered compression algorithms, increases o 486 registered compression algorithms, increases our chances of finding the 512 algorithm that successfully compresses a parti 487 algorithm that successfully compresses a particular page. Sometimes, however, 513 it is convenient (and sometimes even necessary 488 it is convenient (and sometimes even necessary) to limit recompression to 514 only one particular algorithm so that it will 489 only one particular algorithm so that it will not try any other algorithms. 515 This can be achieved by providing a `algo` or !! 490 This can be achieved by providing a algo=NAME parameter::: 516 491 517 #use zstd algorithm only (if registere 492 #use zstd algorithm only (if registered) 518 echo "type=huge algo=zstd" > /sys/bloc 493 echo "type=huge algo=zstd" > /sys/block/zramX/recompress 519 << 520 #use zstd algorithm only (if zstd was << 521 echo "type=huge priority=1" > /sys/blo << 522 494 523 memory tracking 495 memory tracking 524 =============== 496 =============== 525 497 526 With CONFIG_ZRAM_MEMORY_TRACKING, user can kno 498 With CONFIG_ZRAM_MEMORY_TRACKING, user can know information of the 527 zram block. It could be useful to catch cold o 499 zram block. It could be useful to catch cold or incompressible 528 pages of the process with*pagemap. 500 pages of the process with*pagemap. 529 501 530 If you enable the feature, you could see block 502 If you enable the feature, you could see block state via 531 /sys/kernel/debug/zram/zram0/block_state". The 503 /sys/kernel/debug/zram/zram0/block_state". The output is as follows:: 532 504 533 300 75.033841 .wh... 505 300 75.033841 .wh... 534 301 63.806904 s..... 506 301 63.806904 s..... 535 302 63.806919 ..hi.. 507 302 63.806919 ..hi.. 536 303 62.801919 ....r. 508 303 62.801919 ....r. 537 304 146.781902 ..hi.n 509 304 146.781902 ..hi.n 538 510 539 First column 511 First column 540 zram's block index. 512 zram's block index. 541 Second column 513 Second column 542 access time since the system was boote 514 access time since the system was booted 543 Third column 515 Third column 544 state of the block: 516 state of the block: 545 517 546 s: 518 s: 547 same page 519 same page 548 w: 520 w: 549 written page to backing store 521 written page to backing store 550 h: 522 h: 551 huge page 523 huge page 552 i: 524 i: 553 idle page 525 idle page 554 r: 526 r: 555 recompressed page (secondary c 527 recompressed page (secondary compression algorithm) 556 n: 528 n: 557 none (including secondary) of 529 none (including secondary) of algorithms could compress it 558 530 559 First line of above example says 300th block i 531 First line of above example says 300th block is accessed at 75.033841sec 560 and the block's state is huge so it is written 532 and the block's state is huge so it is written back to the backing 561 storage. It's a debugging feature so anyone sh 533 storage. It's a debugging feature so anyone shouldn't rely on it to work 562 properly. 534 properly. 563 535 564 Nitin Gupta 536 Nitin Gupta 565 ngupta@vflare.org 537 ngupta@vflare.org
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.