#!/system/bin/sh

BUSYBOX="/data/data/com.paget96.lsandroid/files/binary/busybox"
LOG="/data/data/com.paget96.lsandroid/files/log/mainLog"
divider="==============================================="

sendToLog() {
  timeZone=$(getprop persist.sys.timezone)
  printDate=$(TZ="$timeZone" $BUSYBOX date +"%H:%M:%S:%3N %d-%m-%Y")

  echo "[$printDate] $1" >>$LOG
}

write() {
  #chmod 0644 "$1"
  echo "$2" >"$1"
}

sendToLog "Activating balanced virtual memory tweaks..."

sync

# Update stats every 10 seconds
write /proc/sys/vm/stat_interval 10

leases_enable=/proc/sys/fs/leases-enable
if [ -e $leases_enable ]; then
  write $leases_enable "1"
  sendToLog "leases_enable=1"
fi

# This file specifies the grace period (in seconds) that the kernel grants
# to a process holding a file lease after it has sent a signal to that process
# notifying it that another process is waiting to open the file.
# If the lease holder does not remove or downgrade the lease within this grace period,
# the kernel forcibly breaks the lease.

lease_break_time=/proc/sys/fs/lease-break-time
if [ -e $lease_break_time ]; then
  write $lease_break_time "20"
  sendToLog "lease_break_time=20"
fi

sendToLog "File system parameters are updated"

enable_process_reclaim=/sys/module/process_reclaim/parameters/enable_process_reclaim
if [ -e $enable_process_reclaim ]; then
  write $enable_process_reclaim "0"
  sendToLog "Reclaiming pages of inactive tasks disabled"
fi

# This parameter tells how much of physical RAM to take when swap is full
overcommit_ratio=/proc/sys/vm/overcommit_ratio
if [ -e overcommit_ratio ]; then
  write $overcommit_ratio "0"
  sendToLog "overcommit_ratio=0"
fi

oom_dump_tasks=/proc/sys/vm/oom_dump_tasks
if [ -e $oom_dump_tasks ]; then
  write $oom_dump_tasks "0"
  sendToLog "OOM dump tasks are disabled"
fi

vfs_cache_pressure=/proc/sys/vm/vfs_cache_pressure
if [ -e $vfs_cache_pressure ]; then
  write $vfs_cache_pressure "100"
  sendToLog "vfs_cache_pressure=100"
fi

# page-cluster controls the number of pages up to which consecutive pages
# are read in from swap in a single attempt. This is the swap counterpart
# to page cache readahead.
# The mentioned consecutivity is not in terms of virtual/physical addresses,
# but consecutive on swap space - that means they were swapped out together.
# It is a logarithmic value - setting it to zero means "1 page", setting
# it to 1 means "2 pages", setting it to 2 means "4 pages", etc.
# Zero disables swap readahead completely.
# The default value is three (eight pages at a time).  There may be some
# small benefits in tuning this to a different value if your workload is
# swap-intensive.
# Lower values mean lower latencies for initial faults, but at the same time
# extra faults and I/O delays for following faults if they would have been part of
# that consecutive pages readahead would have brought in.
page_cluster=/proc/sys/vm/page-cluster
if [ -e $page_cluster ]; then
  write $page_cluster "0"
  sendToLog "page_cluster=0"
fi

# vm.dirty_expire_centisecs is how long something can be in cache
# before it needs to be written.
# When the pdflush/flush/kdmflush processes kick in they will
# check to see how old a dirty page is, and if itâ€™s older than this value itâ€™ll
# be written asynchronously to disk. Since holding a dirty page in memory is
# unsafe this is also a safeguard against data loss.
dirty_expire_centisecs=/proc/sys/vm/dirty_expire_centisecs
if [ -e $dirty_expire_centisecs ]; then
  write $dirty_expire_centisecs "2000"
  sendToLog "dirty_expire_centisecs=2000"
fi

# vm.dirty_writeback_centisecs is how often the pdflush/flush/kdmflush processes wake up
# and check to see if work needs to be done.
dirty_writeback_centisecs=/proc/sys/vm/dirty_writeback_centisecs
if [ -e $dirty_writeback_centisecs ]; then
  write $dirty_writeback_centisecs "2000"
  sendToLog "dirty_writeback_centisecs=2000"
fi

# vm.dirty_background_ratio is the percentage of system memory(RAM)
# that can be filled with â€œdirtyâ€� pages â€” memory pages that
# still need to be written to disk â€” before the pdflush/flush/kdmflush
# background processes kick in to write it to disk.
# It can be 50% or less of dirtyRatio
# If ( dirty_background_ratio >= dirty_ratio ) {
# dirty_background_ratio = dirty_ratio / 2 (or 4)
dirty_background_ratio=/proc/sys/vm/dirty_background_ratio
if [ -e $dirty_background_ratio ]; then
  write $dirty_background_ratio "10"
  sendToLog "dirty_background_ratio=10"
fi

# vm.dirty_ratio is the absolute maximum amount of system memory
# that can be filled with dirty pages before everything must get committed to disk.
# When the system gets to this point all new I/O blocks until dirty pages
# have been written to disk. This is often the source of long I/O pauses,
# but is a safeguard against too much data being cached unsafely in memory.
dirty_ratio=/proc/sys/vm/dirty_ratio
if [ -e $dirty_ratio ]; then
  write $dirty_ratio "30"
  sendToLog "dirty_ratio=30"
fi

sendToLog "Virtual memory tweaks successfully applied balanced profile"
sendToLog "$divider"
