get_io.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <sys/types.h>
00005 #include <sys/stat.h>
00006 #include <fcntl.h>
00007 #include <sys/ioctl.h>
00008 #include <linux/fs.h>
00009 #include <errno.h>
00010 #include "assert.h"
00011 #include "xio.h"
00012 
00013 static time_t times[2];
00014 static unsigned long long bytes[2];
00015 
00016 int __GetIoPoint()
00017 {
00018         FILE *F1 = NULL;
00019         char iface[1025] = { '\0' };
00020         char line[1025] = { '\0' };
00021         char *word = NULL;
00022         unsigned long long iface_bytes[2] = { 0 };
00023         unsigned long long iface_bytes_this = 0;
00024         unsigned long long iface_byte_total = 0;
00025         int skip = 0;
00026         int sector_size = 0;
00027         int sector_fd = 0;
00028         int using_diskstats = 1;
00029         char device_path[255] = { 0 };
00030         char error_str[1025] = { 0 };
00031 
00032         F1 = fopen("/proc/diskstats", "r");
00033         if (F1 == NULL) {
00034                 using_diskstats = 0;
00035                 F1 = fopen("/proc/partitions", "r");
00036                 if (F1 == NULL) {
00037                         perror ("/proc/net/dev\n");
00038                         return(1);
00039                 }
00040         }
00041 
00042         while (!feof(F1)) {
00043                 fgets(line, 1024, F1);
00044 
00045                 if (feof(F1))
00046                         break;
00047 
00048                 word = strtok(line, " \t");
00049                 word = strtok(NULL, " \t");
00050                 if (!using_diskstats) {
00051                         word = strtok(NULL, " \t");
00052                 }
00053                 word = strtok(NULL, " \t");
00054                 if (word == NULL)
00055                         continue;
00056                 strcpy(iface, word);
00057 
00058                 word = strtok(NULL, " \t");
00059                 word = strtok(NULL, " \t");
00060                 word = strtok(NULL, " \t");
00061                 if (word != NULL) {
00062                         sscanf(word, "%llu", &iface_bytes[0]);
00063                 }
00064 
00065                 word = strtok(NULL, " \t");
00066                 word = strtok(NULL, " \t");
00067                 word = strtok(NULL, " \t");
00068                 if (word != NULL) {
00069                         sscanf(word, "%llu", &iface_bytes[1]);
00070                 }
00071 
00072                 iface_bytes_this = 0;
00073                 if (!xio_resources.interface) {
00074                         iface_bytes_this = iface_bytes[0] + iface_bytes[1];
00075                 }
00076                 else {
00077                         int found = 0;
00078 
00079                         word = strtok(xio_resources.interface, ", \t");
00080                         while (word != NULL) {
00081                                 if (!strcasecmp(word,iface)) {
00082                                         iface_bytes_this = iface_bytes[0] + iface_bytes[1];
00083                                 }
00084                                 word = strtok(NULL, ", \t");
00085                         }
00086                 }
00087 
00088                 sector_size = 0;
00089                 if (iface_bytes_this != 0) {
00090                         snprintf(device_path, 254, "/dev/%s", iface);
00091                         /* Using O_NONBLOCK|O_NDELAY means that xio doesn't cause the tray to
00092                          * close on CD/DVD drives
00093                          */
00094                         sector_fd = open(device_path, O_RDONLY|O_NONBLOCK|O_NDELAY);
00095                         if (sector_fd == -1) {
00096                                 if 
00097                                         (errno != 123) /* Ignore "No medium found" for CD/DVD drives */
00098                                 {
00099                                         snprintf(error_str, 1024,
00100                                                 "error[%d]: open() on device %s", errno, device_path);
00101                                         perror(error_str);
00102                                 }
00103                                 continue;
00104                         }
00105                         if (ioctl(sector_fd, BLKSSZGET, &sector_size) == -1) {
00106                                 snprintf(error_str, 1024,
00107                                         "error[%d]: ioctl on device %s", errno, device_path);
00108                                 perror(error_str);
00109                                 close(sector_fd);
00110                                 continue;
00111                         }
00112                         close(sector_fd);
00113                 }
00114                 iface_bytes_this *= sector_size;
00115 
00116                 iface_byte_total += iface_bytes_this;
00117         }
00118         fclose(F1);
00119 
00120         bytes[0] = bytes[1];
00121         bytes[1] = iface_byte_total;
00122         times[0] = times[1];
00123         times[1] = time(NULL);
00124 
00125         /* fprintf(stderr, "%llu %llu %llu %llu\n", bytes[0], bytes[1], times[0], times[1]); /**/
00126 
00127         return(0);
00128 }
00129 
00130 void InitIoPoint()
00131 {
00132         times[0] = 0;
00133         times[1] = 0;
00134         bytes[0] = 0;
00135         bytes[1] = 0;
00136         __GetIoPoint();
00137         __GetIoPoint();
00138 }
00139 
00140 void GetIoPoint( w, closure, call_data )
00141      Widget     w;
00142      XtPointer  closure;
00143      XtPointer  call_data;
00144 {
00145         unsigned long long ull_value = 0;
00146         unsigned long long ull_tdiff = 0;
00147         int i_value = 0.0;
00148         int i_tdiff = 0.0;
00149 
00150         ASSERT(w != NULL);
00151         ASSERT(call_data != NULL);
00152 
00153         __GetIoPoint();
00154 
00155         ASSERT(w != NULL);
00156 
00157         if (bytes[1] > bytes[0]) {
00158                 ull_value = bytes[1] - bytes[0];
00159         }
00160         else {
00161                 ull_value = bytes[0] - bytes[1];
00162         }
00163         /* fprintf(stderr, "%llu ", ull_value); /**/
00164 
00165         if (times[1] > times[0]) {
00166                 ull_tdiff = times[1] - times[0];
00167         }
00168         else {
00169                 ull_tdiff = times[0] - times[1];
00170         }
00171         /* fprintf(stderr, "%llu ", ull_tdiff); /**/
00172         if (ull_tdiff == 0) {
00173                 *(int *)call_data = 0;
00174                 ASSERT(w != NULL);
00175                 return;
00176         }
00177 
00178         i_tdiff = (int)ull_tdiff;
00179         /* fprintf(stderr, "%d ", i_tdiff); /**/
00180 
00181         i_value = (int)ull_value;
00182         /* fprintf(stderr, "%d ", i_value); /**/
00183 
00184         i_value /= i_tdiff;
00185         /* fprintf(stderr, "%d ", i_value); /**/
00186 
00187         /* fprintf(stderr, "\n"); /**/
00188 
00189         *(int *)call_data = i_value;
00190 
00191         ASSERT(w != NULL);
00192 }
00193 

Generated on Mon May 8 16:47:49 2006 for xmonitors by  doxygen 1.4.2