Advertisement
Guest User

cassandra-env.sh

a guest
Oct 31st, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.45 KB | None | 0 0
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements.  See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership.  The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License.  You may obtain a copy of the License at
  8. #
  9. #     http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16.  
  17. calculate_heap_sizes()
  18. {
  19.     case "`uname`" in
  20.         Linux)
  21.             system_memory_in_mb=`free -m | awk '/Mem:/ {print $2}'`
  22.             system_cpu_cores=`egrep -c 'processor([[:space:]]+):.*' /proc/cpuinfo`
  23.         ;;
  24.         FreeBSD)
  25.             system_memory_in_bytes=`sysctl hw.physmem | awk '{print $2}'`
  26.             system_memory_in_mb=`expr $system_memory_in_bytes / 1024 / 1024`
  27.             system_cpu_cores=`sysctl hw.ncpu | awk '{print $2}'`
  28.         ;;
  29.         SunOS)
  30.             system_memory_in_mb=`prtconf | awk '/Memory size:/ {print $3}'`
  31.             system_cpu_cores=`psrinfo | wc -l`
  32.         ;;
  33.         Darwin)
  34.             system_memory_in_bytes=`sysctl hw.memsize | awk '{print $2}'`
  35.             system_memory_in_mb=`expr $system_memory_in_bytes / 1024 / 1024`
  36.             system_cpu_cores=`sysctl hw.ncpu | awk '{print $2}'`
  37.         ;;
  38.         *)
  39.             # assume reasonable defaults for e.g. a modern desktop or
  40.             # cheap server
  41.             system_memory_in_mb="2048"
  42.             system_cpu_cores="2"
  43.         ;;
  44.     esac
  45.  
  46.     # some systems like the raspberry pi don't report cores, use at least 1
  47.     if [ "$system_cpu_cores" -lt "1" ]
  48.     then
  49.         system_cpu_cores="1"
  50.     fi
  51.  
  52.     # set max heap size based on the following
  53.     # max(min(1/2 ram, 1024MB), min(1/4 ram, 8GB))
  54.     # calculate 1/2 ram and cap to 1024MB
  55.     # calculate 1/4 ram and cap to 8192MB
  56.     # pick the max
  57.     half_system_memory_in_mb=`expr $system_memory_in_mb / 2`
  58.     quarter_system_memory_in_mb=`expr $half_system_memory_in_mb / 2`
  59.     if [ "$half_system_memory_in_mb" -gt "1024" ]
  60.     then
  61.         half_system_memory_in_mb="1024"
  62.     fi
  63.     if [ "$quarter_system_memory_in_mb" -gt "8192" ]
  64.     then
  65.         quarter_system_memory_in_mb="8192"
  66.     fi
  67.     if [ "$half_system_memory_in_mb" -gt "$quarter_system_memory_in_mb" ]
  68.     then
  69.         max_heap_size_in_mb="$half_system_memory_in_mb"
  70.     else
  71.         max_heap_size_in_mb="$quarter_system_memory_in_mb"
  72.     fi
  73.     MAX_HEAP_SIZE="${max_heap_size_in_mb}M"
  74.  
  75.     # Young gen: min(max_sensible_per_modern_cpu_core * num_cores, 1/4 * heap size)
  76.     max_sensible_yg_per_core_in_mb="100"
  77.     max_sensible_yg_in_mb=`expr $max_sensible_yg_per_core_in_mb "*" $system_cpu_cores`
  78.  
  79.     desired_yg_in_mb=`expr $max_heap_size_in_mb / 4`
  80.  
  81.     if [ "$desired_yg_in_mb" -gt "$max_sensible_yg_in_mb" ]
  82.     then
  83.         HEAP_NEWSIZE="${max_sensible_yg_in_mb}M"
  84.     else
  85.         HEAP_NEWSIZE="${desired_yg_in_mb}M"
  86.     fi
  87. }
  88.  
  89. # Determine the sort of JVM we'll be running on.
  90.  
  91. java_ver_output=`"${JAVA:-java}" -version 2>&1`
  92.  
  93. jvmver=`echo "$java_ver_output" | awk -F'"' 'NR==1 {print $2}'`
  94. JVM_VERSION=${jvmver%_*}
  95. JVM_PATCH_VERSION=${jvmver#*_}
  96.  
  97. jvm=`echo "$java_ver_output" | awk 'NR==2 {print $1}'`
  98. case "$jvm" in
  99.     OpenJDK)
  100.         JVM_VENDOR=OpenJDK
  101.         # this will be "64-Bit" or "32-Bit"
  102.         JVM_ARCH=`echo "$java_ver_output" | awk 'NR==3 {print $2}'`
  103.         ;;
  104.     "Java(TM)")
  105.         JVM_VENDOR=Oracle
  106.         # this will be "64-Bit" or "32-Bit"
  107.         JVM_ARCH=`echo "$java_ver_output" | awk 'NR==3 {print $3}'`
  108.         ;;
  109.     *)
  110.         # Help fill in other JVM values
  111.         JVM_VENDOR=other
  112.         JVM_ARCH=unknown
  113.         ;;
  114. esac
  115.  
  116.  
  117. # Override these to set the amount of memory to allocate to the JVM at
  118. # start-up. For production use you may wish to adjust this for your
  119. # environment. MAX_HEAP_SIZE is the total amount of memory dedicated
  120. # to the Java heap; HEAP_NEWSIZE refers to the size of the young
  121. # generation. Both MAX_HEAP_SIZE and HEAP_NEWSIZE should be either set
  122. # or not (if you set one, set the other).
  123. #
  124. # The main trade-off for the young generation is that the larger it
  125. # is, the longer GC pause times will be. The shorter it is, the more
  126. # expensive GC will be (usually).
  127. #
  128. # The example HEAP_NEWSIZE assumes a modern 8-core+ machine for decent pause
  129. # times. If in doubt, and if you do not particularly want to tweak, go with
  130. # 100 MB per physical CPU core.
  131.  
  132. MAX_HEAP_SIZE="8G"
  133. HEAP_NEWSIZE="2G"
  134.  
  135. if [ "x$MAX_HEAP_SIZE" = "x" ] && [ "x$HEAP_NEWSIZE" = "x" ]; then
  136.     calculate_heap_sizes
  137. else
  138.     if [ "x$MAX_HEAP_SIZE" = "x" ] ||  [ "x$HEAP_NEWSIZE" = "x" ]; then
  139.         echo "please set or unset MAX_HEAP_SIZE and HEAP_NEWSIZE in pairs (see cassandra-env.sh)"
  140.         exit 1
  141.     fi
  142. fi
  143.  
  144. # Specifies the default port over which Cassandra will be available for
  145. # JMX connections.
  146. JMX_PORT="7199"
  147.  
  148.  
  149. # Here we create the arguments that will get passed to the jvm when
  150. # starting cassandra.
  151.  
  152. # enable assertions.  disabling this in production will give a modest
  153. # performance benefit (around 5%).
  154. JVM_OPTS="$JVM_OPTS -ea"
  155.  
  156. # add the jamm javaagent
  157. if [ "$JVM_VENDOR" != "OpenJDK" -o "$JVM_VERSION" \> "1.6.0" ] \
  158.       || [ "$JVM_VERSION" = "1.6.0" -a "$JVM_PATCH_VERSION" -ge 23 ]
  159. then
  160.     JVM_OPTS="$JVM_OPTS -javaagent:$CASSANDRA_HOME/lib/jamm-0.2.5.jar"
  161. fi
  162.  
  163. # enable thread priorities, primarily so we can give periodic tasks
  164. # a lower priority to avoid interfering with client workload
  165. JVM_OPTS="$JVM_OPTS -XX:+UseThreadPriorities"
  166. # allows lowering thread priority without being root.  see
  167. # http://tech.stolsvik.com/2010/01/linux-java-thread-priorities-workaround.html
  168. JVM_OPTS="$JVM_OPTS -XX:ThreadPriorityPolicy=42"
  169.  
  170. # min and max heap sizes should be set to the same value to avoid
  171. # stop-the-world GC pauses during resize, and so that we can lock the
  172. # heap in memory on startup to prevent any of it from being swapped
  173. # out.
  174. JVM_OPTS="$JVM_OPTS -Xms${MAX_HEAP_SIZE}"
  175. JVM_OPTS="$JVM_OPTS -Xmx${MAX_HEAP_SIZE}"
  176. JVM_OPTS="$JVM_OPTS -Xmn${HEAP_NEWSIZE}"
  177. JVM_OPTS="$JVM_OPTS -XX:+HeapDumpOnOutOfMemoryError"
  178.  
  179. # set jvm HeapDumpPath with CASSANDRA_HEAPDUMP_DIR
  180. if [ "x$CASSANDRA_HEAPDUMP_DIR" != "x" ]; then
  181.     JVM_OPTS="$JVM_OPTS -XX:HeapDumpPath=$CASSANDRA_HEAPDUMP_DIR/cassandra-`date +%s`-pid$$.hprof"
  182. fi
  183.  
  184.  
  185. startswith() { [ "${1#$2}" != "$1" ]; }
  186.  
  187. if [ "`uname`" = "Linux" ] ; then
  188.     # reduce the per-thread stack size to minimize the impact of Thrift
  189.     # thread-per-client.  (Best practice is for client connections to
  190.     # be pooled anyway.) Only do so on Linux where it is known to be
  191.     # supported.
  192.     # u34 and greater need 180k
  193.     JVM_OPTS="$JVM_OPTS -Xss256k"
  194. fi
  195. echo "xss = $JVM_OPTS"
  196.  
  197. # GC tuning options
  198. JVM_OPTS="$JVM_OPTS -XX:+UseParNewGC"
  199. JVM_OPTS="$JVM_OPTS -XX:+UseConcMarkSweepGC"
  200. JVM_OPTS="$JVM_OPTS -XX:+CMSParallelRemarkEnabled"
  201. JVM_OPTS="$JVM_OPTS -XX:SurvivorRatio=4"
  202. JVM_OPTS="$JVM_OPTS -XX:MaxTenuringThreshold=2"
  203. JVM_OPTS="$JVM_OPTS -XX:CMSInitiatingOccupancyFraction=75"
  204. JVM_OPTS="$JVM_OPTS -XX:+UseCMSInitiatingOccupancyOnly"
  205. JVM_OPTS="$JVM_OPTS -XX:MaxPermSize=128m"
  206. JVM_OPTS="$JVM_OPTS -XX:+UseTLAB"
  207. # note: bash evals '1.7.x' as > '1.7' so this is really a >= 1.7 jvm check
  208. if [ "$JVM_VERSION" \> "1.7" ] ; then
  209.     JVM_OPTS="$JVM_OPTS -XX:+UseCondCardMark"
  210. fi
  211.  
  212. # GC logging options -- uncomment to enable
  213. JVM_OPTS="$JVM_OPTS -XX:+PrintGCDetails"
  214. JVM_OPTS="$JVM_OPTS -XX:+PrintGCDateStamps"
  215. JVM_OPTS="$JVM_OPTS -XX:+PrintHeapAtGC"
  216. JVM_OPTS="$JVM_OPTS -XX:+PrintTenuringDistribution"
  217. JVM_OPTS="$JVM_OPTS -XX:+PrintGCApplicationStoppedTime"
  218. JVM_OPTS="$JVM_OPTS -XX:+PrintPromotionFailure"
  219. # JVM_OPTS="$JVM_OPTS -XX:PrintFLSStatistics=1"
  220. #JVM_OPTS="$JVM_OPTS -Xloggc:/var/log/cassandra/gc-`date +%s`.log"
  221. # If you are using JDK 6u34 7u2 or later you can enable GC log rotation
  222. # don't stick the date in the log name if rotation is on.
  223. JVM_OPTS="$JVM_OPTS -Xloggc:/opt/others/logs/cassandra/gc.log"
  224. JVM_OPTS="$JVM_OPTS -XX:+UseGCLogFileRotation"
  225. JVM_OPTS="$JVM_OPTS -XX:NumberOfGCLogFiles=10"
  226. JVM_OPTS="$JVM_OPTS -XX:GCLogFileSize=100M"
  227.  
  228. # uncomment to have Cassandra JVM listen for remote debuggers/profilers on port 1414
  229. # JVM_OPTS="$JVM_OPTS -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1414"
  230.  
  231. # Prefer binding to IPv4 network intefaces (when net.ipv6.bindv6only=1). See
  232. # http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6342561 (short version:
  233. # comment out this entry to enable IPv6 support).
  234. JVM_OPTS="$JVM_OPTS -Djava.net.preferIPv4Stack=true"
  235.  
  236. # jmx: metrics and administration interface
  237. #
  238. # add this if you're having trouble connecting:
  239. # JVM_OPTS="$JVM_OPTS -Djava.rmi.server.hostname=<public name>"
  240. #
  241. # see
  242. # https://blogs.oracle.com/jmxetc/entry/troubleshooting_connection_problems_in_jconsole
  243. # for more on configuring JMX through firewalls, etc. (Short version:
  244. # get it working with no firewall first.)
  245. JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
  246. JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.ssl=false"
  247. JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
  248. JVM_OPTS="$JVM_OPTS $JVM_EXTRA_OPTS"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement