00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00037
00038
00039
00040
00041
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
00103
00104
00105 require(rk_resource_set_get_by_name("rk_util_test") !=
00106 NULL_RESOURCE_SET);
00107
00108
00109
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
00119
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
00142
00143
00144 if( !test_get_usage(rs, &rsu, NULL) )
00145 {
00146 retval = EXIT_FAILURE;
00147 }
00148
00149
00150
00151
00152 else if( !test_get_usage(rs, &rsu, &pc) )
00153 {
00154 retval = EXIT_FAILURE;
00155 }
00156
00157
00158
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 }