summaryrefslogtreecommitdiff
path: root/tools/lldb-perf/common/clang/lldb_perf_clang.cpp
blob: 4fc359db5c761c0d02324d61eae801fb33a179f0 (plain)
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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
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
//===-- lldb_perf_clang.cpp -------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "lldb-perf/lib/Measurement.h"
#include "lldb-perf/lib/Metric.h"
#include "lldb-perf/lib/Results.h"
#include "lldb-perf/lib/TestCase.h"
#include "lldb-perf/lib/Timer.h"
#include "lldb-perf/lib/Xcode.h"
#include "llvm/ADT/STLExtras.h"
#include <fstream>
#include <getopt.h>
#include <iostream>
#include <unistd.h>

using namespace lldb_perf;

#define NUM_EXPR_ITERATIONS 3
class ClangTest : public TestCase {
public:
  ClangTest()
      : TestCase(),
        m_time_create_target(
            [this]() -> void {
              m_memory_change_create_target.Start();
              m_target = m_debugger.CreateTarget(m_exe_path.c_str());
              m_memory_change_create_target.Stop();
            },
            "time-create-target", "The time it takes to create a target."),
        m_time_set_bp_main(
            [this]() -> void {
              m_memory_change_break_main.Start();
              m_target.BreakpointCreateByName("main");
              m_memory_change_break_main.Stop();
            },
            "time-set-break-main",
            "Elapsed time it takes to set a breakpoint at 'main' by name."),
        m_memory_change_create_target(), m_memory_change_break_main(),
        m_memory_total(), m_time_launch_stop_main(), m_time_total(),
        m_expr_first_evaluate(
            [this](SBFrame frame) -> void {
              frame.EvaluateExpression("Diags.DiagArgumentsStr[0].size()")
                  .GetError();
            },
            "time-expr", "Elapsed time it takes to evaluate an expression for "
                         "the first time."),
        m_expr_frame_zero(
            [this](SBFrame frame) -> void {
              frame.EvaluateExpression("Diags.DiagArgumentsStr[0].size()")
                  .GetError();
            },
            "time-expr-frame-zero", "Elapsed time it takes to evaluate an "
                                    "expression 3 times at frame zero."),
        m_expr_frame_non_zero(
            [this](SBFrame frame) -> void {
              frame.EvaluateExpression("Diags.DiagArgumentsStr[0].size()")
                  .GetError();
            },
            "time-expr-frame-non-zero", "Elapsed time it takes to evaluate an "
                                        "expression 3 times at a non-zero "
                                        "frame."),
        m_exe_path(), m_out_path(), m_launch_info(NULL), m_use_dsym(false) {}

  virtual ~ClangTest() {}

  virtual bool Setup(int &argc, const char **&argv) {
    if (m_exe_path.empty())
      return false;
    m_launch_info.SetArguments(argv, false);
    return true;
  }

  void DoTest() {}

  virtual void TestStep(int counter, ActionWanted &next_action) {
    char temp_source_path[PATH_MAX] = "/tmp/main.XXXXXX.cpp";

    switch (counter) {
    case 0: {
      // Xcode::RunCommand(m_debugger,"log enable -f /tmp/packets.txt gdb-remote
      // packets",true);

      m_memory_total.Start();
      m_time_total.Start();

      // Time creating the target
      m_time_create_target();

      m_time_set_bp_main();

      int fd = mkstemps(temp_source_path, 4);

      if (fd >= 0) {
        const char *source_content = R"(
#include <stdint.h>
#include <stdio.h>
#include <vector>
                        
namespace {
    struct Foo
    {
        int i; int j;
    };
    void doit (const Foo &foo)
    {
        printf ("doit(%i)\n", foo.i);
    }
}
                        
int main (int argc, char const *argv[], char const *envp[])
{
    std::vector<int> ints;
    for (int i=0;i<10;++i)
    ints.push_back(i);
    printf ("hello world\n");
    Foo foo = { 12, 13 };
    doit (foo);
    return 0;
}
)";
        write(fd, source_content, strlen(source_content));
        close(fd);
      } else {
        const char *error_cstr = strerror(errno);
        fprintf(stderr,
                "error: failed to created temporary source file: '%s' (%s)",
                temp_source_path, error_cstr);
        exit(2);
      }

      m_time_launch_stop_main.Start();
      const char *clang_argv[] = {"-cc1",
                                  "-triple",
                                  "x86_64-apple-macosx10.8.0",
                                  "-emit-obj",
                                  "-mrelax-all",
                                  "-disable-free",
                                  "-disable-llvm-verifier",
                                  "-main-file-name",
                                  "main.cpp",
                                  "-mrelocation-model",
                                  "pic",
                                  "-pic-level",
                                  "2",
                                  "-mdisable-fp-elim",
                                  "-masm-verbose",
                                  "-munwind-tables",
                                  "-target-cpu",
                                  "core2",
                                  "-target-linker-version",
                                  "132.10.1",
                                  "-v",
                                  "-g",
                                  "-O0",
                                  "-fdeprecated-macro",
                                  "-ferror-limit",
                                  "19",
                                  "-fmessage-length",
                                  "298",
                                  "-stack-protector",
                                  "1",
                                  "-mstackrealign",
                                  "-fblocks",
                                  "-fobjc-runtime=macosx-10.8.0",
                                  "-fobjc-dispatch-method=mixed",
                                  "-fencode-extended-block-signature",
                                  "-fcxx-exceptions",
                                  "-fexceptions",
                                  "-fdiagnostics-show-option",
                                  "-fcolor-diagnostics",
                                  "-backend-option",
                                  "-vectorize-loops",
                                  "-o",
                                  "/tmp/main.o",
                                  "-x",
                                  "c++",
                                  NULL,
                                  NULL};
      clang_argv[llvm::array_lengthof(clang_argv) - 2] = temp_source_path;
      SBLaunchInfo launch_info(clang_argv);
      Launch(launch_info);
      next_action
          .None(); // Don't continue or do anything, just wait for next event...
    } break;
    case 1: {
      m_time_launch_stop_main.Stop();
      m_time_total.Stop();
      SBFrame frame(m_thread.GetFrameAtIndex(0));

      // Time the first expression evaluation
      m_expr_first_evaluate(frame);

      SBValue result;
      for (size_t i = 0; i < NUM_EXPR_ITERATIONS; ++i) {
        m_expr_frame_zero(frame);
      }
      m_target.BreakpointCreateByName("DeclContext::lookup");
      next_action.Continue();
    } break;
    case 2: {
      SBFrame frame(m_thread.GetFrameAtIndex(21));
      SBValue result;
      for (size_t i = 0; i < NUM_EXPR_ITERATIONS; ++i) {
        m_expr_frame_non_zero(frame);
      }
      next_action.Continue();
    } break;
    default:
      m_memory_total.Stop();
      next_action.Kill();
      break;
    }
  }

  void WriteResults(Results &results) {
    Results::Dictionary &results_dict = results.GetDictionary();

    m_time_set_bp_main.WriteAverageAndStandardDeviation(results);
    results_dict.Add(
        "memory-change-create-target",
        "Memory increase that occurs due to creating the target.",
        m_memory_change_create_target.GetDeltaValue().GetResult(NULL, NULL));

    results_dict.Add(
        "memory-change-break-main", "Memory increase that occurs due to "
                                    "setting a breakpoint at main by name.",
        m_memory_change_break_main.GetDeltaValue().GetResult(NULL, NULL));

    m_time_create_target.WriteAverageAndStandardDeviation(results);
    m_expr_first_evaluate.WriteAverageAndStandardDeviation(results);
    m_expr_frame_zero.WriteAverageAndStandardDeviation(results);
    m_expr_frame_non_zero.WriteAverageAndStandardDeviation(results);
    results_dict.Add("memory-total-break-main",
                     "The total memory that the current process is using after "
                     "setting the first breakpoint.",
                     m_memory_total.GetStopValue().GetResult(NULL, NULL));

    results_dict.AddDouble(
        "time-launch-stop-main",
        "The time it takes to launch the process and stop at main.",
        m_time_launch_stop_main.GetDeltaValue());

    results_dict.AddDouble(
        "time-total", "The time it takes to create the target, set breakpoint "
                      "at main, launch clang and hit the breakpoint at main.",
        m_time_total.GetDeltaValue());
    results.Write(GetResultFilePath());
  }

  const char *GetExecutablePath() const {
    if (m_exe_path.empty())
      return NULL;
    return m_exe_path.c_str();
  }

  const char *GetResultFilePath() const {
    if (m_out_path.empty())
      return NULL;
    return m_out_path.c_str();
  }

  void SetExecutablePath(const char *path) {
    if (path && path[0])
      m_exe_path = path;
    else
      m_exe_path.clear();
  }

  void SetResultFilePath(const char *path) {
    if (path && path[0])
      m_out_path = path;
    else
      m_out_path.clear();
  }

  void SetUseDSYM(bool b) { m_use_dsym = b; }

private:
  // C++ formatters
  TimeMeasurement<std::function<void()>> m_time_create_target;
  TimeMeasurement<std::function<void()>> m_time_set_bp_main;
  MemoryGauge m_memory_change_create_target;
  MemoryGauge m_memory_change_break_main;
  MemoryGauge m_memory_total;
  TimeGauge m_time_launch_stop_main;
  TimeGauge m_time_total;
  TimeMeasurement<std::function<void(SBFrame)>> m_expr_first_evaluate;
  TimeMeasurement<std::function<void(SBFrame)>> m_expr_frame_zero;
  TimeMeasurement<std::function<void(SBFrame)>> m_expr_frame_non_zero;
  std::string m_exe_path;
  std::string m_out_path;
  SBLaunchInfo m_launch_info;
  bool m_use_dsym;
};

struct Options {
  std::string clang_path;
  std::string out_file;
  bool verbose;
  bool use_dsym;
  bool error;
  bool print_help;

  Options() : verbose(false), error(false), print_help(false) {}
};

static struct option g_long_options[] = {
    {"verbose", no_argument, NULL, 'v'},
    {"clang", required_argument, NULL, 'c'},
    {"out-file", required_argument, NULL, 'o'},
    {"dsym", no_argument, NULL, 'd'},
    {NULL, 0, NULL, 0}};

std::string GetShortOptionString(struct option *long_options) {
  std::string option_string;
  for (int i = 0; long_options[i].name != NULL; ++i) {
    if (long_options[i].flag == NULL) {
      option_string.push_back((char)long_options[i].val);
      switch (long_options[i].has_arg) {
      default:
      case no_argument:
        break;
      case required_argument:
        option_string.push_back(':');
        break;
      case optional_argument:
        option_string.append(2, ':');
        break;
      }
    }
  }
  return option_string;
}

int main(int argc, const char *argv[]) {

  // Prepare for & make calls to getopt_long_only.

  std::string short_option_string(GetShortOptionString(g_long_options));

  ClangTest test;

  Options option_data;
  bool done = false;

#if __GLIBC__
  optind = 0;
#else
  optreset = 1;
  optind = 1;
#endif
  while (!done) {
    int long_options_index = -1;
    const int short_option = ::getopt_long_only(
        argc, const_cast<char **>(argv), short_option_string.c_str(),
        g_long_options, &long_options_index);

    switch (short_option) {
    case 0:
      // Already handled
      break;

    case -1:
      done = true;
      break;

    case '?':
      option_data.print_help = true;
      break;

    case 'h':
      option_data.print_help = true;
      break;

    case 'v':
      option_data.verbose = true;
      break;

    case 'c': {
      SBFileSpec file(optarg);
      if (file.Exists())
        test.SetExecutablePath(optarg);
      else
        fprintf(stderr, "error: file specified in --clang (-c) option doesn't "
                        "exist: '%s'\n",
                optarg);
    } break;

    case 'o':
      test.SetResultFilePath(optarg);
      break;

    case 'd':
      test.SetUseDSYM(true);
      break;

    default:
      option_data.error = true;
      option_data.print_help = true;
      fprintf(stderr, "error: unrecognized option %c\n", short_option);
      break;
    }
  }

  if (test.GetExecutablePath() == NULL) {
    // --clang is mandatory
    option_data.print_help = true;
    option_data.error = true;
    fprintf(stderr, "error: the '--clang=PATH' option is mandatory\n");
  }

  if (option_data.print_help) {
    puts(R"(
NAME
    lldb_perf_clang -- a tool that measures LLDB peformance while debugging clang.

SYNOPSIS
    lldb_perf_clang --clang=PATH [--out-file=PATH --verbose --dsym] -- [clang options]
             
DESCRIPTION
    Runs a set of static timing and memory tasks against clang and outputs results
    to a plist file.
)");
  }
  if (option_data.error) {
    exit(1);
  }

  // Update argc and argv after parsing options
  argc -= optind;
  argv += optind;

  test.SetVerbose(true);
  TestCase::Run(test, argc, argv);
  return 0;
}