nnictl_utils.py 8.12 KB
Newer Older
Deshui Yu's avatar
Deshui Yu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Copyright (c) Microsoft Corporation
# All rights reserved.
#
# MIT License
#
# Permission is hereby granted, free of charge,
# to any person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and
# to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
# BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import os
import psutil
import json
24
import datetime
Deshui Yu's avatar
Deshui Yu committed
25
from subprocess import call, check_output
26
from .rest_utils import rest_get, rest_delete, check_rest_server_quick, check_response
Deshui Yu's avatar
Deshui Yu committed
27
28
from .config_utils import Config
from .url_utils import trial_jobs_url, experiment_url, trial_job_id_url
goooxu's avatar
goooxu committed
29
from .constants import HOME_DIR
Deshui Yu's avatar
Deshui Yu committed
30
31
32
import time
from .common_utils import print_normal, print_error, detect_process

33
34
35
36
37
38
39
40
41
42
43
44
def convert_time_stamp_to_date(content):
    '''Convert time stamp to date time format'''
    start_time_stamp = content.get('startTime')
    end_time_stamp = content.get('endTime')
    if start_time_stamp:
        start_time = datetime.datetime.utcfromtimestamp(start_time_stamp // 1000).strftime("%Y/%m/%d %H:%M:%S")
        content['startTime'] = str(start_time)
    if end_time_stamp:
        end_time = datetime.datetime.utcfromtimestamp(end_time_stamp // 1000).strftime("%Y/%m/%d %H:%M:%S")
        content['endTime'] = str(end_time)
    return content

Deshui Yu's avatar
Deshui Yu committed
45
46
def check_rest(args):
    '''check if restful server is running'''
goooxu's avatar
goooxu committed
47
    nni_config = Config(args.port)
Deshui Yu's avatar
Deshui Yu committed
48
    rest_port = nni_config.get_config('restServerPort')
49
50
    running, _ = check_rest_server_quick(rest_port)
    if not running:
Deshui Yu's avatar
Deshui Yu committed
51
52
53
54
55
56
57
        print_normal('Restful server is running...')
    else:
        print_normal('Restful server is not running...')

def stop_experiment(args):
    '''Stop the experiment which is running'''
    print_normal('Stoping experiment...')
goooxu's avatar
goooxu committed
58
    nni_config = Config(args.port)
Deshui Yu's avatar
Deshui Yu committed
59
60
61
62
63
    rest_port = nni_config.get_config('restServerPort')
    rest_pid = nni_config.get_config('restServerPid')
    if not detect_process(rest_pid):
        print_normal('Experiment is not running...')
        return
64
    running, _ = check_rest_server_quick(rest_port)
65
    stop_rest_result = True
66
    if running:
Deshui Yu's avatar
Deshui Yu committed
67
        response = rest_delete(experiment_url(rest_port), 20)
68
        if not response or not check_response(response):
Deshui Yu's avatar
Deshui Yu committed
69
            print_error('Stop experiment failed!')
70
            stop_rest_result = False
Deshui Yu's avatar
Deshui Yu committed
71
72
73
74
75
    #sleep to wait rest handler done
    time.sleep(3)
    rest_pid = nni_config.get_config('restServerPid')
    cmds = ['pkill', '-P', str(rest_pid)]
    call(cmds)
76
77
    if stop_rest_result:
        print_normal('Stop experiment success!')
Deshui Yu's avatar
Deshui Yu committed
78
79
80

def trial_ls(args):
    '''List trial'''
goooxu's avatar
goooxu committed
81
    nni_config = Config(args.port)
Deshui Yu's avatar
Deshui Yu committed
82
83
84
85
86
    rest_port = nni_config.get_config('restServerPort')
    rest_pid = nni_config.get_config('restServerPid')
    if not detect_process(rest_pid):
        print_error('Experiment is not running...')
        return
87
88
    running, response = check_rest_server_quick(rest_port)
    if running:
Deshui Yu's avatar
Deshui Yu committed
89
        response = rest_get(trial_jobs_url(rest_port), 20)
90
        if response and check_response(response):
91
92
93
94
            content = json.loads(response.text)
            for index, value in enumerate(content):               
                content[index] = convert_time_stamp_to_date(value)
            print(json.dumps(content, indent=4, sort_keys=True, separators=(',', ':')))
Deshui Yu's avatar
Deshui Yu committed
95
96
97
98
99
100
101
        else:
            print_error('List trial failed...')
    else:
        print_error('Restful server is not running...')

def trial_kill(args):
    '''List trial'''
goooxu's avatar
goooxu committed
102
    nni_config = Config(args.port)
Deshui Yu's avatar
Deshui Yu committed
103
104
105
106
107
    rest_port = nni_config.get_config('restServerPort')
    rest_pid = nni_config.get_config('restServerPid')
    if not detect_process(rest_pid):
        print_error('Experiment is not running...')
        return
108
109
    running, _ = check_rest_server_quick(rest_port)
    if running:
Deshui Yu's avatar
Deshui Yu committed
110
        response = rest_delete(trial_job_id_url(rest_port, args.trialid), 20)
111
        if response and check_response(response):
Deshui Yu's avatar
Deshui Yu committed
112
113
114
115
116
117
118
119
            print(response.text)
        else:
            print_error('Kill trial job failed...')
    else:
        print_error('Restful server is not running...')

def list_experiment(args):
    '''Get experiment information'''
goooxu's avatar
goooxu committed
120
    nni_config = Config(args.port)
Deshui Yu's avatar
Deshui Yu committed
121
122
123
124
125
    rest_port = nni_config.get_config('restServerPort')
    rest_pid = nni_config.get_config('restServerPid')
    if not detect_process(rest_pid):
        print_error('Experiment is not running...')
        return
126
127
    running, _ = check_rest_server_quick(rest_port)
    if running:
Deshui Yu's avatar
Deshui Yu committed
128
        response = rest_get(experiment_url(rest_port), 20)
129
        if response and check_response(response):
130
131
            content = convert_time_stamp_to_date(json.loads(response.text))
            print(json.dumps(content, indent=4, sort_keys=True, separators=(',', ':')))
Deshui Yu's avatar
Deshui Yu committed
132
133
134
135
136
        else:
            print_error('List experiment failed...')
    else:
        print_error('Restful server is not running...')

137
138
def experiment_status(args):
    '''Show the status of experiment'''
goooxu's avatar
goooxu committed
139
    nni_config = Config(args.port)
140
141
142
143
144
145
146
    rest_port = nni_config.get_config('restServerPort')
    result, response = check_rest_server_quick(rest_port)
    if not result:
        print_normal('Restful server is not running...')
    else:
        print(json.dumps(json.loads(response.text), indent=4, sort_keys=True, separators=(',', ':')))

Deshui Yu's avatar
Deshui Yu committed
147
148
149
150
151
152
153
154
155
156
157
def get_log_content(file_name, cmds):
    '''use cmds to read config content'''
    if os.path.exists(file_name):
        rest = check_output(cmds)
        print(rest.decode('utf-8'))
    else:
        print_normal('NULL!')

def log_internal(args, filetype):
    '''internal function to call get_log_content'''
    if filetype == 'stdout':
goooxu's avatar
goooxu committed
158
        file_full_path = os.path.join(HOME_DIR, args.port, 'stdout')
Deshui Yu's avatar
Deshui Yu committed
159
    else:
goooxu's avatar
goooxu committed
160
        file_full_path = os.path.join(HOME_DIR, args.port, 'stderr')
Deshui Yu's avatar
Deshui Yu committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    if args.head:
        get_log_content(file_full_path, ['head', '-' + str(args.head), file_full_path])
    elif args.tail:
        get_log_content(file_full_path, ['tail', '-' + str(args.tail), file_full_path])
    elif args.path:
        print_normal('The path of stdout file is: ' + file_full_path)
    else:
        get_log_content(file_full_path, ['cat', file_full_path])

def log_stdout(args):
    '''get stdout log'''
    log_internal(args, 'stdout')

def log_stderr(args):
    '''get stderr log'''
    log_internal(args, 'stderr')

178
179
180
def log_trial(args):
    ''''get trial log path'''
    trial_id_path_dict = {}
goooxu's avatar
goooxu committed
181
    nni_config = Config(args.port)
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    rest_port = nni_config.get_config('restServerPort')
    rest_pid = nni_config.get_config('restServerPid')
    if not detect_process(rest_pid):
        print_error('Experiment is not running...')
        return
    running, response = check_rest_server_quick(rest_port)
    if running:
        response = rest_get(trial_jobs_url(rest_port), 20)
        if response and check_response(response):
            content = json.loads(response.text)
            for trial in content:
                trial_id_path_dict[trial['id']] = trial['logPath']
    else:
        print_error('Restful server is not running...')
goooxu's avatar
goooxu committed
196
        exit(1)
197
198
199
200
201
    if args.id:
        if trial_id_path_dict.get(args.id):
            print('id:' + args.id + ' path:' + trial_id_path_dict[args.id])
        else:
            print_error('trial id is not valid!')
goooxu's avatar
goooxu committed
202
            exit(1)
203
204
205
206
    else:
        for key in trial_id_path_dict.keys():
            print('id:' + key + ' path:' + trial_id_path_dict[key])

Deshui Yu's avatar
Deshui Yu committed
207
208
def get_config(args):
    '''get config info'''
goooxu's avatar
goooxu committed
209
    nni_config = Config(args.port)
Deshui Yu's avatar
Deshui Yu committed
210
    print(nni_config.get_all_config())