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