| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * counters-saver.c | ||
| 3 | * | ||
| 4 | * Created on: Sep 17, 2021 | ||
| 5 | * Author: Alexey Berezhok | ||
| 6 | */ | ||
| 7 | |||
| 8 | #define _POSIX_C_SOURCE 199309L | ||
| 9 | |||
| 10 | #include <stdio.h> | ||
| 11 | #include <stdlib.h> | ||
| 12 | #include <string.h> | ||
| 13 | #include <time.h> | ||
| 14 | #include <sys/time.h> | ||
| 15 | |||
| 16 | #include "counters-saver.h" | ||
| 17 | |||
| 18 | #define I360_COUNTER_SAVER_MAXLEN 1024 | ||
| 19 | |||
| 20 | static i360_counter_saver_array_item i360_counter_saver_rules_timings[I360_COUNTER_SAVER_MAXLEN] = {{0, 0}}; | ||
| 21 | static i360_counter_saver_array_item *i360_counter_saver_list_pointers[I360_COUNTER_SAVER_MAXLEN] = {NULL}; | ||
| 22 | static i360_counter_saver_array_item i360_dbgcounter_saver_rules_timings[I360_COUNTER_SAVER_MAXLEN] = {{0, 0}}; | ||
| 23 | static i360_counter_saver_array_item *i360_dbgcounter_saver_list_pointers[I360_COUNTER_SAVER_MAXLEN] = {NULL}; | ||
| 24 | static int i360_counter_items = 0; | ||
| 25 | static int i360_dbgcounter_items = 0; | ||
| 26 | |||
| 27 | static i360_ull i360_counter_saver_global_counter = 0; | ||
| 28 | static i360_ull i360_counter_saver_global_counter_in_pd = 0; | ||
| 29 | |||
| 30 | static int is_counter_active = 0; | ||
| 31 | static int need_send_stat = 0; | ||
| 32 | |||
| 33 | static i360_ull i360_connections = 0; | ||
| 34 | static i360_ull i360_rinits_cnt = 0; | ||
| 35 | // rinit_sent, minit_sent | ||
| 36 | static i360_ull i360_packets_stats[2] = {0, 0}; | ||
| 37 | |||
| 38 | 2070 | static inline unsigned int i360_counter_saver_hash_long(unsigned int val) { | |
| 39 | 2070 | unsigned int hash = val ^ (0xaed7fed3 + (val >> 16)) ^ 0xfed3aed7; | |
| 40 | 2070 | unsigned int hash2 = (hash >> 8) & (0x1ff + hash) & 0x1ff; | |
| 41 | 2070 | return ((hash2 * 2) + (hash2 * 4) + (hash ^ 0xfedca987)) & 0x3ff; | |
| 42 | } | ||
| 43 | |||
| 44 | 2090 | static inline i360_ull i360_rdtsc() { | |
| 45 | struct timespec time1; | ||
| 46 | 2090 | clock_gettime(CLOCK_MONOTONIC, &time1); | |
| 47 | 2090 | return (i360_ull)(time1.tv_sec * 1000000000 + time1.tv_nsec); | |
| 48 | } | ||
| 49 | |||
| 50 | 12 | void i360_counter_saver_reset() { | |
| 51 |
4/4✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 2 times.
|
12 | if (is_counter_active && need_send_stat) { |
| 52 | 8 | i360_counter_saver_global_counter = 0; | |
| 53 | 8 | i360_counter_saver_global_counter_in_pd = 0; | |
| 54 | 8 | memset(i360_counter_saver_rules_timings, 0, sizeof(i360_counter_saver_rules_timings)); | |
| 55 | 8 | memset(i360_counter_saver_list_pointers, 0, sizeof(i360_counter_saver_list_pointers)); | |
| 56 | 8 | i360_counter_items = 0; | |
| 57 | 8 | memset(i360_dbgcounter_saver_rules_timings, 0, sizeof(i360_dbgcounter_saver_rules_timings)); | |
| 58 | 8 | memset(i360_dbgcounter_saver_list_pointers, 0, sizeof(i360_dbgcounter_saver_list_pointers)); | |
| 59 | 8 | i360_dbgcounter_items = 0; | |
| 60 | } | ||
| 61 | 12 | } | |
| 62 | |||
| 63 | 139 | void i360_counter_saver_savetimer(i360_ull *i360_timer) { | |
| 64 |
4/4✓ Branch 0 taken 15 times.
✓ Branch 1 taken 124 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 3 times.
|
139 | if (is_counter_active && need_send_stat) { |
| 65 | 12 | *i360_timer = i360_rdtsc(); | |
| 66 | } | ||
| 67 | 139 | } | |
| 68 | |||
| 69 | 1161 | int i360_counter_saver_add_rule(unsigned int ruleid, i360_ull i360_timer) { | |
| 70 |
4/4✓ Branch 0 taken 1037 times.
✓ Branch 1 taken 124 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1036 times.
|
1161 | if (!is_counter_active || !need_send_stat) |
| 71 | 125 | return 1; | |
| 72 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1035 times.
|
1036 | if (i360_counter_items == I360_COUNTER_SAVER_MAXLEN) |
| 73 | 1 | return 0; | |
| 74 | 1035 | i360_ull i360_timer_internal = i360_rdtsc(); | |
| 75 | 1035 | unsigned int hash_id = i360_counter_saver_hash_long(ruleid); | |
| 76 |
4/4✓ Branch 0 taken 5642 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 4608 times.
✓ Branch 3 taken 1034 times.
|
5643 | while (i360_counter_saver_rules_timings[hash_id].key != ruleid && i360_counter_saver_rules_timings[hash_id].key) { |
| 77 | 4608 | hash_id++; | |
| 78 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4606 times.
|
4608 | if (hash_id == I360_COUNTER_SAVER_MAXLEN) |
| 79 | 2 | hash_id = 0; | |
| 80 | } | ||
| 81 |
1/2✓ Branch 0 taken 1035 times.
✗ Branch 1 not taken.
|
1035 | if (!i360_counter_saver_rules_timings[hash_id].key) { |
| 82 | 1035 | i360_counter_saver_rules_timings[hash_id].key = ruleid; | |
| 83 | 1035 | i360_counter_saver_list_pointers[i360_counter_items] = &i360_counter_saver_rules_timings[hash_id]; | |
| 84 | 1035 | i360_counter_items++; | |
| 85 | } | ||
| 86 | 1035 | i360_counter_saver_rules_timings[hash_id].value += i360_timer_internal - i360_timer; | |
| 87 | 1035 | return 1; | |
| 88 | } | ||
| 89 | |||
| 90 | 1039 | int i360_dbgcounter_saver_add_rule(unsigned int ruleid, i360_ull i360_timer) { | |
| 91 |
4/4✓ Branch 0 taken 1037 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1036 times.
|
1039 | if (!is_counter_active || !need_send_stat) |
| 92 | 3 | return 1; | |
| 93 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1035 times.
|
1036 | if (i360_dbgcounter_items == I360_COUNTER_SAVER_MAXLEN) |
| 94 | 1 | return 0; | |
| 95 | 1035 | i360_ull i360_timer_internal = i360_rdtsc(); | |
| 96 | 1035 | unsigned int hash_id = i360_counter_saver_hash_long(ruleid); | |
| 97 |
4/4✓ Branch 0 taken 5642 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 4608 times.
✓ Branch 3 taken 1034 times.
|
5643 | while (i360_dbgcounter_saver_rules_timings[hash_id].key != ruleid && |
| 98 | 5642 | i360_dbgcounter_saver_rules_timings[hash_id].key) { | |
| 99 | 4608 | hash_id++; | |
| 100 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4606 times.
|
4608 | if (hash_id == I360_COUNTER_SAVER_MAXLEN) |
| 101 | 2 | hash_id = 0; | |
| 102 | } | ||
| 103 |
1/2✓ Branch 0 taken 1035 times.
✗ Branch 1 not taken.
|
1035 | if (!i360_dbgcounter_saver_rules_timings[hash_id].key) { |
| 104 | 1035 | i360_dbgcounter_saver_rules_timings[hash_id].key = ruleid; | |
| 105 | 1035 | i360_dbgcounter_saver_list_pointers[i360_dbgcounter_items] = &i360_dbgcounter_saver_rules_timings[hash_id]; | |
| 106 | 1035 | i360_dbgcounter_items++; | |
| 107 | } | ||
| 108 | 1035 | i360_dbgcounter_saver_rules_timings[hash_id].value += i360_timer_internal - i360_timer; | |
| 109 | 1035 | return 1; | |
| 110 | } | ||
| 111 | |||
| 112 | 5 | void i360_counter_saver_add_pd(i360_ull *i360_timer) { | |
| 113 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 time.
|
5 | if (!is_counter_active || !need_send_stat) |
| 114 | 4 | return; | |
| 115 | 1 | i360_ull i360_timer_internal = i360_rdtsc(); | |
| 116 | 1 | i360_counter_saver_global_counter_in_pd += (i360_timer_internal - *i360_timer); | |
| 117 | 1 | *i360_timer = i360_timer_internal; | |
| 118 | } | ||
| 119 | |||
| 120 | 5 | void i360_counter_saver_add_script(i360_ull *i360_timer) { | |
| 121 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 time.
|
5 | if (!is_counter_active || !need_send_stat) |
| 122 | 4 | return; | |
| 123 | 1 | i360_ull i360_timer_internal = i360_rdtsc(); | |
| 124 | 1 | i360_counter_saver_global_counter += (i360_timer_internal - *i360_timer); | |
| 125 | 1 | *i360_timer = i360_timer_internal; | |
| 126 | } | ||
| 127 | |||
| 128 | 1030 | i360_counter_saver_array_item *i360_counter_saver_iterate(int *index) { | |
| 129 |
4/4✓ Branch 0 taken 1028 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1027 times.
|
1030 | if (!is_counter_active || !need_send_stat) |
| 130 | 3 | return NULL; | |
| 131 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1026 times.
|
1027 | if (!index) |
| 132 | 1 | return NULL; | |
| 133 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1025 times.
|
1026 | if (*index >= i360_counter_items) |
| 134 | 1 | return NULL; | |
| 135 | 1025 | i360_counter_saver_array_item *tmp_ptr = i360_counter_saver_list_pointers[*index]; | |
| 136 | 1025 | *index = *index + 1; | |
| 137 | 1025 | return tmp_ptr; | |
| 138 | } | ||
| 139 | |||
| 140 | 5 | i360_counter_saver_array_item *i360_counter_saver_index(int index) { | |
| 141 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 2 times.
|
5 | if (!is_counter_active || !need_send_stat) |
| 142 | 3 | return NULL; | |
| 143 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
|
2 | if (index >= i360_counter_items) |
| 144 | 1 | return NULL; | |
| 145 | 1 | return i360_counter_saver_list_pointers[index]; | |
| 146 | } | ||
| 147 | |||
| 148 | 1030 | i360_counter_saver_array_item *i360_dbgcounter_saver_iterate(int *index) { | |
| 149 |
4/4✓ Branch 0 taken 1028 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1027 times.
|
1030 | if (!is_counter_active || !need_send_stat) |
| 150 | 3 | return NULL; | |
| 151 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1026 times.
|
1027 | if (!index) |
| 152 | 1 | return NULL; | |
| 153 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1025 times.
|
1026 | if (*index >= i360_dbgcounter_items) |
| 154 | 1 | return NULL; | |
| 155 | 1025 | i360_counter_saver_array_item *tmp_ptr = i360_dbgcounter_saver_list_pointers[*index]; | |
| 156 | 1025 | *index = *index + 1; | |
| 157 | 1025 | return tmp_ptr; | |
| 158 | } | ||
| 159 | |||
| 160 | 5 | i360_counter_saver_array_item *i360_dbgcounter_saver_index(int index) { | |
| 161 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 2 times.
|
5 | if (!is_counter_active || !need_send_stat) |
| 162 | 3 | return NULL; | |
| 163 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
|
2 | if (index >= i360_dbgcounter_items) |
| 164 | 1 | return NULL; | |
| 165 | 1 | return i360_dbgcounter_saver_list_pointers[index]; | |
| 166 | } | ||
| 167 | |||
| 168 | 6 | i360_ull i360_counter_saver_global_counter_get() { | |
| 169 | 6 | return i360_counter_saver_global_counter; | |
| 170 | }; | ||
| 171 | |||
| 172 | 6 | i360_ull i360_counter_saver_global_counter_in_pd_get() { | |
| 173 | 6 | return i360_counter_saver_global_counter_in_pd; | |
| 174 | } | ||
| 175 | |||
| 176 | 6 | int i360_counter_saver_get_items() { | |
| 177 | 6 | return i360_counter_items; | |
| 178 | } | ||
| 179 | |||
| 180 | 5 | int i360_dbgcounter_saver_get_items() { | |
| 181 | 5 | return i360_dbgcounter_items; | |
| 182 | } | ||
| 183 | |||
| 184 | 13 | void i360_counter_saver_set_active(int active, int need_send) { | |
| 185 | 13 | is_counter_active = active; | |
| 186 | 13 | need_send_stat = need_send; | |
| 187 | 13 | } | |
| 188 | |||
| 189 | 1 | int i360_counter_saver_is_active() { | |
| 190 |
2/4✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 | return is_counter_active && need_send_stat; |
| 191 | } | ||
| 192 | |||
| 193 | 4 | void i360_counter_saver_savetimer_alltime(i360_ull *i360_timer) { | |
| 194 | 4 | *i360_timer = i360_rdtsc(); | |
| 195 | 4 | } | |
| 196 | |||
| 197 | 2 | i360_ull i360_counter_saver_get_differ(i360_ull i360_timer) { | |
| 198 | 2 | i360_ull i360_timer_internal = i360_rdtsc(); | |
| 199 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
|
2 | if (i360_timer_internal < i360_timer) |
| 200 | 1 | return 0; | |
| 201 | else | ||
| 202 | 1 | return i360_timer_internal - i360_timer; | |
| 203 | } | ||
| 204 | |||
| 205 | 1 | void i360_packets_stats_rinit() { | |
| 206 | 1 | i360_rinits_cnt++; | |
| 207 | 1 | i360_packets_stats[PACKET_STATS_RINIT_SENT] = 0; | |
| 208 | 1 | } | |
| 209 | |||
| 210 | 1 | void i360_packets_stats_new_sent() { | |
| 211 | 1 | i360_packets_stats[PACKET_STATS_RINIT_SENT] += 1; | |
| 212 | 1 | i360_packets_stats[PACKET_STATS_MINIT_SENT] += 1; | |
| 213 | 1 | } | |
| 214 | |||
| 215 | 2 | i360_ull i360_packets_stats_get_rinit_cnt() { | |
| 216 | 2 | return i360_rinits_cnt; | |
| 217 | } | ||
| 218 | |||
| 219 | 6 | i360_ull i360_packets_stats_by_id(int index) { | |
| 220 | 6 | return i360_packets_stats[index]; | |
| 221 | } | ||
| 222 | |||
| 223 | 1 | void i360_connections_stats_new_conn() { | |
| 224 | 1 | i360_connections += 1; | |
| 225 | 1 | } | |
| 226 | |||
| 227 | 2 | i360_ull i360_connections_stats_get_total() { | |
| 228 | 2 | return i360_connections; | |
| 229 | } | ||
| 230 |