s3: Fix the build
[Samba/gebeck_regimport.git] / source3 / profile / profile.c
blob817bbb6b2dcfdc91cf0125f200c0e9276bb09d53
1 /*
2 Unix SMB/CIFS implementation.
3 store smbd profiling information in shared memory
4 Copyright (C) Andrew Tridgell 1999
5 Copyright (C) James Peach 2006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "librpc/gen_ndr/messaging.h"
25 #ifdef WITH_PROFILE
26 #define IPC_PERMS ((S_IRUSR | S_IWUSR) | S_IRGRP | S_IROTH)
27 #endif /* WITH_PROFILE */
29 #ifdef WITH_PROFILE
30 static int shm_id;
31 static bool read_only;
32 #if defined(HAVE_CLOCK_GETTIME)
33 clockid_t __profile_clock;
34 bool have_profiling_clock = False;
35 #endif
36 #endif
38 struct profile_header *profile_h;
39 struct profile_stats *profile_p;
41 bool do_profile_flag = False;
42 bool do_profile_times = False;
44 /****************************************************************************
45 Set a profiling level.
46 ****************************************************************************/
47 void set_profile_level(int level, struct server_id src)
49 #ifdef WITH_PROFILE
50 switch (level) {
51 case 0: /* turn off profiling */
52 do_profile_flag = False;
53 do_profile_times = False;
54 DEBUG(1,("INFO: Profiling turned OFF from pid %d\n",
55 (int)procid_to_pid(&src)));
56 break;
57 case 1: /* turn on counter profiling only */
58 do_profile_flag = True;
59 do_profile_times = False;
60 DEBUG(1,("INFO: Profiling counts turned ON from pid %d\n",
61 (int)procid_to_pid(&src)));
62 break;
63 case 2: /* turn on complete profiling */
65 #if defined(HAVE_CLOCK_GETTIME)
66 if (!have_profiling_clock) {
67 do_profile_flag = True;
68 do_profile_times = False;
69 DEBUG(1,("INFO: Profiling counts turned ON from "
70 "pid %d\n", (int)procid_to_pid(&src)));
71 DEBUGADD(1,("INFO: Profiling times disabled "
72 "due to lack of a suitable clock\n"));
73 break;
75 #endif
77 do_profile_flag = True;
78 do_profile_times = True;
79 DEBUG(1,("INFO: Full profiling turned ON from pid %d\n",
80 (int)procid_to_pid(&src)));
81 break;
82 case 3: /* reset profile values */
83 memset((char *)profile_p, 0, sizeof(*profile_p));
84 DEBUG(1,("INFO: Profiling values cleared from pid %d\n",
85 (int)procid_to_pid(&src)));
86 break;
88 #else /* WITH_PROFILE */
89 DEBUG(1,("INFO: Profiling support unavailable in this build.\n"));
90 #endif /* WITH_PROFILE */
93 #ifdef WITH_PROFILE
95 /****************************************************************************
96 receive a set profile level message
97 ****************************************************************************/
98 static void profile_message(struct messaging_context *msg_ctx,
99 void *private_data,
100 uint32_t msg_type,
101 struct server_id src,
102 DATA_BLOB *data)
104 int level;
106 if (data->length != sizeof(level)) {
107 DEBUG(0, ("got invalid profile message\n"));
108 return;
111 memcpy(&level, data->data, sizeof(level));
112 set_profile_level(level, src);
115 /****************************************************************************
116 receive a request profile level message
117 ****************************************************************************/
118 static void reqprofile_message(struct messaging_context *msg_ctx,
119 void *private_data,
120 uint32_t msg_type,
121 struct server_id src,
122 DATA_BLOB *data)
124 int level;
126 #ifdef WITH_PROFILE
127 level = 1 + (do_profile_flag?2:0) + (do_profile_times?4:0);
128 #else
129 level = 0;
130 #endif
131 DEBUG(1,("INFO: Received REQ_PROFILELEVEL message from PID %u\n",
132 (unsigned int)procid_to_pid(&src)));
133 messaging_send_buf(msg_ctx, src, MSG_PROFILELEVEL,
134 (uint8 *)&level, sizeof(level));
137 /*******************************************************************
138 open the profiling shared memory area
139 ******************************************************************/
141 #ifdef HAVE_CLOCK_GETTIME
143 /* Find a clock. Just because the definition for a particular clock ID is
144 * present doesn't mean the system actually supports it.
146 static void init_clock_gettime(void)
148 struct timespec ts;
150 have_profiling_clock = False;
152 #ifdef HAVE_CLOCK_PROCESS_CPUTIME_ID
153 /* CLOCK_PROCESS_CPUTIME_ID is sufficiently fast that the
154 * always profiling times is plausible. Unfortunately on Linux
155 * it is only accurate if we can guarantee we will not be scheduled
156 * scheduled onto a different CPU between samples. Until there is
157 * some way to set processor affinity, we can only use this on
158 * uniprocessors.
160 if (!this_is_smp()) {
161 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
162 DEBUG(10, ("Using CLOCK_PROCESS_CPUTIME_ID "
163 "for profile_clock\n"));
164 __profile_clock = CLOCK_PROCESS_CPUTIME_ID;
165 have_profiling_clock = True;
168 #endif
170 #ifdef HAVE_CLOCK_MONOTONIC
171 if (!have_profiling_clock &&
172 clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
173 DEBUG(10, ("Using CLOCK_MONOTONIC for profile_clock\n"));
174 __profile_clock = CLOCK_MONOTONIC;
175 have_profiling_clock = True;
177 #endif
179 #ifdef HAVE_CLOCK_REALTIME
180 /* POSIX says that CLOCK_REALTIME should be defined everywhere
181 * where we have clock_gettime...
183 if (!have_profiling_clock &&
184 clock_gettime(CLOCK_REALTIME, &ts) == 0) {
185 __profile_clock = CLOCK_REALTIME;
186 have_profiling_clock = True;
188 SMB_WARN(__profile_clock != CLOCK_REALTIME,
189 ("forced to use a slow profiling clock"));
192 #endif
194 SMB_WARN(have_profiling_clock == True,
195 ("could not find a working clock for profiling"));
196 return;
198 #endif
200 bool profile_setup(struct messaging_context *msg_ctx, bool rdonly)
202 struct shmid_ds shm_ds;
204 read_only = rdonly;
206 #ifdef HAVE_CLOCK_GETTIME
207 init_clock_gettime();
208 #endif
210 again:
211 /* try to use an existing key */
212 shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
214 /* if that failed then create one. There is a race condition here
215 if we are running from inetd. Bad luck. */
216 if (shm_id == -1) {
217 if (read_only) return False;
218 shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_h),
219 IPC_CREAT | IPC_EXCL | IPC_PERMS);
222 if (shm_id == -1) {
223 DEBUG(0,("Can't create or use IPC area. Error was %s\n",
224 strerror(errno)));
225 return False;
229 profile_h = (struct profile_header *)shmat(shm_id, 0,
230 read_only?SHM_RDONLY:0);
231 if ((long)profile_h == -1) {
232 DEBUG(0,("Can't attach to IPC area. Error was %s\n",
233 strerror(errno)));
234 return False;
237 /* find out who created this memory area */
238 if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
239 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n",
240 strerror(errno)));
241 return False;
244 if (shm_ds.shm_perm.cuid != sec_initial_uid() ||
245 shm_ds.shm_perm.cgid != sec_initial_gid()) {
246 DEBUG(0,("ERROR: we did not create the shmem "
247 "(owned by another user, uid %u, gid %u)\n",
248 shm_ds.shm_perm.cuid,
249 shm_ds.shm_perm.cgid));
250 return False;
253 if (shm_ds.shm_segsz != sizeof(*profile_h)) {
254 DEBUG(0,("WARNING: profile size is %d (expected %d). Deleting\n",
255 (int)shm_ds.shm_segsz, (int)sizeof(*profile_h)));
256 if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
257 goto again;
258 } else {
259 return False;
263 if (!read_only && (shm_ds.shm_nattch == 1)) {
264 memset((char *)profile_h, 0, sizeof(*profile_h));
265 profile_h->prof_shm_magic = PROF_SHM_MAGIC;
266 profile_h->prof_shm_version = PROF_SHM_VERSION;
267 DEBUG(3,("Initialised profile area\n"));
270 profile_p = &profile_h->stats;
271 if (msg_ctx != NULL) {
272 messaging_register(msg_ctx, NULL, MSG_PROFILE,
273 profile_message);
274 messaging_register(msg_ctx, NULL, MSG_REQ_PROFILELEVEL,
275 reqprofile_message);
277 return True;
280 const char * profile_value_name(enum profile_stats_values val)
282 static const char * valnames[PR_VALUE_MAX + 1] =
284 "smbd_idle", /* PR_VALUE_SMBD_IDLE */
285 "syscall_opendir", /* PR_VALUE_SYSCALL_OPENDIR */
286 "syscall_readdir", /* PR_VALUE_SYSCALL_READDIR */
287 "syscall_seekdir", /* PR_VALUE_SYSCALL_SEEKDIR */
288 "syscall_telldir", /* PR_VALUE_SYSCALL_TELLDIR */
289 "syscall_rewinddir", /* PR_VALUE_SYSCALL_REWINDDIR */
290 "syscall_mkdir", /* PR_VALUE_SYSCALL_MKDIR */
291 "syscall_rmdir", /* PR_VALUE_SYSCALL_RMDIR */
292 "syscall_closedir", /* PR_VALUE_SYSCALL_CLOSEDIR */
293 "syscall_open", /* PR_VALUE_SYSCALL_OPEN */
294 "syscall_createfile", /* PR_VALUE_SYSCALL_CREATEFILE */
295 "syscall_close", /* PR_VALUE_SYSCALL_CLOSE */
296 "syscall_read", /* PR_VALUE_SYSCALL_READ */
297 "syscall_pread", /* PR_VALUE_SYSCALL_PREAD */
298 "syscall_write", /* PR_VALUE_SYSCALL_WRITE */
299 "syscall_pwrite", /* PR_VALUE_SYSCALL_PWRITE */
300 "syscall_lseek", /* PR_VALUE_SYSCALL_LSEEK */
301 "syscall_sendfile", /* PR_VALUE_SYSCALL_SENDFILE */
302 "syscall_recvfile", /* PR_VALUE_SYSCALL_RECVFILE */
303 "syscall_rename", /* PR_VALUE_SYSCALL_RENAME */
304 "syscall_rename_at", /* PR_VALUE_SYSCALL_RENAME_AT */
305 "syscall_fsync", /* PR_VALUE_SYSCALL_FSYNC */
306 "syscall_stat", /* PR_VALUE_SYSCALL_STAT */
307 "syscall_fstat", /* PR_VALUE_SYSCALL_FSTAT */
308 "syscall_lstat", /* PR_VALUE_SYSCALL_LSTAT */
309 "syscall_unlink", /* PR_VALUE_SYSCALL_UNLINK */
310 "syscall_chmod", /* PR_VALUE_SYSCALL_CHMOD */
311 "syscall_fchmod", /* PR_VALUE_SYSCALL_FCHMOD */
312 "syscall_chown", /* PR_VALUE_SYSCALL_CHOWN */
313 "syscall_fchown", /* PR_VALUE_SYSCALL_FCHOWN */
314 "syscall_chdir", /* PR_VALUE_SYSCALL_CHDIR */
315 "syscall_getwd", /* PR_VALUE_SYSCALL_GETWD */
316 "syscall_ntimes", /* PR_VALUE_SYSCALL_NTIMES */
317 "syscall_ftruncate", /* PR_VALUE_SYSCALL_FTRUNCATE */
318 "syscall_fcntl_lock", /* PR_VALUE_SYSCALL_FCNTL_LOCK */
319 "syscall_kernel_flock", /* PR_VALUE_SYSCALL_KERNEL_FLOCK */
320 "syscall_linux_setlease", /* PR_VALUE_SYSCALL_LINUX_SETLEASE */
321 "syscall_fcntl_getlock", /* PR_VALUE_SYSCALL_FCNTL_GETLOCK */
322 "syscall_readlink", /* PR_VALUE_SYSCALL_READLINK */
323 "syscall_symlink", /* PR_VALUE_SYSCALL_SYMLINK */
324 "syscall_link", /* PR_VALUE_SYSCALL_LINK */
325 "syscall_mknod", /* PR_VALUE_SYSCALL_MKNOD */
326 "syscall_realpath", /* PR_VALUE_SYSCALL_REALPATH */
327 "syscall_get_quota", /* PR_VALUE_SYSCALL_GET_QUOTA */
328 "syscall_set_quota", /* PR_VALUE_SYSCALL_SET_QUOTA */
329 "syscall_get_sd", /* PR_VALUE_SYSCALL_GET_SD */
330 "syscall_set_sd", /* PR_VALUE_SYSCALL_SET_SD */
331 "syscall_brl_lock", /* PR_VALUE_SYSCALL_BRL_LOCK */
332 "syscall_brl_unlock", /* PR_VALUE_SYSCALL_BRL_UNLOCK */
333 "syscall_brl_cancel", /* PR_VALUE_SYSCALL_BRL_CANCEL */
334 "SMBmkdir", /* PR_VALUE_SMBMKDIR */
335 "SMBrmdir", /* PR_VALUE_SMBRMDIR */
336 "SMBopen", /* PR_VALUE_SMBOPEN */
337 "SMBcreate", /* PR_VALUE_SMBCREATE */
338 "SMBclose", /* PR_VALUE_SMBCLOSE */
339 "SMBflush", /* PR_VALUE_SMBFLUSH */
340 "SMBunlink", /* PR_VALUE_SMBUNLINK */
341 "SMBmv", /* PR_VALUE_SMBMV */
342 "SMBgetatr", /* PR_VALUE_SMBGETATR */
343 "SMBsetatr", /* PR_VALUE_SMBSETATR */
344 "SMBread", /* PR_VALUE_SMBREAD */
345 "SMBwrite", /* PR_VALUE_SMBWRITE */
346 "SMBlock", /* PR_VALUE_SMBLOCK */
347 "SMBunlock", /* PR_VALUE_SMBUNLOCK */
348 "SMBctemp", /* PR_VALUE_SMBCTEMP */
349 "SMBmknew", /* PR_VALUE_SMBMKNEW */
350 "SMBcheckpath", /* PR_VALUE_SMBCHECKPATH */
351 "SMBexit", /* PR_VALUE_SMBEXIT */
352 "SMBlseek", /* PR_VALUE_SMBLSEEK */
353 "SMBlockread", /* PR_VALUE_SMBLOCKREAD */
354 "SMBwriteunlock", /* PR_VALUE_SMBWRITEUNLOCK */
355 "SMBreadbraw", /* PR_VALUE_SMBREADBRAW */
356 "SMBreadBmpx", /* PR_VALUE_SMBREADBMPX */
357 "SMBreadBs", /* PR_VALUE_SMBREADBS */
358 "SMBwritebraw", /* PR_VALUE_SMBWRITEBRAW */
359 "SMBwriteBmpx", /* PR_VALUE_SMBWRITEBMPX */
360 "SMBwriteBs", /* PR_VALUE_SMBWRITEBS */
361 "SMBwritec", /* PR_VALUE_SMBWRITEC */
362 "SMBsetattrE", /* PR_VALUE_SMBSETATTRE */
363 "SMBgetattrE", /* PR_VALUE_SMBGETATTRE */
364 "SMBlockingX", /* PR_VALUE_SMBLOCKINGX */
365 "SMBtrans", /* PR_VALUE_SMBTRANS */
366 "SMBtranss", /* PR_VALUE_SMBTRANSS */
367 "SMBioctl", /* PR_VALUE_SMBIOCTL */
368 "SMBioctls", /* PR_VALUE_SMBIOCTLS */
369 "SMBcopy", /* PR_VALUE_SMBCOPY */
370 "SMBmove", /* PR_VALUE_SMBMOVE */
371 "SMBecho", /* PR_VALUE_SMBECHO */
372 "SMBwriteclose", /* PR_VALUE_SMBWRITECLOSE */
373 "SMBopenX", /* PR_VALUE_SMBOPENX */
374 "SMBreadX", /* PR_VALUE_SMBREADX */
375 "SMBwriteX", /* PR_VALUE_SMBWRITEX */
376 "SMBtrans2", /* PR_VALUE_SMBTRANS2 */
377 "SMBtranss2", /* PR_VALUE_SMBTRANSS2 */
378 "SMBfindclose", /* PR_VALUE_SMBFINDCLOSE */
379 "SMBfindnclose", /* PR_VALUE_SMBFINDNCLOSE */
380 "SMBtcon", /* PR_VALUE_SMBTCON */
381 "SMBtdis", /* PR_VALUE_SMBTDIS */
382 "SMBnegprot", /* PR_VALUE_SMBNEGPROT */
383 "SMBsesssetupX", /* PR_VALUE_SMBSESSSETUPX */
384 "SMBulogoffX", /* PR_VALUE_SMBULOGOFFX */
385 "SMBtconX", /* PR_VALUE_SMBTCONX */
386 "SMBdskattr", /* PR_VALUE_SMBDSKATTR */
387 "SMBsearch", /* PR_VALUE_SMBSEARCH */
388 "SMBffirst", /* PR_VALUE_SMBFFIRST */
389 "SMBfunique", /* PR_VALUE_SMBFUNIQUE */
390 "SMBfclose", /* PR_VALUE_SMBFCLOSE */
391 "SMBnttrans", /* PR_VALUE_SMBNTTRANS */
392 "SMBnttranss", /* PR_VALUE_SMBNTTRANSS */
393 "SMBntcreateX", /* PR_VALUE_SMBNTCREATEX */
394 "SMBntcancel", /* PR_VALUE_SMBNTCANCEL */
395 "SMBntrename", /* PR_VALUE_SMBNTRENAME */
396 "SMBsplopen", /* PR_VALUE_SMBSPLOPEN */
397 "SMBsplwr", /* PR_VALUE_SMBSPLWR */
398 "SMBsplclose", /* PR_VALUE_SMBSPLCLOSE */
399 "SMBsplretq", /* PR_VALUE_SMBSPLRETQ */
400 "SMBsends", /* PR_VALUE_SMBSENDS */
401 "SMBsendb", /* PR_VALUE_SMBSENDB */
402 "SMBfwdname", /* PR_VALUE_SMBFWDNAME */
403 "SMBcancelf", /* PR_VALUE_SMBCANCELF */
404 "SMBgetmac", /* PR_VALUE_SMBGETMAC */
405 "SMBsendstrt", /* PR_VALUE_SMBSENDSTRT */
406 "SMBsendend", /* PR_VALUE_SMBSENDEND */
407 "SMBsendtxt", /* PR_VALUE_SMBSENDTXT */
408 "SMBinvalid", /* PR_VALUE_SMBINVALID */
409 "pathworks_setdir", /* PR_VALUE_PATHWORKS_SETDIR */
410 "Trans2_open", /* PR_VALUE_TRANS2_OPEN */
411 "Trans2_findfirst", /* PR_VALUE_TRANS2_FINDFIRST */
412 "Trans2_findnext", /* PR_VALUE_TRANS2_FINDNEXT */
413 "Trans2_qfsinfo", /* PR_VALUE_TRANS2_QFSINFO */
414 "Trans2_setfsinfo", /* PR_VALUE_TRANS2_SETFSINFO */
415 "Trans2_qpathinfo", /* PR_VALUE_TRANS2_QPATHINFO */
416 "Trans2_setpathinfo", /* PR_VALUE_TRANS2_SETPATHINFO */
417 "Trans2_qfileinfo", /* PR_VALUE_TRANS2_QFILEINFO */
418 "Trans2_setfileinfo", /* PR_VALUE_TRANS2_SETFILEINFO */
419 "Trans2_fsctl", /* PR_VALUE_TRANS2_FSCTL */
420 "Trans2_ioctl", /* PR_VALUE_TRANS2_IOCTL */
421 "Trans2_findnotifyfirst", /* PR_VALUE_TRANS2_FINDNOTIFYFIRST */
422 "Trans2_findnotifynext", /* PR_VALUE_TRANS2_FINDNOTIFYNEXT */
423 "Trans2_mkdir", /* PR_VALUE_TRANS2_MKDIR */
424 "Trans2_session_setup", /* PR_VALUE_TRANS2_SESSION_SETUP */
425 "Trans2_get_dfs_referral", /* PR_VALUE_TRANS2_GET_DFS_REFERRAL */
426 "Trans2_report_dfs_inconsistancy", /* PR_VALUE_TRANS2_REPORT_DFS_INCONSISTANCY */
427 "NT_transact_create", /* PR_VALUE_NT_TRANSACT_CREATE */
428 "NT_transact_ioctl", /* PR_VALUE_NT_TRANSACT_IOCTL */
429 "NT_transact_set_security_desc", /* PR_VALUE_NT_TRANSACT_SET_SECURITY_DESC */
430 "NT_transact_notify_change",/* PR_VALUE_NT_TRANSACT_NOTIFY_CHANGE */
431 "NT_transact_rename", /* PR_VALUE_NT_TRANSACT_RENAME */
432 "NT_transact_query_security_desc", /* PR_VALUE_NT_TRANSACT_QUERY_SECURITY_DESC */
433 "NT_transact_get_user_quota",/* PR_VALUE_NT_TRANSACT_GET_USER_QUOTA */
434 "NT_transact_set_user_quota",/* PR_VALUE_NT_TRANSACT_SET_USER_QUOTA */
435 "get_nt_acl", /* PR_VALUE_GET_NT_ACL */
436 "fget_nt_acl", /* PR_VALUE_FGET_NT_ACL */
437 "fset_nt_acl", /* PR_VALUE_FSET_NT_ACL */
438 "chmod_acl", /* PR_VALUE_CHMOD_ACL */
439 "fchmod_acl", /* PR_VALUE_FCHMOD_ACL */
440 "name_release", /* PR_VALUE_NAME_RELEASE */
441 "name_refresh", /* PR_VALUE_NAME_REFRESH */
442 "name_registration", /* PR_VALUE_NAME_REGISTRATION */
443 "node_status", /* PR_VALUE_NODE_STATUS */
444 "name_query", /* PR_VALUE_NAME_QUERY */
445 "host_announce", /* PR_VALUE_HOST_ANNOUNCE */
446 "workgroup_announce", /* PR_VALUE_WORKGROUP_ANNOUNCE */
447 "local_master_announce", /* PR_VALUE_LOCAL_MASTER_ANNOUNCE */
448 "master_browser_announce", /* PR_VALUE_MASTER_BROWSER_ANNOUNCE */
449 "lm_host_announce", /* PR_VALUE_LM_HOST_ANNOUNCE */
450 "get_backup_list", /* PR_VALUE_GET_BACKUP_LIST */
451 "reset_browser", /* PR_VALUE_RESET_BROWSER */
452 "announce_request", /* PR_VALUE_ANNOUNCE_REQUEST */
453 "lm_announce_request", /* PR_VALUE_LM_ANNOUNCE_REQUEST */
454 "domain_logon", /* PR_VALUE_DOMAIN_LOGON */
455 "sync_browse_lists", /* PR_VALUE_SYNC_BROWSE_LISTS */
456 "run_elections", /* PR_VALUE_RUN_ELECTIONS */
457 "election", /* PR_VALUE_ELECTION */
458 "" /* PR_VALUE_MAX */
461 SMB_ASSERT(val >= 0);
462 SMB_ASSERT(val < PR_VALUE_MAX);
463 return valnames[val];
466 #endif /* WITH_PROFILE */