#!/bin/bash
# Copyright (c) 2023 Arista Networks, Inc.  All rights reserved.
# Arista Networks, Inc. Confidential and Proprietary.

logfile="/tmp/EosPreloadPy.log"
exec 1> $logfile
exec 2> /dev/null

if [ -f /mnt/flash/NoEosPreloadPy ]; then
  echo "not preloading Python code (disabled)"
  exit 0
fi

echo "uptime at EosPreloadPy start: $(cat /proc/uptime)"
ts1=$(date +%s%N)

cat /export/swi/Sysdb.preload.gz /export/swi/ConfigAgent.preload.gz | gunzip -c > /tmp/preload

# The meat of this service; parallelize with xargs: tell xargs to run "cat <n files>"
# in m pipes. For now n=10 and m=2. Could be adapted to numbers of CPU cores.
# If only 2 cpu cores, cat | xargs causes too much extra context switching and
# negatively impact performance, so use a single thread preloader in that case.
if [ $(cat /proc/cpuinfo | grep processor | wc -l) -gt 3 ]; then 
  cat /tmp/preload | xargs --max-lines=10 --max-procs=2 cat 2>/dev/null >/dev/null
else
  cat $(cat /tmp/preload) 2>/dev/null >/dev/null
fi

rm /tmp/preload
ts2=$(date +%s%N)
echo "time-taken: $(( (ts2 - ts1)/1000000 )) ms"

exit 0

