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

rk_util_test.c

Go to the documentation of this file.
00001 /*
00002  * rk_util_test.h
00003  *
00004  * Copyright (c) 2003, 2004 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_test.c
00014  *
00015  * Test cases for the functions in rk_util.h.
00016  */
00017 
00018 #include "config.h"
00019 
00020 #ifndef DEBUG
00021 #define DEBUG
00022 #endif
00023 
00024 #include <string.h>
00025 #include <stdlib.h>
00026 #include <sys/types.h>
00027 #include <unistd.h>
00028 
00029 #include <assert_pp.h>
00030 #include <instrumentation.h>
00031 
00032 #include <rk/rk.h>
00033 #include "rk_util.h"
00034 
00035 /**
00036  * Test the rk_resource_set_get_usage function.
00037  *
00038  * @param rs The resource set to query.
00039  * @param rsu The object to store the resource set usage data.
00040  * @param pc The process cache to use when querying the set.
00041  * @return True if the test succeeded, false otherwise.
00042  */
00043 static int test_get_usage(rk_resource_set_t rs,
00044                           rk_resource_set_usage_t rsu,
00045                           rk_resource_set_proc_cache_t pc)
00046 {
00047     unsigned long long last_usage = 0;
00048     unsigned int consecutive = 0;
00049     int lpc, rc, retval = 1;
00050 
00051     require(rs != NULL_RESOURCE_SET);
00052     require(rsu != NULL);
00053     
00054     for( lpc = 0; (lpc < 10000) && (retval == 1); lpc++ )
00055     {
00056         volatile int lpc2;
00057                 
00058         rc = rk_resource_set_get_usage(rs, rsu, pc);
00059 
00060         if( rc != 0 )
00061             printf("%s\n", strerror(rc));
00062         require(rc == 0);
00063         require(rsu->proc_count == 1);
00064         require(rsu->inactive_cpu_usage == 0);
00065 
00066         if( rsu->active_cpu_usage < last_usage )
00067         {
00068             retval = 0;
00069         }
00070         else if( rsu->active_cpu_usage == last_usage )
00071         {
00072             consecutive += 1;
00073         }
00074         if( consecutive > 20 )
00075         {
00076             retval = 0;
00077         }
00078         else
00079         {
00080             consecutive = 0;
00081         }
00082         last_usage = rsu->active_cpu_usage;
00083         
00084         for( lpc2 = 0; lpc2 < 1000; lpc2++ );
00085     }
00086     return( retval );
00087 }
00088 
00089 int main(int argc, char *argv[])
00090 {
00091     int retval = EXIT_FAILURE;
00092     rk_resource_set_t rs;
00093 
00094     instrumentation_data.iid_OutputFileName = "-";
00095     atexit(iPrintPointsAtExit);
00096 
00097     if( (rs = rk_resource_set_create("rk_util_test")) != NULL_RESOURCE_SET )
00098     {
00099         struct rk_resource_set_usage rsu;
00100 
00101         /**
00102          * @test The rk_resource_set_get_by_name function with an existing
00103          * resource set.
00104          */
00105         require(rk_resource_set_get_by_name("rk_util_test") !=
00106                 NULL_RESOURCE_SET);
00107         /**
00108          * @test The rk_resource_set_get_by_name function with a non-existent
00109          * resource set.
00110          */
00111         require(rk_resource_set_get_by_name("non-existent") ==
00112                 NULL_RESOURCE_SET);
00113 
00114         rsu.proc_count = 1;
00115         rsu.inactive_cpu_usage = 0;
00116         rsu.active_cpu_usage = 1;
00117         /**
00118          * @test The rk_resource_set_get_usage function when there are no
00119          * processes in the set.
00120          */
00121         if( rk_resource_set_get_usage(rs, &rsu, NULL) == 0 )
00122         {
00123             ensure(rsu.proc_count == 0);
00124             ensure(rsu.active_cpu_usage == 0);
00125         }
00126         if( rk_resource_set_attach_process(rs, getpid()) != 0 )
00127         {
00128             perror("rk_resource_set_attach_process");
00129         }
00130         else
00131         {
00132             struct rk_resource_set_proc proc_elements[1];
00133             struct rk_resource_set_proc_cache pc;
00134 
00135             memset(proc_elements, 0, sizeof(proc_elements));
00136             pc.data = proc_elements;
00137             pc.used = 0;
00138             pc.length = 1;
00139 
00140             /**
00141              * @test The rk_resource_set_get_usage function with no process
00142              * cache.
00143              */
00144             if( !test_get_usage(rs, &rsu, NULL) )
00145             {
00146                 retval = EXIT_FAILURE;
00147             }
00148             /**
00149              * @test The rk_resource_set_get_usage function with a process
00150              * cache.
00151              */
00152             else if( !test_get_usage(rs, &rsu, &pc) )
00153             {
00154                 retval = EXIT_FAILURE;
00155             }
00156             /**
00157              * @test The rk_resource_set_get_usage function with a process
00158              * cache after the processes have been removed.
00159              */
00160             else if( rk_resource_set_detach_process(rs, getpid()) != 0 )
00161             {
00162                 perror("rk_resource_set_detach_process");
00163             }
00164             else
00165             {
00166                 unsigned long long old_usage;
00167                 int rc;
00168 
00169                 old_usage = rsu.active_cpu_usage;
00170                 rc = rk_resource_set_get_usage(rs, &rsu, &pc);
00171 
00172                 ensure(rsu.proc_count == 0);
00173                 ensure(rsu.inactive_cpu_usage == old_usage);
00174                 ensure(rsu.active_cpu_usage == 0);
00175                 
00176                 retval = EXIT_SUCCESS;
00177             }
00178 
00179             rk_resource_set_proc_cache_release(&pc);
00180         }
00181         rk_resource_set_destroy(rs);
00182     }
00183     else
00184     {
00185         perror("rk_resource_set_create");
00186     }
00187     return( retval );
00188 }

Generated on Fri Oct 22 07:50:24 2004 for CPU Broker by  doxygen 1.3.9.1