/pypy/rpython/memory/gc/test/test_env.py

https://github.com/nicolaslara/pypy · Python · 163 lines · 141 code · 9 blank · 13 comment · 4 complexity · 2c551e08eab704e6d961a32c2f7762cb MD5 · raw file

  1. import os, py
  2. from pypy.rpython.memory.gc import env
  3. from pypy.rlib.rarithmetic import r_uint
  4. from pypy.tool.udir import udir
  5. class FakeEnviron:
  6. def __init__(self, value):
  7. self._value = value
  8. def get(self, varname):
  9. assert varname == 'FOOBAR'
  10. return self._value
  11. def check_equal(x, y):
  12. assert x == y
  13. assert type(x) == type(y)
  14. def test_get_total_memory_darwin():
  15. # this only tests clipping
  16. BIG = 2 * env.addressable_size
  17. SMALL = env.addressable_size / 2
  18. assert env.addressable_size == env.get_total_memory_darwin(0)
  19. assert env.addressable_size == env.get_total_memory_darwin(-1)
  20. assert env.addressable_size == env.get_total_memory_darwin(BIG)
  21. assert SMALL == env.get_total_memory_darwin(SMALL)
  22. def test_get_total_memory():
  23. # total memory should be at least a megabyte
  24. assert env.get_total_memory() > 1024*1024
  25. def test_read_from_env():
  26. saved = os.environ
  27. try:
  28. os.environ = FakeEnviron(None)
  29. check_equal(env.read_from_env('FOOBAR'), 0)
  30. check_equal(env.read_uint_from_env('FOOBAR'), r_uint(0))
  31. check_equal(env.read_float_from_env('FOOBAR'), 0.0)
  32. #
  33. os.environ = FakeEnviron('')
  34. check_equal(env.read_from_env('FOOBAR'), 0)
  35. check_equal(env.read_uint_from_env('FOOBAR'), r_uint(0))
  36. check_equal(env.read_float_from_env('FOOBAR'), 0.0)
  37. #
  38. os.environ = FakeEnviron('???')
  39. check_equal(env.read_from_env('FOOBAR'), 0)
  40. check_equal(env.read_uint_from_env('FOOBAR'), r_uint(0))
  41. check_equal(env.read_float_from_env('FOOBAR'), 0.0)
  42. #
  43. os.environ = FakeEnviron('1')
  44. check_equal(env.read_from_env('FOOBAR'), 1)
  45. check_equal(env.read_uint_from_env('FOOBAR'), r_uint(1))
  46. check_equal(env.read_float_from_env('FOOBAR'), 1.0)
  47. #
  48. os.environ = FakeEnviron('12345678')
  49. check_equal(env.read_from_env('FOOBAR'), 12345678)
  50. check_equal(env.read_uint_from_env('FOOBAR'), r_uint(12345678))
  51. check_equal(env.read_float_from_env('FOOBAR'), 12345678.0)
  52. #
  53. os.environ = FakeEnviron('1234B')
  54. check_equal(env.read_from_env('FOOBAR'), 1234)
  55. check_equal(env.read_uint_from_env('FOOBAR'), r_uint(1234))
  56. check_equal(env.read_float_from_env('FOOBAR'), 1234.0)
  57. #
  58. os.environ = FakeEnviron('1.5')
  59. check_equal(env.read_float_from_env('FOOBAR'), 1.5)
  60. #
  61. os.environ = FakeEnviron('1.5Kb')
  62. check_equal(env.read_from_env('FOOBAR'), 1536)
  63. check_equal(env.read_uint_from_env('FOOBAR'), r_uint(1536))
  64. check_equal(env.read_float_from_env('FOOBAR'), 0.0)
  65. #
  66. os.environ = FakeEnviron('1.5mB')
  67. check_equal(env.read_from_env('FOOBAR'), int(1.5*1024*1024))
  68. check_equal(env.read_uint_from_env('FOOBAR'), r_uint(1.5*1024*1024))
  69. check_equal(env.read_float_from_env('FOOBAR'), 0.0)
  70. #
  71. os.environ = FakeEnviron('1.5g')
  72. check_equal(env.read_from_env('FOOBAR'), int(1.5*1024*1024*1024))
  73. check_equal(env.read_uint_from_env('FOOBAR'), r_uint(1.5*1024*1024*1024))
  74. check_equal(env.read_float_from_env('FOOBAR'), 0.0)
  75. #
  76. finally:
  77. os.environ = saved
  78. def test_get_total_memory_linux2():
  79. filepath = udir.join('get_total_memory_linux2')
  80. filepath.write("""\
  81. MemTotal: 1976804 kB
  82. MemFree: 32200 kB
  83. Buffers: 144092 kB
  84. Cached: 1385196 kB
  85. SwapCached: 8408 kB
  86. Active: 1181436 kB
  87. etc.
  88. """)
  89. result = env.get_total_memory_linux2(str(filepath))
  90. assert result == 1976804 * 1024
  91. def test_get_total_memory_linux2_32bit_limit():
  92. filepath = udir.join('get_total_memory_linux2')
  93. filepath.write("""\
  94. MemTotal: 3145728 kB
  95. etc.
  96. """)
  97. saved = env.addressable_size
  98. try:
  99. env.addressable_size = float(2**31)
  100. result = env.get_total_memory_linux2(str(filepath))
  101. check_equal(result, float(2**31)) # limit hit
  102. #
  103. env.addressable_size = float(2**32)
  104. result = env.get_total_memory_linux2(str(filepath))
  105. check_equal(result, float(3145728 * 1024)) # limit not hit
  106. finally:
  107. env.addressable_size = saved
  108. def test_estimate_best_nursery_size_linux2():
  109. filepath = udir.join('estimate_best_nursery_size_linux2')
  110. filepath.write("""\
  111. processor : 0
  112. vendor_id : GenuineIntel
  113. cpu family : 6
  114. model : 37
  115. model name : Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz
  116. stepping : 5
  117. cpu MHz : 1199.000
  118. cache size : 3072 KB
  119. physical id : 0
  120. siblings : 4
  121. core id : 0
  122. cpu cores : 2
  123. apicid : 0
  124. initial apicid : 0
  125. fpu : yes
  126. fpu_exception : yes
  127. cpuid level : 11
  128. wp : yes
  129. flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 popcnt aes lahf_lm ida arat tpr_shadow vnmi flexpriority ept vpid
  130. bogomips : 5054.78
  131. clflush size : 64
  132. cache_alignment : 64
  133. address sizes : 36 bits physical, 48 bits virtual
  134. power management:
  135. processor : 1
  136. vendor_id : GenuineIntel
  137. cpu family : 6
  138. model : 37
  139. model name : Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz
  140. stepping : 5
  141. cpu MHz : 2534.000
  142. cache size : 3072 KB
  143. physical id : 0
  144. siblings : 4
  145. core id : 0
  146. cpu cores : 2
  147. apicid : 1
  148. initial apicid : 1
  149. fpu : yes
  150. etc.
  151. """)
  152. result = env.get_L2cache_linux2(str(filepath))
  153. assert result == 3072 * 1024