sarahgad
Posts: 30
Joined: Fri Jan 20, 2017 12:07 pm

Re: Raspberry Pi 3 with 2GB RAM playing the devil's advocate

Mon Jun 19, 2017 9:39 am

Do you recommend keep on using the old Kernel prior to 3.15 for the purpose?

User avatar
ric96
Posts: 1254
Joined: Sun Mar 17, 2013 6:03 am
Location: NOIDA, India
Contact: Website

Re: Raspberry Pi 3 with 2GB RAM playing the devil's advocate

Mon Jun 19, 2017 9:57 am

sarahgad wrote:Do you recommend keep on using the old Kernel prior to 3.15 for the purpose?
absolutely NOT, the script works fine as is on any 4.x based kernel. If you want to have a single swap file just modify it, won't be necessary though.
My apologies for shameless YouTube Plugs...
youtube.com/sahajsarup
twitter @sahajsarup
skype srics1996
e-mail: sahajsarup@gmail.com
Blog: http://www.geektillithertz.com/wordpress
Web: http://www.geektillithertz.com

User avatar
PeterO
Posts: 4032
Joined: Sun Jul 22, 2012 4:14 pm

Re: Raspberry Pi 3 with 2GB RAM playing the devil's advocate

Mon Jun 19, 2017 10:31 am

ric96 wrote:https://unix.stackexchange.com/question ... m-and-swap
I'll leave this here...
PeterO wrote:"I have four cores so I need four zram devices" seems like a non sequitur.

PeterO
From that page "Interesting, and this also seems quite clear to understand by reading /etc/init/zram-config.conf "

So what is in /etc/zram-config.conf for those of us who have not installed it ?

EDIT... Google found me a version, and it contains nothing that explains the reasoning.... :roll:

Also, since all my PIs run with USB drives and no SDcards, swapping (when it happens) goes to the USB disk not the slow SDcard. Would zram still provide useful performance increase in this case ?
I'm sceptical because assigning half your ram to these devices is going to make swapping happen much sooner to start with.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

pgeorges
Posts: 1
Joined: Wed Jul 19, 2017 10:21 am

Re: Raspberry Pi 3 with 2GB RAM playing the devil's advocate

Wed Jul 19, 2017 10:27 am

I discovered recently zram and it proved to be so good in terms of performance and for preserving SD Card lifetime that I wrote the script below in file /etc/init.d/zram.

IMO, swap should never be used on an SD Card because it is often so useless (when you start swapping heavily, I often unplug my Pi because it appears frozen, and multiple I/O reduces heavily SD Card's lifetime).
I even put /var/log into another zram device to perserve SD Card.

Code: Select all

#!/bin/bash
### BEGIN INIT INFO
# Provides:          zram
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     S
# Default-Stop:      0 1 6
# Short-Description: Use compressed RAM as in-memory swap
# Description:       Use compressed RAM as in-memory swap
### END INIT INFO

# Adjust the swap size in MB
SWAP_SIZE=800

CPUS=`nproc`
SIZE=$(( SWAP_SIZE * 1024 * 1024 / CPUS ))

case "$1" in
  "start")
    # if a zram swap already exists, bail out
    if [ `grep -c zram /proc/swaps` != 0 ]
      then
      echo "There is already a zram swap"
      exit 1
    fi
    modprobe zram

    for n in `seq $CPUS`; do
      i=`cat /sys/class/zram-control/hot_add`
      echo $SIZE > /sys/block/zram$i/disksize
      mkswap /dev/zram$i
      swapon /dev/zram$i -p 10
    done
    ;;
  "stop")
    readarray arr < "/proc/swaps"
    for line in "${arr[@]}" ; do
      if [ ${line:0:9} == "/dev/zram" ] ; then
          i=${line:9:1}
          echo "Removing swap zram $i"
          swapoff /dev/zram$i
          echo $i > /sys/class/zram-control/hot_remove
      fi
    done
    modprobe -r zram
    ;;
  "status")
    free -h
    cat /proc/swaps
    ;;
  *)
    echo "Usage: `basename $0` (start | stop | status)"
    exit 1
    ;;
esac

tkaiser
Posts: 103
Joined: Fri Aug 05, 2016 1:28 pm

Re: Raspberry Pi 3 with 2GB RAM playing the devil's advocate

Thu Mar 15, 2018 9:33 am

PeterO wrote:
Mon Jun 19, 2017 10:31 am
Also, since all my PIs run with USB drives and no SDcards, swapping (when it happens) goes to the USB disk not the slow SDcard.
There's no need to use slow SD cards other than people being not aware of random IO being the most important performance metric while everywhere only sequential performance is measured. A is the problem but users are only told to look at B (A is random IO, B sequential performance).

In the meantime SD cards do exist that are optimized for exactly this: https://forum.armbian.com/topic/954-sd- ... ment-49811

Those A1 rated cards are at least 3 times faster than spinning rust (HDDs) as long as we're talking about random IO which pretty much is exactly what's happening when running Linux.
PeterO wrote:
Mon Jun 19, 2017 10:31 am
I'm sceptical because assigning half your ram to these devices is going to make swapping happen much sooner to start with.
Nope, this is a misunderstanding. You are not 'assigning half your ram' to zram that's just the amount of memory that will be used once zram would be used and the system starts to run out of memory. If you have 1 GB physical DRAM and activate zram with defaults (configured to use half the size of physical DRAM as maximum size) the sum is 1.5GB and not 1GB.

Still the full 1 GB are available. Only if the kernel is running out of physical memory it starts to send pages to one of the zram devices which then start to slowly use physical RAM. Now the amount of available RAM of course decreases since used by zram but since compression is applied (LZ4 being the default AFAIK) the zram devices do NOT allocate the same amount of physical RAM as would be needed to move pages into.

So a lot more paging activity has to happen since the amount of available physical RAM gets further reduced once data arrives at the zram devices but if the data is highly compressable then you end up with a lot more data fitting into same amount of physical RAM. BTW: since the kernel is not an idiot it takes care that stuff gets pushed into zram that is least frequently accessed (just like with traditional swapping) so with a somewhat decent host (excluding the single core Raspberries) you end up with more available DRAM anyway.

And of course zram is magnitudes faster than swapping especially compared to slow spinning rust (again: there's no need to use slow SD cards since ultra fast ones exist)

User avatar
PeterO
Posts: 4032
Joined: Sun Jul 22, 2012 4:14 pm

Re: Raspberry Pi 3 with 2GB RAM playing the devil's advocate

Thu Mar 15, 2018 11:47 am

It still makes no sense to me. It's still using memory to hold stuff that the kernel has decided isn't needed at the moment.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “Off topic discussion”

Who is online

Users browsing this forum: No registered users and 4 guests