Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit 5122e84

Browse files
authored
add back diagnose script (#1551)
Signed-off-by: Sheng Zha <[email protected]>
1 parent 59167e1 commit 5122e84

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed

tools/diagnose.py

+196
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
"""Diagnose script for checking OS/hardware/python/pip/mxnet/network.
21+
The output of this script can be a very good hint to issue/problem.
22+
"""
23+
import platform, subprocess, sys, os
24+
import socket, time
25+
try:
26+
from urllib.request import urlopen
27+
from urllib.parse import urlparse
28+
except ImportError:
29+
from urlparse import urlparse
30+
from urllib2 import urlopen
31+
import argparse
32+
33+
def parse_args():
34+
"""Parse arguments."""
35+
parser = argparse.ArgumentParser(
36+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
37+
description='Diagnose script for checking the current system.')
38+
choices = ['python', 'pip', 'gluonnlp', 'mxnet', 'os', 'hardware', 'network']
39+
for choice in choices:
40+
parser.add_argument('--' + choice, default=1, type=int,
41+
help='Diagnose {}.'.format(choice))
42+
parser.add_argument('--region', default='', type=str,
43+
help="Additional sites in which region(s) to test. \
44+
Specify 'cn' for example to test mirror sites in China.")
45+
parser.add_argument('--timeout', default=10, type=int,
46+
help="Connection test timeout threshold, 0 to disable.")
47+
args = parser.parse_args()
48+
return args
49+
50+
URLS = {
51+
'MXNet': 'https://github.com/apache/incubator-mxnet',
52+
'GluonNLP GitHub': 'https://github.com/dmlc/gluon-nlp',
53+
'GluonNLP': 'http://gluon-nlp.mxnet.io',
54+
'D2L': 'http://d2l.ai',
55+
'D2L (zh-cn)': 'http://zh.d2l.ai',
56+
'FashionMNIST': 'https://repo.mxnet.io/gluon/dataset/fashion-mnist/train-labels-idx1-ubyte.gz',
57+
'PYPI': 'https://pypi.python.org/pypi/pip',
58+
'Conda': 'https://repo.continuum.io/pkgs/free/',
59+
}
60+
REGIONAL_URLS = {
61+
'cn': {
62+
'PYPI(douban)': 'https://pypi.douban.com/',
63+
'Conda(tsinghua)': 'https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/',
64+
}
65+
}
66+
67+
def test_connection(name, url, timeout=10):
68+
"""Simple connection test"""
69+
urlinfo = urlparse(url)
70+
start = time.time()
71+
try:
72+
ip = socket.gethostbyname(urlinfo.netloc)
73+
except Exception as e:
74+
print('Error resolving DNS for {}: {}, {}'.format(name, url, e))
75+
return
76+
dns_elapsed = time.time() - start
77+
start = time.time()
78+
try:
79+
_ = urlopen(url, timeout=timeout)
80+
except Exception as e:
81+
print("Error open {}: {}, {}, DNS finished in {} sec.".format(name, url, e, dns_elapsed))
82+
return
83+
load_elapsed = time.time() - start
84+
print("Timing for {}: {}, DNS: {:.4f} sec, LOAD: {:.4f} sec.".format(name, url, dns_elapsed, load_elapsed))
85+
86+
def check_python():
87+
print('----------Python Info----------')
88+
print('Version :', platform.python_version())
89+
print('Compiler :', platform.python_compiler())
90+
print('Build :', platform.python_build())
91+
print('Arch :', platform.architecture())
92+
93+
def check_pip():
94+
print('------------Pip Info-----------')
95+
try:
96+
import pip
97+
print('Version :', pip.__version__)
98+
print('Directory :', os.path.dirname(pip.__file__))
99+
except ImportError:
100+
print('No corresponding pip install for current python.')
101+
102+
def check_gluonnlp():
103+
print('----------GluonNLP Info-----------')
104+
try:
105+
import gluonnlp
106+
print('Version :', gluonnlp.__version__)
107+
except ImportError:
108+
print('No GluonNLP installed.')
109+
except Exception as e:
110+
import traceback
111+
if not isinstance(e, IOError):
112+
print("An error occured trying to import gluonnlp.")
113+
print(traceback.format_exc())
114+
115+
def check_mxnet():
116+
print('----------MXNet Info-----------')
117+
try:
118+
import mxnet
119+
print('Version :', mxnet.__version__)
120+
mx_dir = os.path.dirname(mxnet.__file__)
121+
print('Directory :', mx_dir)
122+
print('Num GPUs :', mxnet.util.get_gpu_count())
123+
commit_hash = os.path.join(mx_dir, 'COMMIT_HASH')
124+
with open(commit_hash, 'r') as f:
125+
ch = f.read().strip()
126+
print('Commit Hash :', ch)
127+
except ImportError:
128+
print('No MXNet installed.')
129+
except IOError:
130+
print('Hashtag not found. Not installed from pre-built package.')
131+
except Exception as e:
132+
import traceback
133+
if not isinstance(e, IOError):
134+
print("An error occured trying to import mxnet.")
135+
print("This is very likely due to missing missing or incompatible library files.")
136+
print(traceback.format_exc())
137+
138+
def check_os():
139+
print('----------System Info----------')
140+
print('Platform :', platform.platform())
141+
print('system :', platform.system())
142+
print('node :', platform.node())
143+
print('release :', platform.release())
144+
print('version :', platform.version())
145+
146+
def check_hardware():
147+
print('----------Hardware Info----------')
148+
print('machine :', platform.machine())
149+
print('processor :', platform.processor())
150+
if sys.platform.startswith('darwin'):
151+
pipe = subprocess.Popen(('sysctl', '-a'), stdout=subprocess.PIPE)
152+
output = pipe.communicate()[0]
153+
for line in output.split(b'\n'):
154+
if b'brand_string' in line or b'features' in line:
155+
print(line.strip())
156+
elif sys.platform.startswith('linux'):
157+
subprocess.call(['lscpu'])
158+
elif sys.platform.startswith('win32'):
159+
subprocess.call(['wmic', 'cpu', 'get', 'name'])
160+
161+
def check_network(args):
162+
print('----------Network Test----------')
163+
if args.timeout > 0:
164+
print('Setting timeout: {}'.format(args.timeout))
165+
socket.setdefaulttimeout(10)
166+
for region in args.region.strip().split(','):
167+
r = region.strip().lower()
168+
if not r:
169+
continue
170+
if r in REGIONAL_URLS:
171+
URLS.update(REGIONAL_URLS[r])
172+
else:
173+
import warnings
174+
warnings.warn('Region {} do not need specific test, please refer to global sites.'.format(r))
175+
for name, url in URLS.items():
176+
test_connection(name, url, args.timeout)
177+
178+
if __name__ == '__main__':
179+
args = parse_args()
180+
if args.python:
181+
check_python()
182+
183+
if args.pip:
184+
check_pip()
185+
186+
if args.mxnet:
187+
check_mxnet()
188+
189+
if args.os:
190+
check_os()
191+
192+
if args.hardware:
193+
check_hardware()
194+
195+
if args.network:
196+
check_network(args)

0 commit comments

Comments
 (0)