forked from u-root/u-root
/
time.go
70 lines (65 loc) · 1.55 KB
/
time.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2012 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Time process execution.
//
// Synopsis:
// time CMD [ARG]...
//
// Description:
// After executing CMD, its real, user and system times are printed to
// stderr in the POSIX format.
//
// CMD can be a builtin, e.g. time cd
//
// Example:
// $ time sleep 1.23s
// real 1.230
// user 0.001
// sys 0.000
//
// Note:
// This is different from bash's time command which is built into the shell
// and can time the entire pipeline.
//
// Bugs:
// Time is not reported when exiting due to a signal.
package main
import (
"fmt"
"os"
"os/exec"
"time"
)
func init() {
addBuiltIn("time", runtime)
}
func printTime(label string, t time.Duration) {
fmt.Fprintf(os.Stderr, "%s %.03f\n", label, t.Seconds())
}
func runtime(c *Command) error {
var err error
start := time.Now()
if len(c.argv) > 0 {
c.cmd = c.argv[0]
c.argv = c.argv[1:]
c.args = c.args[1:]
// If we are in a builtin, then the lookup failed.
// The result of the failed lookup remains in
// c.Cmd and will make start fail. We have to make
// a new Cmd.
nCmd := exec.Command(c.cmd, c.argv[:]...)
nCmd.Stdin = c.Stdin
nCmd.Stdout = c.Stdout
nCmd.Stderr = c.Stderr
c.Cmd = nCmd
err = runit(c)
}
realTime := time.Since(start)
printTime("real", realTime)
if c.ProcessState != nil {
printTime("user", c.ProcessState.UserTime())
printTime("sys", c.ProcessState.SystemTime())
}
return err
}