| 1 | #!/bin/sh |
| 2 | # |
| 3 | # Bootchart logger script |
| 4 | # Ziga Mahkovec <[email protected]> |
| 5 | # |
| 6 | # Modified heavily for Alpine Linux bootcharting |
| 7 | # Timo Teras <[email protected]> |
| 8 | # |
| 9 | # This script is used for data collection for the bootchart |
| 10 | # boot performance visualization tool (http://www.bootchart.org). |
| 11 | # |
| 12 | # This script is tied to Alpine Init scripts and charts the |
| 13 | # bootup procedure only. |
| 14 | # |
| 15 | |
| 16 | PATH="/sbin:/bin:/usr/sbin:/usr/bin:$PATH" |
| 17 | |
| 18 | # Configuration for bootchartd, the bootchart logger script. |
| 19 | TMPFS_SIZE=32m |
| 20 | SAMPLE_PERIOD=0.2 |
| 21 | PROCESS_ACCOUNTING="yes" |
| 22 | BOOTLOG_DEST=/var/log/bootchart.tgz |
| 23 | LOGDIR=/bootchart |
| 24 | EXIT_PROC="mingetty agetty rungetty getty fgetty" |
| 25 | |
| 26 | # Monitoring commands |
| 27 | log_cmd_1="cat /proc/stat" |
| 28 | log_target_1=proc_stat.log |
| 29 | |
| 30 | # /proc/diskstats is available in 2.6 kernels |
| 31 | log_cmd_2="cat /proc/diskstats" |
| 32 | log_target_2=proc_diskstats.log |
| 33 | |
| 34 | log_cmd_3="cat /proc/[1-9]*/stat 2>/dev/null" |
| 35 | log_target_3=proc_ps.log |
| 36 | |
| 37 | # Uncomment this line for diskless stations |
| 38 | #log_cmd_4="cat /proc/net/dev" |
| 39 | #log_target_4=proc_netdev.log |
| 40 | |
| 41 | max_log=3 |
| 42 | |
| 43 | do_logging() |
| 44 | { |
| 45 | # Enable process accounting if configured |
| 46 | if [ "$PROCESS_ACCOUNTING" = "yes" ]; then |
| 47 | [ -e kernel_pacct ] || : > kernel_pacct |
| 48 | accton kernel_pacct |
| 49 | fi |
| 50 | |
| 51 | # open file descriptors |
| 52 | i=1 |
| 53 | while [ $i -le $max_log ]; do |
| 54 | eval target=\"\$log_target_$i\" |
| 55 | if [ -z "$target" ]; then |
| 56 | max_log=$i |
| 57 | break |
| 58 | fi |
| 59 | |
| 60 | fd=$((2 + $i)) |
| 61 | eval exec $fd'>>$target' |
| 62 | eval log_fd_$i=$fd |
| 63 | i=$(($i + 1)) |
| 64 | done |
| 65 | |
| 66 | not_stop_logging=true |
| 67 | while $not_stop_logging && \ |
| 68 | { ! pidof $EXIT_PROC >/dev/null; }; do |
| 69 | if [ -r /proc/uptime ]; then |
| 70 | # Write the time (in jiffies). |
| 71 | read uptime < /proc/uptime |
| 72 | uptime=${uptime%% [0-9]*} |
| 73 | uptime=${uptime%.*}${uptime#*.} |
| 74 | |
| 75 | i=1 |
| 76 | while [ $i -le $max_log ]; do |
| 77 | eval fd=\$log_fd_$i\; cmd=\$log_cmd_$i |
| 78 | |
| 79 | { |
| 80 | echo $uptime |
| 81 | # Log the command output |
| 82 | eval $cmd |
| 83 | echo |
| 84 | } >&$fd |
| 85 | i=$(($i + 1)) |
| 86 | done |
| 87 | fi |
| 88 | |
| 89 | sleep $SAMPLE_PERIOD |
| 90 | done |
| 91 | |
| 92 | # close file descriptors |
| 93 | i=1 |
| 94 | while [ $i -le $max_log ]; do |
| 95 | eval fd=\$log_fd_$i |
| 96 | eval exec $fd'>&-' |
| 97 | i=$(($i + 1)) |
| 98 | done |
| 99 | |
| 100 | [ -e kernel_pacct ] && accton |
| 101 | } |
| 102 | |
| 103 | # Stop the boot logger. The lock file is removed to force the loggers in |
| 104 | # background to exit. Some final log files are created and then all log files |
| 105 | # from the tmpfs are packaged and stored in $BOOTLOG_DEST. |
| 106 | finalize() |
| 107 | { |
| 108 | # Stop process accounting if configured |
| 109 | local pacct= |
| 110 | [ -e kernel_pacct ] && pacct=kernel_pacct |
| 111 | |
| 112 | # Write system information |
| 113 | # Log some basic information about the system. |
| 114 | ( |
| 115 | echo "version = $VERSION" |
| 116 | echo "title = Boot chart for $( hostname | sed q ) ($( date ))" |
| 117 | echo "system.uname = $( uname -srvm | sed q )" |
| 118 | if [ -f /etc/alpine-release ]; then |
| 119 | echo "system.release = $( sed q /etc/alpine-release )" |
| 120 | elif [ -f /etc/gentoo-release ]; then |
| 121 | echo "system.release = $( sed q /etc/gentoo-release )" |
| 122 | elif [ -f /etc/SuSE-release ]; then |
| 123 | echo "system.release = $( sed q /etc/SuSE-release )" |
| 124 | elif [ -f /etc/debian_version ]; then |
| 125 | echo "system.release = Debian GNU/$( uname -s ) $( cat /etc/debian_version )" |
| 126 | elif [ -f /etc/frugalware-release ]; then |
| 127 | echo "system.release = $( sed q /etc/frugalware-release )" |
| 128 | elif [ -f /etc/pardus-release ]; then |
| 129 | echo "system.release = $( sed q /etc/pardus-release )" |
| 130 | else |
| 131 | echo "system.release = $( sed 's/\\.//g;q' /etc/issue )" |
| 132 | fi |
| 133 | |
| 134 | # Get CPU count |
| 135 | local cpucount=$(grep -c '^processor' /proc/cpuinfo) |
| 136 | if [ $cpucount -gt 1 -a -n "$(grep 'sibling.*2' /proc/cpuinfo)" ]; then |
| 137 | # Hyper-Threading enabled |
| 138 | cpucount=$(( $cpucount / 2 )) |
| 139 | fi |
| 140 | if grep -q '^model name' /proc/cpuinfo; then |
| 141 | echo "system.cpu = $( grep '^model name' /proc/cpuinfo | sed q )"\ |
| 142 | "($cpucount)" |
| 143 | else |
| 144 | echo "system.cpu = $( grep '^cpu' /proc/cpuinfo | sed q )"\ |
| 145 | "($cpucount)" |
| 146 | fi |
| 147 | |
| 148 | echo "system.kernel.options = $( sed q /proc/cmdline )" |
| 149 | ) >> header |
| 150 | |
| 151 | # Package log files |
| 152 | tar -zcf "$BOOTLOG_DEST" header $pacct *.log |
| 153 | rm "$LOGDIR"/* |
| 154 | rmdir "$LOGDIR" |
| 155 | } |
| 156 | |
| 157 | case "$1" in |
| 158 | start-initfs) |
| 159 | NEWROOT="$2" |
| 160 | ( |
| 161 | cleanup=true |
| 162 | trap "not_stop_logging=false" USR1 |
| 163 | trap "cleanup=false; not_stop_logging=false" USR2 |
| 164 | |
| 165 | mkdir "$LOGDIR" |
| 166 | cd "$LOGDIR" |
| 167 | do_logging |
| 168 | if $cleanup; then |
| 169 | sleep $SAMPLE_PERIOD |
| 170 | finalize |
| 171 | fi |
| 172 | ) & |
| 173 | echo $! > $LOGDIR/bootchart.pid |
| 174 | ;; |
| 175 | stop-initfs) |
| 176 | NEWROOT="$2" |
| 177 | |
| 178 | cd "$LOGDIR" |
| 179 | mkdir "$NEWROOT$LOGDIR" |
| 180 | cp /sbin/bootchartd $NEWROOT/sbin |
| 181 | PID=$(cat bootchart.pid) |
| 182 | kill -USR2 $PID |
| 183 | wait $PID |
| 184 | mv * "$NEWROOT$LOGDIR" |
| 185 | ;; |
| 186 | start-rootfs) |
| 187 | ( |
| 188 | trap "not_stop_logging=false" USR1 |
| 189 | cd "$LOGDIR" |
| 190 | do_logging |
| 191 | finalize |
| 192 | ) & |
| 193 | shift |
| 194 | exec "$@" |
| 195 | ;; |
| 196 | esac |
| 197 | |
| 198 | exit 0 |