1#!/usr/bin/env bash
2#
3#(c) 2004-present, Facebook Inc. All rights reserved.
4#
5#see LICENSE file for more information on use/redistribution rights.
6#
7
8#
9#dbench_monitor: monitor db_bench process for violation of memory utilization
10#
11#default usage will monitor 'virtual memory size'. See below for standard options
12#passed to db_bench during this test.
13#
14# See also: ./pflag for the actual monitoring script that does the work
15#
16#NOTE:
17#  You may end up with some /tmp/ files if db_bench OR
18#  this script OR ./pflag was killed unceremoniously
19#
20#  If you see the script taking a long time, trying "kill"
21#  will usually cleanly exit.
22#
23#
24DIR=`dirname $0`
25LOG=/tmp/`basename $0`.$$
26DB_BENCH="$DIR/../db_bench";
27PFLAG=${DIR}/pflag
28
29usage() {
30    cat <<HELP; exit
31
32Usage: $0  [-h]
33
34-h: prints this help message
35
36This program will run the db_bench script to monitor memory usage
37using the 'pflag' program. It launches db_bench with default settings
38for certain arguments. You can change the defaults passed to
39'db_bench' program, by setting the following environment 
40variables:
41
42  bs [block_size]
43  ztype [compression_type]
44  benches [benchmarks]
45  reads [reads]
46  threads [threads]
47  cs [cache_size]
48  vsize [value_size]
49  comp [compression_ratio]
50  num [num]
51
52See the code for more info
53
54HELP
55
56}
57
58[ ! -x ${DB_BENCH} ] && echo "WARNING: ${DB_BENCH} doesn't exist, abort!" && exit -1;
59
60[ "x$1" = "x-h" ] && usage;
61
62trap 'rm -f ${LOG}; kill ${PID}; echo "Interrupted, exiting";' 1 2 3 15
63
64touch $LOG;
65
66: ${bs:=16384}
67: ${ztype:=zlib}
68: ${benches:=readwhilewriting}
69: ${reads:=$((1*1024*1024))};
70: ${threads:=8}
71: ${vsize:=2000}
72: ${comp:=0.5}
73: ${num:=10000}
74: ${cs:=$((1*1024*1024*1024))};
75
76DEBUG=1    #Set to 0 to remove chattiness
77
78
79if [ "x$DEBUG" != "x" ]; then
80  #
81  #NOTE: under some circumstances, --use_existing_db may leave LOCK files under ${TMPDIR}/rocksdb/*
82  #cleanup the dir and re-run
83  #
84  echo DEBUG: Will run $DB_BENCH --block_size=$bs --compression_type=$ztype --benchmarks="$benches" --reads="$reads" --threads="$threads" --cache_size=$cs  --value_size=$vsize --compression_ratio=$comp --num=$num --use_existing_db
85
86fi
87
88$DB_BENCH --block_size=$bs --compression_type=$ztype --benchmarks="$benches" --reads="$reads" --threads="$threads" --cache_size=$cs  --value_size=$vsize --compression_ratio=$comp --num=$num --use_existing_db >$LOG 2>&1 &
89
90if [ $? -ne 0 ]; then
91  warn "WARNING: ${DB_BENCH} did not launch successfully! Abort!";
92  exit;
93fi
94PID=$!
95
96#
97#Start the monitoring. Default is "vsz" monitoring for upto cache_size ($cs) value of virtual mem
98#You could also monitor RSS and CPUTIME (bsdtime). Try 'pflag -h' for how to do this
99#
100${PFLAG} -p $PID -v
101
102rm -f $LOG;
103