[8834751] | 1 | #include <stdint.h> |
---|
| 2 | #include <stdlib.hfa> |
---|
| 3 | |
---|
| 4 | #include <unistd.h> // STDERR_FILENO |
---|
[7812a7b5] | 5 | #include <inttypes.h> |
---|
[8834751] | 6 | #include "bits/debug.hfa" |
---|
[73f4d08] | 7 | #include "bits/locks.hfa" |
---|
[8834751] | 8 | #include "stats.hfa" |
---|
| 9 | |
---|
| 10 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 11 | void __init_stats( struct __stats_t * stats ) { |
---|
[52769ba] | 12 | stats->ready.pick.push.attempt = 0; |
---|
| 13 | stats->ready.pick.push.success = 0; |
---|
| 14 | stats->ready.pick.push.local = 0; |
---|
| 15 | stats->ready.pick.push.lsuccess = 0; |
---|
[fd1f65e] | 16 | stats->ready.pick.ext.attempt = 0; |
---|
| 17 | stats->ready.pick.ext.success = 0; |
---|
| 18 | stats->ready.pick.ext.local = 0; |
---|
| 19 | stats->ready.pick.ext.lsuccess = 0; |
---|
[52769ba] | 20 | stats->ready.pick.pop .probe = 0; |
---|
| 21 | stats->ready.pick.pop .attempt = 0; |
---|
| 22 | stats->ready.pick.pop .success = 0; |
---|
| 23 | stats->ready.pick.pop .local = 0; |
---|
| 24 | stats->ready.pick.pop .lsuccess = 0; |
---|
[29cb302] | 25 | stats->ready.threads.migration = 0; |
---|
[ec43cf9] | 26 | stats->ready.threads.threads = 0; |
---|
[68f36f4] | 27 | stats->ready.sleep.halts = 0; |
---|
| 28 | stats->ready.sleep.cancels = 0; |
---|
| 29 | stats->ready.sleep.wakes = 0; |
---|
| 30 | stats->ready.sleep.exits = 0; |
---|
[8834751] | 31 | |
---|
[5751a56] | 32 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
[d60d30e] | 33 | stats->io.alloc.fast = 0; |
---|
| 34 | stats->io.alloc.slow = 0; |
---|
| 35 | stats->io.alloc.fail = 0; |
---|
| 36 | stats->io.alloc.revoke = 0; |
---|
| 37 | stats->io.alloc.block = 0; |
---|
| 38 | stats->io.submit.fast = 0; |
---|
| 39 | stats->io.submit.slow = 0; |
---|
| 40 | stats->io.flush.external = 0; |
---|
[dddb3dd0] | 41 | stats->io.calls.flush = 0; |
---|
[d60d30e] | 42 | stats->io.calls.submitted = 0; |
---|
[dddb3dd0] | 43 | stats->io.calls.drain = 0; |
---|
[d60d30e] | 44 | stats->io.calls.completed = 0; |
---|
| 45 | stats->io.calls.errors.busy = 0; |
---|
[150d21a] | 46 | stats->io.poller.sleeps = 0; |
---|
[8834751] | 47 | #endif |
---|
[73f4d08] | 48 | |
---|
| 49 | #if defined(CFA_STATS_ARRAY) |
---|
| 50 | stats->array.values = alloc(CFA_STATS_ARRAY); |
---|
| 51 | stats->array.cnt = 0; |
---|
| 52 | #endif |
---|
[8834751] | 53 | } |
---|
| 54 | |
---|
| 55 | void __tally_stats( struct __stats_t * cltr, struct __stats_t * proc ) { |
---|
[0989e79] | 56 | __atomic_fetch_add( &cltr->ready.pick.push.attempt , proc->ready.pick.push.attempt , __ATOMIC_SEQ_CST ); proc->ready.pick.push.attempt = 0; |
---|
| 57 | __atomic_fetch_add( &cltr->ready.pick.push.success , proc->ready.pick.push.success , __ATOMIC_SEQ_CST ); proc->ready.pick.push.success = 0; |
---|
| 58 | __atomic_fetch_add( &cltr->ready.pick.push.local , proc->ready.pick.push.local , __ATOMIC_SEQ_CST ); proc->ready.pick.push.local = 0; |
---|
| 59 | __atomic_fetch_add( &cltr->ready.pick.push.lsuccess, proc->ready.pick.push.lsuccess, __ATOMIC_SEQ_CST ); proc->ready.pick.push.lsuccess = 0; |
---|
[fd1f65e] | 60 | __atomic_fetch_add( &cltr->ready.pick.ext.attempt , proc->ready.pick.ext.attempt , __ATOMIC_SEQ_CST ); proc->ready.pick.ext.attempt = 0; |
---|
| 61 | __atomic_fetch_add( &cltr->ready.pick.ext.success , proc->ready.pick.ext.success , __ATOMIC_SEQ_CST ); proc->ready.pick.ext.success = 0; |
---|
| 62 | __atomic_fetch_add( &cltr->ready.pick.ext.local , proc->ready.pick.ext.local , __ATOMIC_SEQ_CST ); proc->ready.pick.ext.local = 0; |
---|
| 63 | __atomic_fetch_add( &cltr->ready.pick.ext.lsuccess , proc->ready.pick.ext.lsuccess , __ATOMIC_SEQ_CST ); proc->ready.pick.ext.lsuccess = 0; |
---|
[0989e79] | 64 | __atomic_fetch_add( &cltr->ready.pick.pop .probe , proc->ready.pick.pop .probe , __ATOMIC_SEQ_CST ); proc->ready.pick.pop .probe = 0; |
---|
| 65 | __atomic_fetch_add( &cltr->ready.pick.pop .attempt , proc->ready.pick.pop .attempt , __ATOMIC_SEQ_CST ); proc->ready.pick.pop .attempt = 0; |
---|
| 66 | __atomic_fetch_add( &cltr->ready.pick.pop .success , proc->ready.pick.pop .success , __ATOMIC_SEQ_CST ); proc->ready.pick.pop .success = 0; |
---|
| 67 | __atomic_fetch_add( &cltr->ready.pick.pop .local , proc->ready.pick.pop .local , __ATOMIC_SEQ_CST ); proc->ready.pick.pop .local = 0; |
---|
| 68 | __atomic_fetch_add( &cltr->ready.pick.pop .lsuccess, proc->ready.pick.pop .lsuccess, __ATOMIC_SEQ_CST ); proc->ready.pick.pop .lsuccess = 0; |
---|
| 69 | __atomic_fetch_add( &cltr->ready.threads.migration , proc->ready.threads.migration , __ATOMIC_SEQ_CST ); proc->ready.threads.migration = 0; |
---|
[ec43cf9] | 70 | __atomic_fetch_add( &cltr->ready.threads.threads , proc->ready.threads.threads , __ATOMIC_SEQ_CST ); proc->ready.threads.threads = 0; |
---|
[0989e79] | 71 | __atomic_fetch_add( &cltr->ready.sleep.halts , proc->ready.sleep.halts , __ATOMIC_SEQ_CST ); proc->ready.sleep.halts = 0; |
---|
| 72 | __atomic_fetch_add( &cltr->ready.sleep.cancels , proc->ready.sleep.cancels , __ATOMIC_SEQ_CST ); proc->ready.sleep.cancels = 0; |
---|
| 73 | __atomic_fetch_add( &cltr->ready.sleep.wakes , proc->ready.sleep.wakes , __ATOMIC_SEQ_CST ); proc->ready.sleep.wakes = 0; |
---|
| 74 | __atomic_fetch_add( &cltr->ready.sleep.exits , proc->ready.sleep.exits , __ATOMIC_SEQ_CST ); proc->ready.sleep.exits = 0; |
---|
[8834751] | 75 | |
---|
[5751a56] | 76 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
[d60d30e] | 77 | __atomic_fetch_add( &cltr->io.alloc.fast , proc->io.alloc.fast , __ATOMIC_SEQ_CST ); proc->io.alloc.fast = 0; |
---|
| 78 | __atomic_fetch_add( &cltr->io.alloc.slow , proc->io.alloc.slow , __ATOMIC_SEQ_CST ); proc->io.alloc.slow = 0; |
---|
| 79 | __atomic_fetch_add( &cltr->io.alloc.fail , proc->io.alloc.fail , __ATOMIC_SEQ_CST ); proc->io.alloc.fail = 0; |
---|
| 80 | __atomic_fetch_add( &cltr->io.alloc.revoke , proc->io.alloc.revoke , __ATOMIC_SEQ_CST ); proc->io.alloc.revoke = 0; |
---|
| 81 | __atomic_fetch_add( &cltr->io.alloc.block , proc->io.alloc.block , __ATOMIC_SEQ_CST ); proc->io.alloc.block = 0; |
---|
| 82 | __atomic_fetch_add( &cltr->io.submit.fast , proc->io.submit.fast , __ATOMIC_SEQ_CST ); proc->io.submit.fast = 0; |
---|
| 83 | __atomic_fetch_add( &cltr->io.submit.slow , proc->io.submit.slow , __ATOMIC_SEQ_CST ); proc->io.submit.slow = 0; |
---|
| 84 | __atomic_fetch_add( &cltr->io.flush.external , proc->io.flush.external , __ATOMIC_SEQ_CST ); proc->io.flush.external = 0; |
---|
[dddb3dd0] | 85 | __atomic_fetch_add( &cltr->io.calls.flush , proc->io.calls.flush , __ATOMIC_SEQ_CST ); proc->io.calls.flush = 0; |
---|
[d60d30e] | 86 | __atomic_fetch_add( &cltr->io.calls.submitted , proc->io.calls.submitted , __ATOMIC_SEQ_CST ); proc->io.calls.submitted = 0; |
---|
[dddb3dd0] | 87 | __atomic_fetch_add( &cltr->io.calls.drain , proc->io.calls.drain , __ATOMIC_SEQ_CST ); proc->io.calls.drain = 0; |
---|
[d60d30e] | 88 | __atomic_fetch_add( &cltr->io.calls.completed , proc->io.calls.completed , __ATOMIC_SEQ_CST ); proc->io.calls.completed = 0; |
---|
| 89 | __atomic_fetch_add( &cltr->io.calls.errors.busy, proc->io.calls.errors.busy, __ATOMIC_SEQ_CST ); proc->io.calls.errors.busy = 0; |
---|
[150d21a] | 90 | __atomic_fetch_add( &cltr->io.poller.sleeps , proc->io.poller.sleeps , __ATOMIC_SEQ_CST ); proc->io.poller.sleeps = 0; |
---|
[8834751] | 91 | #endif |
---|
| 92 | } |
---|
| 93 | |
---|
[1b033b8] | 94 | void __print_stats( struct __stats_t * stats, int flags, const char * type, const char * name, void * id ) with( *stats ) { |
---|
[8834751] | 95 | |
---|
[69fbc61] | 96 | if( flags & CFA_STATS_READY_Q ) { |
---|
| 97 | double push_len = ((double)ready.pick.push.attempt) / ready.pick.push.success; |
---|
[fd1f65e] | 98 | double ext_len = ((double)ready.pick.ext .attempt) / ready.pick.ext .success; |
---|
[69fbc61] | 99 | double pop_len = ((double)ready.pick.pop .attempt) / ready.pick.pop .success; |
---|
[8834751] | 100 | |
---|
[69fbc61] | 101 | double lpush_len = ((double)ready.pick.push.local) / ready.pick.push.lsuccess; |
---|
[fd1f65e] | 102 | double lext_len = ((double)ready.pick.ext .local) / ready.pick.ext .lsuccess; |
---|
[69fbc61] | 103 | double lpop_len = ((double)ready.pick.pop .local) / ready.pick.pop .lsuccess; |
---|
[52769ba] | 104 | |
---|
[c34ebf2] | 105 | __cfaabi_bits_print_safe( STDOUT_FILENO, |
---|
[69fbc61] | 106 | "----- %s \"%s\" (%p) - Ready Q Stats -----\n" |
---|
[ec43cf9] | 107 | "- total threads : %'15" PRIu64 "run, %'15" PRIu64 "schd (%'" PRIu64 "ext, %'" PRIu64 "mig, %'" PRId64 " )\n" |
---|
[fb15af5] | 108 | "- push avg probe : %'3.2lf, %'3.2lfl (%'15" PRIu64 " attempts, %'15" PRIu64 " locals)\n" |
---|
[fd1f65e] | 109 | "- ext avg probe : %'3.2lf, %'3.2lfl (%'15" PRIu64 " attempts, %'15" PRIu64 " locals)\n" |
---|
[a06023f] | 110 | "- pop avg probe : %'3.2lf, %'3.2lfl (%'15" PRIu64 " attempts, %'15" PRIu64 " locals)\n" |
---|
[fb15af5] | 111 | "- Idle Sleep : %'15" PRIu64 "h, %'15" PRIu64 "c, %'15" PRIu64 "w, %'15" PRIu64 "e\n" |
---|
[c34ebf2] | 112 | "\n" |
---|
[1b033b8] | 113 | , type, name, id |
---|
[69fbc61] | 114 | , ready.pick.pop.success |
---|
[8c3a3a14] | 115 | , ready.pick.push.success + ready.pick.ext.success |
---|
[ec43cf9] | 116 | , ready.pick.ext.success, ready.threads.migration, ready.threads.threads |
---|
[fb15af5] | 117 | , push_len, lpush_len, ready.pick.push.attempt, ready.pick.push.local |
---|
[fd1f65e] | 118 | , ext_len , lext_len , ready.pick.ext .attempt, ready.pick.ext .local |
---|
[fb15af5] | 119 | , pop_len , lpop_len , ready.pick.pop .attempt, ready.pick.pop .local |
---|
[69fbc61] | 120 | , ready.sleep.halts, ready.sleep.cancels, ready.sleep.wakes, ready.sleep.exits |
---|
[c34ebf2] | 121 | ); |
---|
[69fbc61] | 122 | } |
---|
| 123 | |
---|
[5751a56] | 124 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
[69fbc61] | 125 | if( flags & CFA_STATS_IO ) { |
---|
[d60d30e] | 126 | uint64_t total_allocs = io.alloc.fast + io.alloc.slow; |
---|
| 127 | double avgfasta = ((double)io.alloc.fast) / total_allocs; |
---|
[69fbc61] | 128 | |
---|
[d60d30e] | 129 | uint64_t total_submits = io.submit.fast + io.submit.slow; |
---|
| 130 | double avgfasts = ((double)io.submit.fast) / total_submits; |
---|
[69fbc61] | 131 | |
---|
[dddb3dd0] | 132 | double avgsubs = ((double)io.calls.submitted) / io.calls.flush; |
---|
| 133 | double avgcomp = ((double)io.calls.completed) / io.calls.drain; |
---|
[69fbc61] | 134 | |
---|
| 135 | __cfaabi_bits_print_safe( STDOUT_FILENO, |
---|
| 136 | "----- %s \"%s\" (%p) - I/O Stats -----\n" |
---|
[a06023f] | 137 | "- total allocations : %'" PRIu64 "f, %'" PRIu64 "s (%'2.2lff) \n" |
---|
[fb15af5] | 138 | "- failures : %'" PRIu64 "oom, %'" PRIu64 "rvk, %'" PRIu64 "blk\n" |
---|
[a06023f] | 139 | "- total submits : %'" PRIu64 "f, %'" PRIu64 "s (%'2.2lf) \n" |
---|
[fb15af5] | 140 | "- flush external : %'" PRIu64 "\n" |
---|
| 141 | "- io_uring_enter : %'" PRIu64 " (%'" PRIu64 ", %'" PRIu64 " EBUSY)\n" |
---|
[a06023f] | 142 | "- submits : %'" PRIu64 " (%'.2lf) \n" |
---|
| 143 | "- completes : %'" PRIu64 " (%'.2lf) \n" |
---|
[fb15af5] | 144 | "- poller sleeping : %'" PRIu64 "\n" |
---|
[69fbc61] | 145 | "\n" |
---|
[1e208ea] | 146 | , type, name, id |
---|
[fb15af5] | 147 | , io.alloc.fast, io.alloc.slow, avgfasta |
---|
| 148 | , io.alloc.fail, io.alloc.revoke, io.alloc.block |
---|
| 149 | , io.submit.fast, io.submit.slow, avgfasts |
---|
[d60d30e] | 150 | , io.flush.external |
---|
[dddb3dd0] | 151 | , io.calls.flush, io.calls.drain, io.calls.errors.busy |
---|
[d60d30e] | 152 | , io.calls.submitted, avgsubs |
---|
| 153 | , io.calls.completed, avgcomp |
---|
[150d21a] | 154 | , io.poller.sleeps |
---|
[69fbc61] | 155 | ); |
---|
| 156 | } |
---|
[c34ebf2] | 157 | #endif |
---|
[8834751] | 158 | } |
---|
[73f4d08] | 159 | |
---|
| 160 | #if defined(CFA_STATS_ARRAY) |
---|
| 161 | extern "C" { |
---|
| 162 | #include <stdio.h> |
---|
| 163 | #include <errno.h> |
---|
| 164 | #include <sys/stat.h> |
---|
| 165 | #include <fcntl.h> |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | void __flush_stat( struct __stats_t * this, const char * name, void * handle) { |
---|
| 169 | int ret = mkdir(".cfadata", 0755); |
---|
| 170 | if(ret < 0 && errno != EEXIST) abort("Failed to create directory .cfadata: %d\n", errno); |
---|
| 171 | |
---|
| 172 | char filename[100]; |
---|
| 173 | snprintf(filename, 100, ".cfadata/%s%p.data", name, handle); |
---|
| 174 | |
---|
| 175 | int fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0644); |
---|
| 176 | if(fd < 0) abort("Failed to create file %s: %d\n", filename, errno); |
---|
| 177 | |
---|
| 178 | for(i; this->array.cnt) { |
---|
| 179 | char line[100]; |
---|
| 180 | size_t n = snprintf(line, 100, "%llu, %lld\n", this->array.values[i].ts, this->array.values[i].value); |
---|
| 181 | write(fd, line, n); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | this->array.cnt = 0; |
---|
| 185 | close(fd); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | static __spinlock_t stats_lock; |
---|
| 189 | |
---|
| 190 | void __push_stat( struct __stats_t * this, int64_t value, bool external, const char * name, void * handle ) { |
---|
| 191 | if(external) lock(stats_lock __cfaabi_dbg_ctx2); |
---|
| 192 | |
---|
| 193 | if( this->array.cnt >= CFA_STATS_ARRAY ) __flush_stat( this, name, handle ); |
---|
| 194 | |
---|
| 195 | size_t idx = this->array.cnt; |
---|
| 196 | this->array.cnt++; |
---|
| 197 | |
---|
| 198 | if(external) unlock(stats_lock); |
---|
| 199 | |
---|
| 200 | this->array.values[idx].ts = rdtscl(); |
---|
| 201 | this->array.values[idx].value = value; |
---|
| 202 | } |
---|
| 203 | #endif |
---|
[8834751] | 204 | #endif |
---|