wscript: Add check for --wrap linker flag
[vlendec/samba-autobuild/.git] / source3 / include / smbprofile.h
1 #ifndef _PROFILE_H_
2 #define _PROFILE_H_
3 /*
4    Unix SMB/CIFS implementation.
5    store smbd profiling information in shared memory
6    Copyright (C) Andrew Tridgell 1999
7    Copyright (C) James Peach 2006
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22 */
23
24 struct tevent_context;
25
26 #ifdef WITH_PROFILE
27
28 #define SMBPROFILE_STATS_ALL_SECTIONS \
29         SMBPROFILE_STATS_START \
30         \
31         SMBPROFILE_STATS_SECTION_START(global, "SMBD loop") \
32         SMBPROFILE_STATS_COUNT(connect) \
33         SMBPROFILE_STATS_COUNT(disconnect) \
34         SMBPROFILE_STATS_BASIC(idle) \
35         SMBPROFILE_STATS_TIME(cpu_user) \
36         SMBPROFILE_STATS_TIME(cpu_system) \
37         SMBPROFILE_STATS_COUNT(request) \
38         SMBPROFILE_STATS_BASIC(push_sec_ctx) \
39         SMBPROFILE_STATS_BASIC(set_sec_ctx) \
40         SMBPROFILE_STATS_BASIC(set_root_sec_ctx) \
41         SMBPROFILE_STATS_BASIC(pop_sec_ctx) \
42         SMBPROFILE_STATS_SECTION_END \
43         \
44         SMBPROFILE_STATS_SECTION_START(syscall, "System Calls") \
45         SMBPROFILE_STATS_BASIC(syscall_opendir) \
46         SMBPROFILE_STATS_BASIC(syscall_fdopendir) \
47         SMBPROFILE_STATS_BASIC(syscall_readdir) \
48         SMBPROFILE_STATS_BASIC(syscall_seekdir) \
49         SMBPROFILE_STATS_BASIC(syscall_telldir) \
50         SMBPROFILE_STATS_BASIC(syscall_rewinddir) \
51         SMBPROFILE_STATS_BASIC(syscall_mkdir) \
52         SMBPROFILE_STATS_BASIC(syscall_rmdir) \
53         SMBPROFILE_STATS_BASIC(syscall_closedir) \
54         SMBPROFILE_STATS_BASIC(syscall_open) \
55         SMBPROFILE_STATS_BASIC(syscall_createfile) \
56         SMBPROFILE_STATS_BASIC(syscall_close) \
57         SMBPROFILE_STATS_BYTES(syscall_read) \
58         SMBPROFILE_STATS_BYTES(syscall_pread) \
59         SMBPROFILE_STATS_BYTES(syscall_asys_pread) \
60         SMBPROFILE_STATS_BYTES(syscall_write) \
61         SMBPROFILE_STATS_BYTES(syscall_pwrite) \
62         SMBPROFILE_STATS_BYTES(syscall_asys_pwrite) \
63         SMBPROFILE_STATS_BASIC(syscall_lseek) \
64         SMBPROFILE_STATS_BYTES(syscall_sendfile) \
65         SMBPROFILE_STATS_BYTES(syscall_recvfile) \
66         SMBPROFILE_STATS_BASIC(syscall_rename) \
67         SMBPROFILE_STATS_BASIC(syscall_rename_at) \
68         SMBPROFILE_STATS_BASIC(syscall_fsync) \
69         SMBPROFILE_STATS_BASIC(syscall_asys_fsync) \
70         SMBPROFILE_STATS_BASIC(syscall_stat) \
71         SMBPROFILE_STATS_BASIC(syscall_fstat) \
72         SMBPROFILE_STATS_BASIC(syscall_lstat) \
73         SMBPROFILE_STATS_BASIC(syscall_get_alloc_size) \
74         SMBPROFILE_STATS_BASIC(syscall_unlink) \
75         SMBPROFILE_STATS_BASIC(syscall_chmod) \
76         SMBPROFILE_STATS_BASIC(syscall_fchmod) \
77         SMBPROFILE_STATS_BASIC(syscall_chown) \
78         SMBPROFILE_STATS_BASIC(syscall_fchown) \
79         SMBPROFILE_STATS_BASIC(syscall_lchown) \
80         SMBPROFILE_STATS_BASIC(syscall_chdir) \
81         SMBPROFILE_STATS_BASIC(syscall_getwd) \
82         SMBPROFILE_STATS_BASIC(syscall_ntimes) \
83         SMBPROFILE_STATS_BASIC(syscall_ftruncate) \
84         SMBPROFILE_STATS_BASIC(syscall_fallocate) \
85         SMBPROFILE_STATS_BASIC(syscall_fcntl_lock) \
86         SMBPROFILE_STATS_BASIC(syscall_kernel_flock) \
87         SMBPROFILE_STATS_BASIC(syscall_linux_setlease) \
88         SMBPROFILE_STATS_BASIC(syscall_fcntl_getlock) \
89         SMBPROFILE_STATS_BASIC(syscall_readlink) \
90         SMBPROFILE_STATS_BASIC(syscall_symlink) \
91         SMBPROFILE_STATS_BASIC(syscall_link) \
92         SMBPROFILE_STATS_BASIC(syscall_mknod) \
93         SMBPROFILE_STATS_BASIC(syscall_realpath) \
94         SMBPROFILE_STATS_BASIC(syscall_get_quota) \
95         SMBPROFILE_STATS_BASIC(syscall_set_quota) \
96         SMBPROFILE_STATS_BASIC(syscall_get_sd) \
97         SMBPROFILE_STATS_BASIC(syscall_set_sd) \
98         SMBPROFILE_STATS_BASIC(syscall_brl_lock) \
99         SMBPROFILE_STATS_BASIC(syscall_brl_unlock) \
100         SMBPROFILE_STATS_BASIC(syscall_brl_cancel) \
101         SMBPROFILE_STATS_SECTION_END \
102         \
103         SMBPROFILE_STATS_SECTION_START(acl, "ACL Calls") \
104         SMBPROFILE_STATS_BASIC(get_nt_acl) \
105         SMBPROFILE_STATS_BASIC(fget_nt_acl) \
106         SMBPROFILE_STATS_BASIC(fset_nt_acl) \
107         SMBPROFILE_STATS_BASIC(chmod_acl) \
108         SMBPROFILE_STATS_BASIC(fchmod_acl) \
109         SMBPROFILE_STATS_SECTION_END \
110         \
111         SMBPROFILE_STATS_SECTION_START(statcache, "Stat Cache") \
112         SMBPROFILE_STATS_COUNT(statcache_lookups) \
113         SMBPROFILE_STATS_COUNT(statcache_misses) \
114         SMBPROFILE_STATS_COUNT(statcache_hits) \
115         SMBPROFILE_STATS_SECTION_END \
116         \
117         SMBPROFILE_STATS_SECTION_START(writecache, "Write Cache") \
118         SMBPROFILE_STATS_COUNT(writecache_allocations) \
119         SMBPROFILE_STATS_COUNT(writecache_deallocations) \
120         SMBPROFILE_STATS_COUNT(writecache_cached_reads) \
121         SMBPROFILE_STATS_COUNT(writecache_total_writes) \
122         SMBPROFILE_STATS_COUNT(writecache_init_writes) \
123         SMBPROFILE_STATS_COUNT(writecache_abutted_writes) \
124         SMBPROFILE_STATS_COUNT(writecache_non_oplock_writes) \
125         SMBPROFILE_STATS_COUNT(writecache_direct_writes) \
126         SMBPROFILE_STATS_COUNT(writecache_cached_writes) \
127         SMBPROFILE_STATS_COUNT(writecache_perfect_writes) \
128         SMBPROFILE_STATS_COUNT(writecache_flush_reason_seek) \
129         SMBPROFILE_STATS_COUNT(writecache_flush_reason_read) \
130         SMBPROFILE_STATS_COUNT(writecache_flush_reason_readraw) \
131         SMBPROFILE_STATS_COUNT(writecache_flush_reason_write) \
132         SMBPROFILE_STATS_COUNT(writecache_flush_reason_oplock) \
133         SMBPROFILE_STATS_COUNT(writecache_flush_reason_close) \
134         SMBPROFILE_STATS_COUNT(writecache_flush_reason_sync) \
135         SMBPROFILE_STATS_COUNT(writecache_flush_reason_sizechange) \
136         SMBPROFILE_STATS_SECTION_END \
137         \
138         SMBPROFILE_STATS_SECTION_START(SMB, "SMB Calls") \
139         SMBPROFILE_STATS_BASIC(SMBmkdir) \
140         SMBPROFILE_STATS_BASIC(SMBrmdir) \
141         SMBPROFILE_STATS_BASIC(SMBopen) \
142         SMBPROFILE_STATS_BASIC(SMBcreate) \
143         SMBPROFILE_STATS_BASIC(SMBclose) \
144         SMBPROFILE_STATS_BASIC(SMBflush) \
145         SMBPROFILE_STATS_BASIC(SMBunlink) \
146         SMBPROFILE_STATS_BASIC(SMBmv) \
147         SMBPROFILE_STATS_BASIC(SMBgetatr) \
148         SMBPROFILE_STATS_BASIC(SMBsetatr) \
149         SMBPROFILE_STATS_BASIC(SMBread) \
150         SMBPROFILE_STATS_BASIC(SMBwrite) \
151         SMBPROFILE_STATS_BASIC(SMBlock) \
152         SMBPROFILE_STATS_BASIC(SMBunlock) \
153         SMBPROFILE_STATS_BASIC(SMBctemp) \
154         SMBPROFILE_STATS_BASIC(SMBmknew) \
155         SMBPROFILE_STATS_BASIC(SMBcheckpath) \
156         SMBPROFILE_STATS_BASIC(SMBexit) \
157         SMBPROFILE_STATS_BASIC(SMBlseek) \
158         SMBPROFILE_STATS_BASIC(SMBlockread) \
159         SMBPROFILE_STATS_BASIC(SMBwriteunlock) \
160         SMBPROFILE_STATS_BASIC(SMBreadbraw) \
161         SMBPROFILE_STATS_BASIC(SMBreadBmpx) \
162         SMBPROFILE_STATS_BASIC(SMBreadBs) \
163         SMBPROFILE_STATS_BASIC(SMBwritebraw) \
164         SMBPROFILE_STATS_BASIC(SMBwriteBmpx) \
165         SMBPROFILE_STATS_BASIC(SMBwriteBs) \
166         SMBPROFILE_STATS_BASIC(SMBwritec) \
167         SMBPROFILE_STATS_BASIC(SMBsetattrE) \
168         SMBPROFILE_STATS_BASIC(SMBgetattrE) \
169         SMBPROFILE_STATS_BASIC(SMBlockingX) \
170         SMBPROFILE_STATS_BASIC(SMBtrans) \
171         SMBPROFILE_STATS_BASIC(SMBtranss) \
172         SMBPROFILE_STATS_BASIC(SMBioctl) \
173         SMBPROFILE_STATS_BASIC(SMBioctls) \
174         SMBPROFILE_STATS_BASIC(SMBcopy) \
175         SMBPROFILE_STATS_BASIC(SMBmove) \
176         SMBPROFILE_STATS_BASIC(SMBecho) \
177         SMBPROFILE_STATS_BASIC(SMBwriteclose) \
178         SMBPROFILE_STATS_BASIC(SMBopenX) \
179         SMBPROFILE_STATS_BASIC(SMBreadX) \
180         SMBPROFILE_STATS_BASIC(SMBwriteX) \
181         SMBPROFILE_STATS_BASIC(SMBtrans2) \
182         SMBPROFILE_STATS_BASIC(SMBtranss2) \
183         SMBPROFILE_STATS_BASIC(SMBfindclose) \
184         SMBPROFILE_STATS_BASIC(SMBfindnclose) \
185         SMBPROFILE_STATS_BASIC(SMBtcon) \
186         SMBPROFILE_STATS_BASIC(SMBtdis) \
187         SMBPROFILE_STATS_BASIC(SMBnegprot) \
188         SMBPROFILE_STATS_BASIC(SMBsesssetupX) \
189         SMBPROFILE_STATS_BASIC(SMBulogoffX) \
190         SMBPROFILE_STATS_BASIC(SMBtconX) \
191         SMBPROFILE_STATS_BASIC(SMBdskattr) \
192         SMBPROFILE_STATS_BASIC(SMBsearch) \
193         SMBPROFILE_STATS_BASIC(SMBffirst) \
194         SMBPROFILE_STATS_BASIC(SMBfunique) \
195         SMBPROFILE_STATS_BASIC(SMBfclose) \
196         SMBPROFILE_STATS_BASIC(SMBnttrans) \
197         SMBPROFILE_STATS_BASIC(SMBnttranss) \
198         SMBPROFILE_STATS_BASIC(SMBntcreateX) \
199         SMBPROFILE_STATS_BASIC(SMBntcancel) \
200         SMBPROFILE_STATS_BASIC(SMBntrename) \
201         SMBPROFILE_STATS_BASIC(SMBsplopen) \
202         SMBPROFILE_STATS_BASIC(SMBsplwr) \
203         SMBPROFILE_STATS_BASIC(SMBsplclose) \
204         SMBPROFILE_STATS_BASIC(SMBsplretq) \
205         SMBPROFILE_STATS_BASIC(SMBsends) \
206         SMBPROFILE_STATS_BASIC(SMBsendb) \
207         SMBPROFILE_STATS_BASIC(SMBfwdname) \
208         SMBPROFILE_STATS_BASIC(SMBcancelf) \
209         SMBPROFILE_STATS_BASIC(SMBgetmac) \
210         SMBPROFILE_STATS_BASIC(SMBsendstrt) \
211         SMBPROFILE_STATS_BASIC(SMBsendend) \
212         SMBPROFILE_STATS_BASIC(SMBsendtxt) \
213         SMBPROFILE_STATS_BASIC(SMBinvalid) \
214         SMBPROFILE_STATS_SECTION_END \
215         \
216         SMBPROFILE_STATS_SECTION_START(Trans2, "Trans2 Calls") \
217         SMBPROFILE_STATS_BASIC(Trans2_open) \
218         SMBPROFILE_STATS_BASIC(Trans2_findfirst) \
219         SMBPROFILE_STATS_BASIC(Trans2_findnext) \
220         SMBPROFILE_STATS_BASIC(Trans2_qfsinfo) \
221         SMBPROFILE_STATS_BASIC(Trans2_setfsinfo) \
222         SMBPROFILE_STATS_BASIC(Trans2_qpathinfo) \
223         SMBPROFILE_STATS_BASIC(Trans2_setpathinfo) \
224         SMBPROFILE_STATS_BASIC(Trans2_qfileinfo) \
225         SMBPROFILE_STATS_BASIC(Trans2_setfileinfo) \
226         SMBPROFILE_STATS_BASIC(Trans2_fsctl) \
227         SMBPROFILE_STATS_BASIC(Trans2_ioctl) \
228         SMBPROFILE_STATS_BASIC(Trans2_findnotifyfirst) \
229         SMBPROFILE_STATS_BASIC(Trans2_findnotifynext) \
230         SMBPROFILE_STATS_BASIC(Trans2_mkdir) \
231         SMBPROFILE_STATS_BASIC(Trans2_session_setup) \
232         SMBPROFILE_STATS_BASIC(Trans2_get_dfs_referral) \
233         SMBPROFILE_STATS_BASIC(Trans2_report_dfs_inconsistancy) \
234         SMBPROFILE_STATS_SECTION_END \
235         \
236         SMBPROFILE_STATS_SECTION_START(NT_transact, "NT Transact Calls") \
237         SMBPROFILE_STATS_BASIC(NT_transact_create) \
238         SMBPROFILE_STATS_BASIC(NT_transact_ioctl) \
239         SMBPROFILE_STATS_BASIC(NT_transact_set_security_desc) \
240         SMBPROFILE_STATS_BASIC(NT_transact_notify_change) \
241         SMBPROFILE_STATS_BASIC(NT_transact_rename) \
242         SMBPROFILE_STATS_BASIC(NT_transact_query_security_desc) \
243         SMBPROFILE_STATS_BASIC(NT_transact_get_user_quota) \
244         SMBPROFILE_STATS_BASIC(NT_transact_set_user_quota) \
245         SMBPROFILE_STATS_SECTION_END \
246         \
247         SMBPROFILE_STATS_SECTION_START(smb2, "SMB2 Calls") \
248         SMBPROFILE_STATS_IOBYTES(smb2_negprot) \
249         SMBPROFILE_STATS_IOBYTES(smb2_sesssetup) \
250         SMBPROFILE_STATS_IOBYTES(smb2_logoff) \
251         SMBPROFILE_STATS_IOBYTES(smb2_tcon) \
252         SMBPROFILE_STATS_IOBYTES(smb2_tdis) \
253         SMBPROFILE_STATS_IOBYTES(smb2_create) \
254         SMBPROFILE_STATS_IOBYTES(smb2_close) \
255         SMBPROFILE_STATS_IOBYTES(smb2_flush) \
256         SMBPROFILE_STATS_IOBYTES(smb2_read) \
257         SMBPROFILE_STATS_IOBYTES(smb2_write) \
258         SMBPROFILE_STATS_IOBYTES(smb2_lock) \
259         SMBPROFILE_STATS_IOBYTES(smb2_ioctl) \
260         SMBPROFILE_STATS_IOBYTES(smb2_cancel) \
261         SMBPROFILE_STATS_IOBYTES(smb2_keepalive) \
262         SMBPROFILE_STATS_IOBYTES(smb2_find) \
263         SMBPROFILE_STATS_IOBYTES(smb2_notify) \
264         SMBPROFILE_STATS_IOBYTES(smb2_getinfo) \
265         SMBPROFILE_STATS_IOBYTES(smb2_setinfo) \
266         SMBPROFILE_STATS_IOBYTES(smb2_break) \
267         SMBPROFILE_STATS_SECTION_END \
268         \
269         SMBPROFILE_STATS_END
270
271 /* this file defines the profile structure in the profile shared
272    memory area */
273
274 /* time values in the following structure are in microseconds */
275
276 struct smbprofile_stats_count {
277         uint64_t count;         /* number of events */
278 };
279
280 struct smbprofile_stats_time {
281         uint64_t time;          /* microseconds */
282 };
283
284 struct smbprofile_stats_time_async {
285         uint64_t start;
286         struct smbprofile_stats_time *stats;
287 };
288
289 struct smbprofile_stats_basic {
290         uint64_t count;         /* number of events */
291         uint64_t time;          /* microseconds */
292 };
293
294 struct smbprofile_stats_basic_async {
295         uint64_t start;
296         struct smbprofile_stats_basic *stats;
297 };
298
299 struct smbprofile_stats_bytes {
300         uint64_t count;         /* number of events */
301         uint64_t time;          /* microseconds */
302         uint64_t idle;          /* idle time compared to 'time' microseconds */
303         uint64_t bytes;         /* bytes */
304 };
305
306 struct smbprofile_stats_bytes_async {
307         uint64_t start;
308         uint64_t idle_start;
309         uint64_t idle_time;
310         struct smbprofile_stats_bytes *stats;
311 };
312
313 struct smbprofile_stats_iobytes {
314         uint64_t count;         /* number of events */
315         uint64_t time;          /* microseconds */
316         uint64_t idle;          /* idle time compared to 'time' microseconds */
317         uint64_t inbytes;       /* bytes read */
318         uint64_t outbytes;      /* bytes written */
319 };
320
321 struct smbprofile_stats_iobytes_async {
322         uint64_t start;
323         uint64_t idle_start;
324         uint64_t idle_time;
325         struct smbprofile_stats_iobytes *stats;
326 };
327
328 struct profile_stats {
329         uint64_t magic;
330         struct {
331 #define SMBPROFILE_STATS_START
332 #define SMBPROFILE_STATS_SECTION_START(name, display)
333 #define SMBPROFILE_STATS_COUNT(name) \
334         struct smbprofile_stats_count name##_stats;
335 #define SMBPROFILE_STATS_TIME(name) \
336         struct smbprofile_stats_time name##_stats;
337 #define SMBPROFILE_STATS_BASIC(name) \
338         struct smbprofile_stats_basic name##_stats;
339 #define SMBPROFILE_STATS_BYTES(name) \
340         struct smbprofile_stats_bytes name##_stats;
341 #define SMBPROFILE_STATS_IOBYTES(name) \
342         struct smbprofile_stats_iobytes name##_stats;
343 #define SMBPROFILE_STATS_SECTION_END
344 #define SMBPROFILE_STATS_END
345         SMBPROFILE_STATS_ALL_SECTIONS
346 #undef SMBPROFILE_STATS_START
347 #undef SMBPROFILE_STATS_SECTION_START
348 #undef SMBPROFILE_STATS_COUNT
349 #undef SMBPROFILE_STATS_TIME
350 #undef SMBPROFILE_STATS_BASIC
351 #undef SMBPROFILE_STATS_BYTES
352 #undef SMBPROFILE_STATS_IOBYTES
353 #undef SMBPROFILE_STATS_SECTION_END
354 #undef SMBPROFILE_STATS_END
355         } values;
356 };
357
358 #define _SMBPROFILE_COUNT_INCREMENT(_stats, _area, _v) do { \
359         if (smbprofile_state.config.do_count) { \
360                 (_area)->values._stats.count += (_v); \
361                 smbprofile_dump_schedule(); \
362         } \
363 } while(0)
364 #define SMBPROFILE_COUNT_INCREMENT(_name, _area, _v) \
365         _SMBPROFILE_COUNT_INCREMENT(_name##_stats, _area, _v)
366
367 #define SMBPROFILE_TIME_ASYNC_STATE(_async_name) \
368         struct smbprofile_stats_time_async _async_name;
369 #define _SMBPROFILE_TIME_ASYNC_START(_stats, _area, _async) do { \
370         (_async) = (struct smbprofile_stats_time_async) {}; \
371         if (smbprofile_state.config.do_times) { \
372                 (_async).stats = &((_area)->values._stats), \
373                 (_async).start = profile_timestamp(); \
374         } \
375 } while(0)
376 #define SMBPROFILE_TIME_ASYNC_START(_name, _area, _async) \
377         _SMBPROFILE_TIME_ASYNC_START(_name##_stats, _area, _async)
378 #define SMBPROFILE_TIME_ASYNC_END(_async) do { \
379         if ((_async).start != 0) { \
380                 (_async).stats->time += profile_timestamp() - (_async).start; \
381                 (_async) = (struct smbprofile_stats_basic_async) {}; \
382                 smbprofile_dump_schedule(); \
383         } \
384 } while(0)
385
386 #define SMBPROFILE_BASIC_ASYNC_STATE(_async_name) \
387         struct smbprofile_stats_basic_async _async_name;
388 #define _SMBPROFILE_BASIC_ASYNC_START(_stats, _area, _async) do { \
389         (_async) = (struct smbprofile_stats_basic_async) {}; \
390         if (smbprofile_state.config.do_count) { \
391                 if (smbprofile_state.config.do_times) { \
392                         (_async).start = profile_timestamp(); \
393                         (_async).stats = &((_area)->values._stats); \
394                 } \
395                 (_area)->values._stats.count += 1; \
396                 smbprofile_dump_schedule(); \
397         } \
398 } while(0)
399 #define SMBPROFILE_BASIC_ASYNC_START(_name, _area, _async) \
400         _SMBPROFILE_BASIC_ASYNC_START(_name##_stats, _area, _async)
401 #define SMBPROFILE_BASIC_ASYNC_END(_async) do { \
402         if ((_async).start != 0) { \
403                 (_async).stats->time += profile_timestamp() - (_async).start; \
404                 (_async) = (struct smbprofile_stats_basic_async) {}; \
405                 smbprofile_dump_schedule(); \
406         } \
407 } while(0)
408
409 #define _SMBPROFILE_TIMER_ASYNC_START(_stats, _area, _async) do { \
410         (_async).stats = &((_area)->values._stats); \
411         if (smbprofile_state.config.do_times) { \
412                 (_async).start = profile_timestamp(); \
413         } \
414 } while(0)
415 #define _SMBPROFILE_TIMER_ASYNC_SET_IDLE(_async) do { \
416         if ((_async).start != 0) { \
417                 if ((_async).idle_start == 0) { \
418                         (_async).idle_start = profile_timestamp(); \
419                 } \
420         } \
421 } while(0)
422 #define _SMBPROFILE_TIMER_ASYNC_SET_BUSY(_async) do { \
423         if ((_async).idle_start != 0) { \
424                 (_async).idle_time += \
425                         profile_timestamp() - (_async).idle_start; \
426                 (_async).idle_start = 0; \
427         } \
428 } while(0)
429 #define _SMBPROFILE_TIMER_ASYNC_END(_async) do { \
430         if ((_async).start != 0) { \
431                 _SMBPROFILE_TIMER_ASYNC_SET_BUSY(_async); \
432                 (_async).stats->time += profile_timestamp() - (_async).start; \
433                 (_async).stats->idle += (_async).idle_time; \
434         } \
435 } while(0)
436
437 #define SMBPROFILE_BYTES_ASYNC_STATE(_async_name) \
438         struct smbprofile_stats_bytes_async _async_name;
439 #define _SMBPROFILE_BYTES_ASYNC_START(_stats, _area, _async, _bytes) do { \
440         (_async) = (struct smbprofile_stats_bytes_async) {}; \
441         if (smbprofile_state.config.do_count) { \
442                 _SMBPROFILE_TIMER_ASYNC_START(_stats, _area, _async); \
443                 (_area)->values._stats.count += 1; \
444                 (_area)->values._stats.bytes += (_bytes); \
445                 smbprofile_dump_schedule(); \
446         } \
447 } while(0)
448 #define SMBPROFILE_BYTES_ASYNC_START(_name, _area, _async, _bytes) \
449         _SMBPROFILE_BYTES_ASYNC_START(_name##_stats, _area, _async, _bytes)
450 #define SMBPROFILE_BYTES_ASYNC_SET_IDLE(_async) \
451         _SMBPROFILE_TIMER_ASYNC_SET_IDLE(_async)
452 #define SMBPROFILE_BYTES_ASYNC_SET_BUSY(_async) \
453         _SMBPROFILE_TIMER_ASYNC_SET_BUSY(_async)
454 #define SMBPROFILE_BYTES_ASYNC_END(_async) do { \
455         if ((_async).stats != NULL) { \
456                 _SMBPROFILE_TIMER_ASYNC_END(_async); \
457                 (_async) = (struct smbprofile_stats_bytes_async) {}; \
458                 smbprofile_dump_schedule(); \
459         } \
460 } while(0)
461
462 #define SMBPROFILE_IOBYTES_ASYNC_STATE(_async_name) \
463         struct smbprofile_stats_iobytes_async _async_name;
464 #define _SMBPROFILE_IOBYTES_ASYNC_START(_stats, _area, _async, _inbytes) do { \
465         (_async) = (struct smbprofile_stats_iobytes_async) {}; \
466         if (smbprofile_state.config.do_count) { \
467                 _SMBPROFILE_TIMER_ASYNC_START(_stats, _area, _async); \
468                 (_area)->values._stats.count += 1; \
469                 (_area)->values._stats.inbytes += (_inbytes); \
470                 smbprofile_dump_schedule(); \
471         } \
472 } while(0)
473 #define SMBPROFILE_IOBYTES_ASYNC_START(_name, _area, _async, _inbytes) \
474         _SMBPROFILE_IOBYTES_ASYNC_START(_name##_stats, _area, _async, _inbytes)
475 #define SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(_async) \
476         _SMBPROFILE_TIMER_ASYNC_SET_IDLE(_async)
477 #define SMBPROFILE_IOBYTES_ASYNC_SET_BUSY(_async) \
478         _SMBPROFILE_TIMER_ASYNC_SET_BUSY(_async)
479 #define SMBPROFILE_IOBYTES_ASYNC_END(_async, _outbytes) do { \
480         if ((_async).stats != NULL) { \
481                 (_async).stats->outbytes += (_outbytes); \
482                 _SMBPROFILE_TIMER_ASYNC_END(_async); \
483                 (_async) = (struct smbprofile_stats_iobytes_async) {}; \
484                 smbprofile_dump_schedule(); \
485         } \
486 } while(0)
487
488 extern struct profile_stats *profile_p;
489
490 struct smbprofile_global_state {
491         struct {
492                 struct tdb_wrap *db;
493                 struct tevent_context *ev;
494                 struct tevent_timer *te;
495         } internal;
496
497         struct {
498                 bool do_count;
499                 bool do_times;
500         } config;
501
502         struct {
503                 struct profile_stats global;
504         } stats;
505 };
506
507 extern struct smbprofile_global_state smbprofile_state;
508
509 void smbprofile_dump_schedule_timer(void);
510 void smbprofile_dump_setup(struct tevent_context *ev);
511
512 static inline void smbprofile_dump_schedule(void)
513 {
514         if (likely(smbprofile_state.internal.te != NULL)) {
515                 return;
516         }
517
518         if (unlikely(smbprofile_state.internal.ev == NULL)) {
519                 return;
520         }
521
522         smbprofile_dump_schedule_timer();
523 }
524
525 static inline bool smbprofile_dump_pending(void)
526 {
527         if (smbprofile_state.internal.te == NULL) {
528                 return false;
529         }
530
531         return true;
532 }
533
534 void smbprofile_dump(void);
535
536 void smbprofile_cleanup(pid_t pid, pid_t dst);
537 void smbprofile_stats_accumulate(struct profile_stats *acc,
538                                  const struct profile_stats *add);
539 void smbprofile_collect(struct profile_stats *stats);
540
541 static inline uint64_t profile_timestamp(void)
542 {
543         struct timespec ts;
544
545         /* we might prefer to use the _COARSE clock variant of CLOCK_MONOTONIC
546            that one is faster but cached and "just" tick-wise precise */
547         clock_gettime_mono(&ts);
548         return (ts.tv_sec * 1000000) + (ts.tv_nsec / 1000); /* usec */
549 }
550
551 #define DO_PROFILE_INC(x) \
552         _SMBPROFILE_COUNT_INCREMENT(x##_stats, profile_p, 1); \
553
554 #define START_PROFILE(x) \
555         struct smbprofile_stats_basic_async __profasync_##x = {}; \
556         _SMBPROFILE_BASIC_ASYNC_START(x##_stats, profile_p, __profasync_##x);
557
558 #define START_PROFILE_BYTES(x,n) \
559         struct smbprofile_stats_bytes_async __profasync_##x = {}; \
560         _SMBPROFILE_BYTES_ASYNC_START(x##_stats, profile_p, __profasync_##x, n);
561
562 #define END_PROFILE(x) \
563         SMBPROFILE_BASIC_ASYNC_END(__profasync_##x)
564
565 #define END_PROFILE_BYTES(x) \
566         SMBPROFILE_BYTES_ASYNC_END(__profasync_##x)
567
568 #define PROFILE_TIMESTAMP(x) clock_gettime_mono(x)
569
570 #else /* WITH_PROFILE */
571
572 #define SMBPROFILE_COUNT_INCREMENT(_name, _area, _v)
573
574 #define SMBPROFILE_TIME_ASYNC_STATE(_async_name)
575 #define SMBPROFILE_TIME_ASYNC_START(_name, _area, _async)
576 #define SMBPROFILE_TIME_ASYNC_END(_async)
577
578 #define SMBPROFILE_BASIC_ASYNC_STATE(_async_name)
579 #define SMBPROFILE_BASIC_ASYNC_START(_name, _area, _async)
580 #define SMBPROFILE_BASIC_ASYNC_END(_async)
581
582 #define SMBPROFILE_BYTES_ASYNC_STATE(_async_name)
583 #define SMBPROFILE_BYTES_ASYNC_START(_name, _area, _async, _inbytes)
584 #define SMBPROFILE_BYTES_ASYNC_SET_IDLE(_async)
585 #define SMBPROFILE_BYTES_ASYNC_SET_BUSY(_async)
586 #define SMBPROFILE_BYTES_ASYNC_END(_async)
587
588 #define SMBPROFILE_IOBYTES_ASYNC_STATE(_async_name)
589 #define SMBPROFILE_IOBYTES_ASYNC_START(_name, _area, _async, _inbytes)
590 #define SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(_async)
591 #define SMBPROFILE_IOBYTES_ASYNC_SET_BUSY(_async)
592 #define SMBPROFILE_IOBYTES_ASYNC_END(_async, _outbytes)
593
594 #define DO_PROFILE_INC(x)
595 #define START_PROFILE(x)
596 #define START_PROFILE_BYTES(x,n)
597 #define END_PROFILE(x)
598 #define END_PROFILE_BYTES(x)
599
600 #define PROFILE_TIMESTAMP(x) (*(x)=(struct timespec){0})
601
602 static inline bool smbprofile_dump_pending(void)
603 {
604         return false;
605 }
606
607 static inline void smbprofile_dump_setup(struct tevent_context *ev)
608 {
609         return;
610 }
611
612 static inline void smbprofile_dump(void)
613 {
614         return;
615 }
616
617 static inline void smbprofile_cleanup(pid_t pid, pid_t dst)
618 {
619         return;
620 }
621
622 #endif /* WITH_PROFILE */
623
624 /* The following definitions come from profile/profile.c  */
625 struct server_id;
626
627 void set_profile_level(int level, const struct server_id *src);
628
629 struct messaging_context;
630 bool profile_setup(struct messaging_context *msg_ctx, bool rdonly);
631
632 #endif