Merge lp:~smoser/cloud-init/log_time into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Scott Moser
Status: Merged
Merged at revision: 854
Proposed branch: lp:~smoser/cloud-init/log_time
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 163 lines (+55/-16)
6 files modified
ChangeLog (+3/-0)
bin/cloud-init (+3/-1)
cloudinit/config/cc_growpart.py (+2/-1)
cloudinit/config/cc_resizefs.py (+5/-6)
cloudinit/sources/DataSourceAzure.py (+7/-8)
cloudinit/util.py (+35/-0)
To merge this branch: bzr merge lp:~smoser/cloud-init/log_time
Reviewer Review Type Date Requested Status
Joshua Harlow (community) Approve
Review via email: mp+177666@code.launchpad.net

Description of the change

add util.log_time helper

The reason for this is that more and more things I was wanting to be able to
see how long they took. This puts that time logic into a single place. It
also supports from /proc/uptime as the timing mechanism.

While reading /proc/uptime is almost certainly slower than time.time(), it does
give millisecond granularity and is not affected by 'ntpdate' having run in
between the two events.

To post a comment you must log in.
Revision history for this message
Joshua Harlow (harlowja) wrote :

LGTM!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ChangeLog'
2--- ChangeLog 2013-07-29 16:33:42 +0000
3+++ ChangeLog 2013-07-30 18:35:32 +0000
4@@ -11,6 +11,9 @@
5 - always finalize handlers even if processing failed (LP: #1203368)
6 - support merging into cloud-config via jsonp. (LP: #1200476)
7 - add datasource 'SmartOS' for Joyent Cloud. Adds a dependency on serial.
8+ - add 'log_time' helper to util for timing how long things take
9+ which also reads from uptime. uptime is useful as clock may change during
10+ boot due to ntp.
11 0.7.2:
12 - add a debian watch file
13 - add 'sudo' entry to ubuntu's default user (LP: #1080717)
14
15=== modified file 'bin/cloud-init'
16--- bin/cloud-init 2012-09-20 01:19:43 +0000
17+++ bin/cloud-init 2013-07-30 18:35:32 +0000
18@@ -502,7 +502,9 @@
19 signal_handler.attach_handlers()
20
21 (name, functor) = args.action
22- return functor(name, args)
23+
24+ return util.log_time(logfunc=LOG.debug, msg="cloud-init mode '%s'" % name,
25+ get_uptime=True, func=functor, args=(name, args))
26
27
28 if __name__ == '__main__':
29
30=== modified file 'cloudinit/config/cc_growpart.py'
31--- cloudinit/config/cc_growpart.py 2013-06-19 06:44:00 +0000
32+++ cloudinit/config/cc_growpart.py 2013-07-30 18:35:32 +0000
33@@ -264,7 +264,8 @@
34 raise e
35 return
36
37- resized = resize_devices(resizer, devices)
38+ resized = util.log_time(logfunc=log.debug, msg="resize_devices",
39+ func=resize_devices, args=(resizer, devices))
40 for (entry, action, msg) in resized:
41 if action == RESIZE.CHANGED:
42 log.info("'%s' resized: %s" % (entry, msg))
43
44=== modified file 'cloudinit/config/cc_resizefs.py'
45--- cloudinit/config/cc_resizefs.py 2013-03-26 19:50:25 +0000
46+++ cloudinit/config/cc_resizefs.py 2013-07-30 18:35:32 +0000
47@@ -21,7 +21,6 @@
48 import errno
49 import os
50 import stat
51-import time
52
53 from cloudinit.settings import PER_ALWAYS
54 from cloudinit import util
55@@ -120,9 +119,12 @@
56 if resize_root == NOBLOCK:
57 # Fork to a child that will run
58 # the resize command
59- util.fork_cb(do_resize, resize_cmd, log)
60+ util.fork_cb(
61+ util.log_time(logfunc=log.debug, msg="backgrounded Resizing",
62+ func=do_resize, args=(resize_cmd, log)))
63 else:
64- do_resize(resize_cmd, log)
65+ util.log_time(logfunc=log.debug, msg="Resizing",
66+ func=do_resize, args=(resize_cmd, log))
67
68 action = 'Resized'
69 if resize_root == NOBLOCK:
70@@ -132,13 +134,10 @@
71
72
73 def do_resize(resize_cmd, log):
74- start = time.time()
75 try:
76 util.subp(resize_cmd)
77 except util.ProcessExecutionError:
78 util.logexc(log, "Failed to resize filesystem (cmd=%s)", resize_cmd)
79 raise
80- tot_time = time.time() - start
81- log.debug("Resizing took %.3f seconds", tot_time)
82 # TODO(harlowja): Should we add a fsck check after this to make
83 # sure we didn't corrupt anything?
84
85=== modified file 'cloudinit/sources/DataSourceAzure.py'
86--- cloudinit/sources/DataSourceAzure.py 2013-07-29 16:02:03 +0000
87+++ cloudinit/sources/DataSourceAzure.py 2013-07-30 18:35:32 +0000
88@@ -138,13 +138,11 @@
89 bname = pk['fingerprint'] + ".crt"
90 fp_files += [os.path.join(mycfg['data_dir'], bname)]
91
92- start = time.time()
93- missing = wait_for_files(wait_for + fp_files)
94+ missing = util.log_time(logfunc=LOG.debug, msg="waiting for files",
95+ func=wait_for_files,
96+ args=(wait_for + fp_files,))
97 if len(missing):
98 LOG.warn("Did not find files, but going on: %s", missing)
99- else:
100- LOG.debug("waited %.3f seconds for %d files to appear",
101- time.time() - start, len(wait_for))
102
103 if shcfgxml in missing:
104 LOG.warn("SharedConfig.xml missing, using static instance-id")
105@@ -206,11 +204,12 @@
106 command = BOUNCE_COMMAND
107
108 LOG.debug("pubhname: publishing hostname [%s]", msg)
109- start = time.time()
110 shell = not isinstance(command, (list, tuple))
111 # capture=False, see comments in bug 1202758 and bug 1206164.
112- (output, err) = util.subp(command, shell=shell, capture=False, env=env)
113- LOG.debug("publishing hostname took %.3f seconds", time.time() - start)
114+ util.log_time(logfunc=LOG.debug, msg="publishing hostname",
115+ get_uptime=True, func=util.subp,
116+ kwargs={'command': command, 'shell': shell, 'capture': False,
117+ 'env': env})
118
119
120 def crtfile_to_pubkey(fname):
121
122=== modified file 'cloudinit/util.py'
123--- cloudinit/util.py 2013-07-24 14:37:36 +0000
124+++ cloudinit/util.py 2013-07-30 18:35:32 +0000
125@@ -1770,3 +1770,38 @@
126 return exe_file
127
128 return None
129+
130+
131+def log_time(logfunc, msg, func, args=None, kwargs=None, get_uptime=False):
132+ if args is None:
133+ args = []
134+ if kwargs is None:
135+ kwargs = {}
136+
137+ start = time.time()
138+
139+ ustart = None
140+ if get_uptime:
141+ try:
142+ ustart = float(uptime())
143+ except ValueError:
144+ pass
145+
146+ try:
147+ ret = func(*args, **kwargs)
148+ finally:
149+ delta = time.time() - start
150+ if ustart is not None:
151+ try:
152+ udelta = float(uptime()) - ustart
153+ except ValueError:
154+ udelta = "N/A"
155+
156+ tmsg = " took %0.3f seconds" % delta
157+ if get_uptime:
158+ tmsg += "(%0.2f)" % udelta
159+ try:
160+ logfunc(msg + tmsg)
161+ except:
162+ pass
163+ return ret