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