lldb-perf-stepping.cpp revision 3f2f741bb533b78e2fac5332c4698338ea2fd3ac
1#include <CoreFoundation/CoreFoundation.h>
2
3#include "lldb-perf/lib/Timer.h"
4#include "lldb-perf/lib/Metric.h"
5#include "lldb-perf/lib/Measurement.h"
6#include "lldb-perf/lib/TestCase.h"
7#include "lldb-perf/lib/Xcode.h"
8
9#include <unistd.h>
10#include <string>
11#include <getopt.h>
12
13using namespace lldb_perf;
14
15class StepTest : public TestCase
16{
17    typedef void (*no_function) (void);
18
19public:
20    StepTest(bool use_single_stepping = false) :
21        m_main_source("stepping-testcase.cpp"),
22        m_use_single_stepping(use_single_stepping),
23        m_time_measurements(nullptr)
24    {
25    }
26
27    virtual
28    ~StepTest() {}
29
30    virtual bool
31    Setup (int& argc, const char**& argv)
32    {
33        TestCase::Setup (argc, argv);
34
35        // Toggle the fast stepping command on or off as required.
36        const char *single_step_cmd = "settings set target.use-fast-stepping false";
37        const char *fast_step_cmd   = "settings set target.use-fast-stepping true";
38        const char *cmd_to_use;
39
40        if (m_use_single_stepping)
41            cmd_to_use = single_step_cmd;
42        else
43            cmd_to_use = fast_step_cmd;
44
45        SBCommandReturnObject return_object;
46        m_debugger.GetCommandInterpreter().HandleCommand(cmd_to_use,
47                                                         return_object);
48        if (!return_object.Succeeded())
49        {
50            if (return_object.GetError() != NULL)
51                printf ("Got an error running settings set: %s.\n", return_object.GetError());
52            else
53                printf ("Failed running settings set, no error.\n");
54        }
55
56        m_target = m_debugger.CreateTarget(m_app_path.c_str());
57        m_first_bp = m_target.BreakpointCreateBySourceRegex("Here is some code to stop at originally.", m_main_source);
58
59        const char* file_arg = m_app_path.c_str();
60        const char* empty = nullptr;
61        const char* args[] = {file_arg, empty};
62        SBLaunchInfo launch_info (args);
63
64        return Launch (launch_info);
65    }
66
67    void
68    WriteResults (Results &results)
69    {
70        // Gotta turn off the last timer now.
71        m_individual_step_times.push_back(m_time_measurements.Stop());
72
73        size_t num_time_measurements = m_individual_step_times.size();
74
75        Results::Dictionary& results_dict = results.GetDictionary();
76        const char *short_format_string = "step-time-%0.2d";
77        const size_t short_size = strlen(short_format_string) + 5;
78        char short_buffer[short_size];
79        const char *long_format_string  = "The time it takes for step %d in the step sequence.";
80        const size_t long_size = strlen(long_format_string) + 5;
81        char long_buffer[long_size];
82
83        for (size_t i = 0; i < num_time_measurements; i++)
84        {
85            snprintf (short_buffer, short_size, short_format_string, i);
86            snprintf (long_buffer, long_size, long_format_string, i);
87
88            results_dict.AddDouble(short_buffer,
89                                   long_buffer,
90                                   m_individual_step_times[i]);
91
92        }
93        results_dict.AddDouble ("total-time", "Total time spent stepping.", m_time_measurements.GetMetric().GetSum());
94
95        results.Write(m_out_path.c_str());
96    }
97
98
99    const char *
100    GetExecutablePath () const
101    {
102        if (m_app_path.empty())
103            return NULL;
104        return m_app_path.c_str();
105    }
106
107    const char *
108    GetResultFilePath () const
109    {
110        if (m_out_path.empty())
111            return NULL;
112        return m_out_path.c_str();
113    }
114
115    void
116    SetExecutablePath (const char *path)
117    {
118        if (path && path[0])
119            m_app_path = path;
120        else
121            m_app_path.clear();
122    }
123
124    void
125    SetResultFilePath (const char *path)
126    {
127        if (path && path[0])
128            m_out_path = path;
129        else
130            m_out_path.clear();
131    }
132
133    void
134    SetUseSingleStep (bool use_it)
135    {
136        m_use_single_stepping = use_it;
137    }
138private:
139    virtual void
140	TestStep (int counter, ActionWanted &next_action)
141    {
142        if (counter > 0)
143        {
144            m_individual_step_times.push_back(m_time_measurements.Stop());
145
146        }
147
148        // Disable the breakpoint, just in case it gets multiple locations we don't want that confusing the stepping.
149        if (counter == 0)
150            m_first_bp.SetEnabled(false);
151
152        next_action.StepOver(m_process.GetThreadAtIndex(0));
153        m_time_measurements.Start();
154
155
156    }
157
158    SBBreakpoint m_first_bp;
159    SBFileSpec   m_main_source;
160    TimeMeasurement<no_function> m_time_measurements;
161    std::vector<double>          m_individual_step_times;
162    bool m_use_single_stepping;
163    std::string m_app_path;
164    std::string m_out_path;
165
166
167};
168
169struct Options
170{
171    std::string test_file_path;
172    std::string out_file;
173    bool verbose;
174    bool fast_step;
175    bool error;
176    bool print_help;
177
178    Options() :
179        verbose (false),
180        fast_step (true),
181        error (false),
182        print_help (false)
183    {
184    }
185};
186
187static struct option g_long_options[] = {
188    { "verbose",      no_argument,            NULL, 'v' },
189    { "single-step",  no_argument,            NULL, 's' },
190    { "test-file",    required_argument,      NULL, 't' },
191    { "out-file",     required_argument,      NULL, 'o' },
192    { NULL,           0,                      NULL,  0  }
193};
194
195
196std::string
197GetShortOptionString (struct option *long_options)
198{
199    std::string option_string;
200    for (int i = 0; long_options[i].name != NULL; ++i)
201    {
202        if (long_options[i].flag == NULL)
203        {
204            option_string.push_back ((char) long_options[i].val);
205            switch (long_options[i].has_arg)
206            {
207                default:
208                case no_argument:
209                    break;
210                case required_argument:
211                    option_string.push_back (':');
212                    break;
213                case optional_argument:
214                    option_string.append (2, ':');
215                    break;
216            }
217        }
218    }
219    return option_string;
220}
221
222int main(int argc, const char * argv[])
223{
224
225    // Prepare for & make calls to getopt_long_only.
226
227    std::string short_option_string (GetShortOptionString(g_long_options));
228
229    StepTest test;
230
231    Options option_data;
232    bool done = false;
233
234#if __GLIBC__
235    optind = 0;
236#else
237    optreset = 1;
238    optind = 1;
239#endif
240    while (!done)
241    {
242        int long_options_index = -1;
243        const int short_option = ::getopt_long_only (argc,
244                                                     const_cast<char **>(argv),
245                                                     short_option_string.c_str(),
246                                                     g_long_options,
247                                                     &long_options_index);
248
249        switch (short_option)
250        {
251            case 0:
252                // Already handled
253                break;
254
255            case -1:
256                done = true;
257                break;
258
259            case '?':
260                option_data.print_help = true;
261                break;
262
263            case 'h':
264                option_data.print_help = true;
265                break;
266
267            case 'v':
268                option_data.verbose = true;
269                break;
270
271            case 's':
272                option_data.fast_step = false;
273                test.SetUseSingleStep(true);
274                break;
275
276            case 't':
277                {
278                    SBFileSpec file(optarg);
279                    if (file.Exists())
280                        test.SetExecutablePath(optarg);
281                    else
282                        fprintf(stderr, "error: file specified in --test-file (-t) option doesn't exist: '%s'\n", optarg);
283                }
284                break;
285
286            case 'o':
287                test.SetResultFilePath(optarg);
288                break;
289
290            default:
291                option_data.error = true;
292                option_data.print_help = true;
293                fprintf (stderr, "error: unrecognized option %c\n", short_option);
294                break;
295        }
296    }
297
298
299    if (option_data.print_help)
300    {
301        puts(R"(
302NAME
303    lldb-perf-stepping -- a tool that measures LLDB peformance of simple stepping operations.
304
305SYNOPSIS
306    lldb-perf-stepping --test-file=FILE [--out-file=PATH --verbose --fast-step]
307
308DESCRIPTION
309    Runs a set of stepping operations, timing each step and outputs results
310    to a plist file.
311)");
312        exit(0);
313    }
314    if (option_data.error)
315    {
316        exit(1);
317    }
318
319    if (test.GetExecutablePath() == NULL)
320    {
321        // --clang is mandatory
322        option_data.print_help = true;
323        option_data.error = true;
324        fprintf (stderr, "error: the '--test-file=PATH' option is mandatory\n");
325    }
326
327    // Update argc and argv after parsing options
328    argc -= optind;
329    argv += optind;
330
331    test.SetVerbose(true);
332    TestCase::Run(test, argc, argv);
333    return 0;
334}
335