Differences

This shows you the differences between two versions of the page.

Link to this comparison view

shell [2014/02/27 11:31]
dblume [expect tips]
shell [2018/09/04 12:54] (current)
dblume [Application Memory Usage]
Line 1: Line 1:
 ====== shell tips ====== ====== shell tips ======
  
 +Here's a tutorial: [[http://tldp.org/LDP/abs/html/|Advanced Bash-Scripting Guide]].
 ===== Quick Tips ===== ===== Quick Tips =====
  
Line 40: Line 41:
  
 Here's a template for shell commands that demonstrates a number of arguments, length of argument, etc.  It could still stand a bit of clean-up according to the [[http://google-styleguide.googlecode.com/svn/trunk/shell.xml|Google Shell Style Guide]]. Here's a template for shell commands that demonstrates a number of arguments, length of argument, etc.  It could still stand a bit of clean-up according to the [[http://google-styleguide.googlecode.com/svn/trunk/shell.xml|Google Shell Style Guide]].
 +
 +Another good resource is [[http://robertmuth.blogspot.com/2012/08/better-bash-scripting-in-15-minutes.html|Better Bash Scripting in 15 minutes]].
  
 <code bash> <code bash>
-#!/bin/bash +#!/usr/bin/env bash 
-set -euf -o pipefail # See: https://sipb.mit.edu/doc/safe-shell/+set -eu -o pipefail # See: https://sipb.mit.edu/doc/safe-shell/
  
 declare -r SCRIPT_NAME=$(basename "$BASH_SOURCE") declare -r SCRIPT_NAME=$(basename "$BASH_SOURCE")
Line 108: Line 111:
 fi fi
  
-((VERBOSE==1)) && echo "Starting at `date`"+((VERBOSE==1)) && echo "Starting at $(date)"
 rsync /cygdrive/c/Users/me /cygdrive/"$DISK"/backup/Users rsync /cygdrive/c/Users/me /cygdrive/"$DISK"/backup/Users
 +
 +# We add "|| true" because we don't want to stop 
 +# if the directory was already empty
 +rm -r /cygdrive/c/Users/me/tmp/* || true
 +
 +# Note how we find the number of cores to use
 +make -C build_subdirectory all -j$(grep -c ^processor /proc/cpuinfo)
 </code> </code>
  
Line 151: Line 161:
 <code bash> <code bash>
 find . -name \*.py -type f -print0 | xargs -0 grep -nI "timeit" find . -name \*.py -type f -print0 | xargs -0 grep -nI "timeit"
 +find . -type f \( -name \*.[ch]pp -or -name \*.[ch] \) -print0 | xargs -0 grep -nI printf
 </code> </code>
  
Line 239: Line 250:
 <code bash> <code bash>
 seq 1 50 | xargs -I{} -n1 echo '{} Hello World!' seq 1 50 | xargs -I{} -n1 echo '{} Hello World!'
 +</code>
 +
 +When you've set up Perforce to use an application for diff with ''export P4DIFF='vim -d' '', you can still do a regular diff like so:
 +
 +<code bash>
 +$ P4DIFF=; p4 diff hello-world.cpp
 +</code>
 +
 +It's [[http://stackoverflow.com/questions/47007/determining-the-last-changelist-synced-to-in-perforce|hard to be sure which Perforce changelist you sync'ed if you didn't explicitly sync to a changelist]].
 +
 +So, use ''p4_sync'' to sync to a specific changelist, and update a source file too.
 +
 +<code bash>
 +p4_sync() {
 +    p4 changes -s submitted -m1 ... | tee p4_sync_to_change.txt 
 +    changelist=`cut -d " " -f 2 p4_sync_to_change.txt`
 +    changelist_filename=changelist.h
 +    p4 sync ...@$changelist
 +    if [ -w $changelist_filename ]
 +    then
 + sed -i 's/"[0-9]\+";/"'$changelist'";/' $changelist_filename
 +    fi
 +}
 +</code>
 +
 +Note the use of $@ vs "$*" in the next function that automatically saves an archive of a telnet session. Also note that I remove spaces and colons. (Colons because they screw with opening files directly at line numbers).
 +
 +<code bash>
 +telnet_log() {
 +    curtime=$(date -Iseconds | tr : .)
 +    args=$(echo "$*" | tr ' ' '_')
 +    telnet $@ | tee $HOME/telnetlog/$args\_${curtime::-5}.log
 +}
 +
 +last_telnet_log() {
 +    ls -d1t $HOME/telnetlog/* | head -n 1
 +}
 +</code>
 +
 +Of course if you do that, you'll want to occasionally (via cronjob?) delete old archives.
 +
 +<code>
 +find $HOME/telnetlog/ -type f -mtime +6 -delete
 </code> </code>
  
Line 301: Line 355:
 ====== Perl tips ====== ====== Perl tips ======
  
-The module ''Search::Dict'' has a "''loop''" function that can be used to do a binary search in an ordered dictionary file (a logfile (or log file) that starts with timestamps works). ''File::SortedSeek'' might also be recommended.+The module ''Search::Dict'' has a "''look''" function that can be used to do a binary search in an ordered dictionary file (a logfile (or log file) that starts with timestamps works). ''File::SortedSeek'' might also be recommended. 
 + 
 +====== Application Memory Usage ====== 
 + 
 +Use VM Resident Set Size.  See VmRSS below. (Note the [[http://stackoverflow.com/questions/10400751/how-do-vmrss-and-resident-set-size-match|difference between RSS and VmRSS]]. If one process has memory mapped, it's not usable any any other process) 
 + 
 +<code bash> 
 +host:# ps -ef | grep etflix 
 +default   1532  1081  6 22:06 ?        00:01:21 pkg_/metflix  
 +root      2108  1046  0 22:26 ?        00:00:00 grep etflix 
 +host:# pidof netflix 
 +1532 
 +host:# cat /proc/1532/status 
 +Name:   MAIN 
 +... 
 +Groups: 
 +VmPeak:   220776 kB 
 +VmSize:   210096 kB 
 +VmLck:         0 kB 
 +VmHWM:     95168 kB 
 +VmRSS:     74488 kB 
 +... 
 +</code> 
 + 
 +Or, while running an application, to see how much is free over time, do this from another shell: 
 +<code bash> 
 +while [ 1 ] 
 +do 
 +    free -m | grep Mem 
 +    sleep 3 
 +done 
 +</code> 
 +Alternatively, to see the RSS use of that process alone: 
 +<code bash> 
 +while true; do sync; cat /proc/$(pidof yourprocess)/status | grep VmRSS; sleep 1; done 
 +</code> 
 +====== Measuring Available Memory ====== 
 + 
 +This note doesn't entirely make sense to me. Maybe need to study up on "cat /proc/meminfo" vs. "cat /proc/vmstat" vs. "vmstat". 
 + 
 +The best measure I've found for "available memory" is nr_inactive_file_pages+nr_active_file_pages+nr_free_pages from /proc/vmstat. And then you have to subtract out some heuristically determined value which is base system working set. (That heuristically determined value can be 30-40MB.) 
 + 
 +The command ''free'' just isn't a great indicator in general of how much memory is available because it doesn't account for the cached  file-backed pages that could be dumped to make more memory available. 
 +====== Shared Memory Usage ====== 
 + 
 +To increase limit to 256MB from command line: 
 + 
 +<code bash> 
 +echo "268435456" > /proc/sys/kernel/shmmax 
 +echo "268435456" > /proc/sys/kernel/shmall 
 +</code> 
 + 
 +Or, edit /etc/sysctl.conf: 
 + 
 +<code> 
 +kernel.shmmax= 268435456 
 +kernel.shmall= 268435456 
 +</code> 
 + 
 +====== Performance Metrics ====== 
 + 
 +  * Use [[http://man7.org/linux/man-pages/man1/perf-timechart.1.html|perf-timechart]] 
 +  * [[https://github.com/gperftools/gperftools|gperftools]] 
 + 
 +And you can scrape logs that start with timecodes to create Spreadsheet charts. Given logs like: 
 + 
 +<code> 
 +2016-10-13 19:54:44  memory 22a4 
 +</code> 
 + 
 +On a Macintosh: 
 + 
 +<code bash> 
 +grep memory devicelogs.txt | tr -s ' ' | cut -d " " -f 1,2,4 | \ 
 +sed 's/\([0-9\-]\+\) \([0-9:]\+\).[0-9]\+ \([0-9a-f]\+\)/\1,\2,=DATEVALUE("\1")+TIMEVALUE("\2"),=HEX2DEC("\3")/' >  heapinfo.csv; \ 
 +open heapinfo.csv -a "Microsoft Excel" 
 +</code> 
 + 
 +And on Linux, instead of opening Microsoft Excel, that last line would be: 
 + 
 +<code bash> 
 +libreoffice --calc heapinfo.csv 
 +</code> 
 + 
 +====== Cron ====== 
 + 
 +Keep tasks serialized with [[https://linux.die.net/man/1/flock|flock(1)]]: 
 + 
 +    ( 
 +         flock -n 9 || exit 1 
 +         # ... commands executed under lock ... 
 +    ) 9>/var/lock/mylockfile 
 + 
 + 
 +====== Retrieving Symbols with addr2line ====== 
 + 
 +You can gather a backtrace (stacktrace) with this piped command to addr2line. 
 + 
 +    $ cat << EOF | cut -d " " -f 3 | tr -d "[]" | \ 
 +      addr2line -e builds/austin/src/platform/gibbon/netflix | \ 
 +      xargs -d '\n' realpath --relative-to=. 
 +    > 7/22 app() [0xf7878] (0xf7878) 
 +    > 8/22 app() [0x39c2f8] (0x39c2f8) 
 +    > 9/22 app() [0xe1964] (0xe1964) 
 +    > EOF 
 +    src/Application.h:106 (discriminator 3) 
 +    src/platform/main.cpp:521 
 +    src/Application.cpp:95 
 +     
 +====== Sort by Frequency ====== 
 + 
 +I ran the following P4 command to find out who's been editing a file recently:
  
 +    $ find . -name fname.cpp | xargs p4 filelog -s -m 10 | grep -P "^\.\.\. #" | \
 +      awk '{print $9}' | cut -d @ -f 1 | sort | uniq -c | sort -nr
 +====== Additional Keywords ======
  
 +Linux, Unix, *nix
shell.txt · Last modified: 2018/09/04 12:54 by dblume
 
Recent changes RSS feed Driven by DokuWiki