File: fdr-reinit.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (77 lines) | stat: -rw-r--r-- 2,540 bytes parent folder | download | duplicates (8)
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
// This will sometimes segfault on the AArch64 and Arm bots
// UNSUPPORTED: target={{(aarch64|arm).*}}
// RUN: %clangxx_xray -g -std=c++11 %s -o %t
// RUN: rm xray-log.fdr-reinit* || true
// RUN: XRAY_OPTIONS="verbosity=1" %run %t
// RUN: rm xray-log.fdr-reinit* || true
#include "xray/xray_log_interface.h"
#include <atomic>
#include <cassert>
#include <cstddef>
#include <thread>

volatile uint64_t var = 0;

std::atomic_flag keep_going = ATOMIC_FLAG_INIT;

[[clang::xray_always_instrument]] void __attribute__((noinline)) func() {
  ++var;
}

int main(int argc, char *argv[]) {
  // Start a thread that will just keep calling the function, to spam calls to
  // the function call handler.
  keep_going.test_and_set(std::memory_order_acquire);
  std::thread t([] {
    while (keep_going.test_and_set(std::memory_order_acquire))
      func();
  });

  static constexpr char kConfig[] =
      "buffer_size=1024:buffer_max=10:no_file_flush=true";

  // Then we initialize the FDR mode implementation.
  assert(__xray_log_select_mode("xray-fdr") ==
         XRayLogRegisterStatus::XRAY_REGISTRATION_OK);
  auto init_status = __xray_log_init_mode("xray-fdr", kConfig);
  assert(init_status == XRayLogInitStatus::XRAY_LOG_INITIALIZED);

  // Now we patch the instrumentation points.
  __xray_patch();

  // Spin for a bit, calling func() enough times.
  for (auto i = 0; i < 1 << 20; ++i)
    func();

  // Then immediately finalize the implementation.
  auto finalize_status = __xray_log_finalize();
  assert(finalize_status == XRayLogInitStatus::XRAY_LOG_FINALIZED);

  // Once we're here, we should then flush.
  auto flush_status = __xray_log_flushLog();
  assert(flush_status == XRayLogFlushStatus::XRAY_LOG_FLUSHED);

  for (auto trial = 0; trial < 3; trial++) {
    // Without doing anything else, we should re-initialize.
    init_status = __xray_log_init_mode("xray-fdr", kConfig);
    assert(init_status == XRayLogInitStatus::XRAY_LOG_INITIALIZED);

    // Then we spin for a bit again calling func() enough times.
    for (auto i = 0; i < 1 << 20; ++i)
      func();

    // Then immediately finalize the implementation.
    finalize_status = __xray_log_finalize();
    assert(finalize_status == XRayLogInitStatus::XRAY_LOG_FINALIZED);

    // Once we're here, we should then flush.
    flush_status = __xray_log_flushLog();
    assert(flush_status == XRayLogFlushStatus::XRAY_LOG_FLUSHED);
  }

  // Finally, we should signal the sibling thread to stop.
  keep_going.clear(std::memory_order_release);

  // Then join.
  t.join();
}