packages: don't put oss on cd.
[minix.git] / commands / profile / profile.c
bloba91538b5a76738701d4f6a43a21d9b627e6f8fcf
1 /* profile - profile operating system
3 * The profile command is used to control Statistical and Call Profiling.
4 * It writes the profiling data collected by the kernel to a file.
6 * Changes:
7 * 14 Aug, 2006 Created (Rogier Meurs)
8 */
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <sys/stat.h>
17 #include <minix/profile.h>
19 #define EHELP 1
20 #define ESYNTAX 2
21 #define EMEM 3
22 #define EOUTFILE 4
23 #define EFREQ 5
24 #define EACTION 6
26 #define START 1
27 #define STOP 2
28 #define GET 3
29 #define RESET 4
31 #define SPROF (action==START||action==STOP)
32 #define CPROF (!SPROF)
33 #define DEF_OUTFILE_S "profile.stat.out"
34 #define DEF_OUTFILE_C "profile.call.out"
35 #define DEF_OUTFILE (SPROF?DEF_OUTFILE_S:DEF_OUTFILE_C)
36 #define NPIPE "/tmp/profile.npipe"
37 #define MIN_MEMSIZE 1
38 #define DEF_MEMSIZE 64
39 #define MIN_FREQ 3
40 #define MAX_FREQ 15
41 #define DEF_FREQ 6
42 #define BUFSIZE 1024
43 #define MB (1024*1024)
44 #define SYNCING "SYNC"
45 #define DEV_LOG "/dev/log"
47 int action = 0;
48 int mem_size = 0;
49 int mem_used = 0;
50 int freq = 0;
51 char *outfile = "";
52 char *mem_ptr;
53 int outfile_fd, npipe_fd;
54 struct sprof_info_s sprof_info;
55 struct cprof_info_s cprof_info;
57 _PROTOTYPE(int handle_args, (int argc, char *argv[]));
58 _PROTOTYPE(int start, (void));
59 _PROTOTYPE(int stop, (void));
60 _PROTOTYPE(int get, (void));
61 _PROTOTYPE(int reset, (void));
62 _PROTOTYPE(int create_named_pipe, (void));
63 _PROTOTYPE(int alloc_mem, (void));
64 _PROTOTYPE(int init_outfile, (void));
65 _PROTOTYPE(int write_outfile, (void));
66 _PROTOTYPE(int detach, (void));
68 int main(int argc, char *argv[])
70 int res;
72 if (res = handle_args(argc, argv)) {
73 switch(res) {
74 case ESYNTAX:
75 printf("Error in parameters.\n");
76 return 1;
77 break;
78 case EACTION:
79 printf("Specify one of start|stop|get|reset.\n");
80 return 1;
81 break;
82 case EMEM:
83 printf("Incorrect memory size.\n");
84 return 1;
85 break;
86 case EFREQ:
87 printf("Incorrect frequency.\n");
88 return 1;
89 break;
90 case EOUTFILE:
91 printf("Output filename missing.\n");
92 return 1;
93 break;
94 default:
95 break;
98 printf("Statistical Profiling:\n");
99 printf(" profile start [-m memsize] [-o outfile] [-f frequency]\n");
100 printf(" profile stop\n\n");
101 printf("Call Profiling:\n");
102 printf(" profile get [-m memsize] [-o outfile]\n");
103 printf(" profile reset\n\n");
104 printf(" - memsize in MB, default: %u\n", DEF_MEMSIZE);
105 printf(" - default output file: profile.{stat|call}.out\n");
106 printf( " - sample frequencies (default: %u):\n", DEF_FREQ);
107 printf(" 3 8192 Hz 10 64 Hz\n");
108 printf(" 4 4096 Hz 11 32 Hz\n");
109 printf(" 5 2048 Hz 12 16 Hz\n");
110 printf(" 6 1024 Hz 13 8 Hz\n");
111 printf(" 7 512 Hz 14 4 Hz\n");
112 printf(" 8 256 Hz 15 2 Hz\n");
113 printf(" 9 128 Hz\n\n");
114 printf("Use [sc]profalyze.pl to analyze output file.\n");
115 return 1;
118 switch(action) {
119 case START:
120 if (start()) return 1;
121 break;
122 case STOP:
123 if (stop()) return 1;
124 break;
125 case GET:
126 if (get()) return 1;
127 break;
128 case RESET:
129 if (reset()) return 1;
130 break;
131 default:
132 break;
134 return 0;
138 int handle_args(int argc, char *argv[])
140 while (--argc) {
141 ++argv;
143 if (strcmp(*argv, "-h") == 0 || strcmp(*argv, "help") == 0 ||
144 strcmp(*argv, "--help") == 0) {
145 return EHELP;
146 } else
147 if (strcmp(*argv, "-m") == 0) {
148 if (--argc == 0) return ESYNTAX;
149 if (sscanf(*++argv, "%u", &mem_size) != 1 ||
150 mem_size < MIN_MEMSIZE ) return EMEM;
151 } else
152 if (strcmp(*argv, "-f") == 0) {
153 if (--argc == 0) return ESYNTAX;
154 if (sscanf(*++argv, "%u", &freq) != 1 ||
155 freq < MIN_FREQ || freq > MAX_FREQ) return EFREQ;
156 } else
157 if (strcmp(*argv, "-o") == 0) {
158 if (--argc == 0) return ESYNTAX;
159 outfile = *++argv;
160 } else
161 if (strcmp(*argv, "start") == 0) {
162 if (action) return EACTION;
163 action = START;
164 } else
165 if (strcmp(*argv, "stop") == 0) {
166 if (action) return EACTION;
167 action = STOP;
168 } else
169 if (strcmp(*argv, "get") == 0) {
170 if (action) return EACTION;
171 action = GET;
172 } else
173 if (strcmp(*argv, "reset") == 0) {
174 if (action) return EACTION;
175 action = RESET;
179 /* No action specified. */
180 if (!action) return EHELP;
182 /* Init unspecified parameters. */
183 if (action == START || action == GET) {
184 if (strcmp(outfile, "") == 0) outfile = DEF_OUTFILE;
185 if (mem_size == 0) mem_size = DEF_MEMSIZE;
186 mem_size *= MB; /* mem_size in bytes */
188 if (action == START) {
189 mem_size -= mem_size % sizeof(sprof_sample); /* align to sample size */
190 if (freq == 0) freq = DEF_FREQ; /* default frequency */
192 return 0;
196 int start()
198 /* This is the "starter process" for statistical profiling.
200 * Create output file for profiling data. Create named pipe to
201 * synchronize with stopper process. Fork so the parent can exit.
202 * Allocate memory for profiling data. Start profiling in kernel.
203 * Complete detachment from terminal. Write known string to named
204 * pipe, which blocks until read by stopper process. Redirect
205 * stdout/stderr to the named pipe. Write profiling data to file.
206 * Clean up.
208 int log_fd;
210 if (init_outfile() || create_named_pipe()) return 1;
212 printf("Starting statistical profiling.\n");
214 if (fork() != 0) exit(0);
216 if (alloc_mem()) return 1;
218 if (sprofile(PROF_START, mem_size, freq, &sprof_info, mem_ptr)) {
219 perror("sprofile");
220 printf("Error starting profiling.\n");
221 return 1;
224 detach();
226 /* Temporarily redirect to system log to catch errors. */
227 log_fd = open(DEV_LOG, O_WRONLY);
228 dup2(log_fd, 1);
229 dup2(log_fd, 2);
231 if ((npipe_fd = open(NPIPE, O_WRONLY)) < 0) {
232 printf("Unable to open named pipe %s.\n", NPIPE);
233 return 1;
234 } else
235 /* Synchronize with stopper process. */
236 write(npipe_fd, SYNCING, strlen(SYNCING));
238 /* Now redirect to named pipe. */
239 dup2(npipe_fd, 1);
240 dup2(npipe_fd, 2);
242 mem_used = sprof_info.mem_used;
244 if (mem_used == -1) {
245 printf("WARNING: Profiling was stopped prematurely due to ");
246 printf("insufficient memory.\n");
247 printf("Try increasing available memory using the -m switch.\n");
250 if (write_outfile()) return 1;
252 close(log_fd);
253 close(npipe_fd);
254 unlink(NPIPE);
255 close(outfile_fd);
256 free(mem_ptr);
258 return 0;
262 int stop()
264 /* This is the "stopper" process for statistical profiling.
266 * Stop profiling in kernel. Read known string from named pipe
267 * to synchronize with starter proces. Read named pipe until EOF
268 * and write to stdout, this allows feedback from started process
269 * to be printed.
271 int n;
272 char buf[BUFSIZE];
274 if (sprofile(PROF_STOP, 0, 0, 0, 0)) {
275 perror("sprofile");
276 printf("Error stopping profiling.\n");
277 return 1;
278 } else printf("Statistical profiling stopped.\n");
280 if ((npipe_fd = open(NPIPE, O_RDONLY)) < 0) {
281 printf("Unable to open named pipe %s.\n", NPIPE);
282 return 1;
283 } else
284 /* Synchronize with starter process. */
285 read(npipe_fd, buf, strlen(SYNCING));
287 while ((n = read(npipe_fd, buf, BUFSIZE)) > 0)
288 write(1, buf, n);
290 close(npipe_fd);
292 return 0;
296 int get()
298 /* Get function for call profiling.
300 * Create output file. Allocate memory. Perform system call to get
301 * profiling table and write it to file. Clean up.
303 if (init_outfile()) return 1;
305 printf("Getting call profiling data.\n");
307 if (alloc_mem()) return 1;
309 if (cprofile(PROF_GET, mem_size, &cprof_info, mem_ptr)) {
310 perror("cprofile");
311 printf("Error getting data.\n");
312 return 1;
315 mem_used = cprof_info.mem_used;
317 if (mem_used == -1) {
318 printf("ERROR: unable to get data due to insufficient memory.\n");
319 printf("Try increasing available memory using the -m switch.\n");
320 } else
321 if (cprof_info.err) {
322 printf("ERROR: the following error(s) happened during profiling:\n");
323 if (cprof_info.err & CPROF_CPATH_OVERRUN)
324 printf(" call path overrun\n");
325 if (cprof_info.err & CPROF_STACK_OVERRUN)
326 printf(" call stack overrun\n");
327 if (cprof_info.err & CPROF_TABLE_OVERRUN)
328 printf(" hash table overrun\n");
329 printf("Try changing values in /usr/src/include/minix/profile.h ");
330 printf("and then rebuild the system.\n");
331 } else
332 if (write_outfile()) return 1;
334 close(outfile_fd);
335 free(mem_ptr);
337 return 0;
341 int reset()
343 /* Reset function for call profiling.
345 * Perform system call to reset profiling table.
347 printf("Resetting call profiling data.\n");
349 if (cprofile(PROF_RESET, 0, 0, 0)) {
350 perror("cprofile");
351 printf("Error resetting data.\n");
352 return 1;
355 return 0;
359 int alloc_mem()
361 if ((mem_ptr = malloc(mem_size)) == 0) {
362 printf("Unable to allocate memory.\n");
363 printf("Used chmem to increase available proces memory?\n");
364 return 1;
365 } else memset(mem_ptr, '\0', mem_size);
367 return 0;
371 int init_outfile()
373 if ((outfile_fd = open(outfile, O_CREAT | O_TRUNC | O_WRONLY)) <= 0) {
374 printf("Unable to create outfile %s.\n", outfile);
375 return 1;
376 } else chmod(outfile, S_IRUSR | S_IWUSR);
378 return 0;
382 int create_named_pipe()
384 if ((mkfifo(NPIPE, S_IRUSR | S_IWUSR) == -1) && (errno != EEXIST)) {
385 printf("Unable to create named pipe %s.\n", NPIPE);
386 return 1;
387 } else
388 return 0;
392 int detach()
394 setsid();
395 (void) chdir("/");
396 close(0);
397 close(1);
398 close(2);
402 int write_outfile()
404 int n, towrite, written = 0;
405 char *buf = mem_ptr;
406 char header[80];
408 printf("Writing to %s ...", outfile);
410 /* Write header. */
411 if (SPROF)
412 sprintf(header, "stat\n%d %d %d %d\n", sprof_info.total_samples,
413 sprof_info.idle_samples,
414 sprof_info.system_samples,
415 sprof_info.user_samples);
416 else
417 sprintf(header, "call\n%u %u\n",
418 CPROF_CPATH_MAX_LEN, CPROF_PROCNAME_LEN);
420 n = write(outfile_fd, header, strlen(header));
422 if (n < 0) { printf("Error writing to outfile %s.\n", outfile); return 1; }
424 /* Write data. */
425 towrite = mem_used == -1 ? mem_size : mem_used;
427 while (towrite > 0) {
429 n = write(outfile_fd, buf, towrite);
431 if (n < 0)
432 { printf("Error writing to outfile %s.\n", outfile); return 1; }
434 towrite -= n;
435 buf += n;
436 written += n;
439 printf(" header %d bytes, data %d bytes.\n", strlen(header), written);
441 return 0;