| 1 | /* |
| 2 | * Copyright (C) 2021 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <string.h> |
| 10 | #include <stdint.h> |
| 11 | |
| 12 | #include <lttng/lttng.h> |
| 13 | #include <lttng/handle.h> |
| 14 | #include <lttng/map.h> |
| 15 | #include <lttng/map-query.h> |
| 16 | |
| 17 | #define LOG(fmt, ...) printf("# " fmt "\n", ##__VA_ARGS__); |
| 18 | #define ERR(fmt, ...) fprintf(stderr, "Error: " fmt "\n", ##__VA_ARGS__); |
| 19 | |
| 20 | int main(int argc, char *argv[]) { |
| 21 | enum lttng_map_query_status query_status; |
| 22 | enum lttng_error_code ret_code; |
| 23 | enum lttng_map_status map_status; |
| 24 | struct lttng_domain *domains = NULL; |
| 25 | struct lttng_map_content *map_content = NULL; |
| 26 | struct lttng_map_list *map_list = NULL; |
| 27 | const struct lttng_map *map = NULL; |
| 28 | const struct lttng_map_key_value_pair_list *kv_list; |
| 29 | const struct lttng_map_key_value_pair *kv_pair; |
| 30 | unsigned int map_idx, map_count, list_idx, list_count; |
| 31 | int ret, nb_domains; |
| 32 | struct lttng_map_query *map_query = NULL; |
| 33 | struct lttng_handle *handle = NULL; |
| 34 | int64_t value; |
| 35 | const char *session_name, *map_name, *key, *wanted_key = NULL; |
| 36 | |
| 37 | if (argc < 3) { |
| 38 | ERR("Missing argument(s)"); |
| 39 | ERR("Usage: %s SESSION-NAME MAP-NAME [KEY]", argv[0]); |
| 40 | ret = -1; |
| 41 | goto end; |
| 42 | } |
| 43 | |
| 44 | session_name = argv[1]; |
| 45 | map_name = argv[2]; |
| 46 | if (argc > 3) { |
| 47 | wanted_key = argv[3]; |
| 48 | } |
| 49 | |
| 50 | nb_domains = lttng_list_domains(session_name, &domains); |
| 51 | if (nb_domains < 0) { |
| 52 | ret = -1; |
| 53 | goto end; |
| 54 | } |
| 55 | |
| 56 | handle = lttng_create_handle(session_name, &domains[0]); |
| 57 | if (!handle) { |
| 58 | ret = -1; |
| 59 | goto end; |
| 60 | } |
| 61 | |
| 62 | LOG("Listing all maps of the \"%s\" session", session_name); |
| 63 | |
| 64 | ret_code = lttng_list_maps(handle, &map_list); |
| 65 | if (ret_code != LTTNG_OK) { |
| 66 | ERR("Getting list of all maps"); |
| 67 | ret = -1; |
| 68 | goto end; |
| 69 | } |
| 70 | |
| 71 | map_status = lttng_map_list_get_count(map_list, &map_count); |
| 72 | if (map_status != LTTNG_MAP_STATUS_OK) { |
| 73 | ERR("Getting the number of maps"); |
| 74 | ret = -1; |
| 75 | goto end; |
| 76 | } |
| 77 | |
| 78 | for (map_idx = 0; map_idx < map_count; map_idx++) { |
| 79 | const char *curr_map_name; |
| 80 | const struct lttng_map *curr_map; |
| 81 | curr_map = lttng_map_list_get_at_index(map_list, map_idx); |
| 82 | if (!curr_map) { |
| 83 | ERR("Getting map at index %u", map_idx); |
| 84 | ret = -1; |
| 85 | goto end; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | map_status = lttng_map_get_name(curr_map, &curr_map_name); |
| 90 | if (map_status != LTTNG_MAP_STATUS_OK) { |
| 91 | ERR("Getting the map #%u name", map_idx); |
| 92 | ret = -1; |
| 93 | goto end; |
| 94 | } |
| 95 | |
| 96 | if (strcmp(curr_map_name, map_name) == 0) { |
| 97 | LOG("Found \"%s\" map", map_name); |
| 98 | map = curr_map; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | if (!map) { |
| 103 | ERR("Can't find map \"%s\" in \"%s\" session", map_name, session_name); |
| 104 | ret = -1; |
| 105 | goto end; |
| 106 | } |
| 107 | |
| 108 | LOG("Creating a map query_object"); |
| 109 | map_query = lttng_map_query_create(LTTNG_MAP_QUERY_CONFIG_CPU_ALL, |
| 110 | LTTNG_MAP_QUERY_CONFIG_BUFFER_UST_UID_ALL, |
| 111 | LTTNG_MAP_QUERY_CONFIG_APP_BITNESS_ALL); |
| 112 | if (!map_query) { |
| 113 | ERR("Creating the map query object"); |
| 114 | ret = -1; |
| 115 | goto end; |
| 116 | } |
| 117 | |
| 118 | LOG("Query option: report values for each CPU individually"); |
| 119 | query_status = lttng_map_query_set_sum_by_cpu(map_query, false); |
| 120 | if (query_status != LTTNG_MAP_QUERY_STATUS_OK){ |
| 121 | ERR("Setting the sum by option"); |
| 122 | ret = -1; |
| 123 | goto end; |
| 124 | } |
| 125 | |
| 126 | if (wanted_key) { |
| 127 | LOG("Query option: filter in only \"%s\" key", wanted_key); |
| 128 | query_status = lttng_map_query_add_key_filter(map_query, |
| 129 | wanted_key); |
| 130 | if (query_status != LTTNG_MAP_QUERY_STATUS_OK) { |
| 131 | ERR("Setting the targeted key"); |
| 132 | ret = -1; |
| 133 | goto end; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | LOG("Execute query against the \"%s\" map", map_name); |
| 138 | ret_code = lttng_list_map_content(handle, map, map_query, &map_content); |
| 139 | if (ret_code != LTTNG_OK) { |
| 140 | ERR("Executing the query on map"); |
| 141 | ret = -1; |
| 142 | goto end; |
| 143 | } |
| 144 | |
| 145 | map_status = lttng_map_content_get_count(map_content, &list_count); |
| 146 | if (map_status != LTTNG_MAP_STATUS_OK) { |
| 147 | ERR("Getting the number of key value pair list"); |
| 148 | ret = -1; |
| 149 | goto end; |
| 150 | } |
| 151 | |
| 152 | LOG("Printing query result:"); |
| 153 | for (list_idx = 0; list_idx < list_count; list_idx++) { |
| 154 | unsigned int kv_pair_idx, kv_pair_count; |
| 155 | uint64_t cpu; |
| 156 | |
| 157 | kv_list = lttng_map_content_get_at_index(map_content, list_idx); |
| 158 | if (!kv_list) { |
| 159 | ERR("Getting key value pair list at index 0"); |
| 160 | ret = -1; |
| 161 | goto end; |
| 162 | } |
| 163 | |
| 164 | cpu = lttng_map_key_value_pair_list_get_cpu(kv_list); |
| 165 | |
| 166 | LOG("=== CPU: %"PRIu64" ===", cpu); |
| 167 | |
| 168 | map_status = lttng_map_key_value_pair_list_get_count(kv_list, |
| 169 | &kv_pair_count); |
| 170 | |
| 171 | for (kv_pair_idx = 0; kv_pair_idx < kv_pair_count; kv_pair_idx++) { |
| 172 | kv_pair = lttng_map_key_value_pair_list_get_at_index( |
| 173 | kv_list, kv_pair_idx); |
| 174 | if (!kv_pair) { |
| 175 | ERR("Getting key value pair at index %u", |
| 176 | kv_pair_idx); |
| 177 | ret = -1; |
| 178 | goto end; |
| 179 | } |
| 180 | lttng_map_key_value_pair_get_key(kv_pair, &key); |
| 181 | lttng_map_key_value_pair_get_value(kv_pair, &value); |
| 182 | |
| 183 | LOG("Key: \"%s\", value: %"PRId64, key, value); |
| 184 | } |
| 185 | LOG(); |
| 186 | } |
| 187 | |
| 188 | ret = 0; |
| 189 | end: |
| 190 | lttng_map_query_destroy(map_query); |
| 191 | lttng_destroy_handle(handle); |
| 192 | lttng_map_content_destroy(map_content); |
| 193 | lttng_map_list_destroy(map_list); |
| 194 | return ret; |
| 195 | } |