| 1 | #ifndef BABELTRACE_GRAPH_CLOCK_SNAPSHOT_SET_H |
| 2 | #define BABELTRACE_GRAPH_CLOCK_SNAPSHOT_SET_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright 2018 Philippe Proulx <pproulx@efficios.com> |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include <stdint.h> |
| 27 | #include <glib.h> |
| 28 | #include "common/assert.h" |
| 29 | |
| 30 | #include "clock-snapshot.h" |
| 31 | #include "clock-class.h" |
| 32 | |
| 33 | struct bt_clock_snapshot_set { |
| 34 | /* Unique objects owned by this */ |
| 35 | GPtrArray *clock_snapshots; |
| 36 | |
| 37 | /* Weak; points to one of the clock snapshots above */ |
| 38 | struct bt_clock_snapshot *default_cs; |
| 39 | }; |
| 40 | |
| 41 | static inline |
| 42 | int bt_clock_snapshot_set_initialize(struct bt_clock_snapshot_set *cs_set) |
| 43 | { |
| 44 | int ret = 0; |
| 45 | |
| 46 | cs_set->clock_snapshots = g_ptr_array_sized_new(1); |
| 47 | if (!cs_set->clock_snapshots) { |
| 48 | #ifdef BT_LOGE_STR |
| 49 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
| 50 | #endif |
| 51 | |
| 52 | ret = -1; |
| 53 | goto end; |
| 54 | } |
| 55 | |
| 56 | cs_set->default_cs = NULL; |
| 57 | |
| 58 | end: |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | static inline |
| 63 | void bt_clock_snapshot_set_reset(struct bt_clock_snapshot_set *cs_set) |
| 64 | { |
| 65 | uint64_t i; |
| 66 | |
| 67 | BT_ASSERT(cs_set); |
| 68 | BT_ASSERT(cs_set->clock_snapshots); |
| 69 | |
| 70 | for (i = 0; i < cs_set->clock_snapshots->len; i++) { |
| 71 | struct bt_clock_snapshot *cs = cs_set->clock_snapshots->pdata[i]; |
| 72 | |
| 73 | BT_ASSERT(cs); |
| 74 | bt_clock_snapshot_reset(cs); |
| 75 | } |
| 76 | |
| 77 | cs_set->default_cs = NULL; |
| 78 | } |
| 79 | |
| 80 | static inline |
| 81 | void bt_clock_snapshot_set_finalize(struct bt_clock_snapshot_set *cs_set) |
| 82 | { |
| 83 | uint64_t i; |
| 84 | |
| 85 | BT_ASSERT(cs_set); |
| 86 | |
| 87 | if (cs_set->clock_snapshots) { |
| 88 | for (i = 0; i < cs_set->clock_snapshots->len; i++) { |
| 89 | struct bt_clock_snapshot *cs = |
| 90 | cs_set->clock_snapshots->pdata[i]; |
| 91 | |
| 92 | BT_ASSERT(cs); |
| 93 | bt_clock_snapshot_recycle(cs); |
| 94 | } |
| 95 | |
| 96 | g_ptr_array_free(cs_set->clock_snapshots, TRUE); |
| 97 | } |
| 98 | |
| 99 | cs_set->default_cs = NULL; |
| 100 | } |
| 101 | |
| 102 | static inline |
| 103 | int bt_clock_snapshot_set_set_clock_snapshot(struct bt_clock_snapshot_set *cs_set, |
| 104 | struct bt_clock_class *cc, uint64_t raw_value) |
| 105 | { |
| 106 | int ret = 0; |
| 107 | struct bt_clock_snapshot *clock_snapshot = NULL; |
| 108 | uint64_t i; |
| 109 | |
| 110 | BT_ASSERT(cs_set); |
| 111 | BT_ASSERT(cc); |
| 112 | |
| 113 | /* |
| 114 | * Check if we already have a value for this clock class. |
| 115 | * |
| 116 | * TODO: When we have many clock classes, make this more |
| 117 | * efficient. |
| 118 | */ |
| 119 | for (i = 0; i < cs_set->clock_snapshots->len; i++) { |
| 120 | struct bt_clock_snapshot *cs = cs_set->clock_snapshots->pdata[i]; |
| 121 | |
| 122 | BT_ASSERT(cs); |
| 123 | |
| 124 | if (cs->clock_class == cc) { |
| 125 | clock_snapshot = cs; |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (!clock_snapshot) { |
| 131 | clock_snapshot = bt_clock_snapshot_create(cc); |
| 132 | if (!clock_snapshot) { |
| 133 | #ifdef BT_LIB_LOGE |
| 134 | BT_LIB_LOGE("Cannot create a clock snapshot from a clock class: " |
| 135 | "%![cc-]+K", cc); |
| 136 | #endif |
| 137 | |
| 138 | ret = -1; |
| 139 | goto end; |
| 140 | } |
| 141 | |
| 142 | g_ptr_array_add(cs_set->clock_snapshots, clock_snapshot); |
| 143 | } |
| 144 | |
| 145 | bt_clock_snapshot_set_raw_value(clock_snapshot, raw_value); |
| 146 | |
| 147 | end: |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | static inline |
| 152 | void bt_clock_snapshot_set_set_default_clock_snapshot( |
| 153 | struct bt_clock_snapshot_set *cs_set, uint64_t raw_value) |
| 154 | { |
| 155 | BT_ASSERT(cs_set); |
| 156 | BT_ASSERT(cs_set->default_cs); |
| 157 | bt_clock_snapshot_set_raw_value(cs_set->default_cs, raw_value); |
| 158 | } |
| 159 | |
| 160 | #endif /* BABELTRACE_GRAPH_CLOCK_SNAPSHOT_SET_H */ |