1 //===- include/Core/Instrumentation.h - Instrumentation API ---------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 /// Provide an Instrumentation API that optionally uses VTune interfaces.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLD_CORE_INSTRUMENTATION_H
15 #define LLD_CORE_INSTRUMENTATION_H
17 #include "llvm/Support/Compiler.h"
21 # include <ittnotify.h>
26 /// A unique global scope for instrumentation data.
28 /// Domains last for the lifetime of the application and cannot be destroyed.
29 /// Multiple Domains created with the same name represent the same domain.
31 __itt_domain *_domain;
34 explicit Domain(const char *name) : _domain(__itt_domain_createA(name)) {}
36 operator __itt_domain *() const { return _domain; }
37 __itt_domain *operator->() const { return _domain; }
40 /// A global reference to a string constant.
42 /// These are uniqued by the ITT runtime and cannot be deleted. They are not
43 /// specific to a domain.
45 /// Prefer reusing a single StringHandle over passing a ntbs when the same
46 /// string will be used often.
48 __itt_string_handle *_handle;
51 StringHandle(const char *name) : _handle(__itt_string_handle_createA(name)) {}
53 operator __itt_string_handle *() const { return _handle; }
56 /// A task on a single thread. Nests within other tasks.
58 /// Each thread has its own task stack and tasks nest recursively on that stack.
59 /// A task cannot transfer threads.
61 /// SBRM is used to ensure task starts and ends are ballanced. The lifetime of
62 /// a task is either the lifetime of this object, or until end is called.
64 __itt_domain *_domain;
66 ScopedTask(const ScopedTask &) = delete;
67 ScopedTask &operator=(const ScopedTask &) = delete;
70 /// Create a task in Domain \p d named \p s.
71 ScopedTask(const Domain &d, const StringHandle &s) : _domain(d) {
72 __itt_task_begin(d, __itt_null, __itt_null, s);
75 ScopedTask(ScopedTask &&other) {
76 *this = std::move(other);
79 ScopedTask &operator=(ScopedTask &&other) {
80 _domain = other._domain;
81 other._domain = nullptr;
85 /// Prematurely end this task.
88 __itt_task_end(_domain);
92 ~ScopedTask() { end(); }
95 /// A specific point in time. Allows metadata to be associated.
98 Marker(const Domain &d, const StringHandle &s) {
99 __itt_marker(d, __itt_null, s, __itt_scope_global);
105 Domain(const char *name) {}
110 StringHandle(const char *name) {}
115 ScopedTask(const Domain &d, const StringHandle &s) {}
121 Marker(const Domain &d, const StringHandle &s) {}
125 inline const Domain &getDefaultDomain() {
126 static Domain domain("org.llvm.lld");
129 } // end namespace lld.