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