source: branches/samba-3.0/source/profile/profile.c@ 727

Last change on this file since 727 was 30, checked in by Paul Smedley, 18 years ago

Code updated to Samba 3.0.25rc2 level

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