| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | Copyright 2023 Joel Svensson svenssonjoel@yahoo.se | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 3 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program 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 | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include "lbm_prof.h" | ||
| 19 | #include "platform_mutex.h" | ||
| 20 | |||
| 21 | static lbm_uint num_samples = 0; | ||
| 22 | static lbm_uint num_system_samples = 0; | ||
| 23 | static lbm_uint num_sleep_samples = 0; | ||
| 24 | extern eval_context_t *ctx_running; | ||
| 25 | extern lbm_mutex_t qmutex; | ||
| 26 | extern bool qmutex_initialized; | ||
| 27 | extern volatile bool lbm_system_sleeping; | ||
| 28 | |||
| 29 | static lbm_prof_t *prof_data; | ||
| 30 | static lbm_uint prof_data_num; | ||
| 31 | |||
| 32 | #define TRUNC_SIZE(N) (((N) > LBM_PROF_MAX_NAME_SIZE -1) ? LBM_PROF_MAX_NAME_SIZE-1 : N) | ||
| 33 | |||
| 34 | ✗ | bool lbm_prof_init(lbm_prof_t *prof_data_buf, | |
| 35 | lbm_uint prof_data_buf_num) { | ||
| 36 | ✗ | if (qmutex_initialized && prof_data_buf && prof_data_buf_num > 0) { | |
| 37 | ✗ | num_samples = 0; | |
| 38 | ✗ | num_system_samples = 0; | |
| 39 | ✗ | num_sleep_samples = 0; | |
| 40 | ✗ | prof_data_num = prof_data_buf_num; | |
| 41 | ✗ | prof_data = prof_data_buf; | |
| 42 | ✗ | for (lbm_uint i = 0; i < prof_data_num; i ++) { | |
| 43 | ✗ | prof_data_buf[i].cid = -1; | |
| 44 | ✗ | prof_data[i].has_name = false; | |
| 45 | ✗ | memset(&prof_data_buf[i].name, 0, LBM_PROF_MAX_NAME_SIZE); | |
| 46 | ✗ | prof_data_buf[i].count = 0; | |
| 47 | } | ||
| 48 | ✗ | return true; | |
| 49 | } | ||
| 50 | ✗ | return false; | |
| 51 | } | ||
| 52 | |||
| 53 | ✗ | lbm_uint lbm_prof_get_num_samples(void) { | |
| 54 | ✗ | return num_samples; | |
| 55 | } | ||
| 56 | |||
| 57 | ✗ | lbm_uint lbm_prof_get_num_system_samples(void) { | |
| 58 | ✗ | return num_system_samples; | |
| 59 | } | ||
| 60 | |||
| 61 | ✗ | lbm_uint lbm_prof_get_num_sleep_samples(void) { | |
| 62 | ✗ | return num_sleep_samples; | |
| 63 | } | ||
| 64 | |||
| 65 | ✗ | void lbm_prof_sample(void) { | |
| 66 | ✗ | num_samples ++; | |
| 67 | |||
| 68 | // Lock mutex so context cannot be destroyed until | ||
| 69 | // we are done storing a sample. | ||
| 70 | ✗ | lbm_mutex_lock(&qmutex); | |
| 71 | ✗ | eval_context_t *curr = ctx_running; | |
| 72 | ✗ | if (curr != NULL) { | |
| 73 | ✗ | lbm_cid id = curr->id; | |
| 74 | ✗ | char *name = curr->name; | |
| 75 | ✗ | lbm_uint name_len = 0; | |
| 76 | ✗ | bool doing_gc = false; | |
| 77 | ✗ | if (curr->state & LBM_THREAD_STATE_GC_BIT) { | |
| 78 | ✗ | doing_gc = true; | |
| 79 | } | ||
| 80 | ✗ | if (name) name_len = strlen(name) + 1; | |
| 81 | ✗ | for (lbm_uint i = 0; i < prof_data_num; i ++) { | |
| 82 | ✗ | if (prof_data[i].cid == -1) { | |
| 83 | // add new sample: | ||
| 84 | ✗ | prof_data[i].cid = id; | |
| 85 | ✗ | prof_data[i].count = 1; | |
| 86 | ✗ | prof_data[i].gc_count = doing_gc ? 1 : 0; | |
| 87 | ✗ | if (name) { | |
| 88 | ✗ | memcpy(&prof_data[i].name, name, TRUNC_SIZE(name_len)); | |
| 89 | ✗ | prof_data[i].name[LBM_PROF_MAX_NAME_SIZE - 1] = 0; | |
| 90 | ✗ | prof_data[i].has_name = true; | |
| 91 | } | ||
| 92 | ✗ | break; | |
| 93 | } | ||
| 94 | ✗ | if (prof_data[i].cid == id && | |
| 95 | ✗ | prof_data[i].has_name && | |
| 96 | ✗ | name != NULL && | |
| 97 | ✗ | strncmp(prof_data[i].name, name, TRUNC_SIZE(name_len)) == 0) { | |
| 98 | // found a named existing measurement. | ||
| 99 | ✗ | prof_data[i].count ++; | |
| 100 | ✗ | prof_data[i].gc_count += doing_gc ? 1 : 0; | |
| 101 | ✗ | break; | |
| 102 | } | ||
| 103 | ✗ | if (prof_data[i].cid == id && | |
| 104 | ✗ | !prof_data[i].has_name && | |
| 105 | name == NULL) { | ||
| 106 | ✗ | prof_data[i].count ++; | |
| 107 | ✗ | prof_data[i].gc_count += doing_gc ? 1 : 0; | |
| 108 | ✗ | break; | |
| 109 | } | ||
| 110 | } | ||
| 111 | } else { | ||
| 112 | ✗ | if (lbm_system_sleeping) { | |
| 113 | ✗ | num_sleep_samples ++; | |
| 114 | } else { | ||
| 115 | ✗ | num_system_samples ++; | |
| 116 | } | ||
| 117 | } | ||
| 118 | ✗ | lbm_mutex_unlock(&qmutex); | |
| 119 | ✗ | } | |
| 120 |