1 //===- include/Core/Instrumentation.h - Instrumentation API ---------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Provide an Instrumentation API that optionally uses VTune interfaces.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLD_CORE_INSTRUMENTATION_H
16 #define LLD_CORE_INSTRUMENTATION_H
18 #include "llvm/Support/Compiler.h"
22 # include <ittnotify.h>
27 /// A unique global scope for instrumentation data.
29 /// Domains last for the lifetime of the application and cannot be destroyed.
30 /// Multiple Domains created with the same name represent the same domain.
32 __itt_domain *_domain;
35 explicit Domain(const char *name) : _domain(__itt_domain_createA(name)) {}
37 operator __itt_domain *() const { return _domain; }
38 __itt_domain *operator->() const { return _domain; }
41 /// A global reference to a string constant.
43 /// These are uniqued by the ITT runtime and cannot be deleted. They are not
44 /// specific to a domain.
46 /// Prefer reusing a single StringHandle over passing a ntbs when the same
47 /// string will be used often.
49 __itt_string_handle *_handle;
52 StringHandle(const char *name) : _handle(__itt_string_handle_createA(name)) {}
54 operator __itt_string_handle *() const { return _handle; }
57 /// A task on a single thread. Nests within other tasks.
59 /// Each thread has its own task stack and tasks nest recursively on that stack.
60 /// A task cannot transfer threads.
62 /// SBRM is used to ensure task starts and ends are ballanced. The lifetime of
63 /// a task is either the lifetime of this object, or until end is called.
65 __itt_domain *_domain;
67 ScopedTask(const ScopedTask &) = delete;
68 ScopedTask &operator=(const ScopedTask &) = delete;
71 /// Create a task in Domain \p d named \p s.
72 ScopedTask(const Domain &d, const StringHandle &s) : _domain(d) {
73 __itt_task_begin(d, __itt_null, __itt_null, s);
76 ScopedTask(ScopedTask &&other) {
77 *this = std::move(other);
80 ScopedTask &operator=(ScopedTask &&other) {
81 _domain = other._domain;
82 other._domain = nullptr;
86 /// Prematurely end this task.
89 __itt_task_end(_domain);
93 ~ScopedTask() { end(); }
96 /// A specific point in time. Allows metadata to be associated.
99 Marker(const Domain &d, const StringHandle &s) {
100 __itt_marker(d, __itt_null, s, __itt_scope_global);
106 Domain(const char *name) {}
111 StringHandle(const char *name) {}
116 ScopedTask(const Domain &d, const StringHandle &s) {}
122 Marker(const Domain &d, const StringHandle &s) {}
126 inline const Domain &getDefaultDomain() {
127 static Domain domain("org.llvm.lld");
130 } // end namespace lld.