Branch data Line data Source code
1 : : /* 2 : : * Copyright (C) 2023 James Westman <james@jwestman.net> 3 : : * 4 : : * This library is free software; you can redistribute it and/or 5 : : * modify it under the terms of the GNU Lesser General Public 6 : : * License as published by the Free Software Foundation; either 7 : : * version 2.1 of the License, or (at your option) any later version. 8 : : * 9 : : * This library is distributed in the hope that it will be useful, 10 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 : : * Lesser General Public License for more details. 13 : : * 14 : : * You should have received a copy of the GNU Lesser General Public 15 : : * License along with this library; if not, see <https://www.gnu.org/licenses/>. 16 : : */ 17 : : 18 : : 19 : : #pragma once 20 : : 21 : : 22 : : #if SHUMATE_HAS_SYSPROF 23 : : 24 : : #include <sysprof-capture.h> 25 : : 26 : : #define SHUMATE_PROFILE_CURRENT_TIME SYSPROF_CAPTURE_CURRENT_TIME 27 : : 28 : : typedef struct { 29 : : gint64 begin; 30 : : char *name; 31 : : gboolean exited : 1; 32 : : gboolean free_name : 1; 33 : : } ShumateProfileScope; 34 : : 35 : : static inline void 36 : 36 : shumate_profile_scope_end_with_desc (ShumateProfileScope *self, 37 : : const char *desc) 38 : : { 39 [ + + ]: 36 : if (self->exited) 40 : : return; 41 : : 42 : 21 : self->exited = TRUE; 43 : : 44 : 42 : sysprof_collector_mark (self->begin, 45 : 21 : SYSPROF_CAPTURE_CURRENT_TIME - self->begin, 46 : : "shumate", 47 : 21 : self->name, 48 : : desc); 49 : : 50 [ + + ]: 21 : if (self->free_name) 51 : 12 : g_free (self->name); 52 : : } 53 : : 54 : : static inline void 55 : 21 : shumate_profile_scope_end (ShumateProfileScope *self) 56 : : { 57 : 21 : shumate_profile_scope_end_with_desc (self, NULL); 58 : : } 59 : : 60 : 21 : G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(ShumateProfileScope, shumate_profile_scope_end) 61 : : 62 : : #define SHUMATE_PROFILE_COLLECT(begin, name, desc) \ 63 : : sysprof_collector_mark (begin, \ 64 : : SYSPROF_CAPTURE_CURRENT_TIME - begin, \ 65 : : "shumate", \ 66 : : name, \ 67 : : desc); 68 : : 69 : : #define SHUMATE_PROFILE_START() \ 70 : : G_GNUC_UNUSED g_auto(ShumateProfileScope) __profile_scope = { \ 71 : : .begin = SYSPROF_CAPTURE_CURRENT_TIME, \ 72 : : .name = ((char *)__PRETTY_FUNCTION__), \ 73 : : .free_name = FALSE, \ 74 : : .exited = FALSE, \ 75 : : } 76 : : 77 : : #define SHUMATE_PROFILE_START_NAMED(var_name) \ 78 : : G_GNUC_UNUSED g_auto(ShumateProfileScope) __profile_scope_##var_name = { \ 79 : : .begin = SYSPROF_CAPTURE_CURRENT_TIME, \ 80 : : .name = g_strdup_printf ("%s -- %s", __FUNCTION__, G_STRINGIFY (var_name)), \ 81 : : .free_name = TRUE, \ 82 : : .exited = FALSE, \ 83 : : } 84 : : 85 : : #define SHUMATE_PROFILE_END(desc) \ 86 : : shumate_profile_scope_end_with_desc (&__profile_scope, desc) 87 : : 88 : : #define SHUMATE_PROFILE_END_NAMED(var_name, desc) \ 89 : : shumate_profile_scope_end_with_desc (&__profile_scope_##var_name, desc) 90 : : 91 : : #else 92 : : 93 : : #define SHUMATE_PROFILE_CURRENT_TIME ((gint64) -1) 94 : : 95 : : #define SHUMATE_PROFILE_COLLECT(begin, name, desc) 96 : : #define SHUMATE_PROFILE_START() 97 : : #define SHUMATE_PROFILE_START_NAMED(var_name) 98 : : #define SHUMATE_PROFILE_END(desc) 99 : : #define SHUMATE_PROFILE_END_NAMED(var_name, desc) 100 : : 101 : : #endif 102 : :