|
| 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 | +} |
0 commit comments