Skip to content

Commit be22985

Browse files
committed
update the sample with more usage examples
1 parent 5666ea0 commit be22985

9 files changed

Lines changed: 82 additions & 23 deletions

File tree

cpp/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Lightweight wrapper around `__itt_string_handle*`.
124124
```cpp
125125
ittapi::StringHandle h{"my_handle"};
126126
h.valid(); // true if handle was created
127-
h.native_handle(); // underlying __itt_string_handle*
127+
h.get(); // underlying __itt_string_handle*
128128
```
129129
130130
#### `ittapi::Domain`
@@ -170,7 +170,7 @@ RAII wrapper for region begin/end.
170170
RAII wrapper for frame begin/end. Supports explicit timestamp submission.
171171

172172
```cpp
173-
ittapi::ScopedFrame::submit(domain.native_handle(), begin_ts, end_ts);
173+
ittapi::ScopedFrame::submit(domain.get(), begin_ts, end_ts);
174174
```
175175
176176
#### `ittapi::ScopedPause`

cpp/include/ittapi_domain.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Domain
3333
}
3434
#endif
3535

36-
__itt_domain* native_handle() const noexcept
36+
__itt_domain* get() const noexcept
3737
{
3838
return m_domain;
3939
}
@@ -119,12 +119,12 @@ class Domain
119119

120120
void task_begin(const StringHandle& name) const noexcept
121121
{
122-
__itt_task_begin(m_domain, detail::make_null_id(), detail::make_null_id(), name.native_handle());
122+
__itt_task_begin(m_domain, detail::make_null_id(), detail::make_null_id(), name.get());
123123
}
124124

125125
void task_begin(const StringHandle& name, __itt_id taskid, __itt_id parentid) const noexcept
126126
{
127-
__itt_task_begin(m_domain, taskid, parentid, name.native_handle());
127+
__itt_task_begin(m_domain, taskid, parentid, name.get());
128128
}
129129

130130
void task_end() const noexcept

cpp/include/ittapi_region.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ScopedRegion
6565
, m_id(detail::make_null_id())
6666
, m_active(true)
6767
{
68-
__itt_region_begin(m_domain, m_id, detail::make_null_id(), name.native_handle());
68+
__itt_region_begin(m_domain, m_id, detail::make_null_id(), name.get());
6969
}
7070

7171
ScopedRegion(const __itt_domain* domain, const StringHandle& name,
@@ -74,7 +74,7 @@ class ScopedRegion
7474
, m_id(id)
7575
, m_active(true)
7676
{
77-
__itt_region_begin(m_domain, m_id, parentid, name.native_handle());
77+
__itt_region_begin(m_domain, m_id, parentid, name.get());
7878
}
7979

8080
ScopedRegion(const ScopedRegion&) = delete;

cpp/include/ittapi_string_handle.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class StringHandle
2929
}
3030
#endif
3131

32-
__itt_string_handle* native_handle() const noexcept
32+
__itt_string_handle* get() const noexcept
3333
{
3434
return m_handle;
3535
}

cpp/include/ittapi_task.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ class ScopedTask
6060
: m_domain(domain)
6161
, m_active(true)
6262
{
63-
__itt_task_begin(m_domain, detail::make_null_id(), detail::make_null_id(), name.native_handle());
63+
__itt_task_begin(m_domain, detail::make_null_id(), detail::make_null_id(), name.get());
6464
}
6565

6666
ScopedTask(const __itt_domain* domain, const StringHandle& name,
6767
__itt_id taskid, __itt_id parentid) noexcept
6868
: m_domain(domain)
6969
, m_active(true)
7070
{
71-
__itt_task_begin(m_domain, taskid, parentid, name.native_handle());
71+
__itt_task_begin(m_domain, taskid, parentid, name.get());
7272
}
7373

7474
ScopedTask(const ScopedTask&) = delete;

cpp/samples/task_sample.cpp

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,84 @@
88
#include <chrono>
99
#include <thread>
1010

11+
static void simulate_work(int ms)
12+
{
13+
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
14+
}
15+
1116
int main()
1217
{
18+
// Thread naming
1319
ittapi::set_thread_name("main");
14-
ittapi::Domain domain{"example.task"};
20+
21+
// Domain and string handle creation
22+
ittapi::Domain domain{"example.app"};
1523
ittapi::StringHandle task_name{"process"};
1624

25+
// Collection control — pause/resume
1726
ittapi::pause();
1827
ittapi::resume();
1928

20-
29+
// Scoped task with pre-created StringHandle (zero-overhead path)
2130
{
2231
auto task = domain.task(task_name);
23-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
32+
simulate_work(10);
2433
}
2534

26-
35+
// Scoped task with inline string (convenience path)
2736
{
2837
auto task = domain.task("startup");
29-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
38+
simulate_work(10);
39+
}
40+
41+
// Scoped task with early end
42+
{
43+
auto task = domain.task("partial_work");
44+
simulate_work(5);
45+
task.end(); // end early, destructor is a no-op
46+
}
47+
48+
// Scoped task with IDs for parent-child relationships
49+
{
50+
__itt_id parent_id = __itt_id_make(nullptr, 1);
51+
auto parent = domain.task("parent_task", parent_id, __itt_null);
52+
53+
__itt_id child_id = __itt_id_make(nullptr, 2);
54+
auto child = domain.task("child_task", child_id, parent_id);
55+
simulate_work(5);
56+
}
57+
58+
// Manual task begin/end (non-RAII)
59+
domain.task_begin("manual_work");
60+
simulate_work(5);
61+
domain.task_end();
62+
63+
// Scoped region
64+
{
65+
auto region = domain.region("init_phase");
66+
simulate_work(10);
67+
}
68+
69+
// Scoped frame
70+
{
71+
auto frame = domain.frame();
72+
simulate_work(10);
73+
}
74+
75+
// Frame submit with explicit timestamps
76+
{
77+
__itt_timestamp begin = __itt_get_timestamp();
78+
simulate_work(10);
79+
__itt_timestamp end = __itt_get_timestamp();
80+
ittapi::ScopedFrame::submit(domain.get(), begin, end);
81+
}
82+
83+
// Scoped pause — collection paused within scope
84+
{
85+
ittapi::ScopedPause sp;
86+
simulate_work(10); // not collected
87+
sp.resume_now(); // resume early
88+
simulate_work(10); // collected
3089
}
3190

3291
return 0;

cpp/tests/test_frame.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static void test_submit()
4646
{
4747
ittapi::Domain d{"test.frame.submit"};
4848
// Just verify it compiles and runs without a collector
49-
ittapi::ScopedFrame::submit(d.native_handle(), 0, 100);
49+
ittapi::ScopedFrame::submit(d.get(), 0, 100);
5050
}
5151

5252
int main()

cpp/tests/test_helpers.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,20 @@ namespace test
5252

5353
inline void check_domain_name(const Domain& domain, const char* expected)
5454
{
55-
if (domain.native_handle() == nullptr)
55+
if (domain.get() == nullptr)
5656
{
5757
return; // no collector attached — nothing to verify
5858
}
59-
CHECK_STR_EQ(domain.native_handle()->nameA, expected);
59+
CHECK_STR_EQ(domain.get()->nameA, expected);
6060
}
6161

6262
inline void check_string_handle_name(const StringHandle& handle, const char* expected)
6363
{
64-
if (handle.native_handle() == nullptr)
64+
if (handle.get() == nullptr)
6565
{
6666
return; // no collector attached — nothing to verify
6767
}
68-
CHECK_STR_EQ(handle.native_handle()->strA, expected);
68+
CHECK_STR_EQ(handle.get()->strA, expected);
6969
}
7070

7171
inline void check_id_fields(const __itt_id& id,

docs/src/itt-api-cpp-wrapper.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ Lightweight wrapper around ``__itt_string_handle*``.
154154
.. code-block:: cpp
155155
156156
ittapi::StringHandle h{"my_handle"};
157-
h.valid(); // true if handle was created
158-
h.native_handle(); // underlying __itt_string_handle*
157+
h.valid(); // true if handle was created
158+
h.get(); // underlying __itt_string_handle*
159159
160160
ittapi::Domain
161161
""""""""""""""
@@ -205,7 +205,7 @@ RAII wrapper for frame begin/end. Supports explicit timestamp submission.
205205

206206
.. code-block:: cpp
207207
208-
ittapi::ScopedFrame::submit(domain.native_handle(), begin_ts, end_ts);
208+
ittapi::ScopedFrame::submit(domain.get(), begin_ts, end_ts);
209209
210210
ittapi::ScopedPause
211211
"""""""""""""""""""

0 commit comments

Comments
 (0)