Posts: 1,028
SamK
Joined: 21 Aug 2011
#1
Noticed this months ago but did not get round to looking in to it until yesterday.

Hardware=Single CPU/core
Distro=antiX-13.2-Full-Stable
zram=copied to /etc/init.d/ from /usr/local/bin/
System=rebooted

Diagnosis
All following conducted with root privileges

Report the status of swapspace started by zram

Code: Select all

swapon -s
Filename                Type        Size    Used    Priority
/dev/zram0                              partition    95500    0    100
/dev/zram1                              partition    95500    0    100
This seems incorrect as two instances are reported.

The following line in /etc/init.d/zram calculates the number of CPUs

Code: Select all

...
num_cpus=$(grep -c processor /proc/cpuinfo)
...
It counts the number of times the word processor is present in the file /proc/cpuinfo

Verifying what the line returns in a terminal

Code: Select all

grep -c processor /proc/cpuinfo
2
This figure is used by the zram script to start the above two instances

Explanation of value returned

Code: Select all

grep --line-number processor /proc/cpuinfo
1:processor    : 0
5:model name    : Intel(R) Pentium(R) M processor 1.73GHz
The search term appears twice (lines 1 & 5) and thereby reports an invalid value of 2 CPUs on a single CPU/core system. It begins counting upwards from 1.

Later in the script a section is included to handle the incorrectly reported number of CPUs

Code: Select all

last_cpu=$((num_cpus - 1))
This decreases the incorrectly reported value of 2 to 1

The adjusted value is then fed into the start and stop sections

Code: Select all

for i in $(seq 0 $last_cpu); do...
This starts counting upwards from 0 (zero).

Outcome
Even though the number of CPUs has been adjusted (decreased by 1), an incorrect number of swap areas are created because the relevant section of the script starts counting from 0. A swap area is created for number 0, and a swap area is created for number 1.

Options
Workaround Fix
  • Retain incorrect calculation section
  • Retain section to compensate for incorrect calculation
  • Adjust start and stop sections to begin counting upwards from 1 to match incorrect sections
In start and stop sections of zram script change value from 0 to 1

Code: Select all

for i in $(seq 1 $last_cpu); do...
Full Fix
  • Correctly calculate number of CPUs
  • Jettison section to compensate for incorrect calculation
  • Adjust start and stop sections to begin counting upwards from 1 to match correct correct calculation section
Requires major rewrite.


Suggestions
antiX-13.2-Full-Stable ships with two suitable apps as components of packages which Debian marks as essential
  • getconf is part of libc-bin
  • nproc is part of coreutils
Verification that both return the correct CPU count in a terminal

Code: Select all

getconf _NPROCESSORS_ONLN
1
nproc
1
nproc --all
1
Verification of correct zram operation with any of the above commands substituted for the present command

Code: Select all

sudo swapon -s
Filename                Type        Size    Used    Priority
/dev/zram0                              partition    191004    0    100
Note: the size of the swap area is incorrect as the relevant part of the file was not edited for this quick and dirty test.


Of the two commands my personal preference is for one of the nproc variants because its format is familiar and adaptable. Additionally the online GNU manual indicates that in use"The result is guaranteed to be greater than zero". This will enable the following additional section to be jettisoned

Code: Select all

# if something goes wrong, assume we have 1
["$num_cpus" != 0 ] || num_cpus=1
.


References

========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"https://www.gnu.org/software/coreutils/manual/html_node/nproc-invocation.html"
linktext was:"https://www.gnu.org/software/coreutils/ ... ation.html"
====================================


========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://www.unix.com/man-page/linux/1/getconf/"
linktext was:"http://www.unix.com/man-page/linux/1/getconf/"
====================================