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