Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: shell/desktop/main.cc

Issue 1067023003: Add shell::Tracer object and wire up to --trace-startup on Android (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: review comments Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdio.h> 5 #include <stdio.h>
6 #include <string.h> 6 #include <string.h>
7 7
8 #include <algorithm> 8 #include <algorithm>
9 #include <iostream> 9 #include <iostream>
10 10
11 #include "base/at_exit.h" 11 #include "base/at_exit.h"
12 #include "base/base_switches.h" 12 #include "base/base_switches.h"
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/debug/profiler.h" 15 #include "base/debug/profiler.h"
16 #include "base/files/file_util.h" 16 #include "base/files/file_util.h"
17 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
18 #include "base/synchronization/waitable_event.h" 18 #include "base/synchronization/waitable_event.h"
19 #include "base/trace_event/trace_event.h" 19 #include "base/trace_event/trace_event.h"
20 #include "shell/child_main.h" 20 #include "shell/child_main.h"
21 #include "shell/command_line_util.h" 21 #include "shell/command_line_util.h"
22 #include "shell/context.h" 22 #include "shell/context.h"
23 #include "shell/init.h" 23 #include "shell/init.h"
24 #include "shell/switches.h" 24 #include "shell/switches.h"
25 #include "shell/tracer.h"
25 26
26 namespace { 27 namespace {
27 28
28 void Usage() { 29 void Usage() {
29 std::cerr << "Launch Mojo applications.\n"; 30 std::cerr << "Launch Mojo applications.\n";
30 std::cerr 31 std::cerr
31 << "Usage: mojo_shell" 32 << "Usage: mojo_shell"
32 << " [--" << switches::kArgsFor << "=<mojo-app>]" 33 << " [--" << switches::kArgsFor << "=<mojo-app>]"
33 << " [--" << switches::kContentHandlers << "=<handlers>]" 34 << " [--" << switches::kContentHandlers << "=<handlers>]"
34 << " [--" << switches::kCPUProfile << "]" 35 << " [--" << switches::kCPUProfile << "]"
35 << " [--" << switches::kEnableExternalApplications << "]" 36 << " [--" << switches::kEnableExternalApplications << "]"
36 << " [--" << switches::kDisableCache << "]" 37 << " [--" << switches::kDisableCache << "]"
37 << " [--" << switches::kEnableMultiprocess << "]" 38 << " [--" << switches::kEnableMultiprocess << "]"
38 << " [--" << switches::kOrigin << "=<url-lib-path>]" 39 << " [--" << switches::kOrigin << "=<url-lib-path>]"
39 << " [--" << switches::kTraceStartup << "]" 40 << " [--" << switches::kTraceStartup << "]"
40 << " [--" << switches::kURLMappings << "=from1=to1,from2=to2]" 41 << " [--" << switches::kURLMappings << "=from1=to1,from2=to2]"
41 << " [--" << switches::kPredictableAppFilenames << "]" 42 << " [--" << switches::kPredictableAppFilenames << "]"
42 << " [--" << switches::kWaitForDebugger << "]" 43 << " [--" << switches::kWaitForDebugger << "]"
43 << " <mojo-app> ...\n\n" 44 << " <mojo-app> ...\n\n"
44 << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within " 45 << "A <mojo-app> is a Mojo URL or a Mojo URL and arguments within "
45 << "quotes.\n" 46 << "quotes.\n"
46 << "Example: mojo_shell \"mojo:js_standalone test.js\".\n" 47 << "Example: mojo_shell \"mojo:js_standalone test.js\".\n"
47 << "<url-lib-path> is searched for shared libraries named by mojo URLs.\n" 48 << "<url-lib-path> is searched for shared libraries named by mojo URLs.\n"
48 << "The value of <handlers> is a comma separated list like:\n" 49 << "The value of <handlers> is a comma separated list like:\n"
49 << "text/html,mojo:html_viewer," 50 << "text/html,mojo:html_viewer,"
50 << "application/javascript,mojo:js_content_handler\n"; 51 << "application/javascript,mojo:js_content_handler\n";
51 } 52 }
52 53
53 // Whether we're currently tracing.
54 bool g_tracing = false;
55
56 // Number of tracing blocks written.
57 uint32_t g_blocks = 0;
58
59 // Trace file, if open.
60 FILE* g_trace_file = nullptr;
61
62 void WriteTraceDataCollected(
63 base::WaitableEvent* event,
64 const scoped_refptr<base::RefCountedString>& events_str,
65 bool has_more_events) {
66 if (g_blocks) {
67 fwrite(",", 1, 1, g_trace_file);
68 }
69
70 ++g_blocks;
71 fwrite(events_str->data().c_str(), 1, events_str->data().length(),
72 g_trace_file);
73 if (!has_more_events) {
74 static const char kEnd[] = "]}";
75 fwrite(kEnd, 1, strlen(kEnd), g_trace_file);
76 PCHECK(fclose(g_trace_file) == 0);
77 g_trace_file = nullptr;
78 event->Signal();
79 }
80 }
81
82 void EndTraceAndFlush(base::WaitableEvent* event) {
83 g_trace_file = fopen("mojo_shell.trace", "w+");
84 PCHECK(g_trace_file);
85 static const char kStart[] = "{\"traceEvents\":[";
86 fwrite(kStart, 1, strlen(kStart), g_trace_file);
87 base::trace_event::TraceLog::GetInstance()->SetDisabled();
88 base::trace_event::TraceLog::GetInstance()->Flush(
89 base::Bind(&WriteTraceDataCollected, base::Unretained(event)));
90 }
91
92 void StopTracingAndFlushToDisk() {
93 g_tracing = false;
94 base::trace_event::TraceLog::GetInstance()->SetDisabled();
95 base::WaitableEvent flush_complete_event(false, false);
96 // TraceLog::Flush requires a message loop but we've already shut ours down.
97 // Spin up a new thread to flush things out.
98 base::Thread flush_thread("mojo_shell_trace_event_flush");
99 flush_thread.Start();
100 flush_thread.message_loop()->PostTask(
101 FROM_HERE,
102 base::Bind(EndTraceAndFlush, base::Unretained(&flush_complete_event)));
103 flush_complete_event.Wait();
104 }
105
106 } // namespace 54 } // namespace
107 55
108 int main(int argc, char** argv) { 56 int main(int argc, char** argv) {
109 base::AtExitManager at_exit; 57 base::AtExitManager at_exit;
110 base::CommandLine::Init(argc, argv); 58 base::CommandLine::Init(argc, argv);
111 59
60 mojo::shell::Tracer tracer;
61
112 mojo::shell::InitializeLogging(); 62 mojo::shell::InitializeLogging();
113 63
114 const base::CommandLine& command_line = 64 const base::CommandLine& command_line =
115 *base::CommandLine::ForCurrentProcess(); 65 *base::CommandLine::ForCurrentProcess();
116 66
117 // TODO(vtl): Unify parent and child process cases to the extent possible. 67 // TODO(vtl): Unify parent and child process cases to the extent possible.
118 int exit_code = 0; 68 int exit_code = 0;
119 if (command_line.HasSwitch(switches::kChildProcess)) { 69 if (command_line.HasSwitch(switches::kChildProcess)) {
120 exit_code = mojo::shell::ChildMain(); 70 exit_code = mojo::shell::ChildMain();
121 } else { 71 } else {
122 // Only check the command line for the main process. 72 // Only check the command line for the main process.
123 const std::set<std::string> all_switches = switches::GetAllSwitches(); 73 const std::set<std::string> all_switches = switches::GetAllSwitches();
124 const base::CommandLine::SwitchMap switches = command_line.GetSwitches(); 74 const base::CommandLine::SwitchMap switches = command_line.GetSwitches();
125 bool found_unknown_switch = false; 75 bool found_unknown_switch = false;
126 for (const auto& s : switches) { 76 for (const auto& s : switches) {
127 if (all_switches.find(s.first) == all_switches.end()) { 77 if (all_switches.find(s.first) == all_switches.end()) {
128 std::cerr << "unknown switch: " << s.first << std::endl; 78 std::cerr << "unknown switch: " << s.first << std::endl;
129 found_unknown_switch = true; 79 found_unknown_switch = true;
130 } 80 }
131 } 81 }
132 82
133 if (found_unknown_switch || 83 if (found_unknown_switch ||
134 (!command_line.HasSwitch(switches::kEnableExternalApplications) && 84 (!command_line.HasSwitch(switches::kEnableExternalApplications) &&
135 (command_line.HasSwitch(switches::kHelp) || 85 (command_line.HasSwitch(switches::kHelp) ||
136 command_line.GetArgs().empty()))) { 86 command_line.GetArgs().empty()))) {
137 Usage(); 87 Usage();
138 return 0; 88 return 0;
139 } 89 }
140 90
141 if (command_line.HasSwitch(switches::kTraceStartup)) { 91 bool trace_startup = command_line.HasSwitch(switches::kTraceStartup);
142 g_tracing = true; 92 if (trace_startup)
143 base::trace_event::CategoryFilter category_filter( 93 tracer.Start(command_line.GetSwitchValueASCII(switches::kTraceStartup));
144 command_line.GetSwitchValueASCII(switches::kTraceStartup));
145 base::trace_event::TraceLog::GetInstance()->SetEnabled(
146 category_filter, base::trace_event::TraceLog::RECORDING_MODE,
147 base::trace_event::TraceOptions(
148 base::trace_event::RECORD_UNTIL_FULL));
149 }
150 94
151 if (command_line.HasSwitch(switches::kCPUProfile)) { 95 if (command_line.HasSwitch(switches::kCPUProfile)) {
152 #if !defined(NDEBUG) || !defined(ENABLE_PROFILING) 96 #if !defined(NDEBUG) || !defined(ENABLE_PROFILING)
153 LOG(ERROR) << "Profiling requires is_debug=false and " 97 LOG(ERROR) << "Profiling requires is_debug=false and "
154 << "enable_profiling=true. Continuing without profiling."; 98 << "enable_profiling=true. Continuing without profiling.";
155 // StartProfiling() and StopProfiling() are a no-op. 99 // StartProfiling() and StopProfiling() are a no-op.
156 #endif 100 #endif
157 base::debug::StartProfiling("mojo_shell.pprof"); 101 base::debug::StartProfiling("mojo_shell.pprof");
158 } 102 }
159 103
160 // We want the shell::Context to outlive the MessageLoop so that pipes are 104 // We want the shell::Context to outlive the MessageLoop so that pipes are
161 // all gracefully closed / error-out before we try to shut the Context down. 105 // all gracefully closed / error-out before we try to shut the Context down.
162 mojo::shell::Context shell_context; 106 mojo::shell::Context shell_context;
163 { 107 {
164 base::MessageLoop message_loop; 108 base::MessageLoop message_loop;
165 if (!shell_context.Init()) { 109 if (!shell_context.Init()) {
166 Usage(); 110 Usage();
167 return 0; 111 return 0;
168 } 112 }
169 if (g_tracing) { 113 if (trace_startup) {
170 message_loop.PostDelayedTask(FROM_HERE, 114 message_loop.PostDelayedTask(
171 base::Bind(StopTracingAndFlushToDisk), 115 FROM_HERE,
172 base::TimeDelta::FromSeconds(5)); 116 base::Bind(&mojo::shell::Tracer::StopAndFlushToFile,
117 base::Unretained(&tracer), "mojo_shell.trace"),
118 base::TimeDelta::FromSeconds(5));
173 } 119 }
174 120
175 // The mojo_shell --args-for command-line switch is handled specially 121 // The mojo_shell --args-for command-line switch is handled specially
176 // because it can appear more than once. The base::CommandLine class 122 // because it can appear more than once. The base::CommandLine class
177 // collapses multiple occurrences of the same switch. 123 // collapses multiple occurrences of the same switch.
178 for (int i = 1; i < argc; i++) { 124 for (int i = 1; i < argc; i++) {
179 ApplyApplicationArgs(&shell_context, argv[i]); 125 ApplyApplicationArgs(&shell_context, argv[i]);
180 } 126 }
181 127
182 message_loop.PostTask( 128 message_loop.PostTask(
183 FROM_HERE, 129 FROM_HERE,
184 base::Bind(&mojo::shell::RunCommandLineApps, &shell_context)); 130 base::Bind(&mojo::shell::RunCommandLineApps, &shell_context));
185 message_loop.Run(); 131 message_loop.Run();
186 132
187 // Must be called before |message_loop| is destroyed. 133 // Must be called before |message_loop| is destroyed.
188 shell_context.Shutdown(); 134 shell_context.Shutdown();
189 } 135 }
190 136
191 if (command_line.HasSwitch(switches::kCPUProfile)) { 137 if (command_line.HasSwitch(switches::kCPUProfile)) {
192 base::debug::StopProfiling(); 138 base::debug::StopProfiling();
193 } 139 }
194 } 140 }
195 141
196 if (g_tracing) 142 tracer.StopAndFlushToFile("mojo_shell.trace");
197 StopTracingAndFlushToDisk();
198 return exit_code; 143 return exit_code;
199 } 144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698