00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <unistd.h>
00004 #include <string.h>
00005
00006 #include <aquosa/qres_lib.h>
00007 #include <linux/aquosa/qos_debug.h>
00008
00009 extern char **environ;
00010
00011 static inline void qos_assert(int cond, const char *msg) {
00012 if (! cond) {
00013 printf("%s\n", msg);
00014 exit(-1);
00015 }
00016 }
00017
00018 int main(int argc, char *argv[])
00019 {
00020 int Q_min = -1, Q = -1, P = -1;
00021 qos_bw_t bw_min = -1, bw = -1;
00022 int destroy = 0;
00023 pid_t pid = -1;
00024 tid_t tid = -1;
00025 qres_params_t params;
00026 qos_rv rv;
00027 qres_sid_t sid = -1;
00028 int get_sid = 0;
00029
00030 argc--; argv++;
00031 while (argc > 0) {
00032 if (strcmp(*argv, "-q") == 0) {
00033 qos_assert(argc > 1, "Missing option parameter");
00034 argc--; argv++;
00035 Q_min = atoi(*argv);
00036 } else if (strcmp(*argv, "-Q") == 0) {
00037 qos_assert(argc > 1, "Missing option parameter");
00038 argc--; argv++;
00039 Q = atoi(*argv);
00040 } else if (strcmp(*argv, "-P") == 0) {
00041 qos_assert(argc > 1, "Missing option parameter");
00042 argc--; argv++;
00043 P = atoi(*argv);
00044 } else if (strcmp(*argv, "-b") == 0) {
00045 qos_assert(argc > 1, "Missing option parameter");
00046 argc--; argv++;
00047 bw_min = d2bw(atof(*argv));
00048 } else if (strcmp(*argv, "-B") == 0) {
00049 qos_assert(argc > 1, "Missing option parameter");
00050 argc--; argv++;
00051 bw = d2bw(atof(*argv));
00052 } else if (strcmp(*argv, "-p") == 0) {
00053 qos_assert(argc > 1, "Missing option parameter");
00054 argc--; argv++;
00055 pid = atoi(*argv);
00056 } else if (strcmp(*argv, "-t") == 0) {
00057 qos_assert(argc > 1, "Missing option parameter");
00058 argc--; argv++;
00059 tid = atoi(*argv);
00060 } else if (strcmp(*argv, "-s") == 0) {
00061 qos_assert(argc > 1, "Missing option parameter");
00062 argc--; argv++;
00063 sid = atoi(*argv);
00064 } else if (strcmp(*argv, "-d") == 0) {
00065 destroy = 1;
00066 } else if (strcmp(*argv, "-g") == 0) {
00067 get_sid = 1;
00068 } else if (strcmp(*argv, "-h") == 0 || strcmp(*argv, "--help") == 0) {
00069 printf("Usage: %s [ options... ]\n", argv[0]);
00070 printf(" -q Q_min : set minimum guaranteed budget per server period\n");
00071 printf(" -Q Q_max : set required budget per server period\n");
00072 printf(" -P P : set server period\n");
00073 printf(" -b P : set minimum required bandwidth\n");
00074 printf(" -B P : set maximum bandwidth\n");
00075 printf(" -p pid : set process id to affect\n");
00076 printf(" -t tid : set thread id to affect or 0 for default\n");
00077 printf(" -s sid : set id of existing server to affect\n");
00078 printf(" -g : get server info by sid or pid/tid\n");
00079 exit(0);
00080 } else {
00081 printf("Unknown option: %s\n", *argv);
00082 exit(-1);
00083 }
00084
00085 argc--; argv++;
00086 }
00087
00088 qos_chk_ok_exit(qres_init());
00089
00090 if (tid == -1 && pid != -1)
00091 tid = pid;
00092 else if (pid == -1 && tid != -1)
00093 pid = tid;
00094
00095 if (sid == -1 && pid != -1 && tid != -1)
00096 qres_get_sid(pid, tid, &sid);
00097
00098 if ((destroy == 1) && (sid != -1)) {
00099
00100 qos_chk_ok_exit(qres_destroy_server(sid));
00101 qos_chk_ok_exit(qres_cleanup());
00102 exit(0);
00103 }
00104
00105 if (Q != -1 && P != -1) {
00106 params.Q = Q;
00107 params.P = P;
00108 } else if (bw != -1 && P != -1) {
00109 params.Q = bw2Q(bw, P);
00110 params.P = P;
00111 } else if (sid != -1) {
00112 if (pid != -1) {
00113 rv = qres_attach_thread(sid, pid, tid);
00114 qos_chk_ok_exit(rv);
00115 }
00116 }
00117
00118 if (Q_min != -1)
00119 params.Q_min = Q_min;
00120 else if (bw_min != -1)
00121 params.Q_min = bw2Q(bw_min, params.P);
00122 else
00123 params.Q_min = 0;
00124
00125 params.flags = 0;
00126
00127 if (get_sid) {
00128 qos_chk_ok_exit(qres_get_params(sid, ¶ms));
00129 printf("sid: %d, Q: " QRES_TIME_FMT
00130 ", q: " QRES_TIME_FMT
00131 ", P: " QRES_TIME_FMT
00132 "\n", sid, params.Q, params.Q_min, params.P);
00133 exit(0);
00134 }
00135
00136 qos_chk_ok_exit(qres_create_server(¶ms, &sid));
00137 qos_chk_ok_exit(qres_attach_thread(sid, pid, tid));
00138 if (rv == QOS_E_INCONSISTENT_STATE) {
00139
00140 rv = qres_set_params(sid, ¶ms);
00141 qos_chk_ok_exit(rv);
00142 } else
00143 qos_chk_ok_exit(rv);
00144
00145 qos_chk_ok_exit(qres_cleanup());
00146
00147 return 0;
00148 }