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.
7 * 14 Aug, 2006 Created (Rogier Meurs)
17 #include <minix/profile.h>
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"
38 #define DEF_MEMSIZE 64
43 #define MB (1024*1024)
44 #define SYNCING "SYNC"
45 #define DEV_LOG "/dev/log"
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
[])
72 if (res
= handle_args(argc
, argv
)) {
75 printf("Error in parameters.\n");
79 printf("Specify one of start|stop|get|reset.\n");
83 printf("Incorrect memory size.\n");
87 printf("Incorrect frequency.\n");
91 printf("Output filename missing.\n");
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");
120 if (start()) return 1;
123 if (stop()) return 1;
129 if (reset()) return 1;
138 int handle_args(int argc
, char *argv
[])
143 if (strcmp(*argv
, "-h") == 0 || strcmp(*argv
, "help") == 0 ||
144 strcmp(*argv
, "--help") == 0) {
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
;
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
;
157 if (strcmp(*argv
, "-o") == 0) {
158 if (--argc
== 0) return ESYNTAX
;
161 if (strcmp(*argv
, "start") == 0) {
162 if (action
) return EACTION
;
165 if (strcmp(*argv
, "stop") == 0) {
166 if (action
) return EACTION
;
169 if (strcmp(*argv
, "get") == 0) {
170 if (action
) return EACTION
;
173 if (strcmp(*argv
, "reset") == 0) {
174 if (action
) return EACTION
;
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 */
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.
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
)) {
220 printf("Error starting profiling.\n");
226 /* Temporarily redirect to system log to catch errors. */
227 log_fd
= open(DEV_LOG
, O_WRONLY
);
231 if ((npipe_fd
= open(NPIPE
, O_WRONLY
)) < 0) {
232 printf("Unable to open named pipe %s.\n", NPIPE
);
235 /* Synchronize with stopper process. */
236 write(npipe_fd
, SYNCING
, strlen(SYNCING
));
238 /* Now redirect to named pipe. */
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;
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
274 if (sprofile(PROF_STOP
, 0, 0, 0, 0)) {
276 printf("Error stopping profiling.\n");
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
);
284 /* Synchronize with starter process. */
285 read(npipe_fd
, buf
, strlen(SYNCING
));
287 while ((n
= read(npipe_fd
, buf
, BUFSIZE
)) > 0)
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
)) {
311 printf("Error getting data.\n");
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");
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");
332 if (write_outfile()) return 1;
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)) {
351 printf("Error resetting data.\n");
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");
365 } else memset(mem_ptr
, '\0', mem_size
);
373 if ((outfile_fd
= open(outfile
, O_CREAT
| O_TRUNC
| O_WRONLY
)) <= 0) {
374 printf("Unable to create outfile %s.\n", outfile
);
376 } else chmod(outfile
, S_IRUSR
| S_IWUSR
);
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
);
404 int n
, towrite
, written
= 0;
408 printf("Writing to %s ...", outfile
);
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
);
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; }
425 towrite
= mem_used
== -1 ? mem_size
: mem_used
;
427 while (towrite
> 0) {
429 n
= write(outfile_fd
, buf
, towrite
);
432 { printf("Error writing to outfile %s.\n", outfile
); return 1; }
439 printf(" header %d bytes, data %d bytes.\n", strlen(header
), written
);