Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

rk_util.c

Go to the documentation of this file.
00001 /*
00002  * rk_util.c
00003  *
00004  * Copyright (c) 2003 The University of Utah and the Flux Group.
00005  * All rights reserved.
00006  *
00007  * This file is licensed under the terms of the GNU Public License.  
00008  * See the file "license.terms" for restrictions on redistribution 
00009  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010  */
00011 
00012 /**
00013  * @file rk_util.c
00014  *
00015  * Implementation of the functions in rk_util.h.
00016  */
00017 
00018 #include "config.h"
00019 
00020 #include <errno.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <unistd.h>
00024 
00025 #include <sys/types.h>
00026 
00027 #include "assert_pp.h"
00028 #include "instrumentation.h"
00029 
00030 #include <rk.h>
00031 #include "rk_util.h"
00032 
00033 /* BEGIN Instrumentation data */
00034 
00035 #if defined(INSTR_rk_resource_set_get_usage_data)
00036 static struct iPoint INSTR_get_point = {
00037     INSTR_rk_resource_set_get_usage_data,
00038 };
00039 #endif
00040 
00041 #if defined(INSTR_rk_proc_usage_data)
00042 static struct iPoint INSTR_rk_proc_usage_point = {
00043     INSTR_rk_proc_usage_data,
00044 };
00045 #endif
00046 
00047 /* END Instrumentation data */
00048 
00049 rk_resource_set_t rk_resource_set_get_by_name(const char *name)
00050 {
00051     rk_resource_set_t retval = NULL_RESOURCE_SET;
00052 #define MAX_RK_SETS 128
00053     rk_resource_set_t rk_sets[MAX_RK_SETS];
00054     char rs_name[RK_NAME_LEN + 1];
00055     int lpc, actual;
00056 
00057     require(name != NULL);
00058     
00059     actual = rk_resource_sets_get_list(rk_sets, MAX_RK_SETS);
00060     for( lpc = 0; (lpc < actual) && (retval == NULL_RESOURCE_SET); lpc++ )
00061     {
00062         rs_name[0] = '\0';
00063         if( rk_resource_set_get_name(rk_sets[lpc], rs_name) == 0 )
00064         {
00065             if( strcmp(rs_name, name) == 0 )
00066             {
00067                 retval = rk_sets[lpc];
00068             }
00069         }
00070     }
00071 #undef MAX_RK_SETS
00072     return( retval );
00073 }
00074 
00075 int rk_resource_set_get_usage(rk_resource_set_t rs,
00076                               rk_resource_set_usage_t rsu)
00077 {
00078     int retval = 0;
00079 
00080     if( (rs == NULL) || (rsu == NULL) )
00081     {
00082         retval = EINVAL;
00083     }
00084     else
00085     {
00086 #define MAX_PID_COUNT 128
00087         INSTR_rk_resource_set_get_usage(&INSTR_get_point, ({
00088             static pid_t procs[MAX_PID_COUNT];
00089     
00090             int lpc;
00091             
00092             rsu->proc_count = rk_resource_set_get_proclist(rs,
00093                                                            procs,
00094                                                            MAX_PID_COUNT);
00095             rsu->cpu_usage = 0;
00096             for( lpc = 0; (lpc < rsu->proc_count) && (retval == 0); lpc++ )
00097             {
00098                 static char filename[1024]; // XXX
00099                 
00100                 char *unused_string = filename;
00101                 unsigned long unused_ulong;
00102                 long unused_long;
00103                 char unused_char;
00104                 int unused_int;
00105                 FILE *file;
00106                 
00107                 snprintf(filename,
00108                          sizeof(filename),
00109                          "/proc/%d/stat",
00110                          procs[lpc]);
00111                 if( (file = fopen(filename, "r")) != NULL )
00112                 {
00113                     unsigned long long usage = 0;
00114                     struct timeval utime, stime;
00115                     int scan_count;
00116                     
00117                     memset(&utime, 0, sizeof(utime));
00118                     memset(&stime, 0, sizeof(stime));
00119                     {
00120                         scan_count = fscanf(file,
00121                                             "%d %s %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld %lu %lu %ld %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %d %d %ld %ld %ld %ld",
00122                                             &unused_int,
00123                                             unused_string,
00124                                             &unused_char,
00125                                             &unused_int,
00126                                             &unused_int,
00127                                             &unused_int,
00128                                             &unused_int,
00129                                             &unused_int,
00130                                             &unused_ulong,
00131                                             &unused_ulong,
00132                                             &unused_ulong,
00133                                             &unused_ulong,
00134                                             &unused_ulong,
00135                                             &unused_ulong,
00136                                             &unused_ulong,
00137                                             &unused_long,
00138                                             &unused_long,
00139                                             &unused_long,
00140                                             &unused_long,
00141                                             &unused_long,
00142                                             &unused_long,
00143                                             &unused_ulong,
00144                                             &unused_ulong,
00145                                             &unused_long,
00146                                             &unused_ulong,
00147                                             &unused_ulong,
00148                                             &unused_ulong,
00149                                             &unused_ulong,
00150                                             &unused_ulong,
00151                                             &unused_ulong,
00152                                             &unused_ulong,
00153                                             &unused_ulong,
00154                                             &unused_ulong,
00155                                             &unused_ulong,
00156                                             &unused_ulong,
00157                                             &unused_ulong,
00158                                             &unused_ulong,
00159                                             &unused_int,
00160                                             &unused_int,
00161                                             &utime.tv_sec,
00162                                             &utime.tv_usec,
00163                                             &stime.tv_sec,
00164                                             &stime.tv_usec);
00165 
00166                         if( scan_count != 43 )
00167                         {
00168                             retval = ENOSYS;
00169                         }
00170                     }
00171                     usage += utime.tv_sec * 1000000;
00172                     usage += utime.tv_usec;
00173                     usage += stime.tv_sec * 1000000;
00174                     usage += stime.tv_usec;
00175                     INSTR_rk_proc_usage(&INSTR_rk_proc_usage_point, usage);
00176                     rsu->cpu_usage += usage;
00177                     fclose(file);
00178                 }
00179             }
00180         }));
00181     }
00182     return( retval );
00183 }

Generated on Mon Dec 1 16:29:07 2003 for CPU Broker by doxygen 1.3.4