[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" |
---|
[986cb99] | 9 | #include "strstream.hfa" |
---|
[8834751] | 10 | |
---|
| 11 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 12 | void __init_stats( struct __stats_t * stats ) { |
---|
[d2fadeb] | 13 | stats->ready.push.local.attempt = 0; |
---|
| 14 | stats->ready.push.local.success = 0; |
---|
| 15 | stats->ready.push.share.attempt = 0; |
---|
| 16 | stats->ready.push.share.success = 0; |
---|
| 17 | stats->ready.push.extrn.attempt = 0; |
---|
| 18 | stats->ready.push.extrn.success = 0; |
---|
| 19 | stats->ready.pop.local .attempt = 0; |
---|
| 20 | stats->ready.pop.local .success = 0; |
---|
| 21 | stats->ready.pop.help .attempt = 0; |
---|
| 22 | stats->ready.pop.help .success = 0; |
---|
| 23 | stats->ready.pop.steal .attempt = 0; |
---|
| 24 | stats->ready.pop.steal .success = 0; |
---|
| 25 | stats->ready.pop.search.attempt = 0; |
---|
| 26 | stats->ready.pop.search.success = 0; |
---|
[29cb302] | 27 | stats->ready.threads.migration = 0; |
---|
[3bd4293] | 28 | stats->ready.threads.extunpark = 0; |
---|
[ec43cf9] | 29 | stats->ready.threads.threads = 0; |
---|
[78ea291] | 30 | stats->ready.threads.cthreads = 0; |
---|
[941e14a] | 31 | stats->ready.threads.preempt.yield = 0; |
---|
| 32 | stats->ready.threads.preempt.rllfwd = 0; |
---|
[68f36f4] | 33 | stats->ready.sleep.halts = 0; |
---|
| 34 | stats->ready.sleep.cancels = 0; |
---|
[7cf3b1d] | 35 | stats->ready.sleep.early = 0; |
---|
[68f36f4] | 36 | stats->ready.sleep.wakes = 0; |
---|
[7cf3b1d] | 37 | stats->ready.sleep.seen = 0; |
---|
[68f36f4] | 38 | stats->ready.sleep.exits = 0; |
---|
[8834751] | 39 | |
---|
[5751a56] | 40 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
[d60d30e] | 41 | stats->io.alloc.fast = 0; |
---|
| 42 | stats->io.alloc.slow = 0; |
---|
| 43 | stats->io.alloc.fail = 0; |
---|
| 44 | stats->io.alloc.revoke = 0; |
---|
| 45 | stats->io.alloc.block = 0; |
---|
| 46 | stats->io.submit.fast = 0; |
---|
| 47 | stats->io.submit.slow = 0; |
---|
[7ce8873] | 48 | stats->io.submit.eagr = 0; |
---|
| 49 | stats->io.submit.nblk = 0; |
---|
[d60d30e] | 50 | stats->io.flush.external = 0; |
---|
[70b4aeb9] | 51 | stats->io.flush.dirty = 0; |
---|
| 52 | stats->io.flush.full = 0; |
---|
| 53 | stats->io.flush.idle = 0; |
---|
| 54 | stats->io.flush.eager = 0; |
---|
[dddb3dd0] | 55 | stats->io.calls.flush = 0; |
---|
[d60d30e] | 56 | stats->io.calls.submitted = 0; |
---|
[dddb3dd0] | 57 | stats->io.calls.drain = 0; |
---|
[d60d30e] | 58 | stats->io.calls.completed = 0; |
---|
[54c1196] | 59 | stats->io.calls.locked = 0; |
---|
| 60 | stats->io.calls.helped = 0; |
---|
[d60d30e] | 61 | stats->io.calls.errors.busy = 0; |
---|
[db614d0] | 62 | stats->io.ops.sockread = 0; |
---|
| 63 | stats->io.ops.epllread = 0; |
---|
| 64 | stats->io.ops.sockwrite = 0; |
---|
| 65 | stats->io.ops.epllwrite = 0; |
---|
[8834751] | 66 | #endif |
---|
[73f4d08] | 67 | |
---|
| 68 | #if defined(CFA_STATS_ARRAY) |
---|
| 69 | stats->array.values = alloc(CFA_STATS_ARRAY); |
---|
| 70 | stats->array.cnt = 0; |
---|
| 71 | #endif |
---|
[8834751] | 72 | } |
---|
| 73 | |
---|
[45b9b21] | 74 | static inline void tally_one( volatile uint64_t * agg, volatile uint64_t * val) { |
---|
| 75 | uint64_t add = __atomic_exchange_n(val, 0_l64u, __ATOMIC_RELAXED); |
---|
| 76 | __atomic_fetch_add(agg, add, __ATOMIC_RELAXED); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | static inline void tally_one( volatile int64_t * agg, volatile int64_t * val) { |
---|
| 80 | int64_t add = __atomic_exchange_n(val, 0_l64, __ATOMIC_RELAXED); |
---|
| 81 | __atomic_fetch_add(agg, add, __ATOMIC_RELAXED); |
---|
| 82 | } |
---|
| 83 | |
---|
[8834751] | 84 | void __tally_stats( struct __stats_t * cltr, struct __stats_t * proc ) { |
---|
[941e14a] | 85 | tally_one( &cltr->ready.push.local.attempt , &proc->ready.push.local.attempt ); |
---|
| 86 | tally_one( &cltr->ready.push.local.success , &proc->ready.push.local.success ); |
---|
| 87 | tally_one( &cltr->ready.push.share.attempt , &proc->ready.push.share.attempt ); |
---|
| 88 | tally_one( &cltr->ready.push.share.success , &proc->ready.push.share.success ); |
---|
| 89 | tally_one( &cltr->ready.push.extrn.attempt , &proc->ready.push.extrn.attempt ); |
---|
| 90 | tally_one( &cltr->ready.push.extrn.success , &proc->ready.push.extrn.success ); |
---|
| 91 | tally_one( &cltr->ready.pop.local .attempt , &proc->ready.pop.local .attempt ); |
---|
| 92 | tally_one( &cltr->ready.pop.local .success , &proc->ready.pop.local .success ); |
---|
| 93 | tally_one( &cltr->ready.pop.help .attempt , &proc->ready.pop.help .attempt ); |
---|
| 94 | tally_one( &cltr->ready.pop.help .success , &proc->ready.pop.help .success ); |
---|
| 95 | tally_one( &cltr->ready.pop.steal .attempt , &proc->ready.pop.steal .attempt ); |
---|
| 96 | tally_one( &cltr->ready.pop.steal .success , &proc->ready.pop.steal .success ); |
---|
| 97 | tally_one( &cltr->ready.pop.search.attempt , &proc->ready.pop.search.attempt ); |
---|
| 98 | tally_one( &cltr->ready.pop.search.success , &proc->ready.pop.search.success ); |
---|
| 99 | tally_one( &cltr->ready.threads.migration , &proc->ready.threads.migration ); |
---|
| 100 | tally_one( &cltr->ready.threads.extunpark , &proc->ready.threads.extunpark ); |
---|
| 101 | tally_one( &cltr->ready.threads.threads , &proc->ready.threads.threads ); |
---|
| 102 | tally_one( &cltr->ready.threads.cthreads , &proc->ready.threads.cthreads ); |
---|
| 103 | tally_one( &cltr->ready.threads.preempt.yield , &proc->ready.threads.preempt.yield ); |
---|
| 104 | tally_one( &cltr->ready.threads.preempt.rllfwd, &proc->ready.threads.preempt.rllfwd ); |
---|
| 105 | tally_one( &cltr->ready.sleep.halts , &proc->ready.sleep.halts ); |
---|
| 106 | tally_one( &cltr->ready.sleep.cancels , &proc->ready.sleep.cancels ); |
---|
| 107 | tally_one( &cltr->ready.sleep.early , &proc->ready.sleep.early ); |
---|
| 108 | tally_one( &cltr->ready.sleep.wakes , &proc->ready.sleep.wakes ); |
---|
| 109 | tally_one( &cltr->ready.sleep.seen , &proc->ready.sleep.wakes ); |
---|
| 110 | tally_one( &cltr->ready.sleep.exits , &proc->ready.sleep.exits ); |
---|
[8834751] | 111 | |
---|
[5751a56] | 112 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
[45b9b21] | 113 | tally_one( &cltr->io.alloc.fast , &proc->io.alloc.fast ); |
---|
| 114 | tally_one( &cltr->io.alloc.slow , &proc->io.alloc.slow ); |
---|
| 115 | tally_one( &cltr->io.alloc.fail , &proc->io.alloc.fail ); |
---|
| 116 | tally_one( &cltr->io.alloc.revoke , &proc->io.alloc.revoke ); |
---|
| 117 | tally_one( &cltr->io.alloc.block , &proc->io.alloc.block ); |
---|
| 118 | tally_one( &cltr->io.submit.fast , &proc->io.submit.fast ); |
---|
| 119 | tally_one( &cltr->io.submit.slow , &proc->io.submit.slow ); |
---|
[7ce8873] | 120 | tally_one( &cltr->io.submit.eagr , &proc->io.submit.eagr ); |
---|
| 121 | tally_one( &cltr->io.submit.nblk , &proc->io.submit.nblk ); |
---|
[45b9b21] | 122 | tally_one( &cltr->io.flush.external , &proc->io.flush.external ); |
---|
[70b4aeb9] | 123 | tally_one( &cltr->io.flush.dirty , &proc->io.flush.dirty ); |
---|
| 124 | tally_one( &cltr->io.flush.full , &proc->io.flush.full ); |
---|
| 125 | tally_one( &cltr->io.flush.idle , &proc->io.flush.idle ); |
---|
| 126 | tally_one( &cltr->io.flush.eager , &proc->io.flush.eager ); |
---|
[45b9b21] | 127 | tally_one( &cltr->io.calls.flush , &proc->io.calls.flush ); |
---|
| 128 | tally_one( &cltr->io.calls.submitted , &proc->io.calls.submitted ); |
---|
| 129 | tally_one( &cltr->io.calls.drain , &proc->io.calls.drain ); |
---|
| 130 | tally_one( &cltr->io.calls.completed , &proc->io.calls.completed ); |
---|
[54c1196] | 131 | tally_one( &cltr->io.calls.locked , &proc->io.calls.locked ); |
---|
| 132 | tally_one( &cltr->io.calls.helped , &proc->io.calls.helped ); |
---|
[45b9b21] | 133 | tally_one( &cltr->io.calls.errors.busy, &proc->io.calls.errors.busy ); |
---|
[db614d0] | 134 | tally_one( &cltr->io.ops.sockread , &proc->io.ops.sockread ); |
---|
| 135 | tally_one( &cltr->io.ops.epllread , &proc->io.ops.epllread ); |
---|
| 136 | tally_one( &cltr->io.ops.sockwrite , &proc->io.ops.sockwrite ); |
---|
| 137 | tally_one( &cltr->io.ops.epllwrite , &proc->io.ops.epllwrite ); |
---|
[8834751] | 138 | #endif |
---|
| 139 | } |
---|
| 140 | |
---|
[986cb99] | 141 | #define eng3(X) (ws(3, 3, unit(eng( X )))) |
---|
| 142 | |
---|
[1b033b8] | 143 | void __print_stats( struct __stats_t * stats, int flags, const char * type, const char * name, void * id ) with( *stats ) { |
---|
[8834751] | 144 | |
---|
[986cb99] | 145 | char buf[1024]; |
---|
[bbbd2c4] | 146 | ostrstream sstr = { buf, 1024 }; |
---|
[986cb99] | 147 | |
---|
[69fbc61] | 148 | if( flags & CFA_STATS_READY_Q ) { |
---|
[986cb99] | 149 | |
---|
[78ea291] | 150 | sstr | "----- " | type | " \"" | name | "\" (" | "" | id | "" | ") - Ready Q Stats -----"; |
---|
[986cb99] | 151 | |
---|
| 152 | uint64_t totalR = ready.pop.local.success + ready.pop.help.success + ready.pop.steal.success + ready.pop.search.success; |
---|
| 153 | uint64_t totalS = ready.push.local.success + ready.push.share.success + ready.push.extrn.success; |
---|
[78ea291] | 154 | sstr | "- totals : " | eng3(totalR) | "run," | eng3(totalS) | "schd (" | eng3(ready.push.extrn.success) | "ext," |
---|
| 155 | | eng3(ready.threads.migration) | "mig," | eng3(ready.threads.extunpark) | " eupk," | ready.threads.threads | " t," | ready.threads.cthreads | " cthr)"; |
---|
[986cb99] | 156 | |
---|
| 157 | double push_len = ((double)ready.push.local.attempt + ready.push.share.attempt + ready.push.extrn.attempt) / totalS; |
---|
[d2fadeb] | 158 | double sLcl_len = ready.push.local.success ? ((double)ready.push.local.attempt) / ready.push.local.success : 0; |
---|
| 159 | double sOth_len = ready.push.share.success ? ((double)ready.push.share.attempt) / ready.push.share.success : 0; |
---|
| 160 | double sExt_len = ready.push.extrn.success ? ((double)ready.push.extrn.attempt) / ready.push.extrn.success : 0; |
---|
[986cb99] | 161 | sstr | "- push avg : " | ws(3, 3, push_len) |
---|
| 162 | | "- l: " | eng3(ready.push.local.attempt) | " (" | ws(3, 3, sLcl_len) | ")" |
---|
| 163 | | ", s: " | eng3(ready.push.share.attempt) | " (" | ws(3, 3, sOth_len) | ")" |
---|
| 164 | | ", e: " | eng3(ready.push.extrn.attempt) | " (" | ws(3, 3, sExt_len) | ")"; |
---|
| 165 | |
---|
| 166 | double rLcl_pc = (100.0 * (double)ready.pop.local .success) / totalR; |
---|
| 167 | sstr | "- local : " | eng3(ready.pop.local .success) | "-"| ws(3, 3, rLcl_pc) | '%' |
---|
[78ea291] | 168 | | " (" | eng3(ready.pop.local .attempt) | " try)"; |
---|
[986cb99] | 169 | double rHlp_pc = (100.0 * (double)ready.pop.help .success) / totalR; |
---|
| 170 | sstr | "- help : " | eng3(ready.pop.help .success) | "-"| ws(3, 3, rHlp_pc) | '%' |
---|
[78ea291] | 171 | | " (" | eng3(ready.pop.help .attempt) | " try)"; |
---|
[986cb99] | 172 | double rStl_pc = (100.0 * (double)ready.pop.steal .success) / totalR; |
---|
| 173 | sstr | "- steal : " | eng3(ready.pop.steal .success) | "-"| ws(3, 3, rStl_pc) | '%' |
---|
[78ea291] | 174 | | " (" | eng3(ready.pop.steal .attempt) | " try)"; |
---|
[986cb99] | 175 | double rSch_pc = (100.0 * (double)ready.pop.search.success) / totalR; |
---|
| 176 | sstr | "- search : " | eng3(ready.pop.search.success) | "-"| ws(3, 3, rSch_pc) | '%' |
---|
[78ea291] | 177 | | " (" | eng3(ready.pop.search.attempt) | " try)"; |
---|
[986cb99] | 178 | |
---|
[7cf3b1d] | 179 | sstr | "- Idle Slp : " | eng3(ready.sleep.halts) | "halt," | eng3(ready.sleep.cancels) | "cancel," |
---|
| 180 | | eng3(ready.sleep.wakes + ready.sleep.early) | '(' | eng3(ready.sleep.early) | ',' | eng3(ready.sleep.seen) | ')' | " wake(early, seen)," |
---|
| 181 | | eng3(ready.sleep.exits) | "exit"; |
---|
[941e14a] | 182 | sstr | "- Preemption : " | eng3(ready.threads.preempt.yield) | "yields," | eng3(ready.threads.preempt.rllfwd) | "delayed"; |
---|
[986cb99] | 183 | sstr | nl; |
---|
[69fbc61] | 184 | } |
---|
| 185 | |
---|
[5751a56] | 186 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
[69fbc61] | 187 | if( flags & CFA_STATS_IO ) { |
---|
[78ea291] | 188 | sstr | "----- " | type | " \"" | name | "\" (" | "" | id | "" | ") - I/O Stats -----"; |
---|
[986cb99] | 189 | |
---|
[d60d30e] | 190 | uint64_t total_allocs = io.alloc.fast + io.alloc.slow; |
---|
[69fbc61] | 191 | |
---|
[d60d30e] | 192 | uint64_t total_submits = io.submit.fast + io.submit.slow; |
---|
[78ea291] | 193 | sstr | "- totals : allc" | eng3(io.alloc .fast) | nonl; |
---|
| 194 | if(io.alloc.slow) { |
---|
| 195 | double avgfasta = (100.0 * (double)io.alloc.fast) / total_allocs; |
---|
| 196 | sstr | "fast," | eng3(io.alloc .slow) | "slow (" | ws(3, 3, avgfasta) | "%)" | nonl; |
---|
| 197 | } |
---|
| 198 | sstr | " - subm" | eng3(io.submit.fast) | nonl; |
---|
| 199 | if(io.alloc.slow) { |
---|
| 200 | double avgfasts = (100.0 * (double)io.submit.fast) / total_submits; |
---|
| 201 | sstr | "fast," | eng3(io.submit.slow) | "slow (" | ws(3, 3, avgfasts) | "%)" | nonl; |
---|
| 202 | } |
---|
[7ce8873] | 203 | sstr | " - eager" | eng3(io.submit.eagr) | nonl; |
---|
| 204 | sstr | " - no-wait" | eng3(io.submit.nblk) | nonl; |
---|
[78ea291] | 205 | sstr | nl; |
---|
[986cb99] | 206 | |
---|
[78ea291] | 207 | if(io.alloc.fail || io.alloc.revoke || io.alloc.block) |
---|
| 208 | sstr | "- failures : " | eng3(io.alloc.fail) | "oom, " | eng3(io.alloc.revoke) | "rvk, " | eng3(io.alloc.block) | "blk"; |
---|
[70b4aeb9] | 209 | // if(io.flush.external) |
---|
| 210 | // sstr | "- flush external : " | eng3(io.flush.external); |
---|
[69fbc61] | 211 | |
---|
[dddb3dd0] | 212 | double avgsubs = ((double)io.calls.submitted) / io.calls.flush; |
---|
| 213 | double avgcomp = ((double)io.calls.completed) / io.calls.drain; |
---|
[78ea291] | 214 | sstr | "- syscll : " |
---|
[70b4aeb9] | 215 | | " sub " | eng3(io.calls.submitted) | "/" | eng3(io.calls.flush) | "(" | ws(3, 3, avgsubs) | "/flush)" |
---|
| 216 | | " - cmp " | eng3(io.calls.completed) | "/" | eng3(io.calls.drain) | "(" | ws(3, 3, avgcomp) | "/drain)" |
---|
[54c1196] | 217 | | " - cmp " | eng3(io.calls.locked) | "locked, " | eng3(io.calls.helped) | "helped" |
---|
[78ea291] | 218 | | " - " | eng3(io.calls.errors.busy) | " EBUSY"; |
---|
[70b4aeb9] | 219 | sstr | " - sub: " | eng3(io.flush.full) | "full, " | eng3(io.flush.dirty) | "drty, " | eng3(io.flush.idle) | "idle, " | eng3(io.flush.eager) | "eagr, " | eng3(io.flush.external) | "ext"; |
---|
[db614d0] | 220 | sstr | "- ops blk: " |
---|
| 221 | | " sk rd: " | eng3(io.ops.sockread) | "epll: " | eng3(io.ops.epllread) |
---|
| 222 | | " sk wr: " | eng3(io.ops.sockwrite) | "epll: " | eng3(io.ops.epllwrite); |
---|
[986cb99] | 223 | sstr | nl; |
---|
[69fbc61] | 224 | } |
---|
[c34ebf2] | 225 | #endif |
---|
[986cb99] | 226 | |
---|
| 227 | if(flags) write( sstr, stdout ); |
---|
[8834751] | 228 | } |
---|
[73f4d08] | 229 | |
---|
| 230 | #if defined(CFA_STATS_ARRAY) |
---|
| 231 | extern "C" { |
---|
| 232 | #include <stdio.h> |
---|
| 233 | #include <errno.h> |
---|
| 234 | #include <sys/stat.h> |
---|
| 235 | #include <fcntl.h> |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | void __flush_stat( struct __stats_t * this, const char * name, void * handle) { |
---|
| 239 | int ret = mkdir(".cfadata", 0755); |
---|
| 240 | if(ret < 0 && errno != EEXIST) abort("Failed to create directory .cfadata: %d\n", errno); |
---|
| 241 | |
---|
| 242 | char filename[100]; |
---|
| 243 | snprintf(filename, 100, ".cfadata/%s%p.data", name, handle); |
---|
| 244 | |
---|
| 245 | int fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0644); |
---|
| 246 | if(fd < 0) abort("Failed to create file %s: %d\n", filename, errno); |
---|
| 247 | |
---|
| 248 | for(i; this->array.cnt) { |
---|
| 249 | char line[100]; |
---|
| 250 | size_t n = snprintf(line, 100, "%llu, %lld\n", this->array.values[i].ts, this->array.values[i].value); |
---|
| 251 | write(fd, line, n); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | this->array.cnt = 0; |
---|
| 255 | close(fd); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | static __spinlock_t stats_lock; |
---|
| 259 | |
---|
| 260 | void __push_stat( struct __stats_t * this, int64_t value, bool external, const char * name, void * handle ) { |
---|
| 261 | if(external) lock(stats_lock __cfaabi_dbg_ctx2); |
---|
| 262 | |
---|
| 263 | if( this->array.cnt >= CFA_STATS_ARRAY ) __flush_stat( this, name, handle ); |
---|
| 264 | |
---|
| 265 | size_t idx = this->array.cnt; |
---|
| 266 | this->array.cnt++; |
---|
| 267 | |
---|
| 268 | if(external) unlock(stats_lock); |
---|
| 269 | |
---|
| 270 | this->array.values[idx].ts = rdtscl(); |
---|
| 271 | this->array.values[idx].value = value; |
---|
| 272 | } |
---|
| 273 | #endif |
---|
[bbbd2c4] | 274 | #endif |
---|