Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) ========================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) zram: Compressed RAM-based block devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) ========================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) Introduction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) ============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) The zram module creates RAM-based block devices named /dev/zram<id>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) (<id> = 0, 1, ...). Pages written to these disks are compressed and stored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) in memory itself. These disks allow very fast I/O and compression provides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) good amounts of memory savings. Some of the use cases include /tmp storage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) use as swap disks, various caches under /var and maybe many more. :)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) Statistics for individual zram devices are exported through sysfs nodes at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /sys/block/zram<id>/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) Usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) =====
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) There are several ways to configure and manage zram device(-s):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) a) using zram and zram_control sysfs attributes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) b) using zramctl utility, provided by util-linux (util-linux@vger.kernel.org).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) In this document we will describe only 'manual' zram configuration steps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) IOW, zram and zram_control sysfs attributes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) In order to get a better idea about zramctl please consult util-linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) documentation, zramctl man-page or `zramctl --help`. Please be informed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) that zram maintainers do not develop/maintain util-linux or zramctl, should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) you have any questions please contact util-linux@vger.kernel.org
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) Following shows a typical sequence of steps for using zram.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) WARNING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) =======
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) For the sake of simplicity we skip error checking parts in most of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) examples below. However, it is your sole responsibility to handle errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) zram sysfs attributes always return negative values in case of errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) The list of possible return codes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) ========  =============================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) -EBUSY	  an attempt to modify an attribute that cannot be changed once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	  the device has been initialised. Please reset device first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) -ENOMEM	  zram was not able to allocate enough memory to fulfil your
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	  needs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) -EINVAL	  invalid input has been provided.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) ========  =============================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) If you use 'echo', the returned value is set by the 'echo' utility,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) and, in general case, something like::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	echo 3 > /sys/block/zram0/max_comp_streams
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if [ $? -ne 0 ]; then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		handle_error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	fi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) should suffice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 1) Load Module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) ==============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	modprobe zram num_devices=4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) This creates 4 devices: /dev/zram{0,1,2,3}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) num_devices parameter is optional and tells zram how many devices should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) pre-created. Default: 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 2) Set max number of compression streams
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) ========================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) Regardless of the value passed to this attribute, ZRAM will always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) allocate multiple compression streams - one per online CPU - thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) allowing several concurrent compression operations. The number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) allocated compression streams goes down when some of the CPUs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) become offline. There is no single-compression-stream mode anymore,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) unless you are running a UP system or have only 1 CPU online.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) To find out how many streams are currently available::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	cat /sys/block/zram0/max_comp_streams
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 3) Select compression algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) ===============================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) Using comp_algorithm device attribute one can see available and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) currently selected (shown in square brackets) compression algorithms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) or change the selected compression algorithm (once the device is initialised
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) there is no way to change compression algorithm).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) Examples::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	#show supported compression algorithms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	cat /sys/block/zram0/comp_algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	lzo [lz4]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	#select lzo compression algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	echo lzo > /sys/block/zram0/comp_algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) For the time being, the `comp_algorithm` content does not necessarily
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) show every compression algorithm supported by the kernel. We keep this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) list primarily to simplify device configuration and one can configure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) a new device with a compression algorithm that is not listed in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) `comp_algorithm`. The thing is that, internally, ZRAM uses Crypto API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) and, if some of the algorithms were built as modules, it's impossible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) to list all of them using, for instance, /proc/crypto or any other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) method. This, however, has an advantage of permitting the usage of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) custom crypto compression modules (implementing S/W or H/W compression).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 4) Set Disksize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) ===============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) Set disk size by writing the value to sysfs node 'disksize'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) The value can be either in bytes or you can use mem suffixes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) Examples::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	# Initialize /dev/zram0 with 50MB disksize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	echo $((50*1024*1024)) > /sys/block/zram0/disksize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	# Using mem suffixes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	echo 256K > /sys/block/zram0/disksize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	echo 512M > /sys/block/zram0/disksize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	echo 1G > /sys/block/zram0/disksize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) Note:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) There is little point creating a zram of greater than twice the size of memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) since we expect a 2:1 compression ratio. Note that zram uses about 0.1% of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) size of the disk when not in use so a huge zram is wasteful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 5) Set memory limit: Optional
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) =============================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) Set memory limit by writing the value to sysfs node 'mem_limit'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) The value can be either in bytes or you can use mem suffixes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) In addition, you could change the value in runtime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) Examples::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	# limit /dev/zram0 with 50MB memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	echo $((50*1024*1024)) > /sys/block/zram0/mem_limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	# Using mem suffixes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	echo 256K > /sys/block/zram0/mem_limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	echo 512M > /sys/block/zram0/mem_limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	echo 1G > /sys/block/zram0/mem_limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	# To disable memory limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	echo 0 > /sys/block/zram0/mem_limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 6) Activate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) ===========
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	mkswap /dev/zram0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	swapon /dev/zram0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	mkfs.ext4 /dev/zram1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	mount /dev/zram1 /tmp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 7) Add/remove zram devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ==========================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) zram provides a control interface, which enables dynamic (on-demand) device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) addition and removal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) In order to add a new /dev/zramX device, perform a read operation on the hot_add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) attribute. This will return either the new device's device id (meaning that you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) can use /dev/zram<id>) or an error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) Example::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	cat /sys/class/zram-control/hot_add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) To remove the existing /dev/zramX device (where X is a device id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) execute::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	echo X > /sys/class/zram-control/hot_remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 8) Stats
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) ========
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) Per-device statistics are exported as various nodes under /sys/block/zram<id>/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) A brief description of exported device attributes follows. For more details
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) please read Documentation/ABI/testing/sysfs-block-zram.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) ======================  ======  ===============================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) Name            	access            description
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) ======================  ======  ===============================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) disksize          	RW	show and set the device's disk size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) initstate         	RO	shows the initialization state of the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) reset             	WO	trigger device reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) mem_used_max      	WO	reset the `mem_used_max` counter (see later)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) mem_limit         	WO	specifies the maximum amount of memory ZRAM can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 				use to store the compressed data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) writeback_limit   	WO	specifies the maximum amount of write IO zram
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 				can write out to backing device as 4KB unit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) writeback_limit_enable  RW	show and set writeback_limit feature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) max_comp_streams  	RW	the number of possible concurrent compress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 				operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) comp_algorithm    	RW	show and change the compression algorithm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) compact           	WO	trigger memory compaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) debug_stat        	RO	this file is used for zram debugging purposes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) backing_dev	  	RW	set up backend storage for zram to write out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) idle		  	WO	mark allocated slot as idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) ======================  ======  ===============================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) User space is advised to use the following files to read the device statistics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) File /sys/block/zram<id>/stat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) Represents block layer statistics. Read Documentation/block/stat.rst for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) File /sys/block/zram<id>/io_stat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) The stat file represents device's I/O statistics not accounted by block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) layer and, thus, not available in zram<id>/stat file. It consists of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) single line of text and contains the following stats separated by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) whitespace:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  =============    =============================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  failed_reads     The number of failed reads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  failed_writes    The number of failed writes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  invalid_io       The number of non-page-size-aligned I/O requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  notify_free      Depending on device usage scenario it may account
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)                   a) the number of pages freed because of swap slot free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)                      notifications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)                   b) the number of pages freed because of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)                      REQ_OP_DISCARD requests sent by bio. The former ones are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)                      sent to a swap block device when a swap slot is freed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)                      which implies that this disk is being used as a swap disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)                   The latter ones are sent by filesystem mounted with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)                   discard option, whenever some data blocks are getting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)                   discarded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  =============    =============================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) File /sys/block/zram<id>/mm_stat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) The mm_stat file represents the device's mm statistics. It consists of a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) line of text and contains the following stats separated by whitespace:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  ================ =============================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  orig_data_size   uncompressed size of data stored in this disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)                   Unit: bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  compr_data_size  compressed size of data stored in this disk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  mem_used_total   the amount of memory allocated for this disk. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)                   includes allocator fragmentation and metadata overhead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)                   allocated for this disk. So, allocator space efficiency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)                   can be calculated using compr_data_size and this statistic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)                   Unit: bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)  mem_limit        the maximum amount of memory ZRAM can use to store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)                   the compressed data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)  mem_used_max     the maximum amount of memory zram has consumed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)                   store the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)  same_pages       the number of same element filled pages written to this disk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)                   No memory is allocated for such pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)  pages_compacted  the number of pages freed during compaction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)  huge_pages	  the number of incompressible pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)  ================ =============================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) File /sys/block/zram<id>/bd_stat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) The bd_stat file represents a device's backing device statistics. It consists of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) a single line of text and contains the following stats separated by whitespace:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)  ============== =============================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  bd_count	size of data written in backing device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		Unit: 4K bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)  bd_reads	the number of reads from backing device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		Unit: 4K bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)  bd_writes	the number of writes to backing device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		Unit: 4K bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)  ============== =============================================================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 9) Deactivate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) =============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) ::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	swapoff /dev/zram0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	umount /dev/zram1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 10) Reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) =========
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	Write any positive value to 'reset' sysfs node::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		echo 1 > /sys/block/zram0/reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		echo 1 > /sys/block/zram1/reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	This frees all the memory allocated for the given device and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	resets the disksize to zero. You must set the disksize again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	before reusing the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) Optional Feature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) ================
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) writeback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) ---------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) With CONFIG_ZRAM_WRITEBACK, zram can write idle/incompressible page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) to backing storage rather than keeping it in memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) To use the feature, admin should set up backing device via::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	echo /dev/sda5 > /sys/block/zramX/backing_dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) before disksize setting. It supports only partition at this moment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) If admin wants to use incompressible page writeback, they could do via::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	echo huge > /sys/block/zramX/writeback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) To use idle page writeback, first, user need to declare zram pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) as idle::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	echo all > /sys/block/zramX/idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) From now on, any pages on zram are idle pages. The idle mark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) will be removed until someone requests access of the block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) IOW, unless there is access request, those pages are still idle pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) Admin can request writeback of those idle pages at right timing via::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	echo idle > /sys/block/zramX/writeback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) With the command, zram writeback idle pages from memory to the storage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) If admin want to write a specific page in zram device to backing device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) they could write a page index into the interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	echo "page_index=1251" > /sys/block/zramX/writeback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) If there are lots of write IO with flash device, potentially, it has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) flash wearout problem so that admin needs to design write limitation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) to guarantee storage health for entire product life.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) To overcome the concern, zram supports "writeback_limit" feature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) The "writeback_limit_enable"'s default value is 0 so that it doesn't limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) any writeback. IOW, if admin wants to apply writeback budget, he should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) enable writeback_limit_enable via::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	$ echo 1 > /sys/block/zramX/writeback_limit_enable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) Once writeback_limit_enable is set, zram doesn't allow any writeback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) until admin sets the budget via /sys/block/zramX/writeback_limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) (If admin doesn't enable writeback_limit_enable, writeback_limit's value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) assigned via /sys/block/zramX/writeback_limit is meaningless.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) If admin want to limit writeback as per-day 400M, he could do it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) like below::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	$ MB_SHIFT=20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	$ 4K_SHIFT=12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	$ echo $((400<<MB_SHIFT>>4K_SHIFT)) > \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		/sys/block/zram0/writeback_limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	$ echo 1 > /sys/block/zram0/writeback_limit_enable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) If admins want to allow further write again once the bugdet is exhausted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) he could do it like below::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	$ echo $((400<<MB_SHIFT>>4K_SHIFT)) > \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		/sys/block/zram0/writeback_limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) If admin wants to see remaining writeback budget since last set::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	$ cat /sys/block/zramX/writeback_limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) If admin want to disable writeback limit, he could do::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	$ echo 0 > /sys/block/zramX/writeback_limit_enable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) The writeback_limit count will reset whenever you reset zram (e.g.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) system reboot, echo 1 > /sys/block/zramX/reset) so keeping how many of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) writeback happened until you reset the zram to allocate extra writeback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) budget in next setting is user's job.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) If admin wants to measure writeback count in a certain period, he could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) know it via /sys/block/zram0/bd_stat's 3rd column.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) memory tracking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) ===============
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) With CONFIG_ZRAM_MEMORY_TRACKING, user can know information of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) zram block. It could be useful to catch cold or incompressible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) pages of the process with*pagemap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) If you enable the feature, you could see block state via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /sys/kernel/debug/zram/zram0/block_state". The output is as follows::
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	  300    75.033841 .wh.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	  301    63.806904 s...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	  302    63.806919 ..hi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) First column
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	zram's block index.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) Second column
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	access time since the system was booted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) Third column
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	state of the block:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	s:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		same page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	w:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		written page to backing store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	h:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		huge page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	i:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		idle page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) First line of above example says 300th block is accessed at 75.033841sec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) and the block's state is huge so it is written back to the backing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) storage. It's a debugging feature so anyone shouldn't rely on it to work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) Nitin Gupta
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) ngupta@vflare.org