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