[Q] Moving Linuxswap to NAND - G1 Q&A, Help & Troubleshooting

Hello. i wanted to know if anybody has tried moving your swap to your data or cache partition on your phone since it's faster then sd. I found this and i'm guessing you could remount your your cache as swap instead of sdcard.
i'm new to the android/linux thing and this site, but this has really helped alot!
There's some smart mofo's and TONS of info on here.

i found this more or less. This is a mixture of threads
Enable swap:
mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system
mkswap /dev/block/mmcblk0p3
echo "swapon -a" >> /system/etc/init.d/05userinit (on my phone it was 20userinit)
echo "/dev/block/mmcblk0p3 swap swap" >> /system/etc/fstab (i didnt have fstab, use > instead)
reboot
Move swap to data:
mkdir /data/swap
echo "ln -s /data/swap mtdblk0p3" >> /system/etc/init.d/??userinit (whatever it is on your phone)
so i guess this thread can be deleted. sorry

Please Read Carefully!
Do NOT, I repeat DO NOT put swap on the phone's nand. Swap is accessed very often and causes further wearing of the NAND chips.This is why it was never implemented before, it can end up killing the chip completely, you're better off leaving it on the SD card. And the phone is about 2-3yrs of age now, so the NAND is probably worn something bad...

rdhoggattjr said:
i found this more or less. This is a mixture of threads
Enable swap:
mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system
mkswap /dev/block/mmcblk0p3
echo "swapon -a" >> /system/etc/init.d/05userinit (on my phone it was 20userinit)
echo "/dev/block/mmcblk0p3 swap swap" >> /system/etc/fstab (i didnt have fstab, use > instead)
reboot
Move swap to data:
mkdir /data/swap
echo "ln -s /data/swap mtdblk0p3" >> /system/etc/init.d/??userinit (whatever it is on your phone)
so i guess this thread can be deleted. sorry
Click to expand...
Click to collapse
Uh, this doesn't do what you might think it does. The swap remains on the sd card (mmcblk0p3), assuming you typo'ed on the ln command...
Jeff

Ditto what they said about wearing out the internal flash...
SDcard is replacable, plus if your builtin is faster, you need a new card.
Class 10 cards are much faster, and far cheaper than killing your phone by wearing out the flash.

Related

[SOLVED]help creating persistent swap partition.

I am using CyanogenMod v3.6.8.1 and I decided I wanted a swap partition.
I partitioned my 2GB SD with a 32MB swap partition and formatted using gparted.
From the shell (using adb) I remounted /dev/block/mtdblock3 with rw and edited /system/etc/fstab and added a line for the swap. Here is what the entire file looks like now:
Code:
/dev/block/mtdblock3 /system yaffs2 rw
/dev/block/mtdblock4 /cache yaffs2 rw
/dev/block/mtdblock5 /data yaffs2 rw
/dev/block/mmcblk0p1 /sdcard vfat rw
/dev/block/mmcblk0p2 /system/sd auto rw
/dev/block/mmcblk0p3 none swap sw
/system/modules/modules.sqf /system/modules squashfs ro,loop
/system/xbin/xbin.sqf /system/xbin squashfs ro,loop
I can turn on the swap with 'swapon -a' which works and shows up in /proc/swaps and with the 'free' command, but upon boot, swap isn't activating.
I'm not really familiar with the android init scripts, so I'm not sure how I can enable the swap automatically upon boot. Any advice would be appreciated.
Okay I fixed it.
I downloaded one of Drizzy's roms that I know use swap, extracted, and searched for 'swap' in all the files. I found it in /system/bin/a2sd at the end. Here is what's there:
Code:
if [ -n /dev/block/mmcblk0p3 ];
then
mkswap /dev/block/mmcblk0p3;
fi;
if [ -e /dev/block/mmcblk0p3 ];
then
echo 30 > /proc/sys/vm/swappiness;
swapon /dev/block/mmcblk0p3;
fi;
The first if doesn't make sense to me as it's a string test which will always be true, so I got rid of it and just kept the test for the file existing, but I changed it to -b to be a little more specific (to see if it's a block device), and I commented out the swappiness change to leave it at default.
Im using same rom and i woild love to do this but is there any other way as im totally unfamiliar with adb
turboyo said:
Im using same rom and i woild love to do this but is there any other way as im totally unfamiliar with adb
Click to expand...
Click to collapse
I just used adb because I can use the copy/paste function of my gnome terminal and it's easier than using the shell on the phone itself.
Here is what is needed:
Get into a root shell, either on the phone or using adb.
Code:
mount -t yaffs2 -o rw,remount /dev/block/mtdblock3 /system
vi /system/bin/a2sd
press capital G which will take you to the bottom of the file
press lower case o which will open a line below your cursor for editing.
type in the following:
Code:
if [ -b /dev/block/mmcblk0p3 ];
then
echo 30 > /proc/sys/vm/swappiness;
swapon /dev/block/mmcblk0p3;
fi;
press escape to exit insert mode (I read that in the terminal emulator you have to hold the trackball and press 1 for esc)
type :wq and press enter.
exit terminal and reboot. To see if it's working, get back into a terminal and use the command 'free'
you can also do 'swapon -a' instead of specifying the swap partition itself. Note that the above example uses the third partition of the SD card as the swap.
This also assumes you have already created the swap partition and formatted it as swap.
You can omit the swappiness line to leave it at the linux default of 60.

Question about first time Swap setup....

i am interested in creating a SWAP partition on my sd card to increase rom speed, even tho CMs roms are preety fast already. I hav e 8gb CD with about 500mb ext2. now if i use this method to create Swap partition:
Reboot and enter recovery
Enter console
#parted /dev/block/mmcblk0
#print (to verfiy we know what size card we are dealing with, the details should be straight forward)
rm 1
rm 2
mkpartfs primary fat32 0 7200
mkpartfs primary ext2 7200 7700
mkpartfs primary linux-swap 7700 7732
#print (to verify we have the 3 partitions)
i know how to backup music and everything on fat32 to my PC(to later restore) because doing the above will reformat entire card.
my question is do i need to back up my ext2 with all my apps and data on it, and if so how? or will it do everything itself when i reboot my CM rom? i am trying to create this swap partition without really changing everything and without wiping
There are quite a few threads on this, but granted they aren't that easy to find (I'm looking at you, people who name their threads "PLZ HELPPPPP!!")
boot in to recovery and go to the console:
cd sdcard
mkdir app
mkdir app-private
cp /system/sd/app/* /sdcard/app/
cp /system/sd/app-private/* /sdcard/app-private/
All your apk's are now on your Fat partition, copy those two folders to your computer
after you have remade the partitions go back to the recovery console (don't do this in terminal, but you can use adb shell when the phone is in recovery mode). This time we have to mount your new ext partition as rw.
mount -o rw /dev/block/mmcblk0p2 /system/sd
cd /system/sd
mkdir app
mkdir app-private
cp /sdcard/app/* /system/sd/app/
cp /sdcard/app-private/* /system/sd/app-private/
You're done. expect your phone to take a few minutes to boot up as it re-creates the dalvik-cache.

Howto Enable Swap

I used the method to enable swap in Linux and it works like designed.
I used Amon_RA Recovery 1.52 to partition my SDCARD and the new recovery lets you choose the size of your partitions.
So I created a 256MB Swap.
From a console/terminal do the following after creating your swap partition the size you want it:
Type each line below in order and hit enter after each in the terminal.
mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system
mkswap /dev/block/mmcblk0p3
echo "swapon -a" >> /system/etc/init.d/05userinit
echo "/dev/block/mmcblk0p3 swap swap" >> /system/etc/fstab
reboot
This works on all roms i have run into. It does help the performance on the memory hobbled G1.
Once you have rebooted go back into a terminal and run the "free" command. You should see that you have a working swap partition.
TGA_Gunnman
The UNIX Dude

Use internal sdcard as dalvik-cache

As I cannot use the internal sdcard with CM11, I've made a small script which mounts the internal sdcard as /data/dalik-cache to get more free space for apps & app-data.
WARNING: Only for users who are familiar with adb as you can probably brick your device!
Installation is a bit tricky, maybe someone makes an installer. All steps are done over adb
First format the internal sdcard with ext4: mke2fs -t ext4 /dev/block/mmcblk0p23
Copy script below to /data/local/userinit.sh (e.g. adb push userinit.sh /data/local/userinit.sh)
chmod 755 /data/local/userinit.sh
Reboot and let dalvik cache recreate. After this happend, reboot into recovery, wipe dalvik-cache and enjoy the 200MB more free space in data for apps.
Code:
#!/system/bin/sh
busybox mount -t ext4 -o noatime,nodiratime,nosuid,nodev /dev/block/mmcblk0p23 /data/dalvik-cache;
mountext=`busybox mount | egrep 'ext2|ext3|ext4'`;
if [ -n "$mountext" ];
then
busybox chown 1000:1000 /data/dalvik-cache;
busybox chmod 771 /data/dalvik-cache;
fi;
sync;
Awesome... I just needed it for assigning block on dalvik in my beta app. Works great!

[WIP] Swap in CM 11/12

We have started this discussion in Mentor.37's [RECOVERY]Safestrap 3.75 for unused partitions thread and I decided to create a new thread for it.
Why swap?
Since D4 is low on RAM, especially for Lollipop (CM12), idea of having the system swap out some data instead of closing all apps but the one I currently use seems appealing to me, even for the trade-out of slowing the system down a bit when loading. More comprehensive (dated, but still valid) information on whether to use swap can be gathered from this article: Why Android Swap Doesn’t Make Sense. While even its name suggests you shouldn't swap in Android, the conclusion works in favor of trying swap in my installation of Lollipop where system and background apps eat up over 75% of RAM straight after boot:
Jeffrey Blattman said:
And here’s a few counter arguments that I can imagine,
1. The native Android memory management scheme is there for apps, but not for other processes. That is, having swap allows the many processes that run to support the Linux operating system to be moved out of physical memory.
Click to expand...
Click to collapse
Also in comments there are some persuasive arguments in favor of swapping, such as not having to reload content of apps (especially browsers) after they get killed off by Android. In my use, this is crucial when I'm without Internet connection for some time.
Current status: swapping works, but I have mixed feelings on whether it helps or not. It needs more tinkering with swappiness to find the right value, CM12's default of 60 is way too high.
Script for automated install and init with swappiness parameter in post #2
@Milp's original post:
Milp said:
I placed 550mb swap files in both /webtop and /preinstall (mounted under storage), also smaller ones in cache, system and data. Swapped on all of them, it's like a brand new phone now, speed wise.
Using 40 swappiness. cm11.
I created the swap files in an app called swapper and moved them to the locations. If anyone is interested in the swapon script for smanager, i can post it here.
somehow using dd with if=/dev/null did not work and filling those files with /dev/urandom didn't let me swap on them.
I don't know if its a good test for how fast things got, but i'm now able to watch a 720p video in the desktop site of youtube in firefox with 3 addons. It plays fluently.
Hope this is the right place for it, not sure whether it deserves it's own thread.
Click to expand...
Click to collapse
My next post, summing up how I got there. I've been running the system like this for over a day, with mixed feelings about result.
Milp said:
I placed 550mb swap files in both /webtop and /preinstall (mounted under storage), also smaller ones in cache, system and data. Swapped on all of them, it's like a brand new phone now, speed wise.
Click to expand...
Click to collapse
Great idea, inspired me to this little adventure (CM12 in system slot):
Putting swap file to cache partition (as it's 1 GB and using under 30 MB) and making preinstall swap:
Code:
/ # tar -cv /cache | gzip > /sdcard/cache.tar.gz
/ # dd if=/dev/zero of=/cache/swap bs=1024 count=819200
/ # mkswap /cache/swap
/ # swapon /cache/swap
/ # mkswap /dev/block/preinstall
/ # swapon /dev/block/preinstall
Now we need to make this mapping persistent - let's use /data/local/userinit.sh (don't forget to chmod 755 userinit.sh after you create it):
Code:
#!/system/bin/sh
swapon /cache/swap
swapon /dev/block/preinstall
This should run on startup (invoked by /system/etc/init.d/90userinit). It doesn't and I don't know why (yet), so I use SManager instead.
Results:
Not much tested, but it looks like it works. I ran CG Transit (offline timetables for Czech public transport) and then Hill Climb Racing (only resource-heavy game I have installed), which would usually kill off everything else, making the timetables forget its state. This didn't happen and switching out of the game felt a bit snappier, woohoo We'll see after some time though, this may be just me REALLY wanting this to help.
Code:
/ # free
free
total used free shared buffers
Mem: 859572 845284 14288 0 3336
-/+ buffers: 841948 17624
Swap: 1441784 66716 1375068
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
@Milp what script do you use? How exactly did you create your swap files?
To revert preinstall back to ext3 if you don't want to use it as swap anymore (it's been formatted as swap by mkswap):
Code:
/ # mke2fs /dev/block/preinstall
/ # tune2fs -j /dev/block/preinstall
(following is not finished)
Using webtop as dalvik-cache (and backup in case something goes wrong after restart):
Code:
/data # mv dalvik-cache/ dalvik-cache-old
/data # mkdir dalvik-cache
/data # mount -t ext4 /dev/block/webtop /data/dalvik-cache
/data # rm -rf dalvik-cache/*
/data # cp -r dalvik-cache-old/* dalvik-cache/
/data # rm -rf dalvik-cache-old/
/data # tar -cv dalvik-cache/ | gzip > /sdcard/dalvik-cache.tar.gz
Then it's needed to be mounted when startup. My initial attempt utilizing /data/local/userinit.sh failed, will need to edit ramdisk for this to work :/ Not today.
Random info about my system for understanding why did I also move dalvik cache elsewhere:
Tip: I use SSHDroid to easily ssh to device. adb shell doesn't suit me, especially for its lack of completing paths. And to have it nice and colorful, this is my first command: su -c bash
Code:
/ # cat /proc/partitions
major minor #blocks name alias
7 0 27160 loop0
7 1 25068 loop1
7 2 22980 loop2
7 5 35516 loop5
7 6 43872 loop6
7 7 4096 loop7
179 0 15552512 mmcblk1
179 1 128 mmcblk1p1
179 2 512 mmcblk1p2
179 3 512 mmcblk1p3
179 4 1 mmcblk1p4
179 5 512 mmcblk1p5
179 6 512 mmcblk1p6
179 7 4096 mmcblk1p7 pds
179 8 512 mmcblk1p8 utags
179 9 1024 mmcblk1p9
179 10 2048 mmcblk1p10
179 11 512 mmcblk1p11
179 12 512 mmcblk1p12
179 13 4096 mmcblk1p13
179 14 8192 mmcblk1p14 boot
179 15 9216 mmcblk1p15 recovery
179 16 217088 mmcblk1p16 cdrom
179 17 512 mmcblk1p17 misc
179 18 512 mmcblk1p18 cid
179 19 4096 mmcblk1p19 kpanic
179 20 655360 mmcblk1p20 system
179 21 1048576 mmcblk1p21 cache
179 22 622592 mmcblk1p22 preinstall
179 23 1364992 mmcblk1p23 webtop
179 24 3203072 mmcblk1p24 userdata
179 25 8401792 mmcblk1p25 emstorage
179 26 128 mmcblk1p26
179 32 62367744 mmcblk0
179 33 62366720 mmcblk0p1
254 0 27160 dm-0
254 1 25068 dm-1
254 2 22980 dm-2
254 5 35516 dm-5
254 6 43872 dm-6
7 8 21936 loop8
254 7 21936 dm-7
7 9 22980 loop9
254 8 22980 dm-8
7 10 28204 loop10
254 9 28204 dm-9
7 11 40736 loop11
254 10 40736 dm-10
7 12 5224 loop12
254 11 5224 dm-11
7 13 11492 loop13
254 12 11492 dm-12
7 14 10448 loop14
254 13 10448 dm-13
7 16 42824 loop16
254 15 42824 dm-15
Code:
/data # du -sh *
4.0K adb
6.6M anr
703.4M app
4.0K app-asec
4.0K app-lib
4.0K app-private
4.0K ash_history
116.0K backup
12.0K battd
0 bugreports
0 chargeonlymode
1.0G dalvik-cache
441.6M data
288.0K dontpanic
8.0K download
20.0K drm
4.0K emt
32.0K kernelpanics
32.0K local
4.0K location
84.0K logger
20.0K lost+found
12.0K media
20.0K mediadrm
1.6M misc
8.0K panicreports
4.0M pdsdata.img
44.0K property
4.0K resource-cache
4.0K security
24.0K ssh
4.3M system
16.0K tmp
5.1M tombstones
16.0K tpapi
4.0K user
Code:
/ # df
Filesystem Size Used Free Blksize
/dev 419.7M 60.0K 419.7M 4096
/sys/fs/cgroup 419.7M 12.0K 419.7M 4096
/mnt/asec 419.7M 0.0K 419.7M 4096
/mnt/obb 419.7M 0.0K 419.7M 4096
/system 636.7M 604.2M 32.5M 1024
/data 3.0G 2.2G 798.2M 4096
/cache 1007.9M 33.4M 974.5M 4096
/pds 3.9M 1.1M 2.8M 1024
/storage/sdcard0 8.0G 2.9G 5.1G 8192
/mnt/media_rw/sdcard0 8.0G 2.9G 5.1G 8192
/mnt/secure/asec 59.5G 58.0G 1.5G 32768
/mnt/media_rw/sdcard1 59.5G 58.0G 1.5G 32768
/mnt/secure/asec 59.5G 58.0G 1.5G 32768
/mnt/media_rw/sdcard0/.android_secure 59.5G 58.0G 1.5G 32768
/storage/sdcard1 59.5G 58.0G 1.5G 32768
[asec stripped]
Milp's response showing how did he approach the swapping problem, including setting swappiness to 15:
Milp said:
Hi there, I'm using the following code. I'm using some random app to create swapfiles since dd doesn't work for me with if=/dev/null, it creates 0byte files. And swapping on a file filled with /dev/urandom doesn't seem to work. No idea how else i could go about that.
Code:
#!/system/bin/sh
echo start > /storage/sdcard0/swap.log 2>&1
echo mountstuff > /storage/sdcard0/swap.log 2>&1
umount /preinstall >> /storage/sdcard0/swap.log 2>&1
mount -o rw,remount / >> /storage/sdcard0/swap.log 2>&1
rm -r preinstall >> /storage/sdcard0/swap.log 2>&1
mkdir /storage/preinstall >> /storage/sdcard0/swap.log 2>&1
mkdir /storage/webtop >> /storage/sdcard0/swap.log 2>&1
mount -o ro,remount / >> /storage/sdcard0/swap.log 2>&1
mount -t ext3 /dev/block/preinstall /storage/preinstall >> /storage/sdcard0/swap.log 2>&1
mount -t ext4 /dev/block/webtop /storage/webtop >> /storage/sdcard0/swap.log 2>&1
chmod 777 /storage/preinstall >> /storage/sdcard0/swap.log 2>&1
chmod 777 /storage/webtop >> /storage/sdcard0/swap.log 2>&1
echo swappiness...15 >> /storage/sdcard0/swap.log 2>&1
echo 15 1> /proc/sys/vm/swappiness 2>> /storage/sdcard0/swap.log
echo cping... >> /storage/sdcard0/swap.log >> /storage/sdcard0/swap.log 2>&1
cp /storage/sdcard0/Swapper/swap.swp /storage/preinstall/swap.swp >> /storage/sdcard0/swap.log 2>&1
chmod 666 /storage/preinstall/swap.swp >> /storage/sdcard0/swap.log 2>&1
cp /storage/sdcard0/Swapper/swap.swp /cache/swap.swp >> /storage/sdcard0/swap.log 2>&1
chmod 666 /cache/swap.swp >> /storage/sdcard0/swap.log 2>&1
swapon /storage/preinstall/swap.swp >> /storage/sdcard0/swap.log 2>&1
mount -o remount,rw /dev/block/system /system >> /storage/sdcard0/swap.log 2>&1
swapon /system/swap.swp >> /storage/sdcard0/swap.log 2>&1
echo swappingon... >> /storage/sdcard0/swap.log 2>&1
swapon /storage/sdcard0/Swapper/swap.swp >> /storage/sdcard0/swap.log 2>&1
swapon /storage/webtop/swap.swp >> /storage/sdcard0/swap.log 2>&1
swapon /data/swap.swp >> /storage/sdcard0/swap.log 2>&1
swapon /cache/swap.swp >> /storage/sdcard0/swap.log 2>&1[/HIDE]
This might not be too good of a batch script, but i've never before in my life made linux batch scripts, I'm happy it works. I use smanager to execute it on boot.
There is a pre-created 100mb swapfile in /storage/sdcard0/Swapper/swap.swp that gets copied to /cache/ and /storage/preinstall/, since the files there seem to get wiped regularily.
Of course swapping on sdcard0 does not work since sdcard0 does not support permissions (fat?), i was just too lazy to remove it. A logfile is generated in sdcard0 called swap.log.
Been messing around with the swappiness a lot. 30-40 swappiness really enhances multitasking but the phone seems to get slow after some time because it has to do a lot of swap io i guess. Trying 15 now.
Click to expand...
Click to collapse
In the next post I'll elaborate on the scripts and hopefully upload nice automatic scripts here You still need to use SManager to run them though.
Script for creating and running swap in /cache attached.
Whole script init.sh:
Code:
#!/system/bin/sh
date >> /data/swap/swap-init.log 2>&1
echo "Initializing swap." >> /data/swap/swap-init.log 2>&1
if [ ! -f /cache/swap ]; then # when /cache/swap is not present
echo "Swap file not found, creating it." >> /data/swap/swap-init.log 2>&1
date >> /data/swap/swap-setup.log 2>&1
echo "Creating swap file as it was not present." >> /data/swap/swap-setup.log 2>&1
echo "Space in /cache:" >> /data/swap/swap-setup.log 2>&1
df /cache >> /data/swap/swap-setup.log 2>&1
freeCache=$(busybox df -k /cache | tail -1 | awk '{ print $4 }')
let swapSize=freeCache*80/100 # count 80% of free space in /cache
echo "Creating ${swapSize} KB swap file." >> /data/swap/swap-setup.log 2>&1
dd if=/dev/zero of=/cache/swap bs=1024 count=$swapSize >> /data/swap/swap-setup.log 2>&1 # create swap file called swap in /cache that takes up 80% of its free space.
mkswap /cache/swap >> /data/swap/swap-setup.log 2>&1 # format the previously created 'swap' file for swap, just as you'd do with a partition
echo "Creation of the swap file completed." >> /data/swap/swap-setup.log 2>&1
date >> /data/swap/swap-setup.log 2>&1
fi
if [ ! -f /data/swap/swappines.txt ]; then
echo 15 1> /proc/sys/vm/swappiness 2>> /data/swap/swap-init.log # set swappiness 15
else
cat /data/swap/swappiness.txt 1> /proc/sys/vm/swappiness 2>> /data/swap/swap-init.log # set swappiness as in swappiness.txt - PUT THERE ONLY NUMS 1-99!
fi
swapon /cache/swap >> /data/swap/swap-init.log 2>&1 # use the swap file created in the installation script for swap
echo "Swap initialization done." >> /data/swap/swap-init.log 2>&1
date >> /data/swap/swap-init.log 2>&1
Usage:
Extract init.sh (and optionally swappiness.txt as well) to /data/swap/
Setup SManager to run it on boot: View attachment 3397902 (don't forget to hit the top-right SAVE button after you select Su and Boot)
Run it manually for first-time init (if you don't, the next boot will be pretty slow)
It will create the swap file on the first run, utilizing 80% of free space in the /cache partition. It's better to do step 3 - run it manually first, so it doesn't compete for system resources with other stuff starting on boot. It took me over five minutes to create the swap file on boot, compared to about two minutes when the system was already running. While creating, it eats up system resources and blocks other apps -> wait 5+ mins for it to finish. You have been warned If it fails for whatever reason, just delete the /cache/swap file and run it again. It writes a huge file (about 750M) while everything else competes for the flash memory read and write which is why it's so slow at boot.
My setup log:
Code:
Thu Jul 9 23:46:53 CEST 2015
Creating swap file as it was not present.
Space in /cache:
Filesystem Size Used Free Blksize
/cache 1007.9M 33.3M 974.6M 4096
Creating 756409 KB swap file.
756409+0 records in
756409+0 records out
774562816 bytes transferred in 301.058 secs (2572802 bytes/sec)
Creation of the swap file completed.
Thu Jul 9 23:51:59 CEST 2015
The script logs inits to /data/swap/swap-init.log and the setup (creation of the swap file) to /data/swap/swap-setup.log. It sets swappiness to 15 or whatever you have in /data/swap/swappiness.txt (if it's missing, it defaults to 15).
If you want to see whether it's done creating the swap file yet, either watch the /cache/swap file grow (i.e. over SCP) or watch the setup log grow (tail -f /data/swap/swap-setup.log in shell, wait for the "Creation of the swap file completed." text to appear and then exit tail by Ctrl+C).
If you have set your SELinux to permissive state, you can save init.sh as /data/local/userinit.sh and don't need SManager at all - it will be run by CM itself. Make sure to create the /data/swap folder though so the logs can be saved, otherwise it may exit on error.
Milp said:
Hi there, I'm using the following code. I'm using some random app to create swapfiles since dd doesn't work for me with if=/dev/null, it creates 0byte files.
Click to expand...
Click to collapse
Works with if=/dev/zero
Milp said:
And swapping on a file filled with /dev/urandom doesn't seem to work. No idea how else i could go about that.
Click to expand...
Click to collapse
Well, did you format the file for swap using mkswap?
Milp said:
Code:
#!/system/bin/sh
echo start > /storage/sdcard0/swap.log 2>&1
echo mountstuff > /storage/sdcard0/swap.log 2>&1
umount /preinstall >> /storage/sdcard0/swap.log 2>&1
mount -o rw,remount / >> /storage/sdcard0/swap.log 2>&1
rm -r preinstall >> /storage/sdcard0/swap.log 2>&1
mkdir /storage/preinstall >> /storage/sdcard0/swap.log 2>&1
mkdir /storage/webtop >> /storage/sdcard0/swap.log 2>&1
mount -o ro,remount / >> /storage/sdcard0/swap.log 2>&1
mount -t ext3 /dev/block/preinstall /storage/preinstall >> /storage/sdcard0/swap.log 2>&1
mount -t ext4 /dev/block/webtop /storage/webtop >> /storage/sdcard0/swap.log 2>&1
chmod 777 /storage/preinstall >> /storage/sdcard0/swap.log 2>&1
chmod 777 /storage/webtop >> /storage/sdcard0/swap.log 2>&1
echo swappiness...15 >> /storage/sdcard0/swap.log 2>&1
echo 15 1> /proc/sys/vm/swappiness 2>> /storage/sdcard0/swap.log
echo cping... >> /storage/sdcard0/swap.log >> /storage/sdcard0/swap.log 2>&1
cp /storage/sdcard0/Swapper/swap.swp /storage/preinstall/swap.swp >> /storage/sdcard0/swap.log 2>&1
chmod 666 /storage/preinstall/swap.swp >> /storage/sdcard0/swap.log 2>&1
cp /storage/sdcard0/Swapper/swap.swp /cache/swap.swp >> /storage/sdcard0/swap.log 2>&1
chmod 666 /cache/swap.swp >> /storage/sdcard0/swap.log 2>&1
swapon /storage/preinstall/swap.swp >> /storage/sdcard0/swap.log 2>&1
mount -o remount,rw /dev/block/system /system >> /storage/sdcard0/swap.log 2>&1
swapon /system/swap.swp >> /storage/sdcard0/swap.log 2>&1
echo swappingon... >> /storage/sdcard0/swap.log 2>&1
swapon /storage/sdcard0/Swapper/swap.swp >> /storage/sdcard0/swap.log 2>&1
swapon /storage/webtop/swap.swp >> /storage/sdcard0/swap.log 2>&1
swapon /data/swap.swp >> /storage/sdcard0/swap.log 2>&1
swapon /cache/swap.swp >> /storage/sdcard0/swap.log 2>&1[/HIDE]
This might not be too good of a batch script, but i've never before in my life made linux batch scripts, I'm happy it works. I use smanager to execute it on boot.
There is a pre-created 100mb swapfile in /storage/sdcard0/Swapper/swap.swp that gets copied to /cache/ and /storage/preinstall/, since the files there seem to get wiped regularily.
Click to expand...
Click to collapse
I'm not advanced in batch scripting either, but this looks fine to me for your purpose. Holy Mother of Logging though However, the first "start" won't be in the log and log gets overwritten each run - you use > (save to file) instead of >> (append to file) in first two lines.
Milp said:
Of course swapping on sdcard0 does not work since sdcard0 does not support permissions (fat?), i was just too lazy to remove it. A logfile is generated in sdcard0 called swap.log.
Click to expand...
Click to collapse
All this is quite heavy approach, may I suggest mine?
To put it into shell script format:
One-time installation:
Code:
#!/system/bin/sh
dd if=/dev/zero of=/cache/swap bs=1024 count=819200 # create 800 MB swap file called swap in /cache (this is for systems running in stock slot where there's 1GB partition for cache that's almost not used - adjust size (count=<size in KB>) for your system). I didn't experience any deletion of this. If you do like Milp did, just re-run this script.
mkswap /cache/swap # format the previously created 'swap' file for swap, just as you'd do with a partition
swapon /cache/swap # use this file as swap
umount /preinstall # not needed in CM12, it's not mounted (the folder is there though). Let's do it for legacy compatibility though :)
mkswap /dev/block/preinstall # format the whole /preinstall partition as swap so we don't have to deal with stupid deletions or mounts or whatever later
swapon /dev/block/preinstall # use preinstall as swap (this is kinda like mount for swap partition/device)
I didn't use any logging as I run these over ssh or from adb shell so I can see stdout and stderr. If you wish to, you can add Milp's >> /storage/sdcard0/swap.log 2>&1 at the end of each line before my comments:
Code:
#!/system/bin/sh
date >> /storage/sdcard0/swap-setup.log 2>&1
echo "Swap files installation started." >> /storage/sdcard0/swap-setup.log 2>&1
dd if=/dev/zero of=/cache/swap bs=1024 count=819200 >> /storage/sdcard0/swap-setup.log 2>&1 # create 800 MB swap file called swap in /cache (this is for systems running in stock slot where there's 1GB partition for cache that's almost not used - adjust size (count=<size in KB>) for your system). I didn't experience any deletion of this. If you do like Milp did, just re-run this script.
mkswap /cache/swap >> /storage/sdcard0/swap-setup.log 2>&1 # format the previously created 'swap' file for swap, just as you'd do with a partition
swapon /cache/swap >> /storage/sdcard0/swap-setup.log 2>&1 # use this file as swap
umount /preinstall >> /storage/sdcard0/swap-setup.log 2>&1 # not needed in CM12, it's not mounted (the folder is there though). Let's do it for legacy compatibility though :)
mkswap /dev/block/preinstall >> /storage/sdcard0/swap-setup.log 2>&1 # format the whole /preinstall partition as swap so we don't have to deal with stupid deletions or mounts or whatever later
swapon /dev/block/preinstall >> /storage/sdcard0/swap-setup.log 2>&1 # use preinstall as swap (this is kinda like mount for swap partition/device)
echo "Swap files installation finished." >> /storage/sdcard0/swap-setup.log 2>&1
date >> /storage/sdcard0/swap-setup.log 2>&1
Then you need this script running on boot, I use smanager as well:
Code:
#!/system/bin/sh
swapon /cache/swap # use the swap file created in the installation script for swap
swapon /dev/block/preinstall # use preinstall as swap
echo 15 > /proc/sys/vm/swappiness # set swappiness 15
Version with logs:
Code:
#!/system/bin/sh
date >> /storage/sdcard0/swap-init.log 2>&1
echo "Initializing swap." >> /storage/sdcard0/swap-init.log 2>&1
swapon /cache/swap >> /storage/sdcard0/swap-init.log 2>&1 # use the swap file created in the installation script for swap
swapon /dev/block/preinstall >> /storage/sdcard0/swap-init.log 2>&1 # use preinstall as swap
echo 15 1> /proc/sys/vm/swappiness 2>> /storage/sdcard0/swap-init.log # set swappiness 15
echo "Swap initialization done." >> /storage/sdcard0/swap-init.log 2>&1
date >> /storage/sdcard0/swap-init.log 2>&1
Milp said:
Been messing around with the swappiness a lot. 30-40 swappiness really enhances multitasking but the phone seems to get slow after some time because it has to do a lot of swap io i guess. Trying 15 now.
Click to expand...
Click to collapse
CM12's default is 60, which is way too much - after day of use I have sometimes been unable even to unlock the phone as seemingly the lock screen has been swapped away and it took several wakings of the phone before it allowed me to swipe the lockscreen up (I don't use any lock protection, just plain swipable lockscreen). It even worsened with Poweramp's custom lockscreen.
Will toy around with it for a bit in upcoming days.
I don't think I have ever gotten much over 300 MB swapped, making my current combined of almost 1,5 GB swap huge overkill. Will see after some time, but the /cache/swap file should be very sufficient and there's no need to mess up preinstall
I'm testing swap on stock 4.1.2 and I'm using app called Roehsoft Ram-Expander (I bougth this when I was xperia user). Seems to work but I'm wondering if the app is necessary.
Borchuck said:
I'm testing swap on stock 4.1.2 and I'm using app called Roehsoft Ram-Expander (I bougth this when I was xperia user). Seems to work but I'm wondering if the app is necessary.
Click to expand...
Click to collapse
Just added my scripts to post #2. It still needs a third-party app (SManager) to be run automatically though.
It seems the app you mentioned puts the swap file on the sd card, while my script puts it to internal memory to the /cache partition. If internal storage is faster than SD (haven't tested, but it probably is), then the swap file created by my script should be faster as well. On the other hand, the app is easily controllable through its UI, while my script is just a script and you need to run it by a third-party app anyways. So whatever you like more, use it
Uploaded automated script for creation and initialization of swap in /cache (to be run on startup). Please let me know if you stumble upon any error
If there's a problem with swap file creation, make sure you have busybox installed with support for df - the built-in df in my shell doesn't recognize the -k flag so it's not good for free space counting.
LuH said:
Just added my scripts to post #2. It still needs a third-party app (SManager) to be run automatically though.
It seems the app you mentioned puts the swap file on the sd card, while my script puts it to internal memory to the /cache partition. If internal storage is faster than SD (haven't tested, but it probably is), then the swap file created by my script should be faster as well. On the other hand, the app is easily controllable through its UI, while my script is just a script and you need to run it by a third-party app anyways. So whatever you like more, use it
Click to expand...
Click to collapse
Thanks for answer!
In this app you can choose swap file path and I've chose /preinstall (512 mb).
Scripts can be ran by placing them into init.d folder, am I right?
Borchuck said:
Thanks for answer!
In this app you can choose swap file path and I've chose /preinstall (512 mb).
Scripts can be ran by placing them into init.d folder, am I right?
Click to expand...
Click to collapse
In theory yes, but SELinux somehow blocks it in CM12 - that's why you need to run it using SManager instead. I have no idea what stock system does on boot.
Borchuck said:
Scripts can be ran by placing them into init.d folder, am I right?
Click to expand...
Click to collapse
Unless something has changed, init.d is disabled by default in CM11/12. I haven't checked Cm11 in a long time, nor in 12.1, though. There's an app that emulates it, as well as at least one app that tries to enable it, in the play store. The former works as it should in both KK and LP as long as you accept its limitations (scripts get run when the app autostarts, after boot), but I have no idea if the latter does.
Using /cache is a neat idea, when you're installed into the stock partitions. It's there and almost completely unused in CM12/.1, and it's not like we can usefully reallocate it or anything 'cause Motorola. Getting the webtop/preinstall stuff going is an extra step, and that space might be put to better use anyway. Though, preinstall is just about the perfect size for a swap...
The problems I see with swapping on any internal partition are 1: you're going to wear the eMMC out a lot faster writing to it all the time (though how much faster is anyone's guess), and 2: the eMMC in our phone isn't especially fast. It's not slow, but it's slower than a modern high-end SD card, and definitely slower than RAM.
I'm a little curious about how fast our device can read/write to external SD cards. A good one, with both fast read/write and good small-block performance, would be "safer" to use for swapping. If it happens to wear out, pop in a new one, good to go.
@LuH
Maybe you should use zRAM? I use it on my phone ...
Fervi
Septfox said:
The problems I see with swapping on any internal partition are 1: you're going to wear the eMMC out a lot faster writing to it all the time (though how much faster is anyone's guess),
Click to expand...
Click to collapse
Well, I've had my device for three years now so if it decides to retire, it's worked enough to earn it However, I have yet to see a phone dead because of worn out memory - it always was some radio chip in my previous ones. So 'm taking my chances I have considered the risk, just don't think it will affect me.
Septfox said:
and 2: the eMMC in our phone isn't especially fast. It's not slow, but it's slower than a modern high-end SD card, and definitely slower than RAM. I'm a little curious about how fast our device can read/write to external SD cards. A good one, with both fast read/write and good small-block performance, would be "safer" to use for swapping. If it happens to wear out, pop in a new one, good to go.
Click to expand...
Click to collapse
eMMC is of course slower than RAM, but my SD doesn't seem to outperform it - at least in big file read and writes it's about the same. I haven't found a working benchmark app for random I/O testing, but I think the onboard eMMC and my SanDisk 64GB SDXC U1 are on par in D4. Then again, I've had my SD longer than I've had my D4, originally bought it for my previous Windows Mobile 6.5 Sony Ericsson Xperia X2
Attached benchmark (usually ~600 MB write and read): SD card (SanDisk 64 GB U1) R13.56/W9,57, SD card (eMMC storage partition) R15.82/W5.31, Internal memory (eMMC /data partition) R8.90/W4.23, Custom location (eMMC /cache partition) R13.82/W4.17.
I don't get why three partitions of same eMMC flash memory chip get so different results, either it's differently managed by the system (but why give preference to media storage?), or the parts of chips are worn out differently, or the benchmarking app sucks
ferviverus said:
@LuH
Maybe you should use zRAM? I use it on my phone ...
Click to expand...
Click to collapse
I'll probably try as well, but thought I'll give swapping a try first... I heard more problems than advantages about using zRAM on D4, what's your experience?
LuH said:
Attached benchmark (usually ~600 MB write and read): SD card (SanDisk 64 GB U1) R13.56/W9,57, SD card (eMMC storage partition) R15.82/W5.31, Internal memory (eMMC /data partition) R8.90/W4.23, Custom location (eMMC /cache partition) R13.82/W4.17.
I don't get why three partitions of same eMMC flash memory chip get so different results, either it's differently managed by the system (but why give preference to media storage?), or the parts of chips are worn out differently, or the benchmarking app sucks
Click to expand...
Click to collapse
Double the write speed isn't "outperforming"? :v
Big file transfers are mostly irrelevant, small-block performance is where you need speed. You can mount both SD and internal storage in Safestrap, test there using CrystalDiskMark (which gives not only bulk transfer speeds, but 512k and 4k as well).
My results, using three passes and a 500mb test file
Internal storage
Sequential Read : 19.844 MB/s
Sequential Write : 13.899 MB/s
Random Read 512KB : 18.571 MB/s
Random Write 512KB : 10.263 MB/s
Random Read 4KB (QD=1) : 3.917 MB/s [ 956.3 IOPS]
Random Write 4KB (QD=1) : 1.770 MB/s [ 432.2 IOPS]
Random Read 4KB (QD=32) : 4.964 MB/s [ 1212.0 IOPS]
Random Write 4KB (QD=32) : 1.947 MB/s [ 475.4 IOPS]
External storage/SD card (Sandisk 16gb Class 10/UC1)
Sequential Read : 14.311 MB/s
Sequential Write : 4.907 MB/s (!)
Random Read 512KB : 13.667 MB/s
Random Write 512KB : 1.468 MB/s
Random Read 4KB (QD=1) : 2.267 MB/s [ 553.5 IOPS]
Random Write 4KB (QD=1) : 0.985 MB/s [ 240.5 IOPS]
Random Read 4KB (QD=32) : 2.512 MB/s [ 613.4 IOPS]
Random Write 4KB (QD=32) : 1.251 MB/s [ 305.5 IOPS]
The choice is clear here, but my card is pretty old...yours may fare better. Sandisk cards are generally known for balanced performance.
As for the different results...I dunno. The eMMC partitions should be all the same, it's all on the same flash after all. The only thing I can think of is filesystem differences, but the internal partitions are all ext3, right?
As sad as it makes me to say it, zRAM...isn't very good at the moment. It seems to slow the phone a bit while enabled, for an effectively minimal amount of extra memory. You'd probably be better off increasing minfree thresholds to keep things killed when they're no longer needed, rather than stressing the memory management even more just to keep a couple more apps "loaded".
@LuH
Well, i don't use CM (it doesn't support lapdock), but I use on stock zRam - I saved ~4.5MB (6.9MB compressed to 2.5MB) ... but I read on internet, that zRam is faster than Swap
Fervi
Failed to send the attachment before, giving now. Anyways, probably will try your suggestion to do it from computer later.
Septfox said:
Double the write speed isn't "outperforming"? :v
Click to expand...
Click to collapse
Well, I'm not really concerned by swapping out - that usually takes place when the phone is at rest. I specifically want it to wake up fast -> read speed is the bottleneck and there it's not as big a difference.
Septfox said:
As for the different results...I dunno. The eMMC partitions should be all the same, it's all on the same flash after all. The only thing I can think of is filesystem differences, but the internal partitions are all ext3, right?
Click to expand...
Click to collapse
Well, it looks as though the smaller the partition, the worse the performance is. No idea why. The biggest (and fastest), "Internal SD card" or Media storage or whatever it's called at different system, the one mounted as /sdcard, is exFAT, others are mostly ext3 (preinstall, cache, system and data) and webtop is ext4.
Septfox said:
As sad as it makes me to say it, zRAM...isn't very good at the moment. It seems to slow the phone a bit while enabled, for an effectively minimal amount of extra memory. You'd probably be better off increasing minfree thresholds to keep things killed when they're no longer needed, rather than stressing the memory management even more just to keep a couple more apps "loaded".
Click to expand...
Click to collapse
Exactly, I need hundreds of MBs, not single megabytes, to outweigh the performance loss.
LuH said:
Well, I'm not really concerned by swapping out - that usually takes place when the phone is at rest. I specifically want it to wake up fast -> read speed is the bottleneck and there it's not as big a difference.
Click to expand...
Click to collapse
Swapping should be happening every time the phone starts running out of memory. So, uh, pretty much every other second.
Run an app > OOM > store "oldest" objects in RAM to pagefile/swap partition > load latest app into RAM > user does something in-app requiring a service provider or some other additional data being loaded > OOM > vicious cycle continues
LuH said:
Well, it looks as though the smaller the partition, the worse the performance is. No idea why. The biggest (and fastest), "Internal SD card" or Media storage or whatever it's called at different system, the one mounted as /sdcard, is exFAT, others are mostly ext3 (preinstall, cache, system and data) and webtop is ext4.
Click to expand...
Click to collapse
Oh right, I guess the internal "SD" would be exFAT or some other FAT variant, since it's treated as a removable card.
Write speed being lower on smaller flash partitions makes sense, if they already have data in them, kind of.
If a write is attempted on a page within a flash block which already has data in it, you have to go through the whole read-erase-modify-write process, dragging overall speed down...
Unless our flash supports TRIM, which would pre-emptively erase blocks while the filesystem is idle; I'm pretty sure it unfortunately doesn't. So it's rare that an app writing files is going to happen upon a nice, big contiguous area of already-cleared blocks that can be simply written to.
But then, /cache is almost certainly empty (or minimally filled), so it makes no sense for writes on it to be slow.
*shrugs* I guess you could try formatting it and see if the write speed improves.
LuH said:
Exactly, I need hundreds of MBs, not single megabytes, to outweigh the performance loss.
Click to expand...
Click to collapse
zRAM is 256mb at default, and you can probably count it being effectively double that at least, once things are compressed into it.
It just...doesn't seem to work very quickly. YMMV though. It's enabled by default on the last few CM12 nightlies, so if you've updated recently (well, and are using 12), it's already on.
If you want it to be bigger/smaller, the value isn't baked in; utilities such as Kernel Aduitor can resize it. I tried increasing it to 500mb just to see if I could break something, and the phone...seemed to run slightly better for some reason. So that was a thing.
And reiteration: it's worthwhile to modify minfree settings. At least in Lollipop, if you keep free memory floating above 200mb, it runs much better. Apps will need to relaunch more, but relaunching is usually a lot faster than letting free memory sink to the 160-170mb range that stock values allow, and waiting while the memory manager chokes.
Empty App: 200 and Hidden App: 160 is adequate.
And speaking of the memory manager, joojoobee's kernel has a bunch of patches merged in that aren't in the nightlies yet, which improve memory handling somewhat (phone still starts choking under 200mb, but it's usable).
On the performance side:
Have any of you had any positive long term experiences with swap enabled? While swap gave me the ability to run several huge tasks all at once, in the long run it made my system very unstable. Things would crash out of random, the phone would take ages to wake up from sleep sometimes etc.
I have now upgraded to cm12.1 and disabled my script again.

Categories

Resources