memory.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python
  2. '''
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  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. import optparse
  18. from pprint import pprint
  19. import logging
  20. import sys
  21. import math
  22. import ast
  23. ''' Reserved for OS + DN + NM, Map: Memory => Reservation '''
  24. reservedStack = { 4:1, 8:2, 16:2, 24:4, 48:6, 64:8, 72:8, 96:12,
  25. 128:24, 256:32, 512:64}
  26. ''' Reserved for HBase. Map: Memory => Reservation '''
  27. reservedHBase = {4:1, 8:1, 16:2, 24:4, 48:8, 64:8, 72:8, 96:16,
  28. 128:24, 256:32, 512:64}
  29. GB = 1024
  30. def getMinContainerSize(memory):
  31. if (memory <= 4):
  32. return 256
  33. elif (memory <= 8):
  34. return 512
  35. elif (memory <= 24):
  36. return 1024
  37. else:
  38. return 2048
  39. pass
  40. def getReservedStackMemory(memory):
  41. if (reservedStack.has_key(memory)):
  42. return reservedStack[memory]
  43. if (memory <= 4):
  44. ret = 1
  45. elif (memory >= 512):
  46. ret = 64
  47. else:
  48. ret = 1
  49. return ret
  50. def getReservedHBaseMem(memory):
  51. if (reservedHBase.has_key(memory)):
  52. return reservedHBase[memory]
  53. if (memory <= 4):
  54. ret = 1
  55. elif (memory >= 512):
  56. ret = 64
  57. else:
  58. ret = 2
  59. return ret
  60. def main():
  61. log = logging.getLogger(__name__)
  62. out_hdlr = logging.StreamHandler(sys.stdout)
  63. out_hdlr.setFormatter(logging.Formatter(' %(message)s'))
  64. out_hdlr.setLevel(logging.INFO)
  65. log.addHandler(out_hdlr)
  66. log.setLevel(logging.INFO)
  67. parser = optparse.OptionParser()
  68. memory = 0
  69. cores = 0
  70. disks = 0
  71. hbaseEnabled = True
  72. parser.add_option('-c', '--cores', default = 16,
  73. help = 'Number of cores on each host')
  74. parser.add_option('-m', '--memory', default = 64,
  75. help = 'Amount of Memory on each host in GB')
  76. parser.add_option('-d', '--disks', default = 4,
  77. help = 'Number of disks on each host')
  78. parser.add_option('-k', '--hbase', default = "True",
  79. help = 'True if HBase is installed, False is not')
  80. (options, args) = parser.parse_args()
  81. cores = int (options.cores)
  82. memory = int (options.memory)
  83. disks = int (options.disks)
  84. hbaseEnabled = ast.literal_eval(options.hbase)
  85. log.info("Using cores=" + str(cores) + " memory=" + str(memory) + "GB" +
  86. " disks=" + str(disks) + " hbase=" + str(hbaseEnabled))
  87. minContainerSize = getMinContainerSize(memory)
  88. reservedStackMemory = getReservedStackMemory(memory)
  89. reservedHBaseMemory = 0
  90. if (hbaseEnabled):
  91. reservedHBaseMemory = getReservedHBaseMem(memory)
  92. reservedMem = reservedStackMemory + reservedHBaseMemory
  93. usableMem = memory - reservedMem
  94. memory -= (reservedMem)
  95. if (memory < 2):
  96. memory = 2
  97. reservedMem = max(0, memory - reservedMem)
  98. memory *= GB
  99. containers = int (min(2 * cores,
  100. min(math.ceil(1.8 * float(disks)),
  101. memory/minContainerSize)))
  102. if (containers <= 2):
  103. containers = 3
  104. log.info("Profile: cores=" + str(cores) + " memory=" + str(memory) + "MB"
  105. + " reserved=" + str(reservedMem) + "GB" + " usableMem="
  106. + str(usableMem) + "GB" + " disks=" + str(disks))
  107. container_ram = abs(memory/containers)
  108. if (container_ram > GB):
  109. container_ram = int(math.floor(container_ram / 512)) * 512
  110. log.info("Num Container=" + str(containers))
  111. log.info("Container Ram=" + str(container_ram) + "MB")
  112. log.info("Used Ram=" + str(int (containers*container_ram/float(GB))) + "GB")
  113. log.info("Unused Ram=" + str(reservedMem) + "GB")
  114. log.info("yarn.scheduler.minimum-allocation-mb=" + str(container_ram))
  115. log.info("yarn.scheduler.maximum-allocation-mb=" + str(containers*container_ram))
  116. log.info("yarn.nodemanager.resource.memory-mb=" + str(containers*container_ram))
  117. map_memory = container_ram
  118. reduce_memory = 2*container_ram if (container_ram <= 2048) else container_ram
  119. am_memory = max(map_memory, reduce_memory)
  120. log.info("mapreduce.map.memory.mb=" + str(map_memory))
  121. log.info("mapreduce.map.java.opts=-Xmx" + str(int(0.8 * map_memory)) +"m")
  122. log.info("mapreduce.reduce.memory.mb=" + str(reduce_memory))
  123. log.info("mapreduce.reduce.java.opts=-Xmx" + str(int(0.8 * reduce_memory)) + "m")
  124. log.info("yarn.app.mapreduce.am.resource.mb=" + str(am_memory))
  125. log.info("yarn.app.mapreduce.am.command-opts=-Xmx" + str(int(0.8*am_memory)) + "m")
  126. log.info("mapreduce.task.io.sort.mb=" + str(int(0.4 * map_memory)))
  127. pass
  128. if __name__ == '__main__':
  129. try:
  130. main()
  131. except(KeyboardInterrupt, EOFError):
  132. print("\nAborting ... Keyboard Interrupt.")
  133. sys.exit(1)