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