| 1 |
/* sysprof-document-ctrset.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 "sysprof-document-frame-private.h" |
| 24 |
#include "sysprof-document-ctrset.h" |
| 25 |
|
| 26 |
struct _SysprofDocumentCtrset |
| 27 |
{ |
| 28 |
SysprofDocumentFrame parent_instance; |
| 29 |
}; |
| 30 |
|
| 31 |
struct _SysprofDocumentCtrsetClass |
| 32 |
{ |
| 33 |
SysprofDocumentFrameClass parent_class; |
| 34 |
}; |
| 35 |
|
| 36 |
G_DEFINE_FINAL_TYPE (SysprofDocumentCtrset, sysprof_document_ctrset, SYSPROF_TYPE_DOCUMENT_FRAME) |
| 37 |
|
| 38 |
static void |
| 39 |
sysprof_document_ctrset_class_init (SysprofDocumentCtrsetClass *klass) |
| 40 |
{ |
| 41 |
} |
| 42 |
|
| 43 |
static void |
| 44 |
sysprof_document_ctrset_init (SysprofDocumentCtrset *self) |
| 45 |
{ |
| 46 |
} |
| 47 |
|
| 48 |
guint |
| 49 |
sysprof_document_ctrset_get_n_values (SysprofDocumentCtrset *self) |
| 50 |
{ |
| 51 |
const SysprofCaptureCounterSet *ctrset; |
| 52 |
gconstpointer endptr; |
| 53 |
guint n_groups; |
| 54 |
guint n_values = 0; |
| 55 |
|
| 56 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_CTRSET (self), 0); |
| 57 |
|
| 58 |
endptr = SYSPROF_DOCUMENT_FRAME_ENDPTR (self); |
| 59 |
ctrset = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureCounterSet); |
| 60 |
|
| 61 |
n_groups = SYSPROF_DOCUMENT_FRAME_UINT16 (self, ctrset->n_values); |
| 62 |
|
| 63 |
for (guint i = 0; i < n_groups; i++) |
| 64 |
{ |
| 65 |
const SysprofCaptureCounterValues *values = &ctrset->values[i]; |
| 66 |
|
| 67 |
/* Don't allow overflowing the frame zone */ |
| 68 |
if ((gconstpointer)&values[1] > endptr) |
| 69 |
break; |
| 70 |
|
| 71 |
for (guint j = 0; j < G_N_ELEMENTS (values->ids); j++) |
| 72 |
{ |
| 73 |
if (values->ids[j] == 0) |
| 74 |
break; |
| 75 |
n_values++; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return n_values; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* sysprof_document_ctrset_get_raw_value: (skip) |
| 84 |
* @self: a #SysprofDocumentCtrset |
| 85 |
* @nth: the nth value to get |
| 86 |
* @id: (out): a location for the counter id |
| 87 |
* @value: a location to store the raw value |
| 88 |
* |
| 89 |
* The raw value is 8 bytes and may not be converted to local |
| 90 |
* byte ordering. |
| 91 |
* |
| 92 |
* @nth must be less-than sysprof_document_ctrset_get_n_values(). |
| 93 |
*/ |
| 94 |
void |
| 95 |
sysprof_document_ctrset_get_raw_value (SysprofDocumentCtrset *self, |
| 96 |
guint nth, |
| 97 |
guint *id, |
| 98 |
guint8 value[restrict 8]) |
| 99 |
{ |
| 100 |
const SysprofCaptureCounterSet *ctrset; |
| 101 |
guint group; |
| 102 |
guint pos; |
| 103 |
|
| 104 |
g_return_if_fail (SYSPROF_IS_DOCUMENT_CTRSET (self)); |
| 105 |
g_return_if_fail (nth < sysprof_document_ctrset_get_n_values (self)); |
| 106 |
g_return_if_fail (value != NULL); |
| 107 |
|
| 108 |
group = nth / 8; |
| 109 |
pos = nth % 8; |
| 110 |
|
| 111 |
ctrset = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureCounterSet); |
| 112 |
|
| 113 |
*id = SYSPROF_DOCUMENT_FRAME_UINT32 (self, ctrset->values[group].ids[pos]); |
| 114 |
|
| 115 |
memcpy (value, &ctrset->values[group].values[pos], 8); |
| 116 |
} |