utils.ts 13.6 KB
Newer Older
liuzhe-lz's avatar
liuzhe-lz committed
1
2
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
Deshui Yu's avatar
Deshui Yu committed
3
4
5

'use strict';

6
import * as assert from 'assert';
Deshui Yu's avatar
Deshui Yu committed
7
import { randomBytes } from 'crypto';
8
import * as cpp from 'child-process-promise';
9
10
import * as cp from 'child_process';
import { ChildProcess, spawn, StdioOptions } from 'child_process';
11
import * as dgram from 'dgram';
Deshui Yu's avatar
Deshui Yu committed
12
import * as fs from 'fs';
J-shang's avatar
J-shang committed
13
import * as net from 'net';
Deshui Yu's avatar
Deshui Yu committed
14
15
import * as os from 'os';
import * as path from 'path';
16
import * as lockfile from 'lockfile';
Deshui Yu's avatar
Deshui Yu committed
17
18
19
import { Deferred } from 'ts-deferred';
import { Container } from 'typescript-ioc';
import * as util from 'util';
20
import * as glob from 'glob';
Deshui Yu's avatar
Deshui Yu committed
21
22

import { Database, DataStore } from './datastore';
23
import { getExperimentStartupInfo, setExperimentStartupInfo } from './experimentStartupInfo';
24
import { ExperimentConfig, Manager } from './manager';
25
import { ExperimentManager } from './experimentManager';
QuanluZhang's avatar
QuanluZhang committed
26
import { HyperParameters, TrainingService, TrialJobStatus } from './trainingService';
Deshui Yu's avatar
Deshui Yu committed
27

28
function getExperimentRootDir(): string {
29
    return getExperimentStartupInfo().logDir;
Deshui Yu's avatar
Deshui Yu committed
30
31
}

32
function getLogDir(): string {
Deshui Yu's avatar
Deshui Yu committed
33
34
35
    return path.join(getExperimentRootDir(), 'log');
}

36
function getLogLevel(): string {
37
    return getExperimentStartupInfo().logLevel;
38
39
}

Deshui Yu's avatar
Deshui Yu committed
40
41
42
43
function getDefaultDatabaseDir(): string {
    return path.join(getExperimentRootDir(), 'db');
}

QuanluZhang's avatar
QuanluZhang committed
44
45
46
47
function getCheckpointDir(): string {
    return path.join(getExperimentRootDir(), 'checkpoint');
}

48
49
50
51
function getExperimentsInfoPath(): string {
    return path.join(os.homedir(), 'nni-experiments', '.experiment');
}

Deshui Yu's avatar
Deshui Yu committed
52
53
54
55
56
57
58
59
function mkDirP(dirPath: string): Promise<void> {
    const deferred: Deferred<void> = new Deferred<void>();
    fs.exists(dirPath, (exists: boolean) => {
        if (exists) {
            deferred.resolve();
        } else {
            const parent: string = path.dirname(dirPath);
            mkDirP(parent).then(() => {
60
                fs.mkdir(dirPath, (err: Error | null) => {
Deshui Yu's avatar
Deshui Yu committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
                    if (err) {
                        deferred.reject(err);
                    } else {
                        deferred.resolve();
                    }
                });
            }).catch((err: Error) => {
                deferred.reject(err);
            });
        }
    });

    return deferred.promise;
}

function mkDirPSync(dirPath: string): void {
    if (fs.existsSync(dirPath)) {
        return;
    }
    mkDirPSync(path.dirname(dirPath));
    fs.mkdirSync(dirPath);
}

const delay: (ms: number) => Promise<void> = util.promisify(setTimeout);

/**
 * Convert index to character
 * @param index index
 * @returns a mapping character
 */
function charMap(index: number): number {
    if (index < 26) {
        return index + 97;
    } else if (index < 52) {
        return index - 26 + 65;
    } else {
        return index - 52 + 48;
    }
}

/**
 * Generate a unique string by length
 * @param len length of string
 * @returns a unique string
 */
function uniqueString(len: number): string {
    if (len === 0) {
        return '';
    }
    const byteLength: number = Math.ceil((Math.log2(52) + Math.log2(62) * (len - 1)) / 8);
    let num: number = randomBytes(byteLength).reduce((a: number, b: number) => a * 256 + b, 0);
    const codes: number[] = [];
    codes.push(charMap(num % 52));
    num = Math.floor(num / 52);
    for (let i: number = 1; i < len; i++) {
        codes.push(charMap(num % 62));
        num = Math.floor(num / 62);
    }

    return String.fromCharCode(...codes);
}

123
124
125
126
function randomInt(max: number): number {
    return Math.floor(Math.random() * max);
}

127
128
129
130
131
function randomSelect<T>(a: T[]): T {
    assert(a !== undefined);

    return a[Math.floor(Math.random() * a.length)];
}
132

Deshui Yu's avatar
Deshui Yu committed
133
134
135
136
137
138
139
140
141
142
143
144
function parseArg(names: string[]): string {
    if (process.argv.length >= 4) {
        for (let i: number = 2; i < process.argv.length - 1; i++) {
            if (names.includes(process.argv[i])) {
                return process.argv[i + 1];
            }
        }
    }

    return '';
}

145
function getCmdPy(): string {
146
    let cmd = 'python3';
147
    if (process.platform === 'win32') {
148
149
150
151
152
        cmd = 'python';
    }
    return cmd;
}

153
/**
154
 * Generate command line to start automl algorithm(s),
QuanluZhang's avatar
QuanluZhang committed
155
 * either start advisor or start a process which runs tuner and assessor
156
 *
chicm-ms's avatar
chicm-ms committed
157
 * @param expParams: experiment startup parameters
158
159
 *
 */
160
function getMsgDispatcherCommand(expParams: ExperimentConfig): string {
chicm-ms's avatar
chicm-ms committed
161
162
163
    const clonedParams = Object.assign({}, expParams);
    delete clonedParams.searchSpace;
    return `${getCmdPy()} -m nni --exp_params ${Buffer.from(JSON.stringify(clonedParams)).toString('base64')}`;
164
165
}

166
167
168
169
/**
 * Generate parameter file name based on HyperParameters object
 * @param hyperParameters HyperParameters instance
 */
chicm-ms's avatar
chicm-ms committed
170
function generateParamFileName(hyperParameters: HyperParameters): string {
171
172
173
    assert(hyperParameters !== undefined);
    assert(hyperParameters.index >= 0);

chicm-ms's avatar
chicm-ms committed
174
    let paramFileName: string;
175
    if (hyperParameters.index == 0) {
176
177
178
179
180
181
182
        paramFileName = 'parameter.cfg';
    } else {
        paramFileName = `parameter_${hyperParameters.index}.cfg`
    }
    return paramFileName;
}

Deshui Yu's avatar
Deshui Yu committed
183
184
185
186
187
188
189
190
191
/**
 * Initialize a pseudo experiment environment for unit test.
 * Must be paired with `cleanupUnitTest()`.
 */
function prepareUnitTest(): void {
    Container.snapshot(Database);
    Container.snapshot(DataStore);
    Container.snapshot(TrainingService);
    Container.snapshot(Manager);
192
    Container.snapshot(ExperimentManager);
Deshui Yu's avatar
Deshui Yu committed
193

194
195
196
    const logLevel: string = parseArg(['--log_level', '-ll']);

    setExperimentStartupInfo(true, 'unittest', 8080, 'unittest', undefined, logLevel);
Deshui Yu's avatar
Deshui Yu committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    mkDirPSync(getLogDir());

    const sqliteFile: string = path.join(getDefaultDatabaseDir(), 'nni.sqlite');
    try {
        fs.unlinkSync(sqliteFile);
    } catch (err) {
        // file not exists, good
    }
}

/**
 * Clean up unit test pseudo experiment.
 * Must be paired with `prepareUnitTest()`.
 */
function cleanupUnitTest(): void {
    Container.restore(Manager);
    Container.restore(TrainingService);
    Container.restore(DataStore);
    Container.restore(Database);
216
    Container.restore(ExperimentManager);
217
218
    const logLevel: string = parseArg(['--log_level', '-ll']);
    setExperimentStartupInfo(true, 'unittest', 8080, 'unittest', undefined, logLevel);
Deshui Yu's avatar
Deshui Yu committed
219
220
}

221
222
let cachedIpv4Address: string | null = null;

223
/**
224
 * Get IPv4 address of current machine.
225
 */
liuzhe-lz's avatar
liuzhe-lz committed
226
async function getIPV4Address(): Promise<string> {
227
228
    if (cachedIpv4Address !== null) {
        return cachedIpv4Address;
229
    }
230

231
232
233
234
    // creates "udp connection" to a non-exist target, and get local address of the connection.
    // since udp is connectionless, this does not send actual packets.
    const socket = dgram.createSocket('udp4');
    socket.connect(1, '192.0.2.0');
liuzhe-lz's avatar
liuzhe-lz committed
235
236
237
238
239
    for (let i = 0; i < 10; i++) {  // wait the system to initialize "connection"
        await yield_();
        try { cachedIpv4Address = socket.address().address; } catch (error) { /* retry */ }
    }
    cachedIpv4Address = socket.address().address;  // if it still fails, throw the error
240
    socket.close();
241

242
    return cachedIpv4Address;
243
244
}

liuzhe-lz's avatar
liuzhe-lz committed
245
246
247
248
async function yield_(): Promise<void> {
    /* trigger the scheduler, do nothing */
}

QuanluZhang's avatar
QuanluZhang committed
249
250
251
252
253
254
255
/**
 * Get the status of canceled jobs according to the hint isEarlyStopped
 */
function getJobCancelStatus(isEarlyStopped: boolean): TrialJobStatus {
    return isEarlyStopped ? 'EARLY_STOPPED' : 'USER_CANCELED';
}

256
257
258
259
/**
 * Utility method to calculate file numbers under a directory, recursively
 * @param directory directory name
 */
chicm-ms's avatar
chicm-ms committed
260
function countFilesRecursively(directory: string): Promise<number> {
261
    if (!fs.existsSync(directory)) {
262
263
264
265
266
        throw Error(`Direcotory ${directory} doesn't exist`);
    }

    const deferred: Deferred<number> = new Deferred<number>();

chicm-ms's avatar
chicm-ms committed
267
268
    let timeoutId: NodeJS.Timer
    const delayTimeout: Promise<number> = new Promise((resolve: Function, reject: Function): void => {
269
270
271
272
273
274
275
        // Set timeout and reject the promise once reach timeout (5 seconds)
        timeoutId = setTimeout(() => {
            reject(new Error(`Timeout: path ${directory} has too many files`));
        }, 5000);
    });

    let fileCount: number = -1;
276
    let cmd: string;
277
    if (process.platform === "win32") {
278
279
        cmd = `powershell "Get-ChildItem -Path ${directory} -Recurse -File | Measure-Object | %{$_.Count}"`
    } else {
280
        cmd = `find ${directory} -type f | wc -l`;
281
282
    }
    cpp.exec(cmd).then((result) => {
283
        if (result.stdout && parseInt(result.stdout)) {
284
            fileCount = parseInt(result.stdout);
285
286
287
288
289
290
291
292
        }
        deferred.resolve(fileCount);
    });
    return Promise.race([deferred.promise, delayTimeout]).finally(() => {
        clearTimeout(timeoutId);
    });
}

293
294
295
296
/**
 * get the version of current package
 */
async function getVersion(): Promise<string> {
chicm-ms's avatar
chicm-ms committed
297
    const deferred: Deferred<string> = new Deferred<string>();
298
    import(path.join(__dirname, '..', 'package.json')).then((pkg) => {
299
        deferred.resolve(pkg.version);
300
301
    }).catch(() => {
        deferred.resolve('999.0.0-developing');
302
303
    });
    return deferred.promise;
304
}
305

306
307
308
/**
 * run command as ChildProcess
 */
J-shang's avatar
J-shang committed
309
function getTunerProc(command: string, stdio: StdioOptions, newCwd: string, newEnv: any, newShell: boolean = true, isDetached: boolean = false): ChildProcess {
310
311
    let cmd: string = command;
    let arg: string[] = [];
312
    if (process.platform === "win32") {
313
        cmd = command.split(" ", 1)[0];
314
        arg = command.substr(cmd.length + 1).split(" ");
315
        newShell = false;
316
        isDetached = true;
317
318
319
320
321
    }
    const tunerProc: ChildProcess = spawn(cmd, arg, {
        stdio,
        cwd: newCwd,
        env: newEnv,
322
323
        shell: newShell,
        detached: isDetached
324
325
326
327
328
329
330
    });
    return tunerProc;
}

/**
 * judge whether the process is alive
 */
Yuge Zhang's avatar
Yuge Zhang committed
331
async function isAlive(pid: any): Promise<boolean> {
chicm-ms's avatar
chicm-ms committed
332
    const deferred: Deferred<boolean> = new Deferred<boolean>();
333
    let alive: boolean = false;
Yuge Zhang's avatar
Yuge Zhang committed
334
    if (process.platform === 'win32') {
335
336
337
338
339
340
341
        try {
            const str = cp.execSync(`powershell.exe Get-Process -Id ${pid} -ErrorAction SilentlyContinue`).toString();
            if (str) {
                alive = true;
            }
        }
        catch (error) {
chicm-ms's avatar
chicm-ms committed
342
            //ignore
343
344
        }
    }
Yuge Zhang's avatar
Yuge Zhang committed
345
    else {
346
347
348
349
350
351
352
353
354
355
356
357
        try {
            await cpp.exec(`kill -0 ${pid}`);
            alive = true;
        } catch (error) {
            //ignore
        }
    }
    deferred.resolve(alive);
    return deferred.promise;
}

/**
358
 * kill process
359
 */
Yuge Zhang's avatar
Yuge Zhang committed
360
async function killPid(pid: any): Promise<void> {
chicm-ms's avatar
chicm-ms committed
361
    const deferred: Deferred<void> = new Deferred<void>();
362
363
    try {
        if (process.platform === "win32") {
Yuge Zhang's avatar
Yuge Zhang committed
364
            await cpp.exec(`cmd.exe /c taskkill /PID ${pid} /F`);
365
        }
366
        else {
367
368
369
370
371
372
373
374
375
            await cpp.exec(`kill -9 ${pid}`);
        }
    } catch (error) {
        // pid does not exist, do nothing here
    }
    deferred.resolve();
    return deferred.promise;
}

376
function getNewLine(): string {
377
378
379
    if (process.platform === "win32") {
        return "\r\n";
    }
380
    else {
381
382
383
384
        return "\n";
    }
}

385
386
/**
 * Use '/' to join path instead of '\' for all kinds of platform
387
 * @param path
388
389
390
391
392
393
394
 */
function unixPathJoin(...paths: any[]): string {
    const dir: string = paths.filter((path: any) => path !== '').join('/');
    if (dir === '') return '.';
    return dir;
}

395
396
397
398
399
400
401
402
403
/**
 * lock a file sync
 */
function withLockSync(func: Function, filePath: string, lockOpts: {[key: string]: any}, ...args: any): any {
    const lockName = path.join(path.dirname(filePath), path.basename(filePath) + `.lock.${process.pid}`);
    if (typeof lockOpts.stale === 'number'){
        const lockPath = path.join(path.dirname(filePath), path.basename(filePath) + '.lock.*');
        const lockFileNames: string[] = glob.sync(lockPath);
        const canLock: boolean = lockFileNames.map((fileName) => {
404
405
            return fs.existsSync(fileName) && Date.now() - fs.statSync(fileName).mtimeMs < lockOpts.stale;
        }).filter(unexpired=>unexpired === true).length === 0;
406
407
408
409
410
411
412
413
414
415
        if (!canLock) {
            throw new Error('File has been locked.');
        }
    }
    lockfile.lockSync(lockName, lockOpts);
    const result = func(...args);
    lockfile.unlockSync(lockName);
    return result;
}

J-shang's avatar
J-shang committed
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
async function isPortOpen(host: string, port: number): Promise<boolean> {
    return new Promise<boolean>((resolve, reject) => {
        try{
            const stream = net.createConnection(port, host);
            const id = setTimeout(() => {
                stream.destroy();
                resolve(false);
            }, 1000);

            stream.on('connect', () => {
                clearTimeout(id);
                stream.destroy();
                resolve(true);
            });

            stream.on('error', () => {
                clearTimeout(id);
                stream.destroy();
                resolve(false);
            });
        } catch (error) {
            reject(error);
        }
    });
}

async function getFreePort(host: string, start: number, end: number): Promise<number> {
    if (start > end) {
        throw new Error(`no more free port`);
    }
    if (await isPortOpen(host, start)) {
        return await getFreePort(host, start + 1, end);
    } else {
        return start;
    }
}

453
454
455
456
457
export function importModule(modulePath: string): any {
    module.paths.unshift(path.dirname(modulePath));
    return require(path.basename(modulePath));
}

458
export {
459
    countFilesRecursively, generateParamFileName, getMsgDispatcherCommand, getCheckpointDir, getExperimentsInfoPath,
J-shang's avatar
J-shang committed
460
    getLogDir, getExperimentRootDir, getJobCancelStatus, getDefaultDatabaseDir, getIPV4Address, unixPathJoin, withLockSync, getFreePort, isPortOpen,
461
462
    mkDirP, mkDirPSync, delay, prepareUnitTest, parseArg, cleanupUnitTest, uniqueString, randomInt, randomSelect, getLogLevel, getVersion, getCmdPy, getTunerProc, isAlive, killPid, getNewLine
};