opd_pipe.c

Go to the documentation of this file.
00001 
00012 #include "opd_pipe.h"
00013 #include "opd_printf.h"
00014 #include "op_config.h"
00015 
00016 #include <stdio.h>
00017 #include <stdlib.h>
00018 #include <string.h>
00019 #include <fcntl.h>
00020 #include <unistd.h>
00021 #include <errno.h>
00022 #include <sys/stat.h>
00023 
00024 static int fifo;
00025 static FILE * fifo_fd = NULL;
00026 
00027 void opd_create_pipe(void)
00028 {
00029     mode_t orig_umask = umask(0111);
00030     if (mkfifo(op_pipe_file, 0666) == -1) {
00031         if (errno != EEXIST) {
00032             perror("oprofiled: couldn't create pipe: ");
00033             exit(EXIT_FAILURE);
00034         }
00035     }
00036     umask(orig_umask);
00037 }
00038 
00039 
00040 void opd_open_pipe(void)
00041 {
00042     fifo = open(op_pipe_file, O_RDONLY | O_NONBLOCK);
00043     if (fifo == -1) {
00044         perror("oprofiled: couldn't open pipe: ");
00045         exit(EXIT_FAILURE);
00046     }
00047 }
00048 
00049 
00050 void opd_close_pipe(void)
00051 {
00052     if (fifo_fd)
00053         fclose(fifo_fd);
00054     close(fifo);
00055 }
00056 
00057 
00058 int is_jitconv_requested(void)
00059 {
00060     /* number of dropped (unknown) requests */
00061     static long nr_drops = 0;
00062     /* modulus to output only a few warnings to avoid flooding oprofiled.log */
00063     static int mod_cnt_drops = 1;
00064     char line[256];
00065     int i, ret = 0;
00066 
00067     /* get a file descriptor to the pipe */
00068     if (!fifo_fd)
00069         fifo_fd = fdopen(fifo, "r");
00070 
00071     if (fifo_fd == NULL) {
00072         perror("oprofiled: couldn't create file descriptor: ");
00073         exit(EXIT_FAILURE);
00074     }
00075 
00076     /* read up to 99 lines to check for 'do_jitconv' */
00077     for (i = 0; i < 99; i++) {
00078         /* just break if no new line is found */
00079         if (fgets(line, 256, fifo_fd) == NULL)
00080             break;
00081         line[strlen(line) - 1] = '\0';
00082 
00083         if (strstr(line, "do_jitconv") != NULL) {
00084             ret = 1;
00085         } else {
00086             nr_drops++;
00087 
00088             if (nr_drops % mod_cnt_drops == 0) {
00089                 printf(
00090                        "Warning: invalid pipe request received (dropped request(s): %ld)\n",
00091                        nr_drops);
00092                 /* increase modulus to avoid flooding log file */
00093                 mod_cnt_drops *= 5;
00094             }
00095         }
00096     }
00097 
00098     return ret;
00099 }

Generated on 8 Nov 2012 for Oprofile by  doxygen 1.6.1