10 Detects the number of CPUs on a system. Cribbed from pp.
12 # Linux, Unix and MacOS:
13 if hasattr(os, "sysconf"):
14 if "SC_NPROCESSORS_ONLN" in os.sysconf_names:
16 ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
17 if isinstance(ncpus, int) and ncpus > 0:
20 return int(capture(['sysctl', '-n', 'hw.ncpu']))
22 if "NUMBER_OF_PROCESSORS" in os.environ:
23 ncpus = int(os.environ["NUMBER_OF_PROCESSORS"])
29 """mkdir_p(path) - Make the "path" directory, if it does not exist; this
30 will also make directories for any missing parent directories."""
31 if not path or os.path.exists(path):
34 parent = os.path.dirname(path)
42 # Ignore EEXIST, which may occur during a race condition.
43 if e.errno != errno.EEXIST:
46 def capture(args, env=None):
47 """capture(command) - Run the given command (or argv list) in a shell and
48 return the standard output."""
49 p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
51 out,_ = p.communicate()
54 def which(command, paths = None):
55 """which(command, [paths]) - Look up the given command in the paths string
56 (or the PATH environment variable, if unspecified)."""
59 paths = os.environ.get('PATH','')
61 # Check for absolute match first.
62 if os.path.isfile(command):
65 # Would be nice if Python had a lib function for this.
69 # Get suffixes to search.
70 # On Cygwin, 'PATHEXT' may exist but it should not be used.
72 pathext = os.environ.get('PATHEXT', '').split(';')
77 for path in paths.split(os.pathsep):
79 p = os.path.join(path, command + ext)
85 def checkToolsPath(dir, tools):
87 if not os.path.exists(os.path.join(dir, tool)):
91 def whichTools(tools, paths):
92 for path in paths.split(os.pathsep):
93 if checkToolsPath(path, tools):
97 def printHistogram(items, title = 'Items'):
98 items.sort(key = lambda item: item[1])
100 maxValue = max([v for _,v in items])
102 # Select first "nice" bar height that produces more than 10 bars.
103 power = int(math.ceil(math.log(maxValue, 10)))
104 for inc in itertools.cycle((5, 2, 2.5, 1)):
105 barH = inc * 10**power
106 N = int(math.ceil(maxValue / barH))
112 histo = [set() for i in range(N)]
114 bin = min(int(N * v/maxValue), N-1)
118 hr = '-' * (barW + 34)
119 print('\nSlowest %s:' % title)
121 for name,value in items[-20:]:
122 print('%.2fs: %s' % (value, name))
123 print('\n%s Times:' % title)
125 pDigits = int(math.ceil(math.log(maxValue, 10)))
126 pfDigits = max(0, 3-pDigits)
128 pDigits += pfDigits + 1
129 cDigits = int(math.ceil(math.log(len(items), 10)))
130 print("[%s] :: [%s] :: [%s]" % ('Range'.center((pDigits+1)*2 + 3),
131 'Percentage'.center(barW),
132 'Count'.center(cDigits*2 + 1)))
134 for i,row in enumerate(histo):
135 pct = float(len(row)) / len(items)
137 print("[%*.*fs,%*.*fs) :: [%s%s] :: [%*d/%*d]" % (
138 pDigits, pfDigits, i*barH, pDigits, pfDigits, (i+1)*barH,
139 '*'*w, ' '*(barW-w), cDigits, len(row), cDigits, len(items)))