Kerberos: add kerberos_inject_longterm_key() helper function
[wireshark-sm.git] / ui / io_graph_item.c
blob7d826a4b04157073a5ef054584f447e9f159ad03
1 /* io_graph_item.h
2 * Definitions and functions for I/O graph items
4 * Copied from gtk/io_stat.c, (c) 2002 Ronnie Sahlberg
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "config.h"
16 #include <epan/epan_dissect.h>
18 #include <wsutil/application_flavor.h>
20 #include "ui/io_graph_item.h"
22 // XXX - constant defined elsewhere, any benefit to store it in a global .h ?
23 #define MICROSECS_PER_SEC 1000000
25 int64_t get_io_graph_index(packet_info *pinfo, int interval) {
26 nstime_t time_delta;
28 ws_return_val_if(interval <= 0, -1);
31 * Find in which interval this is supposed to go and store the interval index as idx
33 time_delta = pinfo->rel_ts;
34 if (time_delta.nsecs<0) {
35 time_delta.secs--;
36 time_delta.nsecs += 1000000000;
38 if (time_delta.secs<0) {
39 return -1;
41 return ((time_delta.secs*INT64_C(1000000) + time_delta.nsecs/1000) / interval);
44 GString *check_field_unit(const char *field_name, int *hf_index, io_graph_item_unit_t item_unit)
46 GString *err_str = NULL;
47 if (item_unit >= IOG_ITEM_UNIT_CALC_SUM) {
48 header_field_info *hfi;
50 const char *item_unit_names[NUM_IOG_ITEM_UNITS+1] = {
51 "Packets",
52 "Bytes",
53 "Bits",
54 "SUM",
55 "COUNT FRAMES",
56 "COUNT FIELDS",
57 "MAX",
58 "MIN",
59 "AVG",
60 "THROUGHPUT",
61 "LOAD",
62 NULL
65 if (application_flavor_is_stratoshark()) {
66 item_unit_names[0] = "Events";
69 /* There was no field specified */
70 if ((field_name == NULL) || (field_name[0] == 0)) {
71 err_str = g_string_new("You didn't specify a field name.");
72 return err_str;
75 /* The field could not be found */
76 hfi = proto_registrar_get_byname(field_name);
77 if (hfi == NULL) {
78 err_str = g_string_new("");
79 g_string_printf(err_str, "There is no field named '%s'.", field_name);
80 return err_str;
83 if (hf_index) *hf_index = hfi->id;
85 /* Check that the type is compatible */
86 switch (hfi->type) {
87 case FT_UINT8:
88 case FT_UINT16:
89 case FT_UINT24:
90 case FT_UINT32:
91 case FT_UINT64:
92 case FT_INT8:
93 case FT_INT16:
94 case FT_INT24:
95 case FT_INT32:
96 case FT_INT64:
97 case FT_FLOAT:
98 case FT_DOUBLE:
99 /* These values support all calculations except LOAD */
100 switch (item_unit) {
101 case IOG_ITEM_UNIT_CALC_LOAD:
102 err_str = g_string_new("LOAD is only supported for relative-time fields.");
103 default:
104 break;
106 /* These types support all calculations */
107 break;
108 case FT_RELATIVE_TIME:
109 /* This type only supports COUNT, MAX, MIN, AVG */
110 switch (item_unit) {
111 case IOG_ITEM_UNIT_CALC_SUM:
112 case IOG_ITEM_UNIT_CALC_FRAMES:
113 case IOG_ITEM_UNIT_CALC_FIELDS:
114 case IOG_ITEM_UNIT_CALC_MAX:
115 case IOG_ITEM_UNIT_CALC_MIN:
116 case IOG_ITEM_UNIT_CALC_AVERAGE:
117 case IOG_ITEM_UNIT_CALC_THROUGHPUT:
118 case IOG_ITEM_UNIT_CALC_LOAD:
119 break;
120 default:
121 ws_assert(item_unit < NUM_IOG_ITEM_UNITS);
122 err_str = g_string_new("");
123 g_string_printf(err_str, "\"%s\" is a relative-time field. %s calculations are not supported on it.",
124 field_name,
125 item_unit_names[item_unit]);
127 break;
128 default:
129 if ((item_unit != IOG_ITEM_UNIT_CALC_FRAMES) &&
130 (item_unit != IOG_ITEM_UNIT_CALC_FIELDS)) {
131 err_str = g_string_new("");
132 g_string_printf(err_str, "\"%s\" doesn't have integral or float values. %s calculations are not supported on it.",
133 field_name,
134 item_unit_names[item_unit]);
136 break;
139 return err_str;
142 // Adapted from get_it_value in gtk/io_stat.c.
143 double get_io_graph_item(const io_graph_item_t *items_, io_graph_item_unit_t val_units_, int idx, int hf_index_, const capture_file *cap_file, int interval_, int cur_idx_, bool asAOT)
145 double value = 0; /* FIXME: loss of precision, visible on the graph for small values */
146 int adv_type;
147 const io_graph_item_t *item;
148 uint32_t interval;
150 ws_return_val_if(idx < 0, 0);
152 item = &items_[idx];
154 // Basic units
155 // XXX - Should we divide these counted values by the interval
156 // so that they measure rates (as done with LOAD)? That might be
157 // more meaningful and consistent.
158 switch (val_units_) {
159 case IOG_ITEM_UNIT_PACKETS:
160 return asAOT ? MICROSECS_PER_SEC*item->frames/interval_ : item->frames;
161 case IOG_ITEM_UNIT_BYTES:
162 return (double)(asAOT ? MICROSECS_PER_SEC*item->bytes/interval_ : item->bytes);
163 case IOG_ITEM_UNIT_BITS:
164 return (double)(asAOT ? MICROSECS_PER_SEC*item->bytes*8/interval_ : item->bytes*8);
165 case IOG_ITEM_UNIT_CALC_FRAMES:
166 return item->frames;
167 case IOG_ITEM_UNIT_CALC_FIELDS:
168 return (double) item->fields;
169 default:
170 /* If it's COUNT_TYPE_ADVANCED but not one of the
171 * generic ones we'll get it when we switch on the
172 * adv_type below. */
173 break;
176 if (hf_index_ < 0) {
177 return 0;
179 // Advanced units
180 adv_type = proto_registrar_get_ftype(hf_index_);
181 switch (adv_type) {
183 case FT_INT8:
184 case FT_INT16:
185 case FT_INT24:
186 case FT_INT32:
187 case FT_INT40:
188 case FT_INT48:
189 case FT_INT56:
190 case FT_INT64:
191 switch (val_units_) {
192 case IOG_ITEM_UNIT_CALC_SUM:
193 value = item->double_tot;
194 break;
195 case IOG_ITEM_UNIT_CALC_MAX:
196 value = (double)item->int_max;
197 break;
198 case IOG_ITEM_UNIT_CALC_MIN:
199 value = (double)item->int_min;
200 break;
201 case IOG_ITEM_UNIT_CALC_THROUGHPUT:
202 value = item->double_tot*MICROSECS_PER_SEC/interval_;
203 break;
204 case IOG_ITEM_UNIT_CALC_AVERAGE:
205 if (item->fields) {
206 value = item->double_tot / item->fields;
207 } else {
208 value = 0;
210 break;
211 default:
212 break;
214 break;
216 case FT_UINT8:
217 case FT_UINT16:
218 case FT_UINT24:
219 case FT_UINT32:
220 case FT_UINT40:
221 case FT_UINT48:
222 case FT_UINT56:
223 case FT_UINT64:
224 switch (val_units_) {
225 case IOG_ITEM_UNIT_CALC_SUM:
226 value = item->double_tot;
227 break;
228 case IOG_ITEM_UNIT_CALC_MAX:
229 value = (double)item->uint_max;
230 break;
231 case IOG_ITEM_UNIT_CALC_MIN:
232 value = (double)item->uint_min;
233 break;
234 case IOG_ITEM_UNIT_CALC_THROUGHPUT:
235 value = item->double_tot*MICROSECS_PER_SEC/interval_;
236 break;
237 case IOG_ITEM_UNIT_CALC_AVERAGE:
238 if (item->fields) {
239 value = item->double_tot / item->fields;
240 } else {
241 value = 0;
243 break;
244 default:
245 break;
247 break;
249 case FT_DOUBLE:
250 case FT_FLOAT:
251 switch (val_units_) {
252 case IOG_ITEM_UNIT_CALC_SUM:
253 value = item->double_tot;
254 break;
255 case IOG_ITEM_UNIT_CALC_MAX:
256 value = item->double_max;
257 break;
258 case IOG_ITEM_UNIT_CALC_MIN:
259 value = item->double_min;
260 break;
261 case IOG_ITEM_UNIT_CALC_AVERAGE:
262 if (item->fields) {
263 value = item->double_tot / item->fields;
264 } else {
265 value = 0;
267 break;
268 default:
269 break;
271 break;
273 case FT_RELATIVE_TIME:
274 switch (val_units_) {
275 case IOG_ITEM_UNIT_CALC_MAX:
276 value = nstime_to_sec(&item->time_max);
277 break;
278 case IOG_ITEM_UNIT_CALC_MIN:
279 value = nstime_to_sec(&item->time_min);
280 break;
281 case IOG_ITEM_UNIT_CALC_SUM:
282 value = nstime_to_sec(&item->time_tot);
283 break;
284 case IOG_ITEM_UNIT_CALC_AVERAGE:
285 if (item->fields) {
286 value = nstime_to_sec(&item->time_tot) / item->fields;
287 } else {
288 value = 0;
290 break;
291 case IOG_ITEM_UNIT_CALC_LOAD:
292 // "LOAD graphs plot the QUEUE-depth of the connection over time"
293 // (for response time fields such as smb.time, rpc.time, etc.)
294 // This interval is expressed in microseconds.
295 if (idx == cur_idx_ && cap_file) {
296 // If this is the last interval, it may not be full width.
297 uint64_t start_us = (uint64_t)interval_ * idx;
298 nstime_t timediff = NSTIME_INIT_SECS_USECS(start_us / 1000000, start_us % 1000000);
299 nstime_delta(&timediff, &cap_file->elapsed_time, &timediff);
300 interval = (uint32_t)(1000*nstime_to_msec(&timediff) + 0.5);
301 } else {
302 interval = interval_;
304 value = (1000 * nstime_to_msec(&item->time_tot)) / interval;
305 break;
306 default:
307 break;
309 break;
310 default:
311 break;
313 return value;