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