| 1 |
/* sysprof-document-counter.c |
| 2 |
* |
| 3 |
* Copyright 2023 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <math.h> |
| 24 |
|
| 25 |
#include "timsort/gtktimsortprivate.h" |
| 26 |
|
| 27 |
#include "sysprof-document-counter-private.h" |
| 28 |
#include "sysprof-document-counter-value-private.h" |
| 29 |
#include "sysprof-document-private.h" |
| 30 |
|
| 31 |
enum { |
| 32 |
PROP_0, |
| 33 |
PROP_CATEGORY, |
| 34 |
PROP_DESCRIPTION, |
| 35 |
PROP_ID, |
| 36 |
PROP_KEY, |
| 37 |
PROP_MAX_VALUE, |
| 38 |
PROP_MIN_VALUE, |
| 39 |
PROP_NAME, |
| 40 |
N_PROPS |
| 41 |
}; |
| 42 |
|
| 43 |
static guint |
| 44 |
sysprof_document_counter_get_n_items (GListModel *model) |
| 45 |
{ |
| 46 |
return sysprof_document_counter_get_n_values (SYSPROF_DOCUMENT_COUNTER (model)); |
| 47 |
} |
| 48 |
|
| 49 |
static GType |
| 50 |
sysprof_document_counter_get_item_type (GListModel *model) |
| 51 |
{ |
| 52 |
return SYSPROF_TYPE_DOCUMENT_COUNTER_VALUE; |
| 53 |
} |
| 54 |
|
| 55 |
static gpointer |
| 56 |
sysprof_document_counter_get_item (GListModel *model, |
| 57 |
guint position) |
| 58 |
{ |
| 59 |
SysprofDocumentCounter *self = SYSPROF_DOCUMENT_COUNTER (model); |
| 60 |
const SysprofDocumentTimedValue *value; |
| 61 |
|
| 62 |
if (position >= self->values->len) |
| 63 |
return NULL; |
| 64 |
|
| 65 |
value = &g_array_index (self->values, SysprofDocumentTimedValue, position); |
| 66 |
|
| 67 |
return _sysprof_document_counter_value_new (self->type, value, self); |
| 68 |
} |
| 69 |
|
| 70 |
static void |
| 71 |
list_model_iface_init (GListModelInterface *iface) |
| 72 |
{ |
| 73 |
iface->get_n_items = sysprof_document_counter_get_n_items; |
| 74 |
iface->get_item_type = sysprof_document_counter_get_item_type; |
| 75 |
iface->get_item = sysprof_document_counter_get_item; |
| 76 |
} |
| 77 |
|
| 78 |
G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofDocumentCounter, sysprof_document_counter, G_TYPE_OBJECT, |
| 79 |
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init)) |
| 80 |
|
| 81 |
static GParamSpec *properties [N_PROPS]; |
| 82 |
|
| 83 |
static void |
| 84 |
sysprof_document_counter_finalize (GObject *object) |
| 85 |
{ |
| 86 |
SysprofDocumentCounter *self = (SysprofDocumentCounter *)object; |
| 87 |
|
| 88 |
g_clear_pointer (&self->category, g_ref_string_release); |
| 89 |
g_clear_pointer (&self->description, g_ref_string_release); |
| 90 |
g_clear_pointer (&self->name, g_ref_string_release); |
| 91 |
g_clear_pointer (&self->values, g_array_unref); |
| 92 |
|
| 93 |
G_OBJECT_CLASS (sysprof_document_counter_parent_class)->finalize (object); |
| 94 |
} |
| 95 |
|
| 96 |
static void |
| 97 |
sysprof_document_counter_get_property (GObject *object, |
| 98 |
guint prop_id, |
| 99 |
GValue *value, |
| 100 |
GParamSpec *pspec) |
| 101 |
{ |
| 102 |
SysprofDocumentCounter *self = SYSPROF_DOCUMENT_COUNTER (object); |
| 103 |
|
| 104 |
switch (prop_id) |
| 105 |
{ |
| 106 |
case PROP_CATEGORY: |
| 107 |
g_value_set_string (value, sysprof_document_counter_get_category (self)); |
| 108 |
break; |
| 109 |
|
| 110 |
case PROP_DESCRIPTION: |
| 111 |
g_value_set_string (value, sysprof_document_counter_get_description (self)); |
| 112 |
break; |
| 113 |
|
| 114 |
case PROP_MIN_VALUE: |
| 115 |
g_value_set_double (value, self->min_value); |
| 116 |
break; |
| 117 |
|
| 118 |
case PROP_MAX_VALUE: |
| 119 |
g_value_set_double (value, self->max_value); |
| 120 |
break; |
| 121 |
|
| 122 |
case PROP_NAME: |
| 123 |
g_value_set_string (value, sysprof_document_counter_get_name (self)); |
| 124 |
break; |
| 125 |
|
| 126 |
case PROP_KEY: |
| 127 |
g_value_take_string (value, sysprof_document_counter_dup_key (self)); |
| 128 |
break; |
| 129 |
|
| 130 |
case PROP_ID: |
| 131 |
g_value_set_uint (value, sysprof_document_counter_get_id (self)); |
| 132 |
break; |
| 133 |
|
| 134 |
default: |
| 135 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
static void |
| 140 |
sysprof_document_counter_class_init (SysprofDocumentCounterClass *klass) |
| 141 |
{ |
| 142 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 143 |
|
| 144 |
object_class->finalize = sysprof_document_counter_finalize; |
| 145 |
object_class->get_property = sysprof_document_counter_get_property; |
| 146 |
|
| 147 |
properties [PROP_ID] = |
| 148 |
g_param_spec_uint ("id", NULL, NULL, |
| 149 |
0, G_MAXUINT, 0, |
| 150 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 151 |
|
| 152 |
properties [PROP_CATEGORY] = |
| 153 |
g_param_spec_string ("category", NULL, NULL, |
| 154 |
NULL, |
| 155 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 156 |
|
| 157 |
properties [PROP_DESCRIPTION] = |
| 158 |
g_param_spec_string ("description", NULL, NULL, |
| 159 |
NULL, |
| 160 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 161 |
|
| 162 |
properties [PROP_KEY] = |
| 163 |
g_param_spec_string ("key", NULL, NULL, |
| 164 |
NULL, |
| 165 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 166 |
|
| 167 |
properties [PROP_NAME] = |
| 168 |
g_param_spec_string ("name", NULL, NULL, |
| 169 |
NULL, |
| 170 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 171 |
|
| 172 |
properties[PROP_MIN_VALUE] = |
| 173 |
g_param_spec_double ("min-value", NULL, NULL, |
| 174 |
-G_MAXDOUBLE, G_MAXDOUBLE, 0, |
| 175 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 176 |
|
| 177 |
properties[PROP_MAX_VALUE] = |
| 178 |
g_param_spec_double ("max-value", NULL, NULL, |
| 179 |
-G_MAXDOUBLE, G_MAXDOUBLE, 0, |
| 180 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 181 |
|
| 182 |
g_object_class_install_properties (object_class, N_PROPS, properties); |
| 183 |
} |
| 184 |
|
| 185 |
static void |
| 186 |
sysprof_document_counter_init (SysprofDocumentCounter *self) |
| 187 |
{ |
| 188 |
} |
| 189 |
|
| 190 |
SysprofDocumentCounter * |
| 191 |
_sysprof_document_counter_new (guint id, |
| 192 |
guint type, |
| 193 |
GRefString *category, |
| 194 |
GRefString *name, |
| 195 |
GRefString *description, |
| 196 |
GArray *values, |
| 197 |
gint64 begin_time) |
| 198 |
{ |
| 199 |
SysprofDocumentCounter *self; |
| 200 |
|
| 201 |
self = g_object_new (SYSPROF_TYPE_DOCUMENT_COUNTER, NULL); |
| 202 |
self->id = id; |
| 203 |
self->type = type; |
| 204 |
self->category = category; |
| 205 |
self->name = name; |
| 206 |
self->description = description; |
| 207 |
self->values = values; |
| 208 |
self->begin_time = begin_time; |
| 209 |
|
| 210 |
return self; |
| 211 |
} |
| 212 |
|
| 213 |
const char * |
| 214 |
sysprof_document_counter_get_category (SysprofDocumentCounter *self) |
| 215 |
{ |
| 216 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER (self), NULL); |
| 217 |
|
| 218 |
return self->category; |
| 219 |
} |
| 220 |
|
| 221 |
const char * |
| 222 |
sysprof_document_counter_get_description (SysprofDocumentCounter *self) |
| 223 |
{ |
| 224 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER (self), NULL); |
| 225 |
|
| 226 |
if (self->description == NULL || self->description[0] == 0) |
| 227 |
return NULL; |
| 228 |
|
| 229 |
return self->description; |
| 230 |
} |
| 231 |
|
| 232 |
const char * |
| 233 |
sysprof_document_counter_get_name (SysprofDocumentCounter *self) |
| 234 |
{ |
| 235 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER (self), NULL); |
| 236 |
|
| 237 |
return self->name; |
| 238 |
} |
| 239 |
|
| 240 |
guint |
| 241 |
sysprof_document_counter_get_id (SysprofDocumentCounter *self) |
| 242 |
{ |
| 243 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER (self), 0); |
| 244 |
|
| 245 |
return self->id; |
| 246 |
} |
| 247 |
|
| 248 |
GType |
| 249 |
sysprof_document_counter_get_value_type (SysprofDocumentCounter *self) |
| 250 |
{ |
| 251 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER (self), G_TYPE_INVALID); |
| 252 |
|
| 253 |
if (self->type == SYSPROF_CAPTURE_COUNTER_INT64) |
| 254 |
return G_TYPE_INT64; |
| 255 |
|
| 256 |
if (self->type == SYSPROF_CAPTURE_COUNTER_DOUBLE) |
| 257 |
return G_TYPE_DOUBLE; |
| 258 |
|
| 259 |
g_return_val_if_reached (G_TYPE_INVALID); |
| 260 |
} |
| 261 |
|
| 262 |
guint |
| 263 |
sysprof_document_counter_get_n_values (SysprofDocumentCounter *self) |
| 264 |
{ |
| 265 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER (self), 0); |
| 266 |
|
| 267 |
return self->values->len; |
| 268 |
} |
| 269 |
|
| 270 |
void |
| 271 |
sysprof_document_counter_get_value (SysprofDocumentCounter *self, |
| 272 |
guint nth, |
| 273 |
gint64 *time, |
| 274 |
GValue *value) |
| 275 |
{ |
| 276 |
g_return_if_fail (SYSPROF_IS_DOCUMENT_COUNTER (self)); |
| 277 |
g_return_if_fail (nth < self->values->len); |
| 278 |
g_return_if_fail (value == NULL || G_IS_VALUE (value)); |
| 279 |
|
| 280 |
if (time != NULL) |
| 281 |
*time = g_array_index (self->values, SysprofDocumentTimedValue, nth).time; |
| 282 |
|
| 283 |
if (value == NULL) |
| 284 |
return; |
| 285 |
|
| 286 |
if (G_VALUE_HOLDS_INT64 (value)) |
| 287 |
g_value_set_int64 (value, g_array_index (self->values, SysprofDocumentTimedValue, nth).v_int64); |
| 288 |
else if (G_VALUE_HOLDS_DOUBLE (value)) |
| 289 |
g_value_set_double (value, g_array_index (self->values, SysprofDocumentTimedValue, nth).v_double); |
| 290 |
} |
| 291 |
|
| 292 |
gint64 |
| 293 |
sysprof_document_counter_get_value_int64 (SysprofDocumentCounter *self, |
| 294 |
guint nth, |
| 295 |
gint64 *time) |
| 296 |
{ |
| 297 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER (self), 0); |
| 298 |
g_return_val_if_fail (nth < self->values->len, 0); |
| 299 |
|
| 300 |
if (time != NULL) |
| 301 |
*time = g_array_index (self->values, SysprofDocumentTimedValue, nth).time; |
| 302 |
|
| 303 |
return g_array_index (self->values, SysprofDocumentTimedValue, nth).v_int64; |
| 304 |
} |
| 305 |
|
| 306 |
double |
| 307 |
sysprof_document_counter_get_value_double (SysprofDocumentCounter *self, |
| 308 |
guint nth, |
| 309 |
gint64 *time) |
| 310 |
{ |
| 311 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER (self), 0); |
| 312 |
g_return_val_if_fail (nth < self->values->len, 0); |
| 313 |
|
| 314 |
if (time != NULL) |
| 315 |
*time = g_array_index (self->values, SysprofDocumentTimedValue, nth).time; |
| 316 |
|
| 317 |
return g_array_index (self->values, SysprofDocumentTimedValue, nth).v_double; |
| 318 |
} |
| 319 |
|
| 320 |
static inline double |
| 321 |
value_as_double_with_correction (guint type, |
| 322 |
SysprofDocumentTimedValue *value) |
| 323 |
{ |
| 324 |
if (type == SYSPROF_CAPTURE_COUNTER_DOUBLE) |
| 325 |
{ |
| 326 |
/* sanitize awkward data coming over */ |
| 327 |
if (value->v_double > (double)G_MAXINT64) |
| 328 |
value->v_double = 0; |
| 329 |
return value->v_double; |
| 330 |
} |
| 331 |
else if (type == SYSPROF_CAPTURE_COUNTER_INT64) |
| 332 |
return value->v_int64; |
| 333 |
else |
| 334 |
return .0; |
| 335 |
} |
| 336 |
|
| 337 |
static int |
| 338 |
sort_by_time (gconstpointer aptr, |
| 339 |
gconstpointer bptr) |
| 340 |
{ |
| 341 |
const SysprofDocumentTimedValue *a = aptr; |
| 342 |
const SysprofDocumentTimedValue *b = bptr; |
| 343 |
|
| 344 |
if (a->time < b->time) |
| 345 |
return -1; |
| 346 |
|
| 347 |
if (a->time > b->time) |
| 348 |
return 1; |
| 349 |
|
| 350 |
return 0; |
| 351 |
} |
| 352 |
|
| 353 |
void |
| 354 |
_sysprof_document_counter_calculate_range (SysprofDocumentCounter *self) |
| 355 |
{ |
| 356 |
SysprofDocumentTimedValue *values; |
| 357 |
gboolean min_value_changed = FALSE; |
| 358 |
gboolean max_value_changed = FALSE; |
| 359 |
double min_value; |
| 360 |
double max_value; |
| 361 |
guint n_values; |
| 362 |
|
| 363 |
g_return_if_fail (SYSPROF_IS_DOCUMENT_COUNTER (self)); |
| 364 |
|
| 365 |
if (self->values->len == 0) |
| 366 |
return; |
| 367 |
|
| 368 |
gtk_tim_sort (self->values->data, |
| 369 |
self->values->len, |
| 370 |
sizeof (SysprofDocumentTimedValue), |
| 371 |
(GCompareDataFunc)sort_by_time, |
| 372 |
NULL); |
| 373 |
|
| 374 |
values = &g_array_index (self->values, SysprofDocumentTimedValue, 0); |
| 375 |
n_values = self->values->len; |
| 376 |
|
| 377 |
min_value = value_as_double_with_correction (self->type, &values[0]); |
| 378 |
max_value = min_value; |
| 379 |
|
| 380 |
for (guint i = 1; i < n_values; i++) |
| 381 |
{ |
| 382 |
double value = value_as_double_with_correction (self->type, &values[i]); |
| 383 |
|
| 384 |
min_value = MIN (min_value, value); |
| 385 |
max_value = MAX (max_value, value); |
| 386 |
} |
| 387 |
|
| 388 |
min_value_changed = self->min_value != min_value; |
| 389 |
max_value_changed = self->max_value != max_value; |
| 390 |
|
| 391 |
self->min_value = min_value; |
| 392 |
self->max_value = max_value; |
| 393 |
|
| 394 |
if (min_value_changed) |
| 395 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MIN_VALUE]); |
| 396 |
|
| 397 |
if (max_value_changed) |
| 398 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MAX_VALUE]); |
| 399 |
} |
| 400 |
|
| 401 |
double |
| 402 |
sysprof_document_counter_get_max_value (SysprofDocumentCounter *self) |
| 403 |
{ |
| 404 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER (self), .0); |
| 405 |
|
| 406 |
return self->max_value; |
| 407 |
} |
| 408 |
|
| 409 |
double |
| 410 |
sysprof_document_counter_get_min_value (SysprofDocumentCounter *self) |
| 411 |
{ |
| 412 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER (self), .0); |
| 413 |
|
| 414 |
return self->min_value; |
| 415 |
} |
| 416 |
|
| 417 |
char * |
| 418 |
sysprof_document_counter_dup_key (SysprofDocumentCounter *self) |
| 419 |
{ |
| 420 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_COUNTER (self), NULL); |
| 421 |
|
| 422 |
return g_strdup_printf ("%s/%s", self->category, self->name); |
| 423 |
} |