novaspirit / rpi_zram

script to enable zram for raspberry pi
351 stars 96 forks source link

it doesn't work #8

Open netherwort opened 5 years ago

netherwort commented 5 years ago

sudo nano /etc/rc.local DOES NOT MAKE ANY SENSE! it says "In order to enable or disable this script just change the execution bits." AND DOES NOT TELL YOU HOW TO DO IT!

sgrayban commented 5 years ago

Why do you need to disable rc.local ?

dcpender commented 5 years ago

@netherwort i think you have miss read the read me file, however i have put together a auto install script https://github.com/dcpender/rpi_zram/tree/dcpender-patch-1 its does everything for you from novas github as it is his i just automated it

mikebkk commented 5 years ago

On Raspberry 2B with Raspbian Stretch the original script of zram.sh will not be executed properly. The installed version of Bash is not calculating the values for memory properly. The following worked for me: after update and upgrade first do: sudo apt install -y bc and modify zram.sh as follows:

#!/bin/bash
cores=$(nproc --all)
modprobe zram num_devices=$cores
swapoff -a
totalmem=free | grep -e "^Mem:" | awk '{print $2}'
mem=$(echo "scale=0;2048*($totalmem/$cores)" | bc)
core=0
while [ $core -lt $cores ]; do
    echo $mem > /sys/block/zram$core/disksize
    mkswap /dev/zram$core
    swapon -p 5 /dev/zram$core
    core=$((core+1))
done

#  line 6: added scale=0 and | bc  
#  the number 2048 is working fine for me, can be changed. 
#  2048 causes the size of zram will be two times the size of free memory
#  let core=core+1 is not working with the current version of bash

I commented every line in /etc/dphys-swapfile and removed /etc/init.d/dphys-swapfile

alanmilinovic commented 5 years ago

Don't see any benefits by enabling ZRAM on RPi 3 at all, also don't understand why doubling size of zram by multiplying with number 2048. Do you see any difference in performance? :)

netherwort commented 5 years ago

@dcpender Thanks! This was really useful! (I have near no experience.) I opened 5 applications and experienced no lag, which means it worked!

dcpender commented 5 years ago

@netherwort your welcome, I have another auto script that installs both zram and wbar however i have modded the conky config to suit a 7" Raspberry Pi Touch Screen https://github.com/dcpender/pi_first_boot

dcpender commented 5 years ago

@mikebkk your zram.sh should look like this, can you show us yours ?

1 #!/bin/bash 2 cores=$(nproc --all) 3 modprobe zram num_devices=$cores 4 5 swapoff -a 6 7 totalmem=free | grep -e "^Mem:" | awk '{print $2}' 8 mem=$(( ($totalmem / $cores)* 1024 )) 9 10 core=0 11 while [ $core -lt $cores ]; do 12 echo $mem > /sys/block/zram$core/disksize 13 mkswap /dev/zram$core 14 swapon -p 5 /dev/zram$core 15 let core=core+1 16 done 17

dcpender commented 5 years ago

@amilino personally i have noticed my pi runs fine with or without zram in its current setup. previous attempts it run very sluggish doing even the simplest of tasks but i found that to be due to poor sd card speeds

alanmilinovic commented 5 years ago

Yes, that is my point. It is working same with or without zram.

dcpender commented 5 years ago

true however with it enabled it is there if needed, i beleave the idea of zram is for more graphical processes as well so depends on what your using the pi for. also if someone is not using a decent sd card then zram would be a must

mikebkk commented 5 years ago

I found out that on my pi the instruction 'let' is unknown by the shell That means there is no value set for zram I replace d the expression "let core=core+1" with "core=$((core+1))" What is the output of sudo fdisk -l | grep 'zram' on your machine? on my raspberry 2 it is Disk /dev/zram0: 463.4 MiB, 485863424 bytes, 118619 sectors Disk /dev/zram1: 463.4 MiB, 485863424 bytes, 118619 sectors Disk /dev/zram2: 463.4 MiB, 485863424 bytes, 118619 sectors Disk /dev/zram3: 463.4 MiB, 485863424 bytes, 118619 sectors and it is working dynamically (growing and shrinking) I am using a KDE Desktop. In case yoy are working "headless" there is no need to bother about zram. In all other cases it is clear that the reading/writing speed on the virtual zram is higher compared to operations in a file on the mmc cheers

rts58 commented 5 years ago

I replace d the expression "let core=core+1" with "core=$((core+1))"

Thank you I was getting an error no such file or directory for zram1 to 3. This change fixed the script on Pi 3b+.

alanmilinovic commented 5 years ago

let command is working fine on Pi 2 and 3.

yutayu commented 5 years ago

On Raspberry 2B with Raspbian Stretch the original script of zram.sh will not be executed properly. The installed version of Bash is not calculating the values for memory properly. The following worked for me: after update and upgrade first do: sudo apt install -y bc and modify zram.sh as follows:

#!/bin/bash
cores=$(nproc --all)
modprobe zram num_devices=$cores
swapoff -a
totalmem=free | grep -e "^Mem:" | awk '{print $2}'
mem=$(echo "scale=0;2048*($totalmem/$cores)" | bc)
core=0
while [ $core -lt $cores ]; do
    echo $mem > /sys/block/zram$core/disksize
    mkswap /dev/zram$core
    swapon -p 5 /dev/zram$core
    core=$((core+1))
done

#  line 6: added scale=0 and | bc  
#  the number 2048 is working fine for me, can be changed. 
#  2048 causes the size of zram will be two times the size of free memory
#  let core=core+1 is not working with the current version of bash

I commented every line in /etc/dphys-swapfile and removed /etc/init.d/dphys-swapfile

(standard_in) 1: syntax error /home/pi/Documents/zramnew.sh: line 9: echo: write error: Invalid argument mkswap: error: swap area needs to be at least 40 KiB swapon: /dev/zram0: read swap header failed /home/pi/Documents/zramnew.sh: line 9: echo: write error: Invalid argument mkswap: error: swap area needs to be at least 40 KiB swapon: /dev/zram1: read swap header failed /home/pi/Documents/zramnew.sh: line 9: echo: write error: Invalid argument mkswap: error: swap area needs to be at least 40 KiB swapon: /dev/zram2: read swap header failed /home/pi/Documents/zramnew.sh: line 9: echo: write error: Invalid argument mkswap: error: swap area needs to be at least 40 KiB swapon: /dev/zram3: read swap header failed

I run this script on 3B raspbian. sad.

dcpender commented 5 years ago

@mikebkk Disk /dev/zram0: 234.8 MiB, 246177792 bytes, 60102 sectors Disk /dev/zram1: 234.8 MiB, 246177792 bytes, 60102 sectors Disk /dev/zram2: 234.8 MiB, 246177792 bytes, 60102 sectors Disk /dev/zram3: 234.8 MiB, 246177792 bytes, 60102 sectors

Raspberry Pi 3B+ Raspbian OS

mikebkk commented 5 years ago

On Raspberry 2B with Raspbian Stretch the original script of zram.sh will not be executed properly. The installed version of Bash is not calculating the values for memory properly. The following worked for me: after update and upgrade first do: sudo apt install -y bc and modify zram.sh as follows:

#!/bin/bash
cores=$(nproc --all)
modprobe zram num_devices=$cores
swapoff -a
totalmem=free | grep -e "^Mem:" | awk '{print $2}'
mem=$(echo "scale=0;2048*($totalmem/$cores)" | bc)
core=0
while [ $core -lt $cores ]; do
    echo $mem > /sys/block/zram$core/disksize
    mkswap /dev/zram$core
    swapon -p 5 /dev/zram$core
    core=$((core+1))
done

#  line 6: added scale=0 and | bc  
#  the number 2048 is working fine for me, can be changed. 
#  2048 causes the size of zram will be two times the size of free memory
#  let core=core+1 is not working with the current version of bash

I commented every line in /etc/dphys-swapfile and removed /etc/init.d/dphys-swapfile

(standard_in) 1: syntax error /home/pi/Documents/zramnew.sh: line 9: echo: write error: Invalid argument mkswap: error: swap area needs to be at least 40 KiB swapon: /dev/zram0: read swap header failed /home/pi/Documents/zramnew.sh: line 9: echo: write error: Invalid argument mkswap: error: swap area needs to be at least 40 KiB swapon: /dev/zram1: read swap header failed /home/pi/Documents/zramnew.sh: line 9: echo: write error: Invalid argument mkswap: error: swap area needs to be at least 40 KiB swapon: /dev/zram2: read swap header failed /home/pi/Documents/zramnew.sh: line 9: echo: write error: Invalid argument mkswap: error: swap area needs to be at least 40 KiB swapon: /dev/zram3: read swap header failed

I run this script on 3B raspbian. sad.

With your post you are confirming, that the shell is not calculating correctly. Did you install 'bc'? Before running the final zram.sh you need to verify that the calculation works properly.

#!/bin/bash
cores=$(nproc --all)
###################     commented for testing ############
# modprobe zram num_devices=$cores
# swapoff -a
####################################################
totalmem=free | grep -e "^Mem:" | awk '{print $2}'
# if there is no result for totalmem you can try
totalmem=$(free | grep -e "^Mem:" | awk '{print $2}')
mem=$(echo "scale=0;2048*($totalmem/$cores)" | bc)
core=0
echo "number of cores: $cores"
while [ $core -lt $cores ]; do
    ###################     commented for testing ############
    # echo $mem > /sys/block/zram$core/disksize
    # mkswap /dev/zram$core
    # swapon -p 5 /dev/zram$core
    ####################################################
    echo "calculated memory: $mem, total memory in MB: $totalmem "
    echo "core nr: $core" 
    core=$((core+1))
done

Don't give up so easily People who believe that there is no difference with or without zram probably have installed zram but did not check wether it is effective. I just checked above code on a HP Laptop with openSUSE 15 (because my raspberries are already on the way to my new home).

I needed to use the version totalmem=$(free | grep -e "^Mem:" | awk '{print $2}') to get this output

number of cores: 2
calculated memory: 4069130240, total memory in MB: 3973760 
core nr: 0
calculated memory: 4069130240, total memory in MB: 3973760 
core nr: 1

with totalmem=free | grep -e "^Mem:" | awk '{print $2}' i got following:

(standard_in) 1: syntax error
number of cores: 2
calculated memory: , total memory in Mb:  
core nr: 0
calculated memory: , total memory in Mb:  
core nr: 1

As soon as you get a correctly calculated result you can activate your zram.sh

keep looking up and have fun

alanmilinovic commented 5 years ago

Disk /dev/zram0: 171.3 MiB, 179658752 bytes, 43862 sectors Disk /dev/zram1: 171.3 MiB, 179658752 bytes, 43862 sectors Disk /dev/zram2: 171.3 MiB, 179658752 bytes, 43862 sectors Disk /dev/zram3: 171.3 MiB, 179658752 bytes, 43862 sectors

I have no problems.

yutayu commented 5 years ago

totalmem=free | grep -e "^Mem:" | awk '{print $2}'

if there is no result for totalmem you can try

totalmem=$(free | grep -e "^Mem:" | awk '{print $2}') mem=$(echo "scale=0;2048*($totalmem/$cores)" | bc)

I replaced former to latter in that script.

sudo /home/pi/Documents/zramnew.sh Setting up swapspace version 1, size = 437.8 MiB (459091968 bytes) no label, UUID=27edfb3b-709a-459a-969f-e007d19e2faf Setting up swapspace version 1, size = 437.8 MiB (459091968 bytes) no label, UUID=603d5262-658e-463b-9344-3a4279e4febf Setting up swapspace version 1, size = 437.8 MiB (459091968 bytes) no label, UUID=510a0800-3530-4f98-ace2-67e101523bab Setting up swapspace version 1, size = 437.8 MiB (459091968 bytes) no label, UUID=4c944115-c97c-43e0-bc43-c783b7f09e3e

perfect :) Thanks. ( This is twice of my real memory. I will use this as test.) .

mikebkk commented 5 years ago

totalmem=free | grep -e "^Mem:" | awk '{print $2}'

if there is no result for totalmem you can try

totalmem=$(free | grep -e "^Mem:" | awk '{print $2}') mem=$(echo "scale=0;2048*($totalmem/$cores)" | bc)

I replaced former to latter in that script.

sudo /home/pi/Documents/zramnew.sh Setting up swapspace version 1, size = 437.8 MiB (459091968 bytes) no label, UUID=27edfb3b-709a-459a-969f-e007d19e2faf Setting up swapspace version 1, size = 437.8 MiB (459091968 bytes) no label, UUID=603d5262-658e-463b-9344-3a4279e4febf Setting up swapspace version 1, size = 437.8 MiB (459091968 bytes) no label, UUID=510a0800-3530-4f98-ace2-67e101523bab Setting up swapspace version 1, size = 437.8 MiB (459091968 bytes) no label, UUID=4c944115-c97c-43e0-bc43-c783b7f09e3e

perfect :) Thanks. ( This is twice of my real memory. I will use this as test.) .

Good to hear that you succeeded! Let other users know about the difference in performance before and after. The problem with these things is that most users do not watch their logfiles, just copy and paste code without verifying. You could see that one misinterpreted line in the code was determining fail or success. The reason for different interpretation may be laying in different authentication. Actually the command "totalmem=free | grep -e "^Mem:" | awk '{print $2}'" gave no result for that reason Success for now and keep looking up

yutayu commented 5 years ago

Thank you for your reply :)

I suggest to make zram for 1 core. Because of this.

https://wiki.gentoo.org/wiki/Zram Prior to kernel 3.15, each zram device contains it's own compression buffer, memory pools and other metadata as well as per-device locks. This can become a serious bottleneck for multi-core machines. To work around this problem, zram is capable of initializing multiple devices. For this reason, the recommended amount of devices for swap is 1 per cpu core for kernels prior to 3.15.

If this script uses 1 core ! Block device could use more large files at once.

mikebkk commented 5 years ago

Thank you for your reply :)

I suggest to make zram for 1 core. Because of this.

https://wiki.gentoo.org/wiki/Zram Prior to kernel 3.15, each zram device contains it's own compression buffer, memory pools and other metadata as well as per-device locks. This can become a serious bottleneck for multi-core machines. To work around this problem, zram is capable of initializing multiple devices. For this reason, the recommended amount of devices for swap is 1 per cpu core for kernels prior to 3.15.

If this script uses 1 core ! Block device could use more large files at once.

first of all do
uname -r
to see your kernel version.
It might be 4.xx so the mentioned publication can be ignored After that please read zram.sh attentively to understand what zram.sh is doing.
In the first line the number of cores is being concluded.
In the while block each core gets its zram[$core].
The scripts creates one device per cpu-core. No need to worry about anything. If you feel like verifying the succes you can downlload some benchmarking tools and play with them with enabled and disabled zram (don't forget to reactivate dphys-swapfile in that case) and publish the result here cheers

yutayu commented 5 years ago

UnixBench result. with this script ( not default ) ,lh4 https://github.com/yutayu/dumb/blob/master/raspberrypi-2019-01-31-06.html without all swap https://github.com/yutayu/dumb/blob/master/raspberrypi-2019-01-31-09.html with my script , 1 zram directory per system, lh4 https://github.com/yutayu/dumb/blob/master/raspberrypi-2019-01-31-01.html

If there are any suggestions , no bother :P

yutayu commented 5 years ago

My modified script is below. https://github.com/yutayu/dumb/blob/master/zram1corelzh4.sh

mikebkk commented 5 years ago

UnixBench result. with this script ( not default ) ,lh4 https://github.com/yutayu/dumb/blob/master/raspberrypi-2019-01-31-06.html without all swap https://github.com/yutayu/dumb/blob/master/raspberrypi-2019-01-31-09.html with my script , 1 zram directory per system, lh4 https://github.com/yutayu/dumb/blob/master/raspberrypi-2019-01-31-01.html

If there are any suggestions , no bother :P

Sorry, I don't understand. Benchmarking is something else. If you want to compare results, you need to set your machine up as it was before and test, after performing that benchmark-test activate zram and test again. When posting things like these you may add a few words about what you did. The benefit of zram is -easy to understand- significantly improved writing/reading speed and can be verified with free benchmark tools. You need to look for them (iE google search: benchmark tools for raspberry will give you a rich choice), read the documentation and install them. Try to provide clearer information when you are posting something. I get no clue from your post and don't know what to answer. Cheers

yutayu commented 5 years ago

you mean like steream memory bandwidth test?

no zram,physical memory only

Function Best Rate MB/s Avg time Min time Max time Copy: 1790.7 0.096666 0.089349 0.108442 Scale: 1773.1 0.097871 0.090238 0.110525 Add: 1658.5 0.155777 0.144706 0.173152 Triad: 1458.6 0.173244 0.164540 0.190183

with this script , zram only

Function Best Rate MB/s Avg time Min time Max time Copy: 1750.0 0.094394 0.091427 0.096740 Scale: 1758.9 0.093690 0.090965 0.100613 Add: 1663.5 0.150165 0.144271 0.158898 Triad: 1461.1 0.167054 0.164256 0.170286

mikebkk commented 5 years ago

Don't see any benefits by enabling ZRAM on RPi 3 at all, also don't understand why doubling size of zram by multiplying with number 2048. Do you see any difference in performance? :)

Why these negative comments? Why are you writing that 'let' is working although it is not? By executing the shell-testscript I published earlier you can see that the memory-calculation fails because of this. Keep smiling. If you have no stress and no problems: be happy. Obviously other people are not in that ideal situation and are trying to improve the performance of their Pi. Possibly you have found a better way to improve the performance, in that case you might publish your solution here.

Using zram with factor 2048 made my KDE Desktop work. If I reduce the size of zram by using a lower factor the KDE-Desktop is responding very slowly and Chromium-Browser crashes when more than 6 sites are open. You will experience the benefits of zram when much of your RAM is used. If you are using programs like GIMP, Inkscape, large databases and a demanding desktop you'll be glad that a thing like zram exists and can be used.

image

alanmilinovic commented 5 years ago

I think that bottle neck is SD card, I replaced it and after that things are working faster. Do not see any differences with zram or without and my RPi is quite loaded with things.

Also command 'let' is working just fine. I am on the latest version of Raspbian.