summaryrefslogtreecommitdiff
path: root/source3/include/smbprofile.h
blob: 1183af6a0bfc79a157899868bfb2f1d98e8fbc88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#ifndef _PROFILE_H_
#define _PROFILE_H_
/*
   Unix SMB/CIFS implementation.
   store smbd profiling information in shared memory
   Copyright (C) Andrew Tridgell 1999
   Copyright (C) James Peach 2006

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

struct tevent_context;

#ifdef WITH_PROFILE

#define SMBPROFILE_STATS_ALL_SECTIONS \
	SMBPROFILE_STATS_START \
	\
	SMBPROFILE_STATS_SECTION_START(global, "SMBD loop") \
	SMBPROFILE_STATS_COUNT(connect) \
	SMBPROFILE_STATS_COUNT(disconnect) \
	SMBPROFILE_STATS_BASIC(idle) \
	SMBPROFILE_STATS_TIME(cpu_user) \
	SMBPROFILE_STATS_TIME(cpu_system) \
	SMBPROFILE_STATS_COUNT(request) \
	SMBPROFILE_STATS_BASIC(push_sec_ctx) \
	SMBPROFILE_STATS_BASIC(set_sec_ctx) \
	SMBPROFILE_STATS_BASIC(set_root_sec_ctx) \
	SMBPROFILE_STATS_BASIC(pop_sec_ctx) \
	SMBPROFILE_STATS_SECTION_END \
	\
	SMBPROFILE_STATS_SECTION_START(syscall, "System Calls") \
	SMBPROFILE_STATS_BASIC(syscall_opendir) \
	SMBPROFILE_STATS_BASIC(syscall_fdopendir) \
	SMBPROFILE_STATS_BASIC(syscall_readdir) \
	SMBPROFILE_STATS_BASIC(syscall_seekdir) \
	SMBPROFILE_STATS_BASIC(syscall_telldir) \
	SMBPROFILE_STATS_BASIC(syscall_rewinddir) \
	SMBPROFILE_STATS_BASIC(syscall_mkdir) \
	SMBPROFILE_STATS_BASIC(syscall_rmdir) \
	SMBPROFILE_STATS_BASIC(syscall_closedir) \
	SMBPROFILE_STATS_BASIC(syscall_open) \
	SMBPROFILE_STATS_BASIC(syscall_createfile) \
	SMBPROFILE_STATS_BASIC(syscall_close) \
	SMBPROFILE_STATS_BYTES(syscall_read) \
	SMBPROFILE_STATS_BYTES(syscall_pread) \
	SMBPROFILE_STATS_BYTES(syscall_asys_pread) \
	SMBPROFILE_STATS_BYTES(syscall_write) \
	SMBPROFILE_STATS_BYTES(syscall_pwrite) \
	SMBPROFILE_STATS_BYTES(syscall_asys_pwrite) \
	SMBPROFILE_STATS_BASIC(syscall_lseek) \
	SMBPROFILE_STATS_BYTES(syscall_sendfile) \
	SMBPROFILE_STATS_BYTES(syscall_recvfile) \
	SMBPROFILE_STATS_BASIC(syscall_rename) \
	SMBPROFILE_STATS_BASIC(syscall_rename_at) \
	SMBPROFILE_STATS_BASIC(syscall_fsync) \
	SMBPROFILE_STATS_BASIC(syscall_asys_fsync) \
	SMBPROFILE_STATS_BASIC(syscall_stat) \
	SMBPROFILE_STATS_BASIC(syscall_fstat) \
	SMBPROFILE_STATS_BASIC(syscall_lstat) \
	SMBPROFILE_STATS_BASIC(syscall_get_alloc_size) \
	SMBPROFILE_STATS_BASIC(syscall_unlink) \
	SMBPROFILE_STATS_BASIC(syscall_chmod) \
	SMBPROFILE_STATS_BASIC(syscall_fchmod) \
	SMBPROFILE_STATS_BASIC(syscall_chown) \
	SMBPROFILE_STATS_BASIC(syscall_fchown) \
	SMBPROFILE_STATS_BASIC(syscall_lchown) \
	SMBPROFILE_STATS_BASIC(syscall_chdir) \
	SMBPROFILE_STATS_BASIC(syscall_getwd) \
	SMBPROFILE_STATS_BASIC(syscall_ntimes) \
	SMBPROFILE_STATS_BASIC(syscall_ftruncate) \
	SMBPROFILE_STATS_BASIC(syscall_fallocate) \
	SMBPROFILE_STATS_BASIC(syscall_fcntl_lock) \
	SMBPROFILE_STATS_BASIC(syscall_kernel_flock) \
	SMBPROFILE_STATS_BASIC(syscall_linux_setlease) \
	SMBPROFILE_STATS_BASIC(syscall_fcntl_getlock) \
	SMBPROFILE_STATS_BASIC(syscall_readlink) \
	SMBPROFILE_STATS_BASIC(syscall_symlink) \
	SMBPROFILE_STATS_BASIC(syscall_link) \
	SMBPROFILE_STATS_BASIC(syscall_mknod) \
	SMBPROFILE_STATS_BASIC(syscall_realpath) \
	SMBPROFILE_STATS_BASIC(syscall_get_quota) \
	SMBPROFILE_STATS_BASIC(syscall_set_quota) \
	SMBPROFILE_STATS_BASIC(syscall_get_sd) \
	SMBPROFILE_STATS_BASIC(syscall_set_sd) \
	SMBPROFILE_STATS_BASIC(syscall_brl_lock) \
	SMBPROFILE_STATS_BASIC(syscall_brl_unlock) \
	SMBPROFILE_STATS_BASIC(syscall_brl_cancel) \
	SMBPROFILE_STATS_SECTION_END \
	\
	SMBPROFILE_STATS_SECTION_START(acl, "ACL Calls") \
	SMBPROFILE_STATS_BASIC(get_nt_acl) \
	SMBPROFILE_STATS_BASIC(fget_nt_acl) \
	SMBPROFILE_STATS_BASIC(fset_nt_acl) \
	SMBPROFILE_STATS_BASIC(chmod_acl) \
	SMBPROFILE_STATS_BASIC(fchmod_acl) \
	SMBPROFILE_STATS_SECTION_END \
	\
	SMBPROFILE_STATS_SECTION_START(statcache, "Stat Cache") \
	SMBPROFILE_STATS_COUNT(statcache_lookups) \
	SMBPROFILE_STATS_COUNT(statcache_misses) \
	SMBPROFILE_STATS_COUNT(statcache_hits) \
	SMBPROFILE_STATS_SECTION_END \
	\
	SMBPROFILE_STATS_SECTION_START(writecache, "Write Cache") \
	SMBPROFILE_STATS_COUNT(writecache_allocations) \
	SMBPROFILE_STATS_COUNT(writecache_deallocations) \
	SMBPROFILE_STATS_COUNT(writecache_cached_reads) \
	SMBPROFILE_STATS_COUNT(writecache_total_writes) \
	SMBPROFILE_STATS_COUNT(writecache_init_writes) \
	SMBPROFILE_STATS_COUNT(writecache_abutted_writes) \
	SMBPROFILE_STATS_COUNT(writecache_non_oplock_writes) \
	SMBPROFILE_STATS_COUNT(writecache_direct_writes) \
	SMBPROFILE_STATS_COUNT(writecache_cached_writes) \
	SMBPROFILE_STATS_COUNT(writecache_perfect_writes) \
	SMBPROFILE_STATS_COUNT(writecache_flush_reason_seek) \
	SMBPROFILE_STATS_COUNT(writecache_flush_reason_read) \
	SMBPROFILE_STATS_COUNT(writecache_flush_reason_readraw) \
	SMBPROFILE_STATS_COUNT(writecache_flush_reason_write) \
	SMBPROFILE_STATS_COUNT(writecache_flush_reason_oplock) \
	SMBPROFILE_STATS_COUNT(writecache_flush_reason_close) \
	SMBPROFILE_STATS_COUNT(writecache_flush_reason_sync) \
	SMBPROFILE_STATS_COUNT(writecache_flush_reason_sizechange) \
	SMBPROFILE_STATS_SECTION_END \
	\
	SMBPROFILE_STATS_SECTION_START(SMB, "SMB Calls") \
	SMBPROFILE_STATS_BASIC(SMBmkdir) \
	SMBPROFILE_STATS_BASIC(SMBrmdir) \
	SMBPROFILE_STATS_BASIC(SMBopen) \
	SMBPROFILE_STATS_BASIC(SMBcreate) \
	SMBPROFILE_STATS_BASIC(SMBclose) \
	SMBPROFILE_STATS_BASIC(SMBflush) \
	SMBPROFILE_STATS_BASIC(SMBunlink) \
	SMBPROFILE_STATS_BASIC(SMBmv) \
	SMBPROFILE_STATS_BASIC(SMBgetatr) \
	SMBPROFILE_STATS_BASIC(SMBsetatr) \
	SMBPROFILE_STATS_BASIC(SMBread) \
	SMBPROFILE_STATS_BASIC(SMBwrite) \
	SMBPROFILE_STATS_BASIC(SMBlock) \
	SMBPROFILE_STATS_BASIC(SMBunlock) \
	SMBPROFILE_STATS_BASIC(SMBctemp) \
	SMBPROFILE_STATS_BASIC(SMBmknew) \
	SMBPROFILE_STATS_BASIC(SMBcheckpath) \
	SMBPROFILE_STATS_BASIC(SMBexit) \
	SMBPROFILE_STATS_BASIC(SMBlseek) \
	SMBPROFILE_STATS_BASIC(SMBlockread) \
	SMBPROFILE_STATS_BASIC(SMBwriteunlock) \
	SMBPROFILE_STATS_BASIC(SMBreadbraw) \
	SMBPROFILE_STATS_BASIC(SMBreadBmpx) \
	SMBPROFILE_STATS_BASIC(SMBreadBs) \
	SMBPROFILE_STATS_BASIC(SMBwritebraw) \
	SMBPROFILE_STATS_BASIC(SMBwriteBmpx) \
	SMBPROFILE_STATS_BASIC(SMBwriteBs) \
	SMBPROFILE_STATS_BASIC(SMBwritec) \
	SMBPROFILE_STATS_BASIC(SMBsetattrE) \
	SMBPROFILE_STATS_BASIC(SMBgetattrE) \
	SMBPROFILE_STATS_BASIC(SMBlockingX) \
	SMBPROFILE_STATS_BASIC(SMBtrans) \
	SMBPROFILE_STATS_BASIC(SMBtranss) \
	SMBPROFILE_STATS_BASIC(SMBioctl) \
	SMBPROFILE_STATS_BASIC(SMBioctls) \
	SMBPROFILE_STATS_BASIC(SMBcopy) \
	SMBPROFILE_STATS_BASIC(SMBmove) \
	SMBPROFILE_STATS_BASIC(SMBecho) \
	SMBPROFILE_STATS_BASIC(SMBwriteclose) \
	SMBPROFILE_STATS_BASIC(SMBopenX) \
	SMBPROFILE_STATS_BASIC(SMBreadX) \
	SMBPROFILE_STATS_BASIC(SMBwriteX) \
	SMBPROFILE_STATS_BASIC(SMBtrans2) \
	SMBPROFILE_STATS_BASIC(SMBtranss2) \
	SMBPROFILE_STATS_BASIC(SMBfindclose) \
	SMBPROFILE_STATS_BASIC(SMBfindnclose) \
	SMBPROFILE_STATS_BASIC(SMBtcon) \
	SMBPROFILE_STATS_BASIC(SMBtdis) \
	SMBPROFILE_STATS_BASIC(SMBnegprot) \
	SMBPROFILE_STATS_BASIC(SMBsesssetupX) \
	SMBPROFILE_STATS_BASIC(SMBulogoffX) \
	SMBPROFILE_STATS_BASIC(SMBtconX) \
	SMBPROFILE_STATS_BASIC(SMBdskattr) \
	SMBPROFILE_STATS_BASIC(SMBsearch) \
	SMBPROFILE_STATS_BASIC(SMBffirst) \
	SMBPROFILE_STATS_BASIC(SMBfunique) \
	SMBPROFILE_STATS_BASIC(SMBfclose) \
	SMBPROFILE_STATS_BASIC(SMBnttrans) \
	SMBPROFILE_STATS_BASIC(SMBnttranss) \
	SMBPROFILE_STATS_BASIC(SMBntcreateX) \
	SMBPROFILE_STATS_BASIC(SMBntcancel) \
	SMBPROFILE_STATS_BASIC(SMBntrename) \
	SMBPROFILE_STATS_BASIC(SMBsplopen) \
	SMBPROFILE_STATS_BASIC(SMBsplwr) \
	SMBPROFILE_STATS_BASIC(SMBsplclose) \
	SMBPROFILE_STATS_BASIC(SMBsplretq) \
	SMBPROFILE_STATS_BASIC(SMBsends) \
	SMBPROFILE_STATS_BASIC(SMBsendb) \
	SMBPROFILE_STATS_BASIC(SMBfwdname) \
	SMBPROFILE_STATS_BASIC(SMBcancelf) \
	SMBPROFILE_STATS_BASIC(SMBgetmac) \
	SMBPROFILE_STATS_BASIC(SMBsendstrt) \
	SMBPROFILE_STATS_BASIC(SMBsendend) \
	SMBPROFILE_STATS_BASIC(SMBsendtxt) \
	SMBPROFILE_STATS_BASIC(SMBinvalid) \
	SMBPROFILE_STATS_SECTION_END \
	\
	SMBPROFILE_STATS_SECTION_START(Trans2, "Trans2 Calls") \
	SMBPROFILE_STATS_BASIC(Trans2_open) \
	SMBPROFILE_STATS_BASIC(Trans2_findfirst) \
	SMBPROFILE_STATS_BASIC(Trans2_findnext) \
	SMBPROFILE_STATS_BASIC(Trans2_qfsinfo) \
	SMBPROFILE_STATS_BASIC(Trans2_setfsinfo) \
	SMBPROFILE_STATS_BASIC(Trans2_qpathinfo) \
	SMBPROFILE_STATS_BASIC(Trans2_setpathinfo) \
	SMBPROFILE_STATS_BASIC(Trans2_qfileinfo) \
	SMBPROFILE_STATS_BASIC(Trans2_setfileinfo) \
	SMBPROFILE_STATS_BASIC(Trans2_fsctl) \
	SMBPROFILE_STATS_BASIC(Trans2_ioctl) \
	SMBPROFILE_STATS_BASIC(Trans2_findnotifyfirst) \
	SMBPROFILE_STATS_BASIC(Trans2_findnotifynext) \
	SMBPROFILE_STATS_BASIC(Trans2_mkdir) \
	SMBPROFILE_STATS_BASIC(Trans2_session_setup) \
	SMBPROFILE_STATS_BASIC(Trans2_get_dfs_referral) \
	SMBPROFILE_STATS_BASIC(Trans2_report_dfs_inconsistancy) \
	SMBPROFILE_STATS_SECTION_END \
	\
	SMBPROFILE_STATS_SECTION_START(NT_transact, "NT Transact Calls") \
	SMBPROFILE_STATS_BASIC(NT_transact_create) \
	SMBPROFILE_STATS_BASIC(NT_transact_ioctl) \
	SMBPROFILE_STATS_BASIC(NT_transact_set_security_desc) \
	SMBPROFILE_STATS_BASIC(NT_transact_notify_change) \
	SMBPROFILE_STATS_BASIC(NT_transact_rename) \
	SMBPROFILE_STATS_BASIC(NT_transact_query_security_desc) \
	SMBPROFILE_STATS_BASIC(NT_transact_get_user_quota) \
	SMBPROFILE_STATS_BASIC(NT_transact_set_user_quota) \
	SMBPROFILE_STATS_SECTION_END \
	\
	SMBPROFILE_STATS_SECTION_START(smb2, "SMB2 Calls") \
	SMBPROFILE_STATS_IOBYTES(smb2_negprot) \
	SMBPROFILE_STATS_IOBYTES(smb2_sesssetup) \
	SMBPROFILE_STATS_IOBYTES(smb2_logoff) \
	SMBPROFILE_STATS_IOBYTES(smb2_tcon) \
	SMBPROFILE_STATS_IOBYTES(smb2_tdis) \
	SMBPROFILE_STATS_IOBYTES(smb2_create) \
	SMBPROFILE_STATS_IOBYTES(smb2_close) \
	SMBPROFILE_STATS_IOBYTES(smb2_flush) \
	SMBPROFILE_STATS_IOBYTES(smb2_read) \
	SMBPROFILE_STATS_IOBYTES(smb2_write) \
	SMBPROFILE_STATS_IOBYTES(smb2_lock) \
	SMBPROFILE_STATS_IOBYTES(smb2_ioctl) \
	SMBPROFILE_STATS_IOBYTES(smb2_cancel) \
	SMBPROFILE_STATS_IOBYTES(smb2_keepalive) \
	SMBPROFILE_STATS_IOBYTES(smb2_find) \
	SMBPROFILE_STATS_IOBYTES(smb2_notify) \
	SMBPROFILE_STATS_IOBYTES(smb2_getinfo) \
	SMBPROFILE_STATS_IOBYTES(smb2_setinfo) \
	SMBPROFILE_STATS_IOBYTES(smb2_break) \
	SMBPROFILE_STATS_SECTION_END \
	\
	SMBPROFILE_STATS_END

/* this file defines the profile structure in the profile shared
   memory area */

/* time values in the following structure are in microseconds */

struct smbprofile_stats_count {
	uint64_t count;		/* number of events */
};

struct smbprofile_stats_time {
	uint64_t time;		/* microseconds */
};

struct smbprofile_stats_time_async {
	uint64_t start;
	struct smbprofile_stats_time *stats;
};

struct smbprofile_stats_basic {
	uint64_t count;		/* number of events */
	uint64_t time;		/* microseconds */
};

struct smbprofile_stats_basic_async {
	uint64_t start;
	struct smbprofile_stats_basic *stats;
};

struct smbprofile_stats_bytes {
	uint64_t count;		/* number of events */
	uint64_t time;		/* microseconds */
	uint64_t idle;		/* idle time compared to 'time' microseconds */
	uint64_t bytes;		/* bytes */
};

struct smbprofile_stats_bytes_async {
	uint64_t start;
	uint64_t idle_start;
	uint64_t idle_time;
	struct smbprofile_stats_bytes *stats;
};

struct smbprofile_stats_iobytes {
	uint64_t count;		/* number of events */
	uint64_t time;		/* microseconds */
	uint64_t idle;		/* idle time compared to 'time' microseconds */
	uint64_t inbytes;	/* bytes read */
	uint64_t outbytes;	/* bytes written */
};

struct smbprofile_stats_iobytes_async {
	uint64_t start;
	uint64_t idle_start;
	uint64_t idle_time;
	struct smbprofile_stats_iobytes *stats;
};

struct profile_stats {
	uint64_t magic;
	struct {
#define SMBPROFILE_STATS_START
#define SMBPROFILE_STATS_SECTION_START(name, display)
#define SMBPROFILE_STATS_COUNT(name) \
	struct smbprofile_stats_count name##_stats;
#define SMBPROFILE_STATS_TIME(name) \
	struct smbprofile_stats_time name##_stats;
#define SMBPROFILE_STATS_BASIC(name) \
	struct smbprofile_stats_basic name##_stats;
#define SMBPROFILE_STATS_BYTES(name) \
	struct smbprofile_stats_bytes name##_stats;
#define SMBPROFILE_STATS_IOBYTES(name) \
	struct smbprofile_stats_iobytes name##_stats;
#define SMBPROFILE_STATS_SECTION_END
#define SMBPROFILE_STATS_END
	SMBPROFILE_STATS_ALL_SECTIONS
#undef SMBPROFILE_STATS_START
#undef SMBPROFILE_STATS_SECTION_START
#undef SMBPROFILE_STATS_COUNT
#undef SMBPROFILE_STATS_TIME
#undef SMBPROFILE_STATS_BASIC
#undef SMBPROFILE_STATS_BYTES
#undef SMBPROFILE_STATS_IOBYTES
#undef SMBPROFILE_STATS_SECTION_END
#undef SMBPROFILE_STATS_END
	} values;
};

#define _SMBPROFILE_COUNT_INCREMENT(_stats, _area, _v) do { \
	if (smbprofile_state.config.do_count) { \
		(_area)->values._stats.count += (_v); \
		smbprofile_dump_schedule(); \
	} \
} while(0)
#define SMBPROFILE_COUNT_INCREMENT(_name, _area, _v) \
	_SMBPROFILE_COUNT_INCREMENT(_name##_stats, _area, _v)

#define SMBPROFILE_TIME_ASYNC_STATE(_async_name) \
	struct smbprofile_stats_time_async _async_name;
#define _SMBPROFILE_TIME_ASYNC_START(_stats, _area, _async) do { \
	(_async) = (struct smbprofile_stats_time_async) {}; \
	if (smbprofile_state.config.do_times) { \
		(_async).stats = &((_area)->values._stats), \
		(_async).start = profile_timestamp(); \
	} \
} while(0)
#define SMBPROFILE_TIME_ASYNC_START(_name, _area, _async) \
	_SMBPROFILE_TIME_ASYNC_START(_name##_stats, _area, _async)
#define SMBPROFILE_TIME_ASYNC_END(_async) do { \
	if ((_async).start != 0) { \
		(_async).stats->time += profile_timestamp() - (_async).start; \
		(_async) = (struct smbprofile_stats_basic_async) {}; \
		smbprofile_dump_schedule(); \
	} \
} while(0)

#define SMBPROFILE_BASIC_ASYNC_STATE(_async_name) \
	struct smbprofile_stats_basic_async _async_name;
#define _SMBPROFILE_BASIC_ASYNC_START(_stats, _area, _async) do { \
	(_async) = (struct smbprofile_stats_basic_async) {}; \
	if (smbprofile_state.config.do_count) { \
		if (smbprofile_state.config.do_times) { \
			(_async).start = profile_timestamp(); \
			(_async).stats = &((_area)->values._stats); \
		} \
		(_area)->values._stats.count += 1; \
		smbprofile_dump_schedule(); \
	} \
} while(0)
#define SMBPROFILE_BASIC_ASYNC_START(_name, _area, _async) \
	_SMBPROFILE_BASIC_ASYNC_START(_name##_stats, _area, _async)
#define SMBPROFILE_BASIC_ASYNC_END(_async) do { \
	if ((_async).start != 0) { \
		(_async).stats->time += profile_timestamp() - (_async).start; \
		(_async) = (struct smbprofile_stats_basic_async) {}; \
		smbprofile_dump_schedule(); \
	} \
} while(0)

#define _SMBPROFILE_TIMER_ASYNC_START(_stats, _area, _async) do { \
	(_async).stats = &((_area)->values._stats); \
	if (smbprofile_state.config.do_times) { \
		(_async).start = profile_timestamp(); \
	} \
} while(0)
#define _SMBPROFILE_TIMER_ASYNC_SET_IDLE(_async) do { \
	if ((_async).start != 0) { \
		if ((_async).idle_start == 0) { \
			(_async).idle_start = profile_timestamp(); \
		} \
	} \
} while(0)
#define _SMBPROFILE_TIMER_ASYNC_SET_BUSY(_async) do { \
	if ((_async).idle_start != 0) { \
		(_async).idle_time += \
			profile_timestamp() - (_async).idle_start; \
		(_async).idle_start = 0; \
	} \
} while(0)
#define _SMBPROFILE_TIMER_ASYNC_END(_async) do { \
	if ((_async).start != 0) { \
		_SMBPROFILE_TIMER_ASYNC_SET_BUSY(_async); \
		(_async).stats->time += profile_timestamp() - (_async).start; \
		(_async).stats->idle += (_async).idle_time; \
	} \
} while(0)

#define SMBPROFILE_BYTES_ASYNC_STATE(_async_name) \
	struct smbprofile_stats_bytes_async _async_name;
#define _SMBPROFILE_BYTES_ASYNC_START(_stats, _area, _async, _bytes) do { \
	(_async) = (struct smbprofile_stats_bytes_async) {}; \
	if (smbprofile_state.config.do_count) { \
		_SMBPROFILE_TIMER_ASYNC_START(_stats, _area, _async); \
		(_area)->values._stats.count += 1; \
		(_area)->values._stats.bytes += (_bytes); \
		smbprofile_dump_schedule(); \
	} \
} while(0)
#define SMBPROFILE_BYTES_ASYNC_START(_name, _area, _async, _bytes) \
	_SMBPROFILE_BYTES_ASYNC_START(_name##_stats, _area, _async, _bytes)
#define SMBPROFILE_BYTES_ASYNC_SET_IDLE(_async) \
	_SMBPROFILE_TIMER_ASYNC_SET_IDLE(_async)
#define SMBPROFILE_BYTES_ASYNC_SET_BUSY(_async) \
	_SMBPROFILE_TIMER_ASYNC_SET_BUSY(_async)
#define SMBPROFILE_BYTES_ASYNC_END(_async) do { \
	if ((_async).stats != NULL) { \
		_SMBPROFILE_TIMER_ASYNC_END(_async); \
		(_async) = (struct smbprofile_stats_bytes_async) {}; \
		smbprofile_dump_schedule(); \
	} \
} while(0)

#define SMBPROFILE_IOBYTES_ASYNC_STATE(_async_name) \
	struct smbprofile_stats_iobytes_async _async_name;
#define _SMBPROFILE_IOBYTES_ASYNC_START(_stats, _area, _async, _inbytes) do { \
	(_async) = (struct smbprofile_stats_iobytes_async) {}; \
	if (smbprofile_state.config.do_count) { \
		_SMBPROFILE_TIMER_ASYNC_START(_stats, _area, _async); \
		(_area)->values._stats.count += 1; \
		(_area)->values._stats.inbytes += (_inbytes); \
		smbprofile_dump_schedule(); \
	} \
} while(0)
#define SMBPROFILE_IOBYTES_ASYNC_START(_name, _area, _async, _inbytes) \
	_SMBPROFILE_IOBYTES_ASYNC_START(_name##_stats, _area, _async, _inbytes)
#define SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(_async) \
	_SMBPROFILE_TIMER_ASYNC_SET_IDLE(_async)
#define SMBPROFILE_IOBYTES_ASYNC_SET_BUSY(_async) \
	_SMBPROFILE_TIMER_ASYNC_SET_BUSY(_async)
#define SMBPROFILE_IOBYTES_ASYNC_END(_async, _outbytes) do { \
	if ((_async).stats != NULL) { \
		(_async).stats->outbytes += (_outbytes); \
		_SMBPROFILE_TIMER_ASYNC_END(_async); \
		(_async) = (struct smbprofile_stats_iobytes_async) {}; \
		smbprofile_dump_schedule(); \
	} \
} while(0)

extern struct profile_stats *profile_p;

struct smbprofile_global_state {
	struct {
		struct tdb_wrap *db;
		struct tevent_context *ev;
		struct tevent_timer *te;
	} internal;

	struct {
		bool do_count;
		bool do_times;
	} config;

	struct {
		struct profile_stats global;
	} stats;
};

extern struct smbprofile_global_state smbprofile_state;

void smbprofile_dump_schedule_timer(void);
void smbprofile_dump_setup(struct tevent_context *ev);

static inline void smbprofile_dump_schedule(void)
{
	if (likely(smbprofile_state.internal.te != NULL)) {
		return;
	}

	if (unlikely(smbprofile_state.internal.ev == NULL)) {
		return;
	}

	smbprofile_dump_schedule_timer();
}

static inline bool smbprofile_dump_pending(void)
{
	if (smbprofile_state.internal.te == NULL) {
		return false;
	}

	return true;
}

void smbprofile_dump(void);

void smbprofile_cleanup(pid_t pid, pid_t dst);
void smbprofile_stats_accumulate(struct profile_stats *acc,
				 const struct profile_stats *add);
void smbprofile_collect(struct profile_stats *stats);

static inline uint64_t profile_timestamp(void)
{
	struct timespec ts;

	/* we might prefer to use the _COARSE clock variant of CLOCK_MONOTONIC
	   that one is faster but cached and "just" tick-wise precise */
	clock_gettime_mono(&ts);
	return (ts.tv_sec * 1000000) + (ts.tv_nsec / 1000); /* usec */
}

#define DO_PROFILE_INC(x) \
	_SMBPROFILE_COUNT_INCREMENT(x##_stats, profile_p, 1); \

#define START_PROFILE(x) \
	struct smbprofile_stats_basic_async __profasync_##x = {}; \
	_SMBPROFILE_BASIC_ASYNC_START(x##_stats, profile_p, __profasync_##x);

#define START_PROFILE_BYTES(x,n) \
	struct smbprofile_stats_bytes_async __profasync_##x = {}; \
	_SMBPROFILE_BYTES_ASYNC_START(x##_stats, profile_p, __profasync_##x, n);

#define END_PROFILE(x) \
	SMBPROFILE_BASIC_ASYNC_END(__profasync_##x)

#define END_PROFILE_BYTES(x) \
	SMBPROFILE_BYTES_ASYNC_END(__profasync_##x)

#define PROFILE_TIMESTAMP(x) clock_gettime_mono(x)

#else /* WITH_PROFILE */

#define SMBPROFILE_COUNT_INCREMENT(_name, _area, _v)

#define SMBPROFILE_TIME_ASYNC_STATE(_async_name)
#define SMBPROFILE_TIME_ASYNC_START(_name, _area, _async)
#define SMBPROFILE_TIME_ASYNC_END(_async)

#define SMBPROFILE_BASIC_ASYNC_STATE(_async_name)
#define SMBPROFILE_BASIC_ASYNC_START(_name, _area, _async)
#define SMBPROFILE_BASIC_ASYNC_END(_async)

#define SMBPROFILE_BYTES_ASYNC_STATE(_async_name)
#define SMBPROFILE_BYTES_ASYNC_START(_name, _area, _async, _inbytes)
#define SMBPROFILE_BYTES_ASYNC_SET_IDLE(_async)
#define SMBPROFILE_BYTES_ASYNC_SET_BUSY(_async)
#define SMBPROFILE_BYTES_ASYNC_END(_async)

#define SMBPROFILE_IOBYTES_ASYNC_STATE(_async_name)
#define SMBPROFILE_IOBYTES_ASYNC_START(_name, _area, _async, _inbytes)
#define SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(_async)
#define SMBPROFILE_IOBYTES_ASYNC_SET_BUSY(_async)
#define SMBPROFILE_IOBYTES_ASYNC_END(_async, _outbytes)

#define DO_PROFILE_INC(x)
#define START_PROFILE(x)
#define START_PROFILE_BYTES(x,n)
#define END_PROFILE(x)
#define END_PROFILE_BYTES(x)

#define PROFILE_TIMESTAMP(x) (*(x)=(struct timespec){0})

static inline bool smbprofile_dump_pending(void)
{
	return false;
}

static inline void smbprofile_dump_setup(struct tevent_context *ev)
{
	return;
}

static inline void smbprofile_dump(void)
{
	return;
}

static inline void smbprofile_cleanup(pid_t pid, pid_t dst)
{
	return;
}

#endif /* WITH_PROFILE */

/* The following definitions come from profile/profile.c  */
struct server_id;

void set_profile_level(int level, const struct server_id *src);

struct messaging_context;
bool profile_setup(struct messaging_context *msg_ctx, bool rdonly);

#endif