| 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 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 | 4 | bool lbm_prof_init(lbm_prof_t *prof_data_buf, | |
| 35 | lbm_uint prof_data_buf_num) { | ||
| 36 |
3/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
|
4 | if (qmutex_initialized && prof_data_buf && prof_data_buf_num > 0) { |
| 37 | 4 | num_samples = 0; | |
| 38 | 4 | num_system_samples = 0; | |
| 39 | 4 | num_sleep_samples = 0; | |
| 40 | 4 | prof_data_num = prof_data_buf_num; | |
| 41 | 4 | prof_data = prof_data_buf; | |
| 42 |
2/2✓ Branch 0 taken 400 times.
✓ Branch 1 taken 4 times.
|
404 | for (lbm_uint i = 0; i < prof_data_num; i ++) { |
| 43 | 400 | prof_data_buf[i].cid = -1; | |
| 44 | 400 | prof_data[i].has_name = false; | |
| 45 | 400 | memset(&prof_data_buf[i].name, 0, LBM_PROF_MAX_NAME_SIZE); | |
| 46 | 400 | prof_data_buf[i].count = 0; | |
| 47 | } | ||
| 48 | 4 | return true; | |
| 49 | } | ||
| 50 | ✗ | return false; | |
| 51 | } | ||
| 52 | |||
| 53 | 3 | lbm_uint lbm_prof_get_num_samples(void) { | |
| 54 | 3 | return num_samples; | |
| 55 | } | ||
| 56 | |||
| 57 | 2 | lbm_uint lbm_prof_get_num_system_samples(void) { | |
| 58 | 2 | return num_system_samples; | |
| 59 | } | ||
| 60 | |||
| 61 | 2 | lbm_uint lbm_prof_get_num_sleep_samples(void) { | |
| 62 | 2 | return num_sleep_samples; | |
| 63 | } | ||
| 64 | |||
| 65 | 2100 | void lbm_prof_sample(void) { | |
| 66 | 2100 | num_samples ++; | |
| 67 | |||
| 68 | // Lock mutex so context cannot be destroyed until | ||
| 69 | // we are done storing a sample. | ||
| 70 | 2100 | mutex_lock(&qmutex); | |
| 71 | 2100 | eval_context_t *curr = ctx_running; | |
| 72 |
2/2✓ Branch 0 taken 1000 times.
✓ Branch 1 taken 1100 times.
|
2100 | if (curr != NULL) { |
| 73 | 1000 | lbm_cid id = curr->id; | |
| 74 | 1000 | char *name = curr->name; | |
| 75 | 1000 | lbm_uint name_len = 0; | |
| 76 | 1000 | bool doing_gc = false; | |
| 77 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1000 times.
|
1000 | if (curr->state & LBM_THREAD_STATE_GC_BIT) { |
| 78 | ✗ | doing_gc = true; | |
| 79 | } | ||
| 80 |
2/2✓ Branch 0 taken 516 times.
✓ Branch 1 taken 484 times.
|
1000 | if (name) name_len = strlen(name) + 1; |
| 81 |
1/2✓ Branch 0 taken 1483 times.
✗ Branch 1 not taken.
|
1483 | for (lbm_uint i = 0; i < prof_data_num; i ++) { |
| 82 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1480 times.
|
1483 | if (prof_data[i].cid == -1) { |
| 83 | // add new sample: | ||
| 84 | 3 | prof_data[i].cid = id; | |
| 85 | 3 | prof_data[i].count = 1; | |
| 86 | 3 | prof_data[i].gc_count = doing_gc ? 1 : 0; | |
| 87 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | if (name) { |
| 88 | 1 | memcpy(&prof_data[i].name, name, TRUNC_SIZE(name_len)); | |
| 89 | 1 | prof_data[i].name[LBM_PROF_MAX_NAME_SIZE - 1] = 0; | |
| 90 | 1 | prof_data[i].has_name = true; | |
| 91 | } | ||
| 92 | 3 | break; | |
| 93 | } | ||
| 94 |
2/2✓ Branch 0 taken 997 times.
✓ Branch 1 taken 483 times.
|
1480 | if (prof_data[i].cid == id && |
| 95 |
3/4✓ Branch 0 taken 515 times.
✓ Branch 1 taken 482 times.
✓ Branch 2 taken 515 times.
✗ Branch 3 not taken.
|
997 | prof_data[i].has_name && |
| 96 | 515 | name != NULL && | |
| 97 |
1/2✓ Branch 0 taken 515 times.
✗ Branch 1 not taken.
|
515 | strncmp(prof_data[i].name, name, TRUNC_SIZE(name_len)) == 0) { |
| 98 | // found a named existing measurement. | ||
| 99 | 515 | prof_data[i].count ++; | |
| 100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 515 times.
|
515 | prof_data[i].gc_count += doing_gc ? 1 : 0; |
| 101 | 515 | break; | |
| 102 | } | ||
| 103 |
2/2✓ Branch 0 taken 482 times.
✓ Branch 1 taken 483 times.
|
965 | if (prof_data[i].cid == id && |
| 104 |
2/4✓ Branch 0 taken 482 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 482 times.
✗ Branch 3 not taken.
|
482 | !prof_data[i].has_name && |
| 105 | name == NULL) { | ||
| 106 | 482 | prof_data[i].count ++; | |
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 482 times.
|
482 | prof_data[i].gc_count += doing_gc ? 1 : 0; |
| 108 | 482 | break; | |
| 109 | } | ||
| 110 | } | ||
| 111 | } else { | ||
| 112 |
2/2✓ Branch 0 taken 1086 times.
✓ Branch 1 taken 14 times.
|
1100 | if (lbm_system_sleeping) { |
| 113 | 1086 | num_sleep_samples ++; | |
| 114 | } else { | ||
| 115 | 14 | num_system_samples ++; | |
| 116 | } | ||
| 117 | } | ||
| 118 | 2100 | mutex_unlock(&qmutex); | |
| 119 | 2100 | } | |
| 120 |