| 1 | /* |
| 2 | * trace_handle.c |
| 3 | * |
| 4 | * Babeltrace Library |
| 5 | * |
| 6 | * Copyright 2012 EfficiOS Inc. and Linux Foundation |
| 7 | * |
| 8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 9 | * Julien Desfossez <julien.desfossez@efficios.com> |
| 10 | * |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | * SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #include <stdint.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <babeltrace/babeltrace.h> |
| 33 | #include <babeltrace/context.h> |
| 34 | #include <babeltrace/context-internal.h> |
| 35 | #include <babeltrace/trace-handle.h> |
| 36 | #include <babeltrace/trace-handle-internal.h> |
| 37 | |
| 38 | struct bt_trace_handle *bt_trace_handle_create(struct bt_context *ctx) |
| 39 | { |
| 40 | struct bt_trace_handle *th; |
| 41 | |
| 42 | if (!ctx) |
| 43 | return NULL; |
| 44 | |
| 45 | th = g_new0(struct bt_trace_handle, 1); |
| 46 | th->id = ctx->last_trace_handle_id++; |
| 47 | return th; |
| 48 | } |
| 49 | |
| 50 | void bt_trace_handle_destroy(struct bt_trace_handle *th) |
| 51 | { |
| 52 | th->format->close_trace(th->td); |
| 53 | g_free(th); |
| 54 | } |
| 55 | |
| 56 | int bt_trace_handle_get_id(struct bt_trace_handle *th) |
| 57 | { |
| 58 | if (!th) |
| 59 | return -1; |
| 60 | |
| 61 | return th->id; |
| 62 | } |
| 63 | |
| 64 | const char *bt_trace_handle_get_path(struct bt_context *ctx, int handle_id) |
| 65 | { |
| 66 | struct bt_trace_handle *handle; |
| 67 | |
| 68 | if (!ctx) |
| 69 | return NULL; |
| 70 | |
| 71 | handle = g_hash_table_lookup(ctx->trace_handles, |
| 72 | (gpointer) (unsigned long) handle_id); |
| 73 | if (!handle) |
| 74 | return NULL; |
| 75 | return handle->path; |
| 76 | } |
| 77 | |
| 78 | uint64_t bt_trace_handle_get_timestamp_begin(struct bt_context *ctx, |
| 79 | int handle_id, enum bt_clock_type type) |
| 80 | { |
| 81 | struct bt_trace_handle *handle; |
| 82 | uint64_t ret; |
| 83 | |
| 84 | if (!ctx) |
| 85 | return -1ULL; |
| 86 | |
| 87 | handle = g_hash_table_lookup(ctx->trace_handles, |
| 88 | (gpointer) (unsigned long) handle_id); |
| 89 | if (!handle) { |
| 90 | ret = -1ULL; |
| 91 | goto end; |
| 92 | } |
| 93 | if (type == BT_CLOCK_REAL) { |
| 94 | ret = handle->real_timestamp_begin; |
| 95 | } else if (type == BT_CLOCK_CYCLES) { |
| 96 | ret = handle->cycles_timestamp_begin; |
| 97 | } else { |
| 98 | ret = -1ULL; |
| 99 | } |
| 100 | |
| 101 | end: |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | uint64_t bt_trace_handle_get_timestamp_end(struct bt_context *ctx, |
| 106 | int handle_id, enum bt_clock_type type) |
| 107 | { |
| 108 | struct bt_trace_handle *handle; |
| 109 | uint64_t ret; |
| 110 | |
| 111 | if (!ctx) |
| 112 | return -1ULL; |
| 113 | |
| 114 | handle = g_hash_table_lookup(ctx->trace_handles, |
| 115 | (gpointer) (unsigned long) handle_id); |
| 116 | if (!handle) { |
| 117 | ret = -1ULL; |
| 118 | goto end; |
| 119 | } |
| 120 | if (type == BT_CLOCK_REAL) { |
| 121 | ret = handle->real_timestamp_end; |
| 122 | } else if (type == BT_CLOCK_CYCLES) { |
| 123 | ret = handle->cycles_timestamp_end; |
| 124 | } else { |
| 125 | ret = -1ULL; |
| 126 | } |
| 127 | |
| 128 | end: |
| 129 | return ret; |
| 130 | } |