00001 #define QOS_DEBUG_LEVEL QOS_LEVEL_DEBUG
00002 #include <linux/aquosa/qos_debug.h>
00003 #include <aquosa/qres_lib.h>
00004
00005 #include <assert.h>
00006 #include <unistd.h>
00007 #include <stdio.h>
00008 #include <sys/time.h>
00009 #include <math.h>
00010
00012 #define ORIG_Q 50000L
00013 #define ORIG_P 100000L
00014
00017 static double target_ratios[] = {
00018 0.5, 0.3, 0.1
00019 };
00020
00022 #define RATIO_TOLERANCE 0.15
00023
00026 long count_and_wait() {
00027 unsigned long counted_sheeps = 0;
00028 unsigned long elapsed_usecs;
00029 struct timeval tv1, tv2;
00030
00031 qos_log_debug("Counting sheeps for 2 seconds ...");
00032 gettimeofday(&tv1, NULL);
00033 do {
00034 gettimeofday(&tv2, NULL);
00035 elapsed_usecs = tv2.tv_usec - tv1.tv_usec + (tv2.tv_sec - tv1.tv_sec) * 1000000;
00036 counted_sheeps++;
00037 } while (elapsed_usecs < 2000000);
00038 qos_log_debug("Counted %lu sheeps in 2 seconds", counted_sheeps);
00039 return counted_sheeps;
00040 }
00041
00042 int main(int argc, char **argv) {
00043 qres_params_t params = {
00044 .Q_min = 0,
00045 .Q = ORIG_Q,
00046 .P = ORIG_P,
00047 .flags = 0,
00048 };
00049 unsigned long long cnt1, cnt2;
00050 int i;
00051 int rv = 0;
00052 qres_sid_t sid;
00053
00054 qos_log_debug("Initializing qres library");
00055 qos_chk_ok_exit(qres_init());
00056
00057 qos_log_debug("Creating QRES Server with Q=" QRES_TIME_FMT ", P=" QRES_TIME_FMT, params.Q, params.P);
00058 qos_chk_ok_exit(qres_create_server(¶ms, &sid));
00059 qos_chk_ok_exit(qres_attach_thread(sid, 0, 0));
00060
00061 cnt1 = count_and_wait();
00062
00063 for (i = 0; i < (int) (sizeof(target_ratios) / sizeof(*target_ratios)); i++) {
00064 double target_ratio = target_ratios[i];
00065 double cnt2_exp, reldiff;
00066 params.Q = (qres_time_t) ((double) ORIG_Q * target_ratio);
00067 qos_log_debug("Changing QRES Parameters to Q=" QRES_TIME_FMT ", P=" QRES_TIME_FMT,
00068 params.Q, params.P);
00069 qos_chk_ok_exit(qres_set_params(sid, ¶ms));
00070
00071 cnt2_exp = target_ratio * cnt1;
00072 cnt2 = count_and_wait();
00073
00074 reldiff = (((double) cnt2) - cnt2_exp) / cnt2_exp;
00075 qos_log_debug("reldiff=%2.2g percentage", reldiff*100);
00076 if (fabs(reldiff) > RATIO_TOLERANCE) {
00077 qos_log_err("Expecting to count %g while counted %llu (reldiff=%g)",
00078 cnt2_exp, cnt2, reldiff);
00079 qos_log_err("This is outside defined tolerance of %g",
00080 RATIO_TOLERANCE);
00081 rv = -1;
00082 }
00083 }
00084
00085 qos_log_debug("Destroying QRES Server");
00086 qos_chk_ok_exit(qres_destroy_server(sid));
00087
00088 qos_log_debug("Finalizing QRES library");
00089 qos_chk_ok_exit(qres_cleanup());
00090
00091 return rv;
00092 }