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