Skip to content

Commit 2e4da2c

Browse files
author
Andrew Chambers
committed
[lib] Adding Duration feature, change ht_bag to store objects not pointers
1 parent 7d772ea commit 2e4da2c

File tree

22 files changed

+238
-53
lines changed

22 files changed

+238
-53
lines changed

examples/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
add_subdirectory(tutorials)
2-

examples/tutorials/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
add_subdirectory(hello_world)
2+
add_subdirectory(simple_duration)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_executable(simple_duration simple_duration.cpp)
2+
target_link_libraries(simple_duration hawktracer)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <hawktracer.h>
2+
#include <unistd.h>
3+
4+
int main(int argc, char** argv)
5+
{
6+
ht_init(argc, argv);
7+
8+
HT_FileDumpListener* file_dump_listener = ht_file_dump_listener_create("test_output.htdump", 4096u, NULL);
9+
if (file_dump_listener != NULL)
10+
{
11+
ht_timeline_register_listener(ht_global_timeline_get(), ht_file_dump_listener_callback, file_dump_listener);
12+
}
13+
14+
ht_feature_duration_enable(ht_global_timeline_get());
15+
16+
const int NUM_MEASUREMENTS = 10;
17+
HT_DurationId ids[NUM_MEASUREMENTS];
18+
char names[NUM_MEASUREMENTS][128];
19+
20+
printf ("Gathering %d measurements\n", NUM_MEASUREMENTS);
21+
for (int i = 0; i < NUM_MEASUREMENTS; i++)
22+
{
23+
sprintf(names[i], "Measurement_name_%d", i);
24+
printf("Starting measurement %d, named %s\n", i, names[i]);
25+
ids[i] = ht_feature_duration_start_string(ht_global_timeline_get(), names[i]);
26+
sleep(1);
27+
}
28+
29+
for (int i = 0; i < NUM_MEASUREMENTS; i++)
30+
{
31+
HT_DurationNs duration = ht_feature_duration_stop(ht_global_timeline_get(), ids[i]);
32+
printf("ID: %llu, Duration: %llu nanoseconds.\n", ids[i], duration);
33+
}
34+
35+
ht_timeline_flush(ht_global_timeline_get());
36+
ht_timeline_unregister_all_listeners(ht_global_timeline_get());
37+
ht_file_dump_listener_destroy(file_dump_listener);
38+
ht_feature_duration_disable(ht_global_timeline_get());
39+
40+
ht_deinit();
41+
42+
return 0;
43+
}

lib/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(HAWKTRACER_CORE_HEADERS
1313
include/hawktracer/events.h
1414
include/hawktracer/feature_cached_string.h
1515
include/hawktracer/feature_callstack.h
16+
include/hawktracer/feature_duration.h
1617
include/hawktracer/global_timeline.h
1718
include/hawktracer/init.h
1819
include/hawktracer/listener_buffer.h
@@ -44,6 +45,7 @@ set(HAWKTRACER_CORE_SOURCES
4445
events.c
4546
feature_cached_string.c
4647
feature_callstack.c
48+
feature_duration.c
4749
global_timeline.cpp
4850
init.c
4951
listener_buffer.c

lib/bag.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include "internal/bag.h"
22
#include "hawktracer/alloc.h"
33

4+
#include <string.h>
5+
46
static inline HT_Boolean
57
_ht_bag_resize(HT_Bag* bag, size_t new_capacity)
68
{
7-
void** ptr = (void**)ht_realloc(bag->data, new_capacity * sizeof(void*));
9+
void* ptr = ht_realloc(bag->data, new_capacity * bag->element_size);
810

911
if (ptr == NULL)
1012
{
@@ -18,9 +20,9 @@ _ht_bag_resize(HT_Bag* bag, size_t new_capacity)
1820
}
1921

2022
HT_ErrorCode
21-
ht_bag_init(HT_Bag* bag, size_t min_capacity)
23+
ht_bag_init(HT_Bag* bag, size_t min_capacity, size_t element_size)
2224
{
23-
bag->data = ht_alloc(min_capacity * sizeof(void*));
25+
bag->data = ht_alloc(min_capacity * element_size);
2426

2527
if (bag->data == NULL)
2628
{
@@ -30,6 +32,7 @@ ht_bag_init(HT_Bag* bag, size_t min_capacity)
3032
bag->min_capacity = min_capacity;
3133
bag->capacity = min_capacity;
3234
bag->size = 0;
35+
bag->element_size = element_size;
3336

3437
return HT_ERR_OK;
3538
}
@@ -40,7 +43,7 @@ ht_bag_remove(HT_Bag* bag, void* data)
4043
size_t i;
4144
for (i = 0; i < bag->size; i++)
4245
{
43-
if (bag->data[i] == data)
46+
if (!memcmp(ht_bag_get_n(bag, i), data, bag->element_size))
4447
{
4548
ht_bag_remove_nth(bag, i);
4649
}
@@ -53,7 +56,7 @@ ht_bag_remove_nth(HT_Bag* bag, size_t n)
5356
bag->size--;
5457
if (bag->size > 0)
5558
{
56-
bag->data[n] = bag->data[bag->size];
59+
memcpy(ht_bag_get_n(bag, n), ht_bag_get_n(bag, bag->size), bag->element_size);
5760
}
5861

5962
if (bag->capacity > bag->min_capacity && bag->size < bag->capacity / 4)
@@ -73,7 +76,8 @@ ht_bag_add(HT_Bag* bag, void* data)
7376
}
7477
}
7578

76-
bag->data[bag->size++] = data;
79+
memcpy(ht_bag_get_n(bag, bag->size), data, bag->element_size);
80+
bag->size++;
7781

7882
return HT_ERR_OK;
7983
}
@@ -90,6 +94,13 @@ ht_bag_deinit(HT_Bag* bag)
9094
{
9195
ht_free(bag->data);
9296
bag->data = NULL;
97+
bag->element_size = 0;
9398
bag->capacity = 0;
9499
bag->size = 0;
95100
}
101+
102+
void*
103+
ht_bag_get_n(HT_Bag* bag, size_t n)
104+
{
105+
return HT_PTR_ADD(bag->data, n * bag->element_size);
106+
}

lib/feature_cached_string.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ ht_feature_cached_string_enable(HT_Timeline* timeline)
3131
return HT_ERR_OK;
3232
}
3333

34-
error_code = ht_bag_init(&feature->cached_data, 1024);
34+
error_code = ht_bag_init(&feature->cached_data, 1024, sizeof(void*));
3535

3636
if (error_code != HT_ERR_OK)
3737
{
@@ -65,7 +65,7 @@ ht_feature_cached_string_add_mapping(HT_Timeline* timeline, const char* label)
6565
assert(f);
6666

6767
ht_mutex_lock(f->lock);
68-
error_code = ht_bag_add(&f->cached_data, (void*)label);
68+
error_code = ht_bag_add(&f->cached_data, (void*)&label);
6969
ht_mutex_unlock(f->lock);
7070
if (error_code != HT_ERR_OK)
7171
{
@@ -89,7 +89,7 @@ ht_feature_cached_string_push_map(HT_Timeline* timeline)
8989

9090
for (i = 0; i < f->cached_data.size; i++)
9191
{
92-
HT_TIMELINE_PUSH_EVENT(timeline, HT_StringMappingEvent, (uintptr_t)f->cached_data.data[i], f->cached_data.data[i]);
92+
HT_TIMELINE_PUSH_EVENT(timeline, HT_StringMappingEvent, *(uintptr_t*)ht_bag_get_n(&f->cached_data, i), *(void**)ht_bag_get_n(&f->cached_data, i));
9393
}
9494

9595
ht_mutex_unlock(f->lock);

lib/feature_duration.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "hawktracer/feature_duration.h"
2+
#include "hawktracer/alloc.h"
3+
#include "hawktracer/thread.h"
4+
#include "internal/bag.h"
5+
6+
typedef struct
7+
{
8+
HT_Bag bag;
9+
} HT_FeatureDuration;
10+
11+
HT_ErrorCode
12+
ht_feature_duration_enable(HT_Timeline* timeline)
13+
{
14+
HT_FeatureDuration* feature = HT_CREATE_TYPE(HT_FeatureDuration);
15+
HT_ErrorCode error_code;
16+
17+
if (feature == NULL)
18+
{
19+
return HT_ERR_OUT_OF_MEMORY;
20+
}
21+
22+
error_code = ht_bag_init(&feature->bag, 1024, sizeof(HT_DurationStringEvent));
23+
24+
if (error_code != HT_ERR_OK)
25+
{
26+
ht_free(feature);
27+
return error_code;
28+
}
29+
30+
ht_timeline_set_feature(timeline, HT_FEATURE_DURATION, feature);
31+
32+
return HT_ERR_OK;
33+
}
34+
35+
void
36+
ht_feature_duration_disable(HT_Timeline* timeline)
37+
{
38+
HT_FeatureDuration* f = HT_TIMELINE_FEATURE(timeline, HT_FEATURE_DURATION, HT_FeatureDuration);
39+
ht_bag_deinit(&f->bag);
40+
ht_free(f);
41+
ht_timeline_set_feature(timeline, HT_FEATURE_DURATION, NULL);
42+
}
43+
44+
HT_DurationId ht_feature_duration_start(HT_Timeline* timeline, HT_DurationBaseEvent* event)
45+
{
46+
HT_FeatureDuration* f = HT_TIMELINE_FEATURE(timeline, HT_FEATURE_DURATION, HT_FeatureDuration);
47+
48+
ht_timeline_init_event(timeline, HT_EVENT(event));
49+
/* TODO: handle ht_bag_add() error */
50+
ht_bag_add(&f->bag, event);
51+
return f->bag.size - 1;
52+
}
53+
54+
HT_DurationNs ht_feature_duration_stop(HT_Timeline* timeline, HT_DurationId id)
55+
{
56+
HT_FeatureDuration* f = HT_TIMELINE_FEATURE(timeline, HT_FEATURE_DURATION, HT_FeatureDuration);
57+
HT_DurationBaseEvent* event = (HT_DurationBaseEvent*) ht_bag_get_n(&f->bag, id);
58+
59+
HT_DurationNs duration = ht_monotonic_clock_get_timestamp() - HT_EVENT(event)->timestamp;
60+
event->duration = duration;
61+
ht_timeline_push_event((HT_Timeline*)timeline, HT_EVENT(event));
62+
ht_bag_remove_nth(&f->bag, id);
63+
return duration;
64+
}
65+
66+
HT_DurationId ht_feature_duration_start_int(HT_Timeline* timeline, HT_DurationEventLabel label)
67+
{
68+
HT_DECL_EVENT(HT_DurationIntEvent, event);
69+
event.label = label;
70+
71+
return ht_feature_duration_start(timeline, (HT_DurationBaseEvent*)&event);
72+
}
73+
74+
HT_DurationId ht_feature_duration_start_string(HT_Timeline* timeline, const char* label)
75+
{
76+
HT_DECL_EVENT(HT_DurationStringEvent, event);
77+
event.label = label;
78+
79+
return ht_feature_duration_start(timeline, (HT_DurationBaseEvent*)&event);
80+
}

lib/include/hawktracer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <hawktracer/events.h>
1212
#include <hawktracer/feature_cached_string.h>
1313
#include <hawktracer/feature_callstack.h>
14+
#include <hawktracer/feature_duration.h>
1415
#include <hawktracer/global_timeline.h>
1516
#include <hawktracer/init.h>
1617
#include <hawktracer/listeners.h>

lib/include/hawktracer/base_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ typedef uint32_t HT_EventKlassId;
1414
typedef uint64_t HT_TimestampNs;
1515
/** An unsigned integer used for event identifiers. */
1616
typedef uint64_t HT_EventId;
17+
/** An unsigned integer used for duration identifiers. */
18+
typedef uint64_t HT_DurationId;
1719
/** An unsigned integer used for representing duration in nanoseconds. */
1820
typedef uint64_t HT_DurationNs;
1921
/** A standard boolean type, possible values: #HT_TRUE, #HT_FALSE */

0 commit comments

Comments
 (0)