r15837: starting sync up for 3.0.23rc1 (in sync with SAMBA_3_0 r15822)
[Samba.git] / source / profile / profile.c
blobba9596301c6491d3a25e615b34749c5a8d903337
1 /*
2 Unix SMB/CIFS implementation.
3 store smbd profiling information in shared memory
4 Copyright (C) Andrew Tridgell 1999
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 #endif
34 #endif
36 struct profile_header *profile_h;
37 struct profile_stats *profile_p;
39 BOOL do_profile_flag = False;
40 BOOL do_profile_times = False;
42 /****************************************************************************
43 receive a set profile level message
44 ****************************************************************************/
45 void profile_message(int msg_type, struct process_id src, void *buf, size_t len)
47 int level;
49 memcpy(&level, buf, sizeof(int));
50 #ifdef WITH_PROFILE
51 switch (level) {
52 case 0: /* turn off profiling */
53 do_profile_flag = False;
54 do_profile_times = False;
55 DEBUG(1,("INFO: Profiling turned OFF from pid %d\n",
56 (int)procid_to_pid(&src)));
57 break;
58 case 1: /* turn on counter profiling only */
59 do_profile_flag = True;
60 do_profile_times = False;
61 DEBUG(1,("INFO: Profiling counts turned ON from pid %d\n",
62 (int)procid_to_pid(&src)));
63 break;
64 case 2: /* turn on complete profiling */
65 do_profile_flag = True;
66 do_profile_times = True;
67 DEBUG(1,("INFO: Full profiling turned ON from pid %d\n",
68 (int)procid_to_pid(&src)));
69 break;
70 case 3: /* reset profile values */
71 memset((char *)profile_p, 0, sizeof(*profile_p));
72 DEBUG(1,("INFO: Profiling values cleared from pid %d\n",
73 (int)procid_to_pid(&src)));
74 break;
76 #else /* WITH_PROFILE */
77 DEBUG(1,("INFO: Profiling support unavailable in this build.\n"));
78 #endif /* WITH_PROFILE */
81 /****************************************************************************
82 receive a request profile level message
83 ****************************************************************************/
84 void reqprofile_message(int msg_type, struct process_id src,
85 void *buf, size_t len)
87 int level;
89 #ifdef WITH_PROFILE
90 level = 1 + (do_profile_flag?2:0) + (do_profile_times?4:0);
91 #else
92 level = 0;
93 #endif
94 DEBUG(1,("INFO: Received REQ_PROFILELEVEL message from PID %u\n",
95 (unsigned int)procid_to_pid(&src)));
96 message_send_pid(src, MSG_PROFILELEVEL, &level, sizeof(int), True);
99 /*******************************************************************
100 open the profiling shared memory area
101 ******************************************************************/
102 #ifdef WITH_PROFILE
103 BOOL profile_setup(BOOL rdonly)
105 struct shmid_ds shm_ds;
107 read_only = rdonly;
109 #if defined(HAVE_CLOCK_GETTIME)
110 if (this_is_smp()) {
111 /* This is faster that gettimeofday, but not fast enough to
112 * leave it enabled in production.
114 __profile_clock = CLOCK_MONOTONIC;
115 } else {
116 /* CLOCK_PROCESS_CPUTIME_ID is sufficiently fast that the
117 * always profiling times is plausible. Unfortunately it is
118 * only accurate if we can guarantee we will not be scheduled
119 * onto a different CPU between samples. Until there is some
120 * way to set processor affinity, we can only use this on
121 * uniprocessors.
123 __profile_clock = CLOCK_PROCESS_CPUTIME_ID;
125 #endif
127 again:
128 /* try to use an existing key */
129 shm_id = shmget(PROF_SHMEM_KEY, 0, 0);
131 /* if that failed then create one. There is a race condition here
132 if we are running from inetd. Bad luck. */
133 if (shm_id == -1) {
134 if (read_only) return False;
135 shm_id = shmget(PROF_SHMEM_KEY, sizeof(*profile_h),
136 IPC_CREAT | IPC_EXCL | IPC_PERMS);
139 if (shm_id == -1) {
140 DEBUG(0,("Can't create or use IPC area. Error was %s\n",
141 strerror(errno)));
142 return False;
146 profile_h = (struct profile_header *)shmat(shm_id, 0,
147 read_only?SHM_RDONLY:0);
148 if ((long)profile_p == -1) {
149 DEBUG(0,("Can't attach to IPC area. Error was %s\n",
150 strerror(errno)));
151 return False;
154 /* find out who created this memory area */
155 if (shmctl(shm_id, IPC_STAT, &shm_ds) != 0) {
156 DEBUG(0,("ERROR shmctl : can't IPC_STAT. Error was %s\n",
157 strerror(errno)));
158 return False;
161 if (shm_ds.shm_perm.cuid != sec_initial_uid() ||
162 shm_ds.shm_perm.cgid != sec_initial_gid()) {
163 DEBUG(0,("ERROR: we did not create the shmem "
164 "(owned by another user, uid %u, gid %u)\n",
165 shm_ds.shm_perm.cuid,
166 shm_ds.shm_perm.cgid));
167 return False;
170 if (shm_ds.shm_segsz != sizeof(*profile_h)) {
171 DEBUG(0,("WARNING: profile size is %d (expected %d). Deleting\n",
172 (int)shm_ds.shm_segsz, sizeof(*profile_h)));
173 if (shmctl(shm_id, IPC_RMID, &shm_ds) == 0) {
174 goto again;
175 } else {
176 return False;
180 if (!read_only && (shm_ds.shm_nattch == 1)) {
181 memset((char *)profile_h, 0, sizeof(*profile_h));
182 profile_h->prof_shm_magic = PROF_SHM_MAGIC;
183 profile_h->prof_shm_version = PROF_SHM_VERSION;
184 DEBUG(3,("Initialised profile area\n"));
187 profile_p = &profile_h->stats;
188 message_register(MSG_PROFILE, profile_message);
189 message_register(MSG_REQ_PROFILELEVEL, reqprofile_message);
190 return True;
192 #endif /* WITH_PROFILE */